-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.go
203 lines (178 loc) · 4.81 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) Harri Rautila, 2012
// This file is part of go.opt/matrix package. It is free software, distributed
// under the terms of GNU Lesser General Public License Version 3, or any later
// version. See the COPYING tile included in this archive.
// Package matrix implements column major matrices.
package matrix
// Minimal interface for linear algebra packages BLAS/LAPACK
type Matrix interface {
// The number of rows in this matrix.
Rows() int
// The number of columns in this matrix.
Cols() int
// The number of elements in this matrix.
NumElements() int
// Returns underlying float64 array for BLAS/LAPACK routines. Returns nil
// if matrix is complex128 valued.
FloatArray() []float64
// Returns underlying complex128 array for BLAS/LAPACK routines. Returns nil
// if matrix is float64 valued matrix.
ComplexArray() []complex128
// Returns true if matrix is complex valued. False otherwise.
IsComplex() bool
// For all float valued matrices return the value of A[0,0]. Returns NaN
// if not float valued.
Float() float64
// For all complex valued matrices return the value of A[0,0]. Returns
// NaN if not complex valued.
Complex() complex128
// Matrix in string format.
String() string
// Make a copy and return as Matrix interface type.
MakeCopy() Matrix
// Match size. Return true if equal.
SizeMatch(int, int) bool
// Get matrix size. Return pair (rows, cols).
Size() (int, int)
// Test for type equality.
EqualTypes(...Matrix) bool
}
// Interface for real and complex scalars.
type Scalar interface {
Float() float64
Complex() complex128
}
// Float constant
type FScalar float64
// Return self
func (self FScalar) Float() float64 { return float64(self) }
// Return complex(self, 0)
func (self FScalar) Complex() complex128 { return complex(float64(self), 0) }
func (self FScalar) Add(a float64) FScalar { return FScalar(float64(self)+a) }
func (self FScalar) Scale(a float64) FScalar{ return FScalar(float64(self)*a) }
func (self FScalar) Inv() FScalar { return FScalar(1.0/float64(self)) }
// Return real(self).
type CScalar complex128
func (self CScalar) Float() float64 { return float64(real(self)) }
// Return self.
func (self CScalar) Complex() complex128 { return complex128(self) }
// Stacking direction for matrix constructor.
type Stacking int
const StackDown = Stacking(0)
const StackRight = Stacking(1)
// Matrix constructor data order
type DataOrder int
const RowOrder = DataOrder(0)
const ColumnOrder = DataOrder(1)
// Matrix dimensions, rows, cols and leading index. For column major matrices
// leading index is equal to row count.
type dimensions struct {
rows int
cols int
// actual offset between leading index
step int
}
// Return number of rows.
func (A *dimensions) Rows() int {
if A == nil {
return 0
}
return A.rows
}
// Return number of columns.
func (A *dimensions) Cols() int {
if A == nil {
return 0
}
return A.cols
}
// Return size of the matrix as rows, cols pair.
func (A *dimensions) Size() (int, int) {
if A == nil {
return 0, 0
}
return A.rows, A.cols
}
// Set dimensions. Does not affect element allocations.
func (A *dimensions) SetSize(nrows, ncols int) {
A.rows = nrows
A.cols = ncols
A.step = A.rows
}
// Return the leading index size. Column major matrices it is row count.
func (A *dimensions) LeadingIndex() int {
return A.step
}
// Return total number of elements.
func (A *dimensions) NumElements() int {
if A == nil {
return 0
}
return A.rows * A.cols
}
// Return true if size of A is equal to parameter size (rows, cols).
func (A *dimensions) SizeMatch(rows, cols int) bool {
return A != nil && A.rows == rows && A.cols == cols
}
// Create a set of indexes from start to end-1 with interval step.
func MakeIndexSet(start, end, step int) []int {
if start < 0 {
start = 0
}
if end < 0 {
end = 0
}
if end-start == 0 {
return make([]int, 0, 1)
}
if step < 0 {
step = 1
}
//sz := (end-start)/step + 1
inds := make([]int, 0)
for k := start; k < end; k += step {
inds = append(inds, k)
}
return inds
}
// Create index set to access diagonal entries of matrix.
// indexes := MakeDiagonalSet(A.Size())
func MakeDiagonalSet(rows, cols int) []int {
if rows != cols {
return []int{}
}
inds := make([]int, rows)
for i :=0; i < rows; i++ {
inds[i] = i*rows + i
}
return inds
}
// Create index set for a row in matrix M.
func RowIndexes(m Matrix, row int) []int {
nrows, N := m.Size()
if row > nrows {
return []int{}
}
iset := make([]int, N)
for i := 0; i < N; i++ {
k := (row + i) * m.Cols()
iset[i] = k
}
return iset
}
// Create index set for a column in matrix M.
func ColumnIndexes(m Matrix, col int) []int {
N, ncols := m.Size()
if col > ncols {
return []int{}
}
iset := make([]int, N)
for i := 0; i < N; i++ {
k := col * N + i
iset[i] = k
}
return iset
}
// Local Variables:
// tab-width: 4
// End: