-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
192 lines (170 loc) · 3.16 KB
/
math.js
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
'use strict'
console.log('math.js loaded')
/*
RETURNS:
*/
function mCreate(rows,cols,amp){
mCreateTest(rows,cols,amp)
const newM = new Array(rows)
for(let i=0;i<rows;i++){
newM[i] = new Array(cols)
for(let j=0;j<cols;j++){
newM[i][j] = 2*amp*Math.random() - amp
}
}
return newM
}
/*
MODIFIES m
*/
function mMutate(m,amp){
mMutateTest(m,amp)
const rows = m.length
const cols = m[0].length
for(let i=0;i<rows;i++){
for(let j=0;j<cols;j++){
m[i][j] += amp*2*Math.random() - amp
}
}
}
/*
ASSUMES:
1) v1 and v2 are VECTORS
MODIFIES v1
*/
function mAddSig(v1,v2){
mAddSigTest(v1,v2)
for(let i=0;i<v1.length;i++){
v1[i][0] = sig(v1[i][0]+v2[i][0])
}
}
function sig(x){
return 1/(1+Math.exp(-x))
}
/*
m1 - matrix
v - vector
RETURNS
*/
function mMulti(m1,v){
mMultiTest(m1,v)
const newM = new Array(m1.length)
let sum = 0
for(let i=0;i<m1.length;i++){
sum = 0
for(let j=0;j<v.length;j++){
sum+=m1[i][j]*v[j][0]
}
newM[i] = [sum]
}
return newM
}
/*
MODIFIES
*/
// function arrToVec(arr){
// arrToVecTest(arr)
// for(let i=arr.length-1;i>-1;i--){
// arr[i] = [arr[i]]
// }
// }
function arrToVec(arr){
arrToVecTest(arr)
for(let i=0;i<arr.length;i++){
arr[i] = [arr[i]]
}
}
/*
RETURNS
*/
function mCopy(m){
mCopyTest(m)
const newM = new Array(m.length)
for(let i=0;i<m.length;i++){
newM[i] = new Array(m[0].length)
for(let j=0;j<m[0].length;j++){
newM[i][j] = m[i][j]
}
}
return newM
}
//#############
// TESTS
//#############
/*
m.length < 1
m[0].length < 0
*/
function mCopyTest(m){
if(m.length <1){
throw new Error('ERROR: m.length < 1')
}
if(m[0].length < 0){
throw new Error('ERROR: m[0].length < 0')
}
}
/*
arr.length < 1
arr[0].length !== undefined
*/
function arrToVecTest(arr){
if(arr[0].length !== undefined){
console.log(arr)
throw new Error('ERROR: arr[0].length !== undefined')
}
}
/*
v[0].length !== 1
m1[0].length !== m2.length
m1.length < 1 || m2.length < 1
m1[0].length < 1 || m2[0].length < 1
*/
function mMultiTest(m1,v){
if(v[0].length !== 1){
throw new Error('ERROR: v[0].length !== 1')
}
if(m1[0].length !== v.length){
throw new Error('ERROR: m1[0].length !== v.length')
}
if(m1.length < 1 || v.length < 1){
throw new Error('ERROR: m1.length < 1 || v.length < 1')
}
if(m1[0].length < 1 || v[0].length < 1){
throw new Error('ERROR: m1[0].length < 1 || v[0].length < 1')
}
}
/*
v1.length !== v2.length
v1 or v2 NOT column matrix
*/
function mAddSigTest(v1,v2){
if(v1.length !== v2.length){
throw new Error('ERROR: v1.length !== v2.length')
}
if(v1.length < 1 || v2.length < 1){
throw new Error('ERROR: v1.length < 1 || v2.length < 1')
}
if(v1[0].length !== 1 || v2[0].length !== 1){
throw new Error('ERROR: v1[0].length !== 1 || v2[0].length !== 1')
}
}
function mMutateTest(m,amp){
if(m.length < 1 || m[0].length < 1){
throw new Error('ERROR: m.length < 1 || m[0].length < 1')
}
if(isNaN(amp)){
throw new Error('ERROR: isNaN(amp)')
}
}
/*
1) row or col === 0
2) amp is non number
*/
function mCreateTest(rows,cols,amp){
if(rows === 0 || cols === 0){
throw new Error('ERROR: rows === 0 || cols === 0')
}
if(isNaN(amp)){
throw new Error('ERROR: isNaN(amp)')
}
}