-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.js
More file actions
130 lines (110 loc) · 3.31 KB
/
matrix.js
File metadata and controls
130 lines (110 loc) · 3.31 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
class Matrix {
constructor(rows, cols) {
if (rows && rows > 0) {
this.rows = rows;
} else {
throw new Error("rows arg required")
}
if (cols && cols > 0) {
this.cols = cols;
} else {
throw new Error("rows arg required")
}
this.data = [];
//* Add Rows
for (let i = 0; i < this.rows; i++) {
this.data[i] = [];
for (let j = 0; j < this.cols; j++) {
this.data[i][j] = 0
}
}
}
static multiply(a, b) {
if (a.cols !== b.rows) {
console.log("Number of Columns of A must match number of rows of B");
return undefined;
}
return new Matrix(a.rows, b.cols)
.map((_, i, j) => {
let sum = 0;
for (let k = 0; k < a.cols; k++) {
sum += a.data[i][k] * b.data[k][j]
}
return sum;
});
}
/**
* @returns {Matrix} a - b
*/
static subtract(a, b) {
if (a.rows !== b.rows || a.cols !== b.cols) {
console.log("Columns and Rows of A must match Columns and Rows of B");
return;
}
return new Matrix(a.rows, a.cols)
.map((_, i, j) => a.data[i][j] - b.data[i][j]);
}
static fromArray(arr) {
return new Matrix(arr.length, 1).map((_, i) => arr[i]);
}
toArray() {
let array = [];
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
array.push(this.data[i][j]);
}
}
return array;
}
add(n) {
if (n instanceof Matrix) {
if (this.rows !== n.rows || this.cols !== n.cols) {
console.log("Columns and Rows of A must match Columns and Rows of B");
return;
}
//* Element-wise
return this.map((e, i, j) => e + n.data[i][j]);
} else {
//* Scalor Operation
return this.map(e => e + n);
}
}
static transpose(matrix) {
return new Matrix(matrix.cols, matrix.rows)
.map((_, i, j) => matrix.data[j][i]);
}
map(fn) {
//* Apply a function to every element of a matrix
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
let val = this.data[i][j];
this.data[i][j] = fn(val, i, j);
}
}
return this;
}
static map(matrix, fn) {
return new Matrix(matrix.rows, matrix.cols)
.map((e, i, j) => fn(matrix.data[i][j], i, j));
}
multiply(n) {
if (n instanceof Matrix) {
if (this.rows !== n.rows || this.cols !== n.cols) {
console.log("Columns and Rows of A must match Columns and Rows of B");
return;
}
//? hadamard product (element-wise)
return this.map((e, i, j) => e * n.data[i][j]);
} else {
//* Scalor Operation
return this.map(e => e * n);
}
}
//* Helper Functions
randomize() {
return this.map(e => Math.random() * 2 - 1);
}
print() {
console.table(this.data);
}
}