-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatrix.go
More file actions
619 lines (565 loc) · 15.3 KB
/
matrix.go
File metadata and controls
619 lines (565 loc) · 15.3 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package matrix
// http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html
import (
"math/cmplx"
"sort"
)
// Private
// Get a column in the form of a slice from a matrix
func column(matrix [][]float64, columnNumber int) (result []float64) {
result = make([]float64, len(matrix))
for ii := 0; ii < len(matrix); ii++ {
for jj := 0; jj < len(matrix[0]); jj++ {
result[ii] = matrix[ii][columnNumber]
}
}
return
}
// Returns the sum of the given two matrix
func Add(firstMatrix [][]float64, secondMatrix [][]float64) (result [][]float64) {
result = make([][]float64, len(firstMatrix))
for ii := 0; ii < len(firstMatrix); ii++ {
result[ii] = make([]float64, len(firstMatrix[0]))
for jj := 0; jj < len(firstMatrix[0]); jj++ {
result[ii][jj] = firstMatrix[ii][jj] + secondMatrix[ii][jj]
}
}
return
}
// Subtraction operation on two matrices
func Subtract(firstMatrix [][]float64, secondMatrix [][]float64) [][]float64 {
secondMatrix = MultiplyByScalar(secondMatrix, -1.0)
return Add(firstMatrix, secondMatrix)
}
// Dot (Inner) product
func DotProduct(firstMatrix [][]float64, secondMatrix [][]float64) (result [][]float64) {
result = make([][]float64, len(firstMatrix))
for ii := 0; ii < len(firstMatrix); ii++ {
result[ii] = make([]float64, len(secondMatrix[0]))
for jj := 0; jj < len(secondMatrix[0]); jj++ {
for kk := 0; kk < len(secondMatrix); kk++ {
result[ii][jj] += firstMatrix[ii][kk] * secondMatrix[kk][jj]
}
}
}
return
}
// Calculates the determinant of the matrix
func Determinant(matrix [][]float64) (result float64) {
matrixLength := len(matrix)
sums := make([]float64, matrixLength*2)
for ii := 0; ii < len(sums); ii++ {
sums[ii] = 1
}
for ii := 0; ii < matrixLength; ii++ {
for jj := 0; jj < matrixLength; jj++ {
if ii-jj < 0 {
sums[matrixLength+ii-jj] *= matrix[ii][jj]
} else {
sums[ii-jj] *= matrix[ii][jj]
}
if ii+jj >= matrixLength {
sums[ii+jj] *= matrix[ii][jj]
} else {
sums[ii+jj+matrixLength] *= matrix[ii][jj]
}
}
}
dim := matrixLength * 2
if matrixLength == 2 {
dim = 2
matrixLength = 1
}
for ii := 0; ii < dim; ii++ {
if ii >= matrixLength {
result -= sums[ii]
} else {
result += sums[ii]
}
}
return
}
// Minor matrix of a given matrix
func MinorMatrix(matrix [][]float64) (result [][]float64) {
var (
matrixLength int
)
matrixLength = len(matrix)
result = make([][]float64, matrixLength)
for ii := 0; ii < matrixLength; ii++ {
result[ii] = make([]float64, matrixLength)
for jj := 0; jj < matrixLength; jj++ {
auxM := [][]float64{}
for iik := 0; iik < matrixLength; iik++ {
if iik != ii {
auxR := []float64{}
for jjk := 0; jjk < matrixLength; jjk++ {
if jjk != jj {
auxR = append(auxR, matrix[iik][jjk])
}
}
auxM = append(auxM, auxR)
}
}
result[ii][jj] = Determinant(auxM)
}
}
return
}
// Returns the Cofactor Matrix
func CofactorMatrix(matrix [][]float64) (result [][]float64) {
result = make([][]float64, len(matrix))
for ii := 0; ii < len(matrix); ii++ {
result[ii] = make([]float64, len(matrix[0]))
for jj := 0; jj < len(matrix[0]); jj++ {
if (ii+jj)%2 == 0 {
result[ii][jj] = matrix[ii][jj]
} else {
result[ii][jj] = -matrix[ii][jj]
}
}
}
return
}
// Calculates the inverse matrix
func Inverse(matrix [][]float64) [][]float64 {
determinant := Determinant(matrix)
adj := Transpose(CofactorMatrix(MinorMatrix(matrix)))
return MultiplyByScalar(adj, 1./determinant)
}
// Divide the first matrix by the second one
func Divide(firstMatrix [][]float64, secondMatrix [][]float64) [][]float64 {
return DotProduct(firstMatrix, Inverse(secondMatrix))
}
// Returns the rm of multiply all the elements of a matrix by a float number
func MultiplyByScalar(matrix [][]float64, scalar float64) [][]float64 {
function := func(x float64) float64 {
return x * scalar
}
return Apply(matrix, function)
}
// Multiply on matrix by the Transposepose of the second matrix
func MultTranspose(firstMatrix [][]float64, secondMatrix [][]float64) (result [][]float64) {
result = make([][]float64, len(firstMatrix))
for ii := 0; ii < len(firstMatrix); ii++ {
result[ii] = make([]float64, len(secondMatrix))
for jj := 0; jj < len(secondMatrix); jj++ {
for kk := 0; kk < len(secondMatrix[0]); kk++ {
result[ii][jj] += firstMatrix[ii][kk] * secondMatrix[jj][kk]
}
}
}
return
}
// Multiplication of two matrices; element-wise
func Multiply(firstMatrix [][]float64, secondMatrix [][]float64) (result [][]float64) {
result = make([][]float64, len(firstMatrix))
for ii := 0; ii < len(firstMatrix); ii++ {
result[ii] = make([]float64, len(firstMatrix[0]))
for jj := 0; jj < len(firstMatrix[0]); jj++ {
result[ii][jj] = firstMatrix[ii][jj] * secondMatrix[ii][jj]
}
}
return
}
// Matrix Transpose
func Transpose(matrix [][]float64) (result [][]float64) {
result = make([][]float64, len(matrix[0]))
// Initialize the matrix
for ii := 0; ii < len(matrix[0]); ii++ {
result[ii] = make([]float64, len(matrix))
for jj := 0; jj < len(matrix); jj++ {
result[ii][jj] = matrix[jj][ii]
}
}
return
}
// Sum of the matrix along with axis
// axis=0 => row-wise
// axis=1 => column-wise
func Sum(matrix [][]float64, axis int) (result []float64, ok bool) {
rowSum := make([]float64, len(matrix))
columnSum := make([]float64, len(matrix[0]))
for ii := 0; ii < len(matrix); ii++ {
for jj := 0; jj < len(matrix[0]); jj++ {
rowSum[ii] += matrix[ii][jj]
columnSum[jj] += matrix[ii][jj]
}
}
if axis == 1 {
result = rowSum
ok = true
} else {
result = columnSum
ok = true
}
return
}
// Maximum of matrix along with axis
// axis=0 => row-wise
// axis=1 => column-wise
func Max(matrix [][]float64, axis int) (result []float64, ok bool) {
var (
firstDimension int
secondDimension int
)
firstDimension = len(matrix)
secondDimension = len(matrix[0])
rowMax := make([]float64, firstDimension)
columnMax := make([]float64, secondDimension)
deepMatrix := deepCopyMatrix(matrix)
for ii := 0; ii < firstDimension; ii++ {
sort.Float64s(deepMatrix[ii])
rowMax[ii] = deepMatrix[ii][secondDimension-1]
if ii == firstDimension-1 {
for jj := 0; jj < secondDimension; jj++ {
deepCol := deepCopyArray(column(matrix, jj))
sort.Float64s(deepCol)
columnMax[jj] = deepCol[len(deepCol)-1]
}
}
}
if axis == 1 {
result = rowMax
ok = true
} else {
result = columnMax
ok = true
}
return
}
// Minimum of matrix along with axis
// axis=0 => row-wise
// axis=1 => column-wise
func Min(matrix [][]float64, axis int) (result []float64, ok bool) {
var (
firstDimension int
secondDimension int
)
firstDimension = len(matrix)
secondDimension = len(matrix[0])
rowMin := make([]float64, firstDimension)
columnMin := make([]float64, secondDimension)
deepMatrix := deepCopyMatrix(matrix)
for ii := 0; ii < firstDimension; ii++ {
sort.Float64s(deepMatrix[ii])
rowMin[ii] = deepMatrix[ii][0]
if ii == firstDimension-1 {
for jj := 0; jj < secondDimension; jj++ {
deepCol := deepCopyArray(column(matrix, jj))
sort.Float64s(deepCol)
columnMin[jj] = deepCol[0]
}
}
}
if axis == 1 {
result = rowMin
ok = true
} else {
result = columnMin
ok = true
}
return
}
// Median of matrix along with axis
// axis=0 => row-wise
// axis=1 => column-wise
func Median(matrix [][]float64, axis int) (result []float64, ok bool) {
var (
firstDimension int
secondDimension int
halfRow int
halfCol int
isFirstDivisibleBy2 bool
isSecondDivisibleBy2 bool
)
firstDimension = len(matrix)
secondDimension = len(matrix[0])
if firstDimension%2 == 0 {
halfRow = firstDimension / 2
isFirstDivisibleBy2 = true
} else {
halfRow = (firstDimension - 1) / 2
}
if secondDimension%2 == 0 {
halfCol = secondDimension / 2
isSecondDivisibleBy2 = true
} else {
halfCol = (secondDimension - 1) / 2
}
rowMedian := make([]float64, firstDimension)
columnMedian := make([]float64, secondDimension)
deepMatrix := deepCopyMatrix(matrix)
for ii := 0; ii < firstDimension; ii++ {
sort.Float64s(deepMatrix[ii])
if isSecondDivisibleBy2 && secondDimension > halfRow+1 {
rowMedian[ii] = (deepMatrix[ii][halfRow] + deepMatrix[ii][halfRow+1]) / 2.
} else if secondDimension == 2 {
rowMedian[ii] = (deepMatrix[ii][0] + deepMatrix[ii][1]) / 2.
} else {
rowMedian[ii] = deepMatrix[ii][halfRow]
}
if ii == firstDimension-1 {
for jj := 0; jj < secondDimension; jj++ {
deepCol := deepCopyArray(column(matrix, jj))
sort.Float64s(deepCol)
if isFirstDivisibleBy2 && len(deepCol) > halfCol+1 {
columnMedian[jj] = (deepCol[halfCol] + deepCol[halfCol+1]) / 2.
} else if len(deepCol) == 2 {
columnMedian[jj] = (deepCol[0] + deepCol[1]) / 2.
} else {
columnMedian[jj] = deepCol[halfCol]
}
}
}
}
if axis == 1 {
result = rowMedian
ok = true
} else {
result = columnMedian
ok = true
}
return
}
// Mean of matrix along with axis
// axis=0 => row-wise
// axis=1 => column-wise
func Mean(matrix [][]float64, axis int) (result []float64, ok bool) {
var dim int
sum, ok := Sum(matrix, axis)
if !ok {
return
}
result = make([]float64, len(sum))
if axis == 0 {
dim = len(matrix)
} else {
dim = len(matrix[0])
}
for ii, value := range sum {
result[ii] = value / float64(dim)
}
return
}
// Cumulative Sum of Matrix along with axis
// axis=0 => row-wise
// axis=1 => column-wise
func CumulativeSum(matrix [][]float64, axis int) (result []float64, ok bool) {
result, ok = Sum(matrix, axis)
temp := 0.
for ii, jj := range result {
temp += jj
result[ii] = temp
}
return
}
// Sum all the elements in a matrix
func SumAll(m [][]float64) (result float64) {
for ii := 0; ii < len(m); ii++ {
for jj := 0; jj < len(m[0]); jj++ {
result += m[ii][jj]
}
}
return
}
// Apply a function to all the elements of a matrix,
//the function will receive a float64 as param and returns a float64
func Apply(matrix [][]float64, function func(x float64) float64) (result [][]float64) {
result = make([][]float64, len(matrix))
for ii := 0; ii < len(matrix); ii++ {
result[ii] = make([]float64, len(matrix[0]))
for jj := 0; jj < len(matrix[0]); jj++ {
result[ii][jj] = function(matrix[ii][jj])
}
}
return
}
// Apply a function to a complex matrix
// the function will receive a complex128 and returns a complex128
func ComplexApply(matrix [][]complex128, function func(x complex128) complex128) (result [][]complex128) {
result = make([][]complex128, len(matrix))
for ii := 0; ii < len(matrix); ii++ {
result[ii] = make([]complex128, len(matrix[0]))
for jj := 0; jj < len(matrix[0]); jj++ {
result[ii][jj] = function(matrix[ii][jj])
}
}
return
}
// Deep Copy of an Array
func deepCopyMatrix(matrix [][]float64) (deepCopy [][]float64) {
deepCopy = make([][]float64, len(matrix))
for ii := 0; ii < len(matrix); ii++ {
deepCopy[ii] = make([]float64, len(matrix[ii]))
for jj := 0; jj < len(matrix[ii]); jj++ {
deepCopy[ii][jj] = matrix[ii][jj]
}
}
return
}
func deepCopyArray(array []float64) (deepCopy []float64) {
deepCopy = make([]float64, len(array))
for ii := 0; ii < len(array); ii++ {
deepCopy[ii] = array[ii]
}
return
}
// Concatenate two matrices along with their axises
// axis=0 => row-wise
// axis=1 => column-wise
func Concatenate(firstMatrix [][]float64, secondMatrix [][]float64, axis int) (result [][]float64) {
if axis == 0 {
result = make([][]float64, len(firstMatrix)+len(secondMatrix))
for ii := 0; ii < len(firstMatrix)+len(secondMatrix); ii++ {
if ii < len(firstMatrix) {
result[ii] = firstMatrix[ii]
} else {
result[ii] = secondMatrix[ii-len(firstMatrix)]
}
}
} else {
result = make([][]float64, len(firstMatrix))
for i := 0; i < len(firstMatrix); i++ {
result[i] = make([]float64, len(firstMatrix[i])+len(secondMatrix[i]))
for j := 0; j < len(firstMatrix[i]); j++ {
result[i][j] = firstMatrix[i][j]
}
for j := 0; j < len(secondMatrix[i]); j++ {
result[i][j+len(firstMatrix[i])] = secondMatrix[i][j]
}
}
}
return
}
// Returns an array where diagonal elements are 1 and remaining
// positions are 0
// If it has been passed one parameter, it yields a square matrix
// Two parameters define the size of the matrix
func Eye(args ...int) (result [][]float64, ok bool) {
result, ok = Zeros(args)
if ok {
for ii := 0; ii < len(result); ii++ {
for jj := 0; jj < len(result[0]); jj++ {
if ii == jj {
result[ii][jj] = 1.
}
}
}
}
return
}
// Returns an array filled with 0.s
// If it has been passed one parameter, it yields a square matrix
// Two parameters define the size of the matrix
func Zeros(args []int) (result [][]float64, ok bool) {
dims := make([]int, 2)
var isValid bool
if len(args) == 1 {
dims[0] = args[0]
dims[1] = args[0]
isValid = true
} else if len(args) == 2 {
dims[0] = args[0]
dims[1] = args[1]
isValid = true
}
if isValid {
result = make([][]float64, dims[0])
for ii := 0; ii < dims[0]; ii++ {
result[ii] = make([]float64, dims[1])
}
ok = true
}
return
}
// Returns the diagonal of a matrix
func Diagonal(matrix [][]float64) (result []float64, ok bool) {
var (
minDimension int
ii int
jj int
)
firstDimension, secondDimension := len(matrix), len(matrix[0])
if firstDimension < secondDimension {
minDimension = firstDimension
} else {
minDimension = secondDimension
}
for ii = 0; ii < minDimension; ii++ {
for jj = 0; jj < minDimension; jj++ {
if ii == jj {
result = append(result, matrix[ii][jj])
}
}
ok = true
}
return
}
// Lower Triangle Matrix
func LowerTriangle(matrix [][]float64) (result [][]float64) {
result = make([][]float64, len(matrix))
for ii := 0; ii < len(matrix); ii++ {
result[ii] = make([]float64, len(matrix[ii]))
for jj := 0; jj < len(matrix[0]); jj++ {
if ii >= jj {
result[ii][jj] = matrix[ii][jj]
}
}
}
return
}
// Upper Triangle of Matrix
func UpperTriangle(matrix [][]float64) (result [][]float64) {
firstDimension, secondDimension := len(matrix), len(matrix[0])
result = make([][]float64, firstDimension)
for ii := 0; ii < firstDimension; ii++ {
result[ii] = make([]float64, secondDimension)
for jj := 0; jj < secondDimension; jj++ {
if ii <= jj {
result[ii][jj] = matrix[ii][jj]
}
}
}
return
}
// Take the elements of the matrix given in indices
// uses so called fancy indexing to determine the positions of the
// array
func Take(matrix [][]float64, indices []int) (result []float64, ok bool) {
var (
first int
second int
)
firstDimension, secondDimension := len(matrix), len(matrix[0])
sort.Ints(indices)
if indices[len(indices)-1] < firstDimension*secondDimension {
ok = true
}
if ok {
for ii := 0; ii < len(indices); ii++ {
first, second = indices[ii]/secondDimension, indices[ii]%secondDimension
result = append(result, matrix[first][second])
}
}
return
}
// Returns the elements of the matrix which returns true for a given function
func Where(matrix [][]float64, function func(x float64) bool) (result []float64) {
for ii := 0; ii < len(matrix); ii++ {
for jj := 0; jj < len(matrix[0]); jj++ {
if function(matrix[ii][jj]) {
result = append(result, matrix[ii][jj])
}
}
}
return
}
// Returns the Congugate Matrix
func ConjugateMatrix(matrix [][]complex128) (result [][]complex128) {
function := func(number complex128) (result complex128) {
return cmplx.Conj(number)
}
result = ComplexApply(matrix, function)
return
}