-
Notifications
You must be signed in to change notification settings - Fork 72
/
matrix.go
231 lines (191 loc) · 3.9 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
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
// 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.
//target:gomatrix.googlecode.com/hg/matrix
//Linear algebra.
package matrix
import (
"errors"
"fmt"
"strconv"
"strings"
)
//The MatrixRO interface defines matrix operations that do not change the
//underlying data, such as information requests or the creation of transforms
/*
Read-only matrix types (at the moment, PivotMatrix).
*/
type MatrixRO interface {
//Returns true if the underlying object is nil.
Nil() bool
//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
//The size pair, (Rows(), Cols())
GetSize() (int, int)
//The element in the ith row and jth column.
Get(i, j int) float64
Plus(MatrixRO) (Matrix, error)
Minus(MatrixRO) (Matrix, error)
Times(MatrixRO) (Matrix, error)
//The determinant of this matrix.
Det() float64
//The trace of this matrix.
Trace() float64
//A pretty-print string.
String() string
DenseMatrix() *DenseMatrix
SparseMatrix() *SparseMatrix
}
/*
A mutable matrix.
*/
type Matrix interface {
MatrixRO
//Set the element at the ith row and jth column to v.
Set(i int, j int, v float64)
Add(MatrixRO) error
Subtract(MatrixRO) error
Scale(float64)
}
type matrix struct {
rows int
cols int
}
func (A *matrix) Nil() bool { return A == nil }
func (A *matrix) Rows() int { return A.rows }
func (A *matrix) Cols() int { return A.cols }
func (A *matrix) NumElements() int { return A.rows * A.cols }
func (A *matrix) GetSize() (rows, cols int) {
rows = A.rows
cols = A.cols
return
}
/*
Take a matlab-style matrix representation
eg [a b c; d e f]
*/
func ParseMatlab(txt string) (A *DenseMatrix, err error) {
var arrays [][]float64
spaceSep := strings.Fields(txt)
tok := func() (t string, eos bool) {
defer func() {
for len(spaceSep) != 0 && len(spaceSep[0]) == 0 {
spaceSep = spaceSep[1:]
}
}()
isNotNumber := func(c byte) bool {
return c != '[' || c != ']' || c == ';'
}
if len(spaceSep) == 0 {
eos = true
return
}
top := spaceSep[0]
var lof int
for ; lof < len(top) && !isNotNumber(top[lof]); lof++ {
}
if lof != 0 {
t = top[:lof]
spaceSep[0] = top[lof:]
return
} else {
t = top[:1]
spaceSep[0] = top[1:]
return
}
panic("unreachable")
}
stack := func(row []float64) (err error) {
if len(arrays) == 0 {
arrays = [][]float64{row}
return
}
if len(arrays[0]) != len(row) {
err = errors.New("misaligned row")
}
arrays = append(arrays, row)
return
}
var row []float64
loop:
for {
t, eos := tok()
if eos {
break loop
}
switch t {
case "[":
case ";":
err = stack(row)
if err != nil {
return
}
row = []float64{}
case "]":
err = stack(row)
if err != nil {
return
}
break loop
default:
var v float64
v, err = strconv.ParseFloat(t, 64)
if err != nil {
return
}
row = append(row, v)
}
}
A = MakeDenseMatrixStacked(arrays)
return
}
func String(A MatrixRO) string {
condense := func(vs string) string {
if strings.Index(vs, ".") != -1 {
for vs[len(vs)-1] == '0' {
vs = vs[0 : len(vs)-1]
}
}
if vs[len(vs)-1] == '.' {
vs = vs[0 : len(vs)-1]
}
return vs
}
if A == nil {
return "{nil}"
}
s := "{"
maxLen := 0
for i := 0; i < A.Rows(); i++ {
for j := 0; j < A.Cols(); j++ {
v := A.Get(i, j)
vs := condense(fmt.Sprintf("%f", v))
maxLen = maxInt(maxLen, len(vs))
}
}
for i := 0; i < A.Rows(); i++ {
for j := 0; j < A.Cols(); j++ {
v := A.Get(i, j)
vs := condense(fmt.Sprintf("%f", v))
for len(vs) < maxLen {
vs = " " + vs
}
s += vs
if i != A.Rows()-1 || j != A.Cols()-1 {
s += ","
}
if j != A.Cols()-1 {
s += " "
}
}
if i != A.Rows()-1 {
s += "\n "
}
}
s += "}"
return s
}