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
|
// matrix and vector functions.
//
//
function vector_add(a, b, result){
if(!result) result = [];
if(a.length > b.lemgth){
var tmp = a;
a = b; b = tmp; }
for(var i = 0; i < a.length; ++i){
result[i] = a[i] + b[i]; }
for(var i = a.length; i < b.length; ++i){
result[i] = b[i]; }
return result; }
function vector_cpy(result, a){
for(var i = 0; i < a.length; ++i){
result[i] = a[i]; }
}
function vector_minus(a, b, result){
if(!result) result = [];
var lim = Math.max(a.length, b.length);
for(var i = 0; i < lim; ++i){
var aValue, bValue;
if(i < a.length)
aValue = a[i];
|
>
>
>
>
>
|
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
|
// matrix and vector functions.
//
//
function vector_add(a, b, result){
if(!result) result = [];
if(!a) throw "first argument to vector_add missing";
if(!b) throw "second argument to vector_add missing";
if(a.length > b.lemgth){
var tmp = a;
a = b; b = tmp; }
for(var i = 0; i < a.length; ++i){
result[i] = a[i] + b[i]; }
for(var i = a.length; i < b.length; ++i){
result[i] = b[i]; }
return result; }
function vector_cpy(result, a){
if(!a) throw "first argument to vector_cpy missing";
for(var i = 0; i < a.length; ++i){
result[i] = a[i]; }
}
function vector_minus(a, b, result){
if(!result) result = [];
if(!a) throw "first argument to vector_minus missing";
if(!b) throw "second argument to vector_minus missing";
var lim = Math.max(a.length, b.length);
for(var i = 0; i < lim; ++i){
var aValue, bValue;
if(i < a.length)
aValue = a[i];
|
︙ | | | ︙ | |
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
|
return result; }
function vector_dot(a, b){
var lim = Math.min(a.length, b.length);
var result = 0;
for(var i = 0; i < lim; ++i){
result += a[i] * b[i]; }
return result; }
function vector_norm(a){
return Math.sqrt(vector_dot(a,a)); }
function vector_scale(a, s, result){
if(!result) result = [];
for(var i = 0; i < a.length; ++i){
result[i] = s * a[i]; }
return result; }
// returns true iff the zero tail padded vectors match
function vector_cmp(a, b){
if(a.length > b.length){
var tmp = a;
a = b; b = tmp; }
for(var i = 0; i < a.length; ++i){
if(a[i] != b[i])
return false; }
for(var i = a.length; i < b.length; ++i){
if(b[i] != 0)
return false; }
return true; }
function midpoint(a, b, result){
if(b.length > a.length){
var tmp = a;
a = b; b = tmp; }
if(!result) result = new Array(b.length);
|
>
>
>
>
>
>
>
>
|
>
>
|
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
|
return result; }
function vector_dot(a, b){
var lim = Math.min(a.length, b.length);
if(!a) throw "first argument to vector_dot missing";
if(!b) throw "second argument to vector_dot missing";
var result = 0;
for(var i = 0; i < lim; ++i){
result += a[i] * b[i]; }
return result; }
function vector_norm(a){
if(!a) throw "argument to vector_norm missing";
return Math.sqrt(vector_dot(a,a)); }
function vector_scale(a, s, result){
if(!a) throw "first argument to vector_scale missing";
if(s === undefined || s === null) throw "scalar argument to vector_scale missing";
if(!result) result = [];
for(var i = 0; i < a.length; ++i){
result[i] = s * a[i]; }
return result; }
// returns true iff the zero tail padded vectors match
function vector_cmp(a, b){
if(!a) throw "first argument to vector_cmp missing";
if(!b) throw "second argument to vector_cmp missing";
if(a.length > b.length){
var tmp = a;
a = b; b = tmp; }
for(var i = 0; i < a.length; ++i){
if(a[i] != b[i])
return false; }
for(var i = a.length; i < b.length; ++i){
if(b[i] != 0)
return false; }
return true; }
function vector_midpoint(a, b, result){
if(!a) throw "first argument to vector_midpoint missing";
if(!b) throw "second argument to vector_midpoint missing";
if(b.length > a.length){
var tmp = a;
a = b; b = tmp; }
if(!result) result = new Array(b.length);
|
︙ | | | ︙ | |
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
}
//this function may not care, but the expected matrix format in this project is a list of column vectors.
function matrix_transpose(matrix){
var result = [];
var dim0 = matrix.length;
if(!dim0)
return result;
var dim1 = matrix[0].length;
|
>
|
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
}
//this function may not care, but the expected matrix format in this project is a list of column vectors.
function matrix_transpose(matrix){
if(!matrix) throw "argument to matrix_transpose missing";
var result = [];
var dim0 = matrix.length;
if(!dim0)
return result;
var dim1 = matrix[0].length;
|
︙ | | | ︙ | |
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
function matrix_mult(a, b){
var result = [];
var a = matrix_transpose(a);
var dim0 = a.length;
var dim1 = b.length;
for(var i = 0; i < dim1; ++i){
|
>
>
|
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
function matrix_mult(a, b){
if(!a) throw "first argument to matrix_mult missing";
if(!b) throw "second argument to matrix_mult missing";
var result = [];
var a = matrix_transpose(a);
var dim0 = a.length;
var dim1 = b.length;
for(var i = 0; i < dim1; ++i){
|
︙ | | | ︙ | |
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// finds the inverse of a 3 by 3 matrix, returns false if matrix has determinant zero.
//
function matrix33inv(m){
// the matrix format is a list of column vectors.
var a = m[0][0];
var b = m[1][0];
var c = m[2][0];
var d = m[0][1];
var e = m[1][1];
|
>
|
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
// finds the inverse of a 3 by 3 matrix, returns false if matrix has determinant zero.
//
function matrix33inv(m){
if(!m) throw "argument to matrix33inv missing";
// the matrix format is a list of column vectors.
var a = m[0][0];
var b = m[1][0];
var c = m[2][0];
var d = m[0][1];
var e = m[1][1];
|
︙ | | | ︙ | |
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// the matrix format is a list of column vectors.
var result = [[di*A, di*B, di*C],
[di*D, di*E, di*F],
[di*G, di*H, di*K]];
return result; }
function compute_gradient(func, point, error){
if(!error) error = 0.00001;
// find gradient
var otherpoint = point.slice(0);
var gradient = new Array(point.length);
var pointvalue = func(point);
|
|
>
>
>
>
|
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// the matrix format is a list of column vectors.
var result = [[di*A, di*B, di*C],
[di*D, di*E, di*F],
[di*G, di*H, di*K]];
return result; }
// does not check if function is differentiable.
function compute_gradient(func, point, error){
if(!func) throw "function argument to compute_gradient missing";
if(!point) throw "point argument to compute_gradient missing";
if(!error) error = 0.00001;
// find gradient
var otherpoint = point.slice(0);
var gradient = new Array(point.length);
var pointvalue = func(point);
|
︙ | | | ︙ | |
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
//
// equation at:
// http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/ArbitraryAxisRotation.html
// creates a rotation matrix about the given vector.
//
function vector_rotation(vector, s){
var len = Math.sqrt(vector[0]*vector[0]
+ vector[1]*vector[1]
+ vector[2]*vector[2]);
var u = vector[0]/len;
var v = vector[1]/len;
var w = vector[2]/len;
|
>
>
|
|
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
//
// equation at:
// http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/ArbitraryAxisRotation.html
// creates a rotation matrix about the given vector.
//
function vector_rotation(vector, s){
if(!vector) throw "vector argument to vector_rotation missing";
if(!s) throw "angle argument to vector_rotation missing";
var len = Math.sqrt(vector[0]*vector[0]
+ vector[1]*vector[1]
+ vector[2]*vector[2]);
var u = vector[0]/len;
var v = vector[1]/len;
var w = vector[2]/len;
|
︙ | | | ︙ | |