-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.js
More file actions
184 lines (141 loc) · 4.21 KB
/
Matrix.js
File metadata and controls
184 lines (141 loc) · 4.21 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
class Matrix {
constructor(rows, cols, init) {
this.rows = rows;
this.cols = cols;
this.v = [];
for (let i = 0; i < this.rows; i++) {
this.v[i] = [];
for (let j = 0; j < this.cols; j++) {
if (init) {
this.v[i][j] = init[i][j];
} else {
this.v[i][j] = 0;
}
}
}
}
mult = function(n) {
if (n instanceof Matrix) {
if (this.cols !== n.rows) {
console.log('Columns don\'t match rows!');
return undefined;
}
let toReturn = new Matrix(this.rows, n.cols)
for (let i = 0; i < toReturn.rows; i++) {
for (let j = 0; j < toReturn.cols; j++) {
let sum = 0;
for (let k = 0; k < this.cols; k++) {
sum += this.v[i][k] * n.v[k][j];
}
toReturn.v[i][j] = sum
}
}
return toReturn;
} else {
return this.apply(x => x*n);
}
}
add = function(n) {
if (n instanceof Matrix) {
let toReturn = new Matrix(this.rows, this.cols);
for (let i = 0; i < this.matrix.length; i++) {
for (let j = 0; j < this.matrix[i].length; j++) {
toReturn.v[i][j] += n.v[i][j];
}
}
return toReturn;
} else {
return this.apply(x => x+n);
}
}
randomize = function(min, max) {
if (!min) min = 0;
if (!max) max = 10;
return this.apply(x => Math.random() * (max - min) + min);
}
apply = function(callback) {
let toReturn = new Matrix(this.rows, this.cols);
for (let i = 0; i < toReturn.rows; i++) {
for (let j = 0; j < toReturn.cols; j++) {
toReturn.v[i][j] = callback(toReturn.v[i][j]);
}
}
return toReturn;
}
getX = function() {
return this.v[0][0];
}
getY = function() {
return this.v[1][0];
}
getZ = function() {
return this.v[2][0];
}
static cubicPoints = function(r) {
return [
new Matrix(3, 1, [[-r], [-r], [-r]]),
new Matrix(3, 1, [[-r], [-r], [r]]),
new Matrix(3, 1, [[-r], [r], [-r]]),
new Matrix(3, 1, [[-r], [r], [r]]),
new Matrix(3, 1, [[r], [-r], [-r]]),
new Matrix(3, 1, [[r], [-r], [r]]),
new Matrix(3, 1, [[r], [r], [-r]]),
new Matrix(3, 1, [[r], [r], [r]])
]
}
static nDimensionSquare = function(r, d) {
let toReturn = [];
for (let i = 0; i < pow(2, d); i++) {
let init = [];
for(let j = 0; j < d; j++){
init.push([(1 << j) & i ? r : -r])
}
toReturn.push(new Matrix(d, 1, init));
}
return toReturn;
}
static rotateX = function(theta) {
return new Matrix(3, 3, [
[1, 0, 0],
[0, cos(theta), -sin(theta)],
[0, sin(theta), cos(theta)]
])
}
static rotateY = function(theta) {
return new Matrix(3, 3, [
[cos(theta), 0, sin(theta)],
[0, 1, 0],
[-sin(theta), 0, cos(theta)]
])
}
static rotateZ = function(theta) {
return new Matrix(3, 3, [
[cos(theta), -sin(theta), 0],
[sin(theta), cos(theta), 0],
[0, 0, 1]
])
}
static nDimensionProjection = function(d) {
let init = [];
for (let i = 0; i < d; i++) {
let toPush = [];
for (let j = 0; j < d+1; j++) {
if (i == j) {
toPush.push(1);
} else {
toPush.push(0)
}
}
init.push(toPush);
}
return new Matrix(d, d+1, init);
}
static nDimensionRotation = function(d) {
}
static utils = {
orthoProjection: new Matrix(2, 3, [
[1, 0, 0],
[0, 1, 0]
])
}
}