-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix.mc
167 lines (153 loc) · 3.72 KB
/
Matrix.mc
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
/**
* Vector and matrix math routines.
*
* All vectors and matrices are stored in arrays with n*m elements, enumerating
* first in x (column), then in y (row), e.g., the array
* a = [0, 1, 2, 3, 4, 5, 6, 7, 8];
* represents the 3x3 matrix
* / 0 1 2 \
* | 3 4 5 |
* \ 6 7 8 /
*
* The functions don't check the inputs (for performance reasons), e.g., for
* proper array sizes. The caller has to ensure that.
*
* The functions are not specially optimized.
*/
using Toybox.Math;
module Matrix {
/**
* Creates and returns a len=x*y matrix with 0.0.
*/
function getZero(len) {
var result = new [len];
var i;
for (i = 0; i < len; i++) {
result[i] = 0.0;
}
return result;
}
/**
* Creates and returns an xy*xy identity matrix.
*/
function getEye(xy) {
var result = getZero(xy*xy);
var i;
for (i = 0; i < xy*xy; i+=(xy+1)) {
result[i] = 1.0;
}
return result;
}
/**
* Adds two vectors or matrices and returns the result.
*/
function add(a, b) {
var result = new [a.size()]; // don't overwrite a or b, because Arrays are passed by referece
var i;
for (i = 0; i < a.size(); i++) {
result[i] = a[i] + b[i];
}
return result;
}
/**
* Subtracts two vectors or matrices and returns the result.
*/
function sub(a, b) {
var result = new [a.size()]; // don't overwrite a or b, because Arrays are passed by referece
var i;
for (i = 0; i < a.size(); i++) {
result[i] = a[i] - b[i];
}
return result;
}
/**
* Calculates the dot product of two vectors of identical length.
*/
function dot(a, b) {
var result = 0.0;
var i;
for (i = 0; i < a.size(); i++) {
result += a[i] * b[i];
}
return result;
}
/**
* Calculates the cross product of two 3x1 vectors.
*/
function cross(a, b) {
var result = new [3];
result[0] = a[1]*b[2] - b[1]*a[2];
result[1] = a[2]*b[0] - b[2]*a[0];
result[2] = a[0]*b[1] - b[0]*a[1];
return result;
}
/**
* Calculates the length/norm of a vector of arbitrary length.
*/
function length(a) {
var result = 0.0;
var i;
for (i = 0; i < a.size(); i++) {
result += a[i]*a[i];
}
return Math.sqrt(result);
}
/**
* Create a vector with the same direction but with a length of 1.0.
*/
function normalize(a) {
var result = new [a.size()]; // don't overwrite a or b, because Arrays are passed by referece
var len = length(a);
if (len == 0.0) {
return NaN;
}
var m = 1.0/len; // small optimization: device once, multiply multiple times
var i;
for (i = 0; i < a.size(); i++) {
result[i] = a[i] * m;
}
return result;
}
/**
* Matrix multiplication of two 4x4 matrices
*/
function mul4x4(a, b) {
var result = new [4*4];
var sum;
var x,y,i;
for (x = 0; x < 4; x++) {
for (y = 0; y < 4; y++) {
sum = 0.0;
for (i = 0; i < 4; i++) {
sum += a[y*4+i] * b[i*4+x];
}
result[y*4+x] = sum;
}
}
//System.println("mul4x4: " + a);
//System.println("mul4x4: " + b);
//System.println("mul4x4: " + result);
return result;
}
/**
* Matrix multiplication of a 4x4 matrix with a 1x4 vector, resulting in a
* 1x4 vector.
*/
function mul4x4x1x4(a, b) {
var result = new [1*4];
var sum;
var y,i;
for (y = 0; y < 4; y++) {
sum = 0.0;
for (i = 0; i < 4; i++) {
sum += a[y*4+i] * b[i];
}
result[y] = sum;
}
//System.println("mul4x4x1x4: a = " + a);
//System.println("mul4x4x1x4: b = " + b);
//System.println("mul4x4x1x4: r = " + result);
return result;
}
}
// vi:syntax=javascript filetype=javascript