/
gonum.go
137 lines (118 loc) · 4.01 KB
/
gonum.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
// generated by narray; DO NOT EDIT
// Copyright (c) 2015 AKUALAB INC., All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package na64
// Matrix is as an NArray of rank 2 that satisfies the gonum Matrix interfaces.
type Matrix NArray
// Vector is as an NArray of rank 1 that satisfies the gonum Vectorer interface.
type Vector NArray
// Matrix creates a subarray of rank 2.
// Equivalent to SubArray but restricted to the case where the
// resulting subarray has rank=2. (It will panic otherwise.)
// See SubArray for details.
func (na *NArray) Matrix(query ...int) *Matrix {
mat := na.SubArray(query...)
if len(mat.Shape) != 2 {
panic("matrix must have rank equal two")
}
return (*Matrix)(mat)
}
// Dims returns the dimensions of a Matrix.
func (mat *Matrix) Dims() (r, c int) {
na := (*NArray)(mat)
return na.Shape[0], na.Shape[1]
}
// At returns the value of a matrix element at (r, c). It will panic if r or c are
// out of bounds for the matrix.
func (mat *Matrix) At(r, c int) float64 {
na := (*NArray)(mat)
return na.At(r, c)
}
// Set alters the matrix element at (r, c) to v. It will panic if r or c are out of
// bounds for the matrix.
func (mat *Matrix) Set(r, c int, v float64) {
na := (*NArray)(mat)
na.Set(v, r, c)
}
// String returns vector as a printable string.
func (mat *Matrix) String() string {
na := (*NArray)(mat)
return na.String()
}
// Vector creates a subarray of rank 1.
// Equivalent to SubArray but restricted to the case where the
// resulting subarray has rank=1. (It will panic otherwise.)
// See SubArray for details.
//
// Example, given a 5x10 matrix (rank=2), return the vector
// of dim 10 for row idx=3:
//
// x := New(5,10)
// y := x.Vector(3,-1)
// // y = {x_30, x_31, ... , x_39}
func (na *NArray) Vector(query ...int) *Vector {
vec := na.SubArray(query...)
if len(vec.Shape) != 1 {
panic("vector must have rank equal one")
}
return (*Vector)(vec)
}
// Row returns a slice of float64 for the row specified. It will panic if the index
// is out of bounds. If the call requires a copy and dst is not nil it will be used and
// returned, if it is not nil the number of elements copied will be the minimum of the
// length of the slice and the number of columns in the matrix.
func (mat *Matrix) Row(dst []float64, i int) []float64 {
_, ncols := mat.Dims()
if dst == nil {
dst = make([]float64, ncols, ncols)
}
for j, _ := range dst {
dst[j] = mat.At(i, j)
}
return dst
}
// Col returns a slice of float64 for the column specified. It will panic if the index
// is out of bounds. If the call requires a copy and dst is not nil it will be used and
// returned, if it is not nil the number of elements copied will be the minimum of the
// length of the slice and the number of rows in the matrix.
func (mat *Matrix) Col(dst []float64, j int) []float64 {
nrows, _ := mat.Dims()
if dst == nil {
dst = make([]float64, nrows, nrows)
}
for i, _ := range dst {
dst[i] = mat.At(i, j)
}
return dst
}
// SetRow sets the values of the specified row to the values held in a slice of float64.
// It will panic if the index is out of bounds. The number of elements copied is
// returned and will be the minimum of the length of the slice and the number of columns
// in the matrix.
func (mat *Matrix) SetRow(i int, src []float64) int {
numCopied := len(src)
if len(src) > mat.Shape[1] {
numCopied = mat.Shape[1]
}
for j := 0; j < numCopied; j++ {
mat.Set(i, j, src[j])
}
return numCopied
}
// SetCol sets the values of the specified column to the values held in a slice of float64.
// It will panic if the index is out of bounds. The number of elements copied is
// returned and will be the minimum of the length of the slice and the number of rows
// in the matrix.
func (mat *Matrix) SetCol(j int, src []float64) int {
numCopied := len(src)
if len(src) > mat.Shape[0] {
numCopied = mat.Shape[0]
}
for i := 0; i < numCopied; i++ {
mat.Set(i, j, src[i])
}
return numCopied
}
// TODO finish implementing gonum interfaces.