-
Notifications
You must be signed in to change notification settings - Fork 4
/
cdense_math.go
322 lines (296 loc) · 9.18 KB
/
cdense_math.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// 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
import "math/cmplx"
// Compute in-place product A[i,j] *= alpha
func (A *ComplexMatrix) Scale(alpha complex128, indexes ...int) *ComplexMatrix {
nrows := A.Rows()
step := A.LeadingIndex()
if len(indexes) == 0 {
for k := 0; k < A.NumElements(); k++ {
rk := realIndex(k, nrows, step)
A.elements[rk] *= alpha
}
} else {
N := A.NumElements()
for k := 0; k < A.NumElements(); k++ {
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] *= alpha
}
}
return A
}
// Compute in-place A[indexes[i]] *= values[i]. Indexes are in column-major order.
func (A *ComplexMatrix) ScaleIndexes(indexes []int, values []complex128) *ComplexMatrix {
nrows := A.Rows()
step := A.LeadingIndex()
if len(indexes) == 0 {
return A
}
N := A.NumElements()
for i, k := range indexes {
if i >= len(values) {
return A
}
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] *= values[i]
}
return A
}
// Compute in-place sum A[i,j] += alpha
func (A *ComplexMatrix) Add(alpha complex128, indexes ...int) *ComplexMatrix {
nrows := A.Rows()
step := A.LeadingIndex()
N := A.NumElements()
if len(indexes) == 0 {
for k := 0; k < N; k++ {
rk := realIndex(k, nrows, step)
A.elements[rk] += alpha
}
} else {
for _, k := range indexes {
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] += alpha
}
}
return A
}
// Compute in-place A[indexes[i]] += values[i]. Indexes are in column-major order.
func (A *ComplexMatrix) AddIndexes(indexes []int, values []complex128) *ComplexMatrix {
if len(indexes) == 0 {
return A
}
nrows := A.Rows()
step := A.LeadingIndex()
N := A.NumElements()
for i, k := range indexes {
if i >= len(values) {
return A
}
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] += values[i]
}
return A
}
// Compute in-place inverse A[i,j] = 1.0/A[i,j]. If indexes is empty calculates for
// all elements
func (A *ComplexMatrix) Inv(indexes ...int) *ComplexMatrix {
nrows := A.Rows()
step := A.LeadingIndex()
N := A.NumElements()
if len(indexes) == 0 {
for k := 0; k < N; k++ {
rk := realIndex(k, nrows, step)
A.elements[rk] = 1.0 / A.elements[rk]
}
} else {
for _, k := range indexes {
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] = 1.0 / A.elements[rk]
}
}
return A
}
// Compute in-place negation -A[i,j]
func (A *ComplexMatrix) Neg() *ComplexMatrix {
for k, v := range A.elements {
A.elements[k] = -v
}
return A
}
// Compute element-wise division A /= B. Return A. If A and B sizes
// do not match A is returned unaltered.
func (A *ComplexMatrix) Div(B *ComplexMatrix) *ComplexMatrix {
if !A.SizeMatch(B.Size()) {
return A
}
N := A.NumElements()
for k := 0; k < N; k++ {
rka := realIndex(k, A.Rows(), A.LeadingIndex())
rkb := realIndex(k, B.Rows(), B.LeadingIndex())
A.elements[rka] /= B.elements[rkb]
}
return A
}
// Compute element-wise product A *= B. Return A. If A and B sizes
// do not match A is returned unaltered.
func (A *ComplexMatrix) Mul(B *ComplexMatrix) *ComplexMatrix {
if !A.SizeMatch(B.Size()) {
return A
}
N := A.NumElements()
for k := 0; k < N; k++ {
rka := realIndex(k, A.Rows(), A.LeadingIndex())
rkb := realIndex(k, B.Rows(), B.LeadingIndex())
A.elements[rka] *= B.elements[rkb]
}
return A
}
// Compute element-wise sum A += B. Return A. If A and B sizes
// do not match A is returned unaltered.
func (A *ComplexMatrix) Plus(B *ComplexMatrix) *ComplexMatrix {
if !A.SizeMatch(B.Size()) {
return A
}
N := A.NumElements()
for k := 0; k < N; k++ {
rka := realIndex(k, A.Rows(), A.LeadingIndex())
rkb := realIndex(k, B.Rows(), B.LeadingIndex())
A.elements[rka] += B.elements[rkb]
}
return A
}
// Compute element-wise difference A -= B. Return A. If A and B sizes
// do not match A is returned unaltered.
func (A *ComplexMatrix) Minus(B *ComplexMatrix) *ComplexMatrix {
if !A.SizeMatch(B.Size()) {
return A
}
N := A.NumElements()
for k := 0; k < N; k++ {
rka := realIndex(k, A.Rows(), A.LeadingIndex())
rkb := realIndex(k, B.Rows(), B.LeadingIndex())
A.elements[rka] -= B.elements[rkb]
}
return A
}
// Compute matrix product C = A * B where A is m*p and B is p*n.
// Returns a new m*n matrix.
func (A *ComplexMatrix) Times(B *ComplexMatrix) *ComplexMatrix {
if A.Cols() != B.Rows() {
return nil
}
rows := A.Rows()
cols := B.Cols()
acols := A.Cols()
C := ComplexZeros(rows, cols)
Ar := A.elements
Br := B.elements
// Basic idea:
// Loop through each matrix always in memory order ie. down each column.
// cc: start of column j in matrix C (=j*C.Rows)
// cr: index to C row i in column j (=j*C.Rows+i)
// br: index to B, row i in column k (=k*B.Rows+i)
// ar: index to A, row
cc := 0
br := 0
for j := 0; j < cols; j++ {
ar := 0
for k := 0; k < acols; k++ {
// move C index to first row in current column
cr := cc
// beta is value of B[k,j]
beta := Br[br]
// zero in B[k,j] does not increment value in C[:,j]
if beta != 0.0 {
// C[:,j] += A[:,k]*B[k,j]
for i := 0; i < rows; i++ {
C.elements[cr] += Ar[ar] * beta
// move to next row in memory order
cr += 1
ar += 1
}
} else {
// we skipped all rows in this column, move to start of next column
ar += rows
}
// move to next row in B, here ar points to start of next column in A
br += 1
}
// forward to start of next column in C
cc += rows
}
return C
}
// Compute A = fn(A) by applying function fn element wise to A.
// If indexes array is non-empty function is applied to elements of A
// indexed by the contents of indexes.
func (A *ComplexMatrix) Apply(fn func(complex128) complex128, indexes ...int) *ComplexMatrix {
nrows := A.Rows()
step := A.LeadingIndex()
N := A.NumElements()
if len(indexes) == 0 {
for k := 0; k < N; k++ {
rk := realIndex(k, nrows, step)
A.elements[rk] = fn(A.elements[rk])
}
} else {
for _, k := range indexes {
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] = fn(A.elements[rk])
}
}
return A
}
// Compute A = fn(A, x) by applying function fn element wise to A.
// If indexes array is non-empty function is applied to elements of A
// indexed by the contents of indexes.
func (A *ComplexMatrix) ApplyConst(x complex128, fn func(complex128, complex128) complex128, indexes ...int) *ComplexMatrix {
nrows := A.Rows()
step := A.LeadingIndex()
N := A.NumElements()
if len(indexes) == 0 {
for k := 0; k < N; k++ {
rk := realIndex(k, nrows, step)
A.elements[rk] = fn(A.elements[rk], x)
}
} else {
for _, k := range indexes {
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] = fn(A.elements[rk], x)
}
}
return A
}
// Compute A = fn(A, x) by applying function fn element wise to A.
// For all i in indexes: A[indexes[i]] = fn(A[indexes[i]], values[i])
func (A *ComplexMatrix) ApplyConstValues(values []complex128, fn func(complex128, complex128) complex128, indexes ...int) *ComplexMatrix {
nrows := A.Rows()
step := A.LeadingIndex()
N := A.NumElements()
for i, k := range indexes {
if i > len(values) {
return A
}
k = (k + N) % N
rk := realIndex(k, nrows, step)
A.elements[rk] = fn(A.elements[rk], values[i])
}
return A
}
// Compute in-place conjugate A[i,j]
func (A *ComplexMatrix) Conj() *ComplexMatrix {
return A.Apply(cmplx.Conj)
}
// Compute in-place Exp(A)
func (A *ComplexMatrix) Exp() *ComplexMatrix {
return A.Apply(cmplx.Exp)
}
// Compute in-place Log(A)
func (A *ComplexMatrix) Log() *ComplexMatrix {
return A.Apply(cmplx.Log)
}
// Compute in-place Log10(A)
func (A *ComplexMatrix) Log10() *ComplexMatrix {
return A.Apply(cmplx.Log10)
}
// Compute in-place Sqrt(A)
func (A *ComplexMatrix) Sqrt() *ComplexMatrix {
return A.Apply(cmplx.Sqrt)
}
// Compute in-place Pow(A, x)
func (A *ComplexMatrix) Pow(x complex128) *ComplexMatrix {
return A.ApplyConst(x, cmplx.Pow)
}
// Local Variables:
// tab-width: 4
// End: