-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
dense_generated.go
168 lines (161 loc) · 3.36 KB
/
dense_generated.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
// Code generated by genlib2. DO NOT EDIT.
package tensor
import "reflect"
// Ones creates a *Dense with the provided shape and type
func Ones(dt Dtype, shape ...int) *Dense {
d := recycledDense(dt, shape)
switch d.t.Kind() {
case reflect.Int:
d.Memset(int(1))
case reflect.Int8:
d.Memset(int8(1))
case reflect.Int16:
d.Memset(int16(1))
case reflect.Int32:
d.Memset(int32(1))
case reflect.Int64:
d.Memset(int64(1))
case reflect.Uint:
d.Memset(uint(1))
case reflect.Uint8:
d.Memset(uint8(1))
case reflect.Uint16:
d.Memset(uint16(1))
case reflect.Uint32:
d.Memset(uint32(1))
case reflect.Uint64:
d.Memset(uint64(1))
case reflect.Float32:
d.Memset(float32(1))
case reflect.Float64:
d.Memset(float64(1))
case reflect.Complex64:
d.Memset(complex64(1))
case reflect.Complex128:
d.Memset(complex128(1))
case reflect.Bool:
d.Memset(true)
default:
// TODO: add a Oner interface
}
return d
}
// I creates the identity matrix (usually a square) matrix with 1s across the diagonals, and zeroes elsewhere, like so:
// Matrix(4,4)
// ⎡1 0 0 0⎤
// ⎢0 1 0 0⎥
// ⎢0 0 1 0⎥
// ⎣0 0 0 1⎦
// While technically an identity matrix is a square matrix, in attempt to keep feature parity with Numpy,
// the I() function allows you to create non square matrices, as well as an index to start the diagonals.
//
// For example:
// T = I(Float64, 4, 4, 1)
// Yields:
// ⎡0 1 0 0⎤
// ⎢0 0 1 0⎥
// ⎢0 0 0 1⎥
// ⎣0 0 0 0⎦
//
// The index k can also be a negative number:
// T = I(Float64, 4, 4, -1)
// Yields:
// ⎡0 0 0 0⎤
// ⎢1 0 0 0⎥
// ⎢0 1 0 0⎥
// ⎣0 0 1 0⎦
func I(dt Dtype, r, c, k int) *Dense {
ret := New(Of(dt), WithShape(r, c))
i := k
if k < 0 {
i = (-k) * c
}
var s *Dense
var err error
end := c - k
if end > r {
s, err = sliceDense(ret, nil)
} else {
s, err = sliceDense(ret, rs{0, end, 1})
}
if err != nil {
panic(err)
}
var nexts []int
iter := newFlatIterator(&s.AP)
nexts, err = iter.Slice(rs{i, s.Size(), c + 1})
switch s.t.Kind() {
case reflect.Int:
data := s.Ints()
for _, v := range nexts {
data[v] = 1
}
case reflect.Int8:
data := s.Int8s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Int16:
data := s.Int16s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Int32:
data := s.Int32s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Int64:
data := s.Int64s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Uint:
data := s.Uints()
for _, v := range nexts {
data[v] = 1
}
case reflect.Uint8:
data := s.Uint8s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Uint16:
data := s.Uint16s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Uint32:
data := s.Uint32s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Uint64:
data := s.Uint64s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Float32:
data := s.Float32s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Float64:
data := s.Float64s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Complex64:
data := s.Complex64s()
for _, v := range nexts {
data[v] = 1
}
case reflect.Complex128:
data := s.Complex128s()
for _, v := range nexts {
data[v] = 1
}
}
// TODO: create Oner interface for custom types
return ret
}