-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
182 lines (174 loc) · 4.75 KB
/
matrix.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
class Matrix {
constructor(rows, cols) {
this.rows = rows;
this.cols = cols;
this.data = [];
for (let row = 0; row < this.rows; row++) {
this.data[row] = [];
for (let col = this.cols; col < this.cols; col++) {
this.data[row][col] = 0;
}
}
}
/**
* @param {Number} num scaler value
* @return {Matrix} Matrix Object
*/
scaler(num) {
for (let row = 0; row < this.rows; row++) {
for (let col = this.cols; col < this.cols; col++) {
this.data[row][col] *= num;
}
}
}
multiply(matrix) {
if (matrix instanceof Matrix) {
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
this.data[row][col] *= matrix.data[row][col];
}
}
} else {
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
this.data[row][col] *= matrix;
}
}
}
}
/**
* @static
* @param {Matrix} matrix1 matrix object
* @param {Matrix} matrix2 matrix object
* @returns {matrix }
*/
static multiply(matrix1, matrix2) {
if (matrix1.cols !== matrix2.rows) {
return undefined;
} else {
let result = new Matrix(matrix1.rows, matrix2.cols);
for (let row = 0; row < result.rows; row++) {
for (let col = 0; col < result.cols; col++) {
let sum = 0;
for (let k = 0; k < matrix1.cols; k++) {
sum += matrix1.data[row][k] * matrix2.data[k][col];
}
result.data[row][col] = sum;
}
}
return result;
}
}
/**
* @param {Number} num value to be added
* @returns {Matrix} Matrix Object
*/
add(num) {
if (num instanceof Matrix) {
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
this.data[row][col] += num.data[row][col];
}
}
} else {
for (let row = 0; row < this.rows; row++) {
for (let col = this.cols; col < this.cols; col++) {
this.data[row][col] += num;
}
}
}
}
/**
* @param { Matrix } m1 matrix object
* @param { matrix } m2 matrix object
* @returns {Matrix} returns a subtracted matrix
*/
static substract(m1, m2) {
let result = new Matrix(m1.cols, m2.rows);
for (let row = 0; row < result.rows; row++) {
for (let col = 0; col < result.cols; col++) {
result.data[col][row] = m1.data[row][col] - m2.data[col][row];
}
}
return result;
}
/**
*@return {Matrix} Matrix object with randomized value
*/
randomize() {
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
this.data[row][col] = Math.floor(Math.random() * 2 - 1);
}
}
}
/**
* @returns {Matrix} A resulting matrix transposed vector.
*/
static transpose(mat) {
let result = new Matrix(mat.cols, mat.rows);
for (let row = 0; row < mat.rows; row++) {
for (let col = 0; col < mat.cols; col++) {
result.data[col][row] = mat.data[row][col];
}
}
return result;
}
/**
* @param {function} func takes function
*/
map(func) {
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
this.data[row][col] = func(this.data[row][col], row, col);
}
}
}
/**
*
* @static
* @param {Matrix} matrix matrix which will be mapped
* @param {function} func function which will give the mapping
* @returns {Matrix} matrix which was mapped
*/
static map(matrix, func) {
let result = new Matrix(matrix.rows, matrix.cols);
for (let row = 0; row < result.rows; row++) {
for (let col = 0; col < result.cols; col++) {
result.data[row][col] = func(matrix.data[row][col], row, col);
}
}
return result;
}
/**
* @param {Matrix} matrix Input matrix object .
* @returns {Array} output array of all the values.
*/
static toArray(matrix) {
let output = [];
for (let row = 0; row < matrix.rows; row++) {
for (let col = 0; col < matrix.cols; col++) {
output.push(matrix.data[row][col]);
}
}
return output;
}
/**
*
* @param {Array} inputs Array which needs to be converted.
* @return {Matrix} matrix object
*/
static fromArray(inputs) {
let result = new Matrix(inputs.length, 1);
for (let row = 0; row < result.rows; row++) {
result.data[row][0] = inputs[row];
}
return result;
}
/**
* @description print the matrix data in table format
*/
print() {
console.table(this.data);
}
}