-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrixmath.js
More file actions
executable file
·161 lines (144 loc) · 4.26 KB
/
matrixmath.js
File metadata and controls
executable file
·161 lines (144 loc) · 4.26 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
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
class Matrix {
constructor(rows, cols) {
this.rows = rows;
this.cols = cols;
//MATRIX DE ZEROS;
this.mInfo = [];
for(let i = 0; i < this.rows; i++){
this.mInfo[i] = [];
for(let j = 0; j < this.cols; j++){
this.mInfo[i][j] = 0;
}
}
//
}
copy() {
let m = new Matrix(this.rows, this.cols);
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
m.mInfo[i][j] = this.mInfo[i][j]
}
}
return m;
}
static transfArrayM(array) {
let m = new Matrix(array.length, 1);
for (let i = 0; i < array.length; i++) {
m.mInfo[i][0] = array[i];
}
return m;
}
transfMatrixA() {
let array = [];
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
array.push(this.mInfo[i][j]);
}
}
return array;
}
randomMatrix() {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
this.mInfo[i][j] = floor(random() * 2 - 1);
}
}
}
static transp(a) {
let c = new Matrix(a.cols, a.rows);
for (let i = 0; i < a.rows; i++) {
for (let j = 0; j < a.cols; j++) {
c.mInfo[j][i] = a.mInfo[i][j];
}
}
return c;
}
add(s) {
if (s instanceof Matrix) {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
this.mInfo[i][j] += s.mInfo[i][j];
}
}
} else {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
this.mInfo[i][j] += s;
}
}
}
}
static sub(a, b) {
if (a.rows !== b.rows || a.cols !== b.cols) {
console.log('Rows and Columns must be equal');
return undefined;
}
//sub c = A - B
let c = new Matrix(a.rows, a.cols);
for (let i = 0; i < c.rows; i++) {
for (let j = 0; j < c.cols; j++) {
c.mInfo[i][j] = a.mInfo[i][j] - b.mInfo[i][j];
}
}
return c;
}
mult(s) {
if (s instanceof Matrix) {
//hadamard product
for(let i = 0 ; i < this.rows; i++){
for(let j = 0 ; j < this.cols; j++){
this.mInfo[i][j] *= s.mInfo[i][j]
}
}
} else {
//produto por escalar
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
this.mInfo[i][j] *= s;
}
}
}
}
static mult(a, b) {
if (a.cols !== b.rows) {
console.log('Number of Columns of A must be equal to the number Rows of B');
return undefined;
}
//multiplicando c = A x B
let c = new Matrix(a.rows, b.cols);
for (let i = 0; i < c.rows; i++) {
for (let j = 0; j < c.cols; j++) {
let sum = 0;
for (let k = 0; k < a.cols; k++) {
sum += a.mInfo[i][k] * b.mInfo[k][j];
}
c.mInfo[i][j] = sum;
}
}
return c;
}
addFunc(f) {
//aplicar funcao para cada elemento
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
let n = this.mInfo[i][j];
this.mInfo[i][j] = f(n);
}
}
}
static addFunc(a, f) {
//aplicar funcao para cada elemento
let c = new Matrix(a.rows, a.cols);
for (let i = 0; i < a.rows; i++) {
for (let j = 0; j < a.cols; j++) {
let n = a.mInfo[i][j];
c.mInfo[i][j] = f(n);
}
}
return c;
}
show() {
console.table(this.mInfo);
return this;
}
}