-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.go
67 lines (57 loc) · 1.78 KB
/
matrix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gdsp
// Matrix types represent an array of vectors.
type Matrix []Vector
// MatrixComplex types represent an array of complex vectors.
type MatrixComplex []VectorComplex
// MARK: Constructors
// MakeMatrix creates and returns a new matrix in row major order.
func MakeMatrix(repeating float64, rows int, columns int) Matrix {
m := make([]Vector, 0)
for i := 0; i < rows; i++ {
m = append(m, MakeVector(repeating, columns))
}
return m
}
// MakeMatrixComplex creates and returns a new matrix in row major order.
func MakeMatrixComplex(repeating complex128, rows int, columns int) MatrixComplex {
m := make([]VectorComplex, 0)
for i := 0; i < rows; i++ {
m = append(m, MakeVectorComplex(repeating, columns))
}
return m
}
// MARK: Conversion methods
// FlipOrder reverses the row and column order when indexing the matrix.
// The NewMatrix... constructors assume row major order and it is up to the
// programmer to keep track of what order their instance of Matrix has.
func (m Matrix) FlipOrder() Matrix {
if len(m) == 0 || len(m[0]) == 0 {
return nil
}
var flipped []Vector
for i := 0; i < len(m[0]); i++ {
var vec Vector
for j := 0; j < len(m); j++ {
vec = append(vec, m[j][i])
}
flipped = append(flipped, vec)
}
return flipped
}
// FlipOrderComplex reverses the row and column order when indexing the matrix.
// The NewMatrix... constructors assume row major order and it is up to the
// programmer to keep track of what order their instance of Matrix has.
func (m MatrixComplex) FlipOrderComplex() MatrixComplex {
if len(m) == 0 || len(m[0]) == 0 {
return nil
}
var flipped []VectorComplex
for i := 0; i < len(m[0]); i++ {
var vec VectorComplex
for j := 0; j < len(m); j++ {
vec = append(vec, m[j][i])
}
flipped = append(flipped, vec)
}
return flipped
}