-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumjs.js
More file actions
319 lines (297 loc) · 9.24 KB
/
numjs.js
File metadata and controls
319 lines (297 loc) · 9.24 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* [nums description]
* @param {[type]} num [description]
* @param {[type]} shape [description]
* @return {[type]} [description]
*/
exports.nums = function(num, shape){
var shape = (typeof(shape)=='number')?[shape,shape]:shape||[1,1];
var array = new Array(shape[0]*shape[1]);
if(typeof(num)=='function'){
for (var i = 0; i < shape[0]; i++) {
for (var j = 0; j < shape[1]; j++) {
array[i*shape[1]+j] = num();
}
}
}else{
for (var i = 0; i < shape[0]; i++) {
for (var j = 0; j < shape[1]; j++) {
array[i*shape[1]+j] = num;
}
}
}
return new exports.matrix(array, shape);
};
/**
* [ones description]
* @param {[type]} shape [description]
* @return {[type]} [description]
*/
exports.ones = function(shape){
return exports.nums(1, shape);
};
/**
* [zeros description]
* @param {[type]} shape [description]
* @return {[type]} [description]
*/
exports.zeros = function(shape){
return exports.nums(0, shape);
};
/**
* [number description]
* @param {[type]} num [description]
* @return {[type]} [description]
*/
exports.number = function(num){
return new exports.matrix([num]);
};
/**
* [linspace description]
* @param {[type]} str [description]
* @param {[type]} end [description]
* @param {[type]} step [description]
* @return {[type]} [description]
*/
exports.linspace = function(str, end, step){
var step = step;
var array = [];
if(step>0){
array = new Array(step);
array[step-1] = end;
}
for (var i = step-2; i > -1; i--) {
array[i] = str + i*(end-str)/(step-1);
}
return new exports.matrix(array);
};
/**
* [istype description]
* @param {[type]} obj [description]
* @param {[type]} cname [description]
* @return {[type]} [description]
*/
exports.istype = function(obj, cname){
return typeof(obj)===cname;
};
/**
* [random description]
* @param {[type]} shape [description]
* @return {[type]} [description]
*/
exports.random = function(shape){
return exports.nums(Math.random, shape);
};
exports.nextpow = function(n, pow){
var pow = pow || 2;
return Math.ceil(Math.log(n)/Math.log(pow));
};
/**
* [matrix description]
* @param {[type]} array [description]
* @param {[type]} shape [description]
* @return {[type]} [description]
*/
exports.matrix = function(array, shape){
this.array = array||[];
this.shape = shape||[1,array.length];
/**
* [length description]
* @return {[type]} [description]
*/
this.length = function(){
return this.shape[0]*this.shape[1];
};
/**
* [islike description]
* @param {[type]} mat [description]
* @return {[type]} [description]
*/
this.islike = function(mat){
return (this.shape[0]==mat.shape[0] && this.shape[1]==mat.shape[1]);
}
/**
* [transpose description]
* @return {[type]} [description]
*/
this.transpose = function(){
var array = new Array(this.length());
var shape = [this.shape[1],this.shape[0]];
for (var i = 0; i < shape[0]; i++) {
for (var j = 0; j < shape[1]; j++) {
array[i*shape[1]+j] = this.array[j*shape[0]+i];
}
}
return new exports.matrix(array, shape);
};
/**
* [plus description]
* @param {[type]} mat [description]
* @return {[type]} [description]
*/
this.plus = function(mat){
var array = new Array(this.length());
if(exports.istype(mat, 'number')){
mat = exports.number(mat);
}
if(this.islike(mat)){
for (var i = 0; i < this.length(); i++) {
array[i] = this.array[i]+mat.array[i];
}
return new exports.matrix(array, this.shape);
}else if(mat.length()==1){
for (var i = 0; i < this.length(); i++) {
array[i] = this.array[i]+mat.array[0];
}
return new exports.matrix(array, this.shape);
}else{
console.log("shape of mat isn't fit with this !");
}
};
/**
* [dot description]
* @param {[type]} mat [description]
* @return {[type]} [description]
*/
this.dot = function(mat){
var array = new Array(this.length());
if(exports.istype(mat, 'number')){
mat = exports.number(mat);
}
if(this.islike(mat)){
for (var i = 0; i < this.length(); i++) {
array[i] = this.array[i]*mat.array[i];
}
return new exports.matrix(array, this.shape);
}else if(mat.length()==1){
for (var i = 0; i < this.length(); i++) {
array[i] = this.array[i]*mat.array[0];
}
return new exports.matrix(array, this.shape);
}else{
return console.error("shape of mat isn't fit with this !");
}
};
/**
* [cross description]
* @param {[type]} mat [description]
* @return {[type]} [description]
*/
this.cross = function(mat){
if (this.shape[1]==mat.shape[0]) {
var mat = mat.transpose();
var shape = [this.shape[0], mat.shape[0]];
var array = new Array(shape[0]*shape[1]);
for (var i = 0; i < shape[0]; i++) {
for (var j = 0; j < shape[1]; j++) {
array[i*shape[1]+j] = this.slice(i).dot(mat.slice(j)).sum2();
}
}
return new exports.matrix(array, shape);
}else{
return console.error("row of mat isn't equal to col of this !");
}
};
/**
* [slice description]
* @param {[type]} rows [description]
* @param {[type]} cols [description]
* @return {[type]} [description]
*/
this.slice = function(rows, cols){
var rows = (typeof(rows)=='number')?[rows, rows]:rows||[0,-1];
var cols = (typeof(cols)=='number')?[cols, cols]:cols||[0,-1];
rows[0] = rows[0]<0?rows[0]+this.shape[0]:rows[0];
rows[1] = rows[1]<0?rows[1]+this.shape[0]:rows[1];
cols[0] = cols[0]<0?cols[0]+this.shape[1]:cols[0];
cols[1] = cols[1]<0?cols[1]+this.shape[1]:cols[1];
var shape = [(rows[1]-rows[0]+1),(cols[1]-cols[0]+1)];
var array = new Array(shape[0]*shape[1]);
for (var i = rows[0]; i <= rows[1]; i++) {
for (var j = cols[0]; j <= cols[1]; j++) {
array[(i-rows[0])*shape[1]+(j-cols[0])] = this.array[i*this.shape[1]+j];
}
}
return new exports.matrix(array, shape);
};
/**
* [trans description]
* @param {[type]} func [description]
* @return {[type]} [description]
*/
this.trans = function(func){
var array = [];
for (var i = 0; i < this.length(); i++) {
array[i] = func(this.array[i]);
}
return new exports.matrix(array, this.shape);
};
/**
* [pow description]
* @param {[type]} pow [description]
* @return {[type]} [description]
*/
this.pow = function(pow){
var pow = pow||2;
var array = [];
for (var i = 0; i < this.length(); i++) {
array[i] = Math.pow(this.array[i], pow);
}
return new exports.matrix(array, this.shape);
};
/**
* [exp description]
* @param {[type]} base [description]
* @return {[type]} [description]
*/
this.exp = function(base){
var base = base||Math.e;
var array = [];
for (var i = 0; i < this.length(); i++) {
array[i] = Math.pow(base, this.array[i]);
}
return new exports.matrix(array, this.shape);
};
/**
* [sum description]
* @param {[type]} dim [description]
* @return {[type]} [description]
*/
this.sum = function(dim){
var dim = dim || 0;
if(dim==0){
var array = new Array(this.shape[1]);
for (var i = 0; i < array.length; i++) {
array[i] = 0;
for (var j = 0; j < this.shape[0]; j++) {
array[i] += this.array[i*this.shape[0]+j];
}
}
return new exports.matrix(array);
}else{
return this.transpose().sum().transpose();
}
}
/**
* [sum2 description]
* @return {[type]} [description]
*/
this.sum2 = function(){
return this.sum(0).sum(1).array[0] || 0;
};
/**
* [print description]
* @return {[type]} [description]
*/
this.print = function(){
var array = new Array(this.shape[0]);
for (var i = 0; i < this.shape[0]; i++) {
array[i] = new Array(this.shape[1]);
for (var j = 0; j < this.shape[1]; j++) {
array[i][j] = this.array[i*this.shape[1]+j];
}
}
console.log(array);
};
return this;
};