-
Notifications
You must be signed in to change notification settings - Fork 72
/
arithmetic.go
205 lines (181 loc) · 3.58 KB
/
arithmetic.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
// Copyright 2009 The GoMatrix Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package matrix
import "math"
/*
Finds the sum of two matrices.
*/
func Sum(A MatrixRO, Bs ...MatrixRO) (C *DenseMatrix) {
C = MakeDenseCopy(A)
var err error
for _, B := range Bs {
err = C.Add(MakeDenseCopy(B))
if err != nil {
break
}
}
if err != nil {
C = nil
}
return
}
/*
Finds the difference between two matrices.
*/
func Difference(A, B MatrixRO) (C *DenseMatrix) {
C = MakeDenseCopy(A)
err := C.Subtract(MakeDenseCopy(B))
if err != nil {
C = nil
}
return
}
/*
Finds the Product of two matrices.
*/
func Product(A MatrixRO, Bs ...MatrixRO) (C *DenseMatrix) {
C = MakeDenseCopy(A)
for _, B := range Bs {
Cm, err := C.Times(B)
if err != nil {
return
}
C = Cm.(*DenseMatrix)
}
return
}
func Transpose(A MatrixRO) (B Matrix) {
switch Am := A.(type) {
case *DenseMatrix:
B = Am.Transpose()
return
case *SparseMatrix:
B = Am.Transpose()
return
}
B = A.DenseMatrix().Transpose()
return
}
func Inverse(A MatrixRO) (B Matrix) {
var err error
switch Am := A.(type) {
case *DenseMatrix:
B, err = Am.Inverse()
if err != nil {
panic(err)
}
return
}
B, err = A.DenseMatrix().Inverse()
if err != nil {
panic(err)
}
return
}
/*
The Kronecker product. (http://en.wikipedia.org/wiki/Kronecker_product)
*/
func Kronecker(A, B MatrixRO) (C *DenseMatrix) {
ars, acs := A.Rows(), A.Cols()
brs, bcs := B.Rows(), B.Cols()
C = Zeros(ars*brs, acs*bcs)
for i := 0; i < ars; i++ {
for j := 0; j < acs; j++ {
Cij := C.GetMatrix(i*brs, j*bcs, brs, bcs)
Cij.SetMatrix(0, 0, Scaled(B, A.Get(i, j)))
}
}
return
}
func Vectorize(Am MatrixRO) (V *DenseMatrix) {
elems := Am.DenseMatrix().Transpose().Array()
V = MakeDenseMatrix(elems, Am.Rows()*Am.Cols(), 1)
return
}
func Unvectorize(V MatrixRO, rows, cols int) (A *DenseMatrix) {
A = MakeDenseMatrix(V.DenseMatrix().Array(), cols, rows).Transpose()
return
}
/*
Uses a number of goroutines to do the dot products necessary
for the matrix multiplication in parallel.
*/
func ParallelProduct(A, B MatrixRO) (C *DenseMatrix) {
if A.Cols() != B.Rows() {
return nil
}
C = Zeros(A.Rows(), B.Cols())
in := make(chan int)
quit := make(chan bool)
dotRowCol := func() {
for {
select {
case i := <-in:
sums := make([]float64, B.Cols())
for k := 0; k < A.Cols(); k++ {
for j := 0; j < B.Cols(); j++ {
sums[j] += A.Get(i, k) * B.Get(k, j)
}
}
for j := 0; j < B.Cols(); j++ {
C.Set(i, j, sums[j])
}
case <-quit:
return
}
}
}
threads := 2
for i := 0; i < threads; i++ {
go dotRowCol()
}
for i := 0; i < A.Rows(); i++ {
in <- i
}
for i := 0; i < threads; i++ {
quit <- true
}
return
}
/*
Scales a matrix by a scalar.
*/
func Scaled(A MatrixRO, f float64) (B *DenseMatrix) {
B = MakeDenseCopy(A)
B.Scale(f)
return
}
/*
Tests the element-wise equality of the two matrices.
*/
func Equals(A, B MatrixRO) bool {
if A.Rows() != B.Rows() || A.Cols() != B.Cols() {
return false
}
for i := 0; i < A.Rows(); i++ {
for j := 0; j < A.Cols(); j++ {
if A.Get(i, j) != B.Get(i, j) {
return false
}
}
}
return true
}
/*
Tests to see if the difference between two matrices,
element-wise, exceeds ε.
*/
func ApproxEquals(A, B MatrixRO, ε float64) bool {
if A.Rows() != B.Rows() || A.Cols() != B.Cols() {
return false
}
for i := 0; i < A.Rows(); i++ {
for j := 0; j < A.Cols(); j++ {
if math.Abs(A.Get(i, j)-B.Get(i, j)) > ε {
return false
}
}
}
return true
}