-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
52 lines (52 loc) · 1.41 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
var Matrix = (function(){
var dim = 4;
function Matrix(data) {
this.data = data;
}
Matrix.zero = function() {
var data = [];
for(var i=0; i<dim; i++) {
data.push([]);
for(var j=0; j<dim; j++) {
data[i].push(0);
}
}
return new Matrix(data);
}
Matrix.identity = function() {
var id = Matrix.zero();
for(var i=0; i<dim; i++) {
id.data[i][i] = 1;
}
return id;
}
Matrix.rotation = function(x1, x2, theta) {
var R = Matrix.identity();
R.data[x1][x1] = Math.cos(theta);
R.data[x1][x2] = -Math.sin(theta);
R.data[x2][x1] = Math.sin(theta);
R.data[x2][x2] = Math.cos(theta);
return R;
}
Matrix.multiply = function(m1, m2) {
var result = Matrix.zero();
for(var i=0; i<dim; i++) {
for(var j=0; j<dim; j++) {
for(var k=0; k<dim; k++) {
result.data[i][j] += m1.data[i][k]*m2.data[k][j];
}
}
}
return result;
}
Matrix.apply = function(m, v) {
var result = [0,0,0,0];
for(var i=0; i<dim; i++) {
for(var k=0; k<dim; k++) {
result[i] += m.data[i][k]*v[k];
}
}
return result;
}
return Matrix;
})();