-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
163 lines (124 loc) · 3.83 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
class Matrix {
constructor(rows, columns) {
this.rows = rows;
this.columns = columns;
this.data = [];
for (let i = 0; i < rows; i++) {
this.data[i] = [];
for (let j = 0; j < columns; j++) {
this.data[i][j] = 0;
}
}
}
static fromArray(input_array) {
let inputs = new Matrix(input_array.length, 1);
for (let index = 0; index < input_array.length; index++) {
inputs.data[index][0] = input_array[index];
}
return inputs;
}
toArray() {
let result = []
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.columns; j++) {
result[i + j] = this.data[i][j];
}
}
return result;
}
//Multiply two matrices
static multiply(a, b) {
// console.log(a,b)
if (a.columns !== b.rows)
return console.error("The rows of a don't match the columns of b");
let result = new Matrix(a.rows, b.columns);
// result.rows = a.rows;
// result.columns = b.columns;
for (let i = 0; i < a.rows; i++) {
for (let j = 0; j < b.columns; j++) {
let sum = 0;
for (let k = 0; k < a.columns; k++) {
sum += a.data[i][k] * b.data[k][j];
}
result.data[i][j] = sum;
}
}
return result;
}
multiply(k) {
if(k instanceof Matrix)
{
for (let i = 0; i < this.rows; i++)
for (let j = 0; j < this.columns; j++)
this.data[i][j] *= k.data[i][j];
}
else
{
for (let i = 0; i < this.rows; i++)
for (let j = 0; j < this.columns; j++)
this.data[i][j] *= k;
}
}
static subtract(a, b) {
let result = new Matrix(a.rows, a.columns);
for (let i = 0; i < result.rows; i++) {
for (let j = 0; j < result.columns; j++) {
result.data[i][j] = a.data[i][j] - b.data[i][j];
}
}
return result;
}
//Add two matrices
add(b) {
if (b instanceof Matrix) {
for (let i = 0; i < this.rows; i++)
for (let j = 0; j < this.columns; j++)
this.data[i][j] += b.data[i][j];
}
else {
for (let i = 0; i < this.rows; i++)
for (let j = 0; j < this.columns; j++)
this.data[i][j] += b;
}
}
//Gerate a random matrix
randomize() {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.columns; j++) {
this.data[i][j] = (Math.random() * 2 - 1);
}
}
}
//Transpose a matrix
static transpose(a) {
let result = new Matrix(a.columns, a.rows);
for (let i = 0; i < a.rows; i++) {
for (let j = 0; j < a.columns; j++) {
result.data[j][i] = a.data[i][j];
}
}
return result;
}
//Print a matrix
print() {
console.table(this.data)
}
map(func) {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.columns; j++) {
this.data[i][j] = func(this.data[i][j]);
}
}
}
static map(a, func) {
let result = new Matrix(a.rows, a.columns);
for (let i = 0; i < a.rows; i++) {
for (let j = 0; j < a.columns; j++) {
result.data[i][j] = func(a.data[i][j]);
}
}
return result;
}
}
module.exports = Matrix;
// export default Matrix;