Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ func OuterProduct(column, row []float64) (output [][]float64) {
}
return
}

func Transpose(matrix [][]float64, lineSize int) (output [][]float64, err error) {
output = make([][]float64, lineSize)
for i, v := range matrix {
if len(v) != lineSize {
err = fmt.Errorf(
"Inconsistent matrix line size. Expected: %d. Actual: %d",
lineSize, len(v),
)
return
}
for j := range v {
// To do transpose in place switch matrix items
//matrix[i][j], matrix[j][i] = matrix[j][j], matrix[i][j]
output[j] = append(output[j], matrix[i][j])
}
}
return
}
43 changes: 43 additions & 0 deletions matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,46 @@ func TestEntrywiseSum(t *testing.T) {
})
}
}

func TestTranspose(t *testing.T) {
type args struct {
matrix [][]float64
lineSize int
}
tests := []struct {
name string
args args
wantOutput [][]float64
wantErr bool
}{
{
name: "transpose",
args: args{
[][]float64{
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
},
4,
},
wantOutput: [][]float64{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOutput, err := Transpose(tt.args.matrix, tt.args.lineSize)
if (err != nil) != tt.wantErr {
t.Errorf("Transpose() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOutput, tt.wantOutput) {
t.Errorf("Transpose() = %v, want %v", gotOutput, tt.wantOutput)
}
})
}
}