-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndarray.go
271 lines (226 loc) · 6.29 KB
/
ndarray.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
package ndvek
import (
"errors"
"fmt"
"github.com/viterin/vek"
)
// NdArray represents a multi-dimensional array with shape and data.
type NdArray struct {
shape []int
Data []float64
}
// NewNdArray creates a new NdArray given a shape and initial data.
func NewNdArray(shape []int, data []float64) (*NdArray, error) {
size := 1
for _, dim := range shape {
size *= dim
}
if size != len(data) {
return nil, errors.New("data length does not match shape dimensions")
}
return &NdArray{shape: shape, Data: data}, nil
}
// broadcastShapes finds a broadcasted shape from two shapes.
func broadcastShapes(shape1, shape2 []int) ([]int, error) {
len1, len2 := len(shape1), len(shape2)
maxLen := len1
if len2 > len1 {
maxLen = len2
}
broadcastedShape := make([]int, maxLen)
for i := 0; i < maxLen; i++ {
dim1, dim2 := 1, 1
if i < len1 {
dim1 = shape1[len1-1-i]
}
if i < len2 {
dim2 = shape2[len2-1-i]
}
if dim1 != 1 && dim2 != 1 && dim1 != dim2 {
return nil, fmt.Errorf("incompatible shapes for broadcasting: %v and %v", shape1, shape2)
}
if dim1 > dim2 {
broadcastedShape[maxLen-1-i] = dim1
} else {
broadcastedShape[maxLen-1-i] = dim2
}
}
return broadcastedShape, nil
}
// broadcastIndex converts a single index to the correct offset for broadcasting.
func broadcastIndex(shape, broadcastShape []int, index int) (int, error) {
// Check dimensions match broadcasting rules
if len(shape) > len(broadcastShape) {
return 0, errors.New("original shape cannot be larger than broadcast shape")
}
// Initialize result index in original array
originalIndex := 0
stride := 1
// Traverse from last dimension to first to handle broadcasting correctly
for i := len(broadcastShape) - 1; i >= 0; i-- {
broadcastDim := broadcastShape[i]
shapeDim := 1 // Assume a broadcasted dimension
if i-len(broadcastShape)+len(shape) >= 0 {
shapeDim = shape[i-len(broadcastShape)+len(shape)]
}
// Current coordinate along this dimension in the broadcasted array
currentCoord := index % broadcastDim
index /= broadcastDim
// Map the coordinate to the original shape (if broadcasted, use 0)
if shapeDim != broadcastDim && shapeDim == 1 {
currentCoord = 0
}
// Calculate the corresponding index in the original array
originalIndex += currentCoord * stride
stride *= shapeDim
}
return originalIndex, nil
}
func (a *NdArray) ApplyHadamardOp(op func(float64) float64) error {
dSize := len(a.Data)
for i := 0; i < dSize; i++ {
a.Data[i] = op(a.Data[i])
}
return nil
}
// applyOp applies an arithmetic operation with broadcasting.
func ApplyOp(a, b *NdArray, op func(float64, float64) float64) (*NdArray, error) {
bShape, err := broadcastShapes(a.shape, b.shape)
if err != nil {
return nil, err
}
sizeOf := 1
for _, dim := range bShape {
sizeOf *= dim
}
resultData := make([]float64, sizeOf)
result := &NdArray{shape: bShape, Data: resultData}
for i := 0; i < len(result.Data); i++ {
aIndex, err := broadcastIndex(a.shape, bShape, i)
if err != nil {
panic(err)
}
bIndex, err := broadcastIndex(b.shape, bShape, i)
if err != nil {
panic(err)
}
result.Data[i] = op(a.Data[aIndex], b.Data[bIndex])
}
return result, nil
}
// Add performs element-wise addition with broadcasting.
func Add(a, b *NdArray) (*NdArray, error) {
return ApplyOp(a, b, func(x, y float64) float64 { return x + y })
}
// Subtract performs element-wise subtraction with broadcasting.
func Subtract(a, b *NdArray) (*NdArray, error) {
return ApplyOp(a, b, func(x, y float64) float64 { return x - y })
}
// Multiply performs element-wise multiplication with broadcasting.
func Multiply(a, b *NdArray) (*NdArray, error) {
return ApplyOp(a, b, func(x, y float64) float64 { return x * y })
}
// Divide performs element-wise division with broadcasting.
func Divide(a, b *NdArray) (*NdArray, error) {
return ApplyOp(a, b, func(x, y float64) float64 { return x / y })
}
// Shape returns the shape of the ndarray.
func (a *NdArray) Shape() []int {
return a.shape
}
func ProdInt(x []int) int {
out := 1
for _, y := range x {
out *= y
}
return out
}
func Zeros(shape []int) *NdArray {
size := ProdInt(shape)
data := make([]float64, size)
for i := 0; i < size; i++ {
data[i] = 0
}
return &NdArray{shape: shape, Data: data}
}
func (a *NdArray) AddScalar(b float64) *NdArray {
newData := vek.AddNumber(a.Data, b)
newVek, err := NewNdArray(a.shape, newData)
if err != nil {
panic(err)
}
return newVek
}
func (a *NdArray) SubScalar(b float64) *NdArray {
newData := vek.AddNumber(a.Data, -b)
newVek, err := NewNdArray(a.shape, newData)
if err != nil {
panic(err)
}
return newVek
}
func (a *NdArray) MulScalar(b float64) *NdArray {
newData := vek.MulNumber(a.Data, b)
newVek, err := NewNdArray(a.shape, newData)
if err != nil {
panic(err)
}
return newVek
}
func (x *NdArray) InsertAxis(pos int) *NdArray {
rank := len(x.shape)
if pos < 0 {
pos += rank + 1
}
result := make([]int, rank+1)
copy(result[:pos], (x.shape)[:pos])
// Insert the new value
result[pos] = 1
// Copy the remaining elements
copy(result[pos+1:], x.shape[pos:])
y, err := NewNdArray(result, x.Data)
if err != nil {
panic(err)
}
return y
}
func (x *NdArray) Reshape(shape []int) *NdArray {
if !(ProdInt(shape) == ProdInt(x.shape)) {
return nil
}
x.shape = shape
return x
}
func (a *NdArray) Get(index []int) (float64, error) {
// Check if the index length matches the number of dimensions
if len(index) != len(a.shape) {
return 0, errors.New("index length does not match array dimensions")
}
// Validate and calculate offset in row-major order
offset := 0
for i, coord := range index {
dim := a.shape[i]
// Check for out-of-bounds access
if coord >= dim || coord < 0 {
return 0, errors.New("index out of bounds")
}
// Calculate offset based on row and column indices (assuming C-style indexing)
if i == 0 { // First dimension (rows)
offset = coord * a.shape[1] // Multiply by number of elements in a row
} else {
offset += coord // Add column index for subsequent dimensions
}
}
return a.Data[offset], nil
}
func Linspace(start, stop float64, numPoints int) []float64 {
if numPoints <= 0 {
return nil
}
step := (stop - start) / float64(numPoints-1)
result := make([]float64, numPoints)
for i := 0; i < numPoints; i++ {
result[i] = start + float64(i)*step
}
return result
}