-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.js
337 lines (292 loc) · 8.26 KB
/
functions.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//////////////////////////////////////////////////////////////////////////////
// //
// MATRIX ARITHMETICS AND RELATED FUNCTIONS //
// //
//////////////////////////////////////////////////////////////////////////////
// Matrix multiplication
function mult(a, b) {
let a_rows = a.length,
a_cols = a[0].length,
b_rows = b.length,
b_cols = b[0].length;
if (a_cols !== b_rows) {
$('#here').append("Multiplication error: a_cols != b rows");
}
let prod = new Array(a_rows);
for (let i = 0; i < a_rows; i++) {
prod[i] = new Array(b_cols);
}
for (let i = 0; i < a_rows; i++) {
for (let j = 0; j < b_cols; j++) {
prod[i][j] = 0;
for (let y = 0; y < a_cols; y++) {
prod[i][j] += a[i][y] * b[y][j];
}
}
}
return prod;
}
// Matrix addition
function add(a, b) {
let a_rows = a.length,
a_cols = a[0].length,
b_rows = b.length,
b_cols = b[0].length;
if (a_rows !== b_rows) $('#here').append("Addition error: a_rows != b_rows");
if (a_cols !== b_cols) $('#here').append("Addition error: a_cols != b_cols");
let sum = init_array(a_rows, a_cols);
for (let i = 0; i < a_rows; i++) {
for (let j = 0; j < a_cols; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
return sum;
}
// Matrix subtraction
function sub(a, b) {
let a_rows = a.length,
a_cols = a[0].length,
b_rows = b.length,
b_cols = b[0].length;
if (a_rows !== b_rows) $('#here').append("Subtraction error: a_rows != b_rows");
if (a_cols !== b_cols) $('#here').append("Subtraction error: a_cols != b_cols");
let diff = init_array(a_rows, a_cols);
for (let i = 0; i < a_rows; i++) {
for (let j = 0; j < a_cols; j++) {
diff[i][j] = a[i][j] - b[i][j];
}
}
return diff;
}
// Scalar division for matrices
function scalar_div(array, scalar) {
let rows = array.length;
let cols = array[0].length;
let div = [];
for (let i = 0; i < rows; i++) {
let tmp = [];
for (let j = 0; j < cols; j++) {
if (scalar !== 0) {
tmp.push(array[i][j] / scalar);
}
else {
tmp.push(0);
}
}
div.push(tmp);
}
return div;
}
// Scalar multiplication for matrices
function scalar_mult(array, scalar) {
let rows = array.length;
let cols = array[0].length;
let prod = [];
for (let i = 0; i < rows; i++) {
let tmp = [];
for (let j = 0; j < cols; j++) {
tmp.push(array[i][j] * scalar);
}
prod.push(tmp);
}
return prod;
}
// Matrix transposition
function transpose(array) {
let rows = array.length,
cols = array[0].length;
let tp = [];
for (let i = 0; i < cols; i++) {
let tmp_array = [];
for (let j = 0; j < rows; j++) {
tmp_array.push(array[j][i]);
}
tp.push(tmp_array);
}
return tp;
}
// Matrix row appendition
function row_append(a, b) {
let a_rows = a.length,
a_cols = a[0].length,
b_rows = b.length,
b_cols = b[0].length;
if (a_cols !== b_cols) $('#here').append("Row appendition error: a_cols != b_cols");
let result = init_array((a_rows + b_rows), a_cols);
for (let i = 0; i < (a_rows + b_rows); i++) {
if (i < a_rows) {
for (let j = 0; j < a_cols; j++) {
result[i][j] = a[i][j];
}
}
else {
for (let j = 0; j < a_cols; j++) {
result[i][j] = b[(i - a_rows)][j];
}
}
}
return result;
}
// Matrix column appention
function col_append(a, b) {
let a_rows = a.length,
a_cols = a[0].length,
b_rows = b.length,
b_cols = b[0].length;
if (a_rows !== b_rows) $('#here').append("Column appention error: a_rows != b_rows");
let result = init_array(a_rows, (a_cols + b_cols));
for (let i = 0; i < (a_cols + b_cols); i++) {
if (i < a_cols) {
for (let j = 0; j < a_rows; j++) {
result[j][i] = a[j][i];
}
}
else {
for (let j = 0; j < a_rows; j++) {
result[j][i] = b[j][(i - a_cols)];
}
}
}
return result;
}
// Dynamic Matrix row appendition. Appends a row of element to the matrix
function dyn_row_append(a, element) {
let a_rows = a.length,
a_cols = a[0].length;
let result = init_array((a_rows + 1), a_cols);
for (let i = 0; i < (a_rows + 1); i++) {
if (i === a_rows) {
for (let j = 0; j < a_cols; j++) {
result[i][j] = element;
}
}
else {
for (let j = 0; j < a_cols; j++) {
result[i][j] = a[i][j];
}
}
}
return result;
}
// Dynamic Matrix row prepention. Prepends a row of element to the matrix
function dyn_row_prepend(a, element) {
let a_rows = a.length,
a_cols = a[0].length;
let result = new Array((a_rows + 1));
for (let i = 0; i < (a_rows + 1); i++) {
result[i] = new Array(a_cols);
if (i === 0) {
for (let j = 0; j < a_cols; j++) {
result[i][j] = element;
}
}
else {
for (let j = 0; j < a_cols; j++) {
result[i][j] = a[i - 1][j];
}
}
}
return result;
}
// Dynamic Matrix column appention. Appends colum of element to the matrix
function dyn_col_append(a, element) {
let a_rows = a.length,
a_cols = a[0].length;
let result = init_array(a_rows, (a_cols + 1));
for (let i = 0; i < (a_cols + 1); i++) {
if (i === a_cols) {
for (let j = 0; j < a_rows; j++) {
result[j][i] = element;
}
}
else {
for (let j = 0; j < a_rows; j++) {
result[j][i] = a[j][i];
}
}
}
return result;
}
function rows(matrix, start, end) {
let new_matrix = [];
for (let i = start; i < end; i++) {
new_matrix.push(matrix[i]);
}
return new_matrix;
}
function columns(matrix, start, end) {
let rows = matrix.length;
let new_matrix = [];
for (let i = 0; i < rows; i++) {
let tmp = [];
for (let j = start; j < end; j++) {
tmp.push(matrix[i][j]);
}
new_matrix.push(tmp);
}
return new_matrix;
}
function extract_row(a, index) {
let row = a[index];
let matrix = [];
matrix.push(row);
return matrix;
}
function column(a, index) {
let a_rows = a.length;
let matrix = [];
for (let i = 0; i < a_rows; i++) {
let tmp = [];
tmp.push(a[i][index]);
matrix.push(tmp);
}
return matrix;
}
function get_value(matrix) {
return matrix[0][0];
}
// Euclidean norm of a vector
function euclid_norm(matrix) {
let rows = matrix.length,
cols = matrix[0].length;
let norm = 0;
if (cols === 1) { //it's a column vector
for (let i = 0; i < rows; i++) {
norm += Math.pow(matrix[i][0], 2);
}
}
else { //it's a row vector
for (let i = 0; i < cols; i++) {
norm += Math.pow(matrix[0][i], 2);
}
}
return Math.sqrt(norm);
}
function norm(vector) {
let length = vector[0].length;
let tmp = 0;
for (let i = 0; i < length; i++) {
tmp += vector[0][i];
}
return Math.sqrt(tmp);
}
function clone_array(array) {
let rows = array.length;
let clone = [];
for (let i = 0; i < rows; i++) {
clone.push(array[i].slice(0));
}
return clone;
}
// Helper function that initializes and returns an array of arrays (that represents a matrix) of given size with null values
function init_array(rows, columns) {
let array = [];
for (let row_index = 0; row_index < rows; row_index++) {
let tmp_array = [];
for (let column_index = 0; column_index < columns; column_index++) {
tmp_array.push(null);
}
array.push(tmp_array);
}
return array;
}