Debian Codegraph @

closure-library/closure/goog/math/matrix_test.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.math.MatrixTest');
16 goog.setTestOnly('goog.math.MatrixTest');
17
18 goog.require('goog.math.Matrix');
19 goog.require('goog.testing.jsunit');
20
21 function testConstuctorWithGoodArray() {
22 var a1 = [[1, 2], [2, 3], [4, 5]];
23 var m1 = new goog.math.Matrix(a1);
24 assertArrayEquals('1. Internal array should be the same', m1.toArray(), a1);
25 assertEquals(3, m1.getSize().height);
26 assertEquals(2, m1.getSize().width);
27
28 var a2 = [[-61, 45, 123], [11112, 343, 1235]];
29 var m2 = new goog.math.Matrix(a2);
30 assertArrayEquals('2. Internal array should be the same', m2.toArray(), a2);
31 assertEquals(2, m2.getSize().height);
32 assertEquals(3, m2.getSize().width);
33
34 var a3 = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]];
35 var m3 = new goog.math.Matrix(a3);
36 assertArrayEquals('3. Internal array should be the same', m3.toArray(), a3);
37 assertEquals(4, m3.getSize().height);
38 assertEquals(4, m3.getSize().width);
39 }
40
41
42 function testConstructorWithBadArray() {
43 assertThrows('1. All arrays should be of equal length', function() {
44 new goog.math.Matrix([[1, 2, 3], [1, 2], [1]]);
45 });
46
47 assertThrows('2. All arrays should be of equal length', function() {
48 new goog.math.Matrix([[1, 2], [1, 2], [1, 2, 3, 4]]);
49 });
50
51 assertThrows('3. Arrays should contain only numeric values', function() {
52 new goog.math.Matrix([[1, 2], [1, 2], [1, 'a']]);
53 });
54
55 assertThrows('4. Arrays should contain only numeric values', function() {
56 new goog.math.Matrix([[1, 2], [1, 2], [1, {a: 3}]]);
57 });
58
59 assertThrows('5. Arrays should contain only numeric values', function() {
60 new goog.math.Matrix([[1, 2], [1, 2], [1, [1, 2, 3]]]);
61 });
62 }
63
64
65 function testConstructorWithGoodNumbers() {
66 var m1 = new goog.math.Matrix(2, 2);
67 assertEquals('Height should be 2', 2, m1.getSize().height);
68 assertEquals('Width should be 2', 2, m1.getSize().width);
69
70 var m2 = new goog.math.Matrix(4, 2);
71 assertEquals('Height should be 4', 4, m2.getSize().height);
72 assertEquals('Width should be 2', 2, m2.getSize().width);
73
74 var m3 = new goog.math.Matrix(4, 6);
75 assertEquals('Height should be 4', 4, m3.getSize().height);
76 assertEquals('Width should be 6', 6, m3.getSize().width);
77 }
78
79
80 function testConstructorWithBadNumbers() {
81 assertThrows('1. Negative argument should have errored', function() {
82 new goog.math.Matrix(-4, 6);
83 });
84
85 assertThrows('2. Negative argument should have errored', function() {
86 new goog.math.Matrix(4, -6);
87 });
88
89 assertThrows('3. Zero argument should have errored', function() {
90 new goog.math.Matrix(4, 0);
91 });
92
93 assertThrows('4. Zero argument should have errored', function() {
94 new goog.math.Matrix(0, 1);
95 });
96 }
97
98
99 function testConstructorWithMatrix() {
100 var a1 = [[1, 2], [2, 3], [4, 5]];
101 var m1 = new goog.math.Matrix(a1);
102 var m2 = new goog.math.Matrix(m1);
103 assertArrayEquals(
104 'Internal arrays should be the same', m1.toArray(), m2.toArray());
105 assertNotEquals(
106 'Should be different objects', goog.getUid(m1), goog.getUid(m2));
107 }
108
109
110 function testCreateIdentityMatrix() {
111 var m1 = goog.math.Matrix.createIdentityMatrix(3);
112 assertArrayEquals([[1, 0, 0], [0, 1, 0], [0, 0, 1]], m1.toArray());
113
114 var m2 = goog.math.Matrix.createIdentityMatrix(4);
115 assertArrayEquals(
116 [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], m2.toArray());
117 }
118
119
120 function testIsValidArrayWithGoodArrays() {
121 var fn = goog.math.Matrix.isValidArray;
122 assertTrue('2x2 array should be fine', fn([[1, 2], [3, 5]]));
123 assertTrue('3x2 array should be fine', fn([[1, 2, 3], [3, 5, 6]]));
124 assertTrue(
125 '3x3 array should be fine', fn([[1, 2, 3], [3, 5, 6], [10, 10, 10]]));
126 assertTrue('[[1]] should be fine', fn([[1]]));
127 assertTrue('1D array should work', fn([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]));
128 assertTrue(
129 'Negs and decimals should be ok',
130 fn([[0], [-4], [-10], [1.2345], [123.53]]));
131 assertTrue('Hex, Es and decimals are ok', fn([[0x100, 10E-2], [1.213, 213]]));
132 }
133
134
135 function testIsValidArrayWithBadArrays() {
136 var fn = goog.math.Matrix.isValidArray;
137 assertFalse('Arrays should have same size', fn([[1, 2], [3]]));
138 assertFalse('Arrays should have same size 2', fn([[1, 2], [3, 4, 5]]));
139 assertFalse('2D arrays are ok', fn([[1, 2], [3, 4], []]));
140 assertFalse('Values should be numeric', fn([[1, 2], [3, 'a']]));
141 assertFalse('Values can not be strings', fn([['bah'], ['foo']]));
142 assertFalse('Flat array not supported', fn([1, 2, 3, 4, 5]));
143 }
144
145
146 function testForEach() {
147 var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
148 var count = 0, sum = 0, xs = '', ys = '';
149 goog.math.Matrix.forEach(m, function(val, x, y) {
150 count++;
151 sum += val;
152 xs += x;
153 ys += y;
154 });
155 assertEquals('forEach should have visited every item', 9, count);
156 assertEquals('forEach should have summed all values', 45, sum);
157 assertEquals('Xs should have been visited in order', '000111222', xs);
158 assertEquals('Ys should have been visited sequentially', '012012012', ys);
159 }
160
161
162 function testMap() {
163 var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
164 var m2 = goog.math.Matrix.map(m1, function(val, x, y) { return val + 1; });
165 assertArrayEquals([[2, 3, 4], [5, 6, 7], [8, 9, 10]], m2.toArray());
166 }
167
168
169 function testSetValueAt() {
170 var m = new goog.math.Matrix(3, 3);
171 for (var x = 0; x < 3; x++) {
172 for (var y = 0; y < 3; y++) {
173 m.setValueAt(x, y, 3 * x - y);
174 }
175 }
176 assertArrayEquals([[0, -1, -2], [3, 2, 1], [6, 5, 4]], m.toArray());
177 }
178
179
180 function testGetValueAt() {
181 var m = new goog.math.Matrix([[0, -1, -2], [3, 2, 1], [6, 5, 4]]);
182 for (var x = 0; x < 3; x++) {
183 for (var y = 0; y < 3; y++) {
184 assertEquals(
185 'Value at (x, y) should equal 3x - y', 3 * x - y, m.getValueAt(x, y));
186 }
187 }
188 assertNull('Out of bounds value should be null', m.getValueAt(-1, 2));
189 assertNull('Out of bounds value should be null', m.getValueAt(-1, 0));
190 assertNull('Out of bounds value should be null', m.getValueAt(0, 4));
191 }
192
193
194 function testSum1() {
195 var m1 = new goog.math.Matrix([[1, 1, 1], [2, 2, 2], [3, 3, 3]]);
196 var m2 = new goog.math.Matrix([[3, 3, 3], [2, 2, 2], [1, 1, 1]]);
197 assertArrayEquals(
198 'Sum should be all the 4s', [[4, 4, 4], [4, 4, 4], [4, 4, 4]],
199 m1.add(m2).toArray());
200 assertArrayEquals(
201 'Addition should be commutative', m1.add(m2).toArray(),
202 m2.add(m1).toArray());
203 }
204
205
206 function testSum2() {
207 var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
208 var m2 = new goog.math.Matrix([[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]]);
209 assertArrayEquals(
210 'Sum should be all 0s', [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
211 m1.add(m2).toArray());
212 assertArrayEquals(
213 'Addition should be commutative', m1.add(m2).toArray(),
214 m2.add(m1).toArray());
215 }
216
217
218 function testSubtract1() {
219 var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
220 var m2 = new goog.math.Matrix([[5, 5, 5], [5, 5, 5], [5, 5, 5]]);
221
222 assertArrayEquals(
223 [[-4, -3, -2], [-1, 0, 1], [2, 3, 4]], m1.subtract(m2).toArray());
224 assertArrayEquals(
225 [[4, 3, 2], [1, 0, -1], [-2, -3, -4]], m2.subtract(m1).toArray());
226 }
227
228
229 function testSubtract2() {
230 var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
231 var m2 = new goog.math.Matrix([[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]]);
232 assertArrayEquals(
233 [[2, 4, 6], [8, 10, 12], [14, 16, 18]], m1.subtract(m2).toArray());
234 assertArrayEquals(
235 [[-2, -4, -6], [-8, -10, -12], [-14, -16, -18]],
236 m2.subtract(m1).toArray());
237 }
238
239
240 function testScalarMultiplication() {
241 var m1 = new goog.math.Matrix([[1, 1, 1], [2, 2, 2], [3, 3, 3]]);
242 assertArrayEquals(
243 [[2, 2, 2], [4, 4, 4], [6, 6, 6]], m1.multiply(2).toArray());
244 assertArrayEquals(
245 [[3, 3, 3], [6, 6, 6], [9, 9, 9]], m1.multiply(3).toArray());
246 assertArrayEquals(
247 [[4, 4, 4], [8, 8, 8], [12, 12, 12]], m1.multiply(4).toArray());
248
249 var m2 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
250 assertArrayEquals(
251 [[2, 4, 6], [8, 10, 12], [14, 16, 18]], m2.multiply(2).toArray());
252 }
253
254
255 function testMatrixMultiplication() {
256 var m1 = new goog.math.Matrix([[1, 2], [3, 4]]);
257 var m2 = new goog.math.Matrix([[3, 4], [5, 6]]);
258 // m1 * m2
259 assertArrayEquals(
260 [[1 * 3 + 2 * 5, 1 * 4 + 2 * 6], [3 * 3 + 4 * 5, 3 * 4 + 4 * 6]],
261 m1.multiply(m2).toArray());
262 // m2 * m1 != m1 * m2
263 assertArrayEquals(
264 [[3 * 1 + 4 * 3, 3 * 2 + 4 * 4], [5 * 1 + 6 * 3, 5 * 2 + 6 * 4]],
265 m2.multiply(m1).toArray());
266 var m3 = new goog.math.Matrix([[1, 2, 3, 4], [5, 6, 7, 8]]);
267 var m4 =
268 new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]);
269 // m3 * m4
270 assertArrayEquals(
271 [
272 [
273 1 * 1 + 2 * 4 + 3 * 7 + 4 * 10, 1 * 2 + 2 * 5 + 3 * 8 + 4 * 11,
274 1 * 3 + 2 * 6 + 3 * 9 + 4 * 12
275 ],
276 [
277 5 * 1 + 6 * 4 + 7 * 7 + 8 * 10, 5 * 2 + 6 * 5 + 7 * 8 + 8 * 11,
278 5 * 3 + 6 * 6 + 7 * 9 + 8 * 12
279 ]
280 ],
281 m3.multiply(m4).toArray());
282 assertThrows(
283 'Matrix dimensions should not line up.', function() { m4.multiply(m3); });
284 }
285
286 function testMatrixMultiplicationIsAssociative() {
287 var A = new goog.math.Matrix([[1, 2], [3, 4]]);
288 var B = new goog.math.Matrix([[3, 4], [5, 6]]);
289 var C = new goog.math.Matrix([[2, 7], [9, 1]]);
290
291 assertArrayEquals(
292 'A(BC) == (AB)C', A.multiply(B.multiply(C)).toArray(),
293 A.multiply(B).multiply(C).toArray());
294 }
295
296
297 function testMatrixMultiplicationIsDistributive() {
298 var A = new goog.math.Matrix([[1, 2], [3, 4]]);
299 var B = new goog.math.Matrix([[3, 4], [5, 6]]);
300 var C = new goog.math.Matrix([[2, 7], [9, 1]]);
301
302 assertArrayEquals(
303 'A(B + C) = AB + AC', A.multiply(B.add(C)).toArray(),
304 A.multiply(B).add(A.multiply(C)).toArray());
305
306 assertArrayEquals(
307 '(A + B)C = AC + BC', A.add(B).multiply(C).toArray(),
308 A.multiply(C).add(B.multiply(C)).toArray());
309 }
310
311
312 function testTranspose() {
313 var m = new goog.math.Matrix([[1, 3, 1], [0, -6, 0]]);
314 var t = [[1, 0], [3, -6], [1, 0]];
315 assertArrayEquals(t, m.getTranspose().toArray());
316 }
317
318
319 function testAppendColumns() {
320 var m = new goog.math.Matrix([[1, 3, 2], [2, 0, 1], [5, 2, 2]]);
321 var b = new goog.math.Matrix([[4], [3], [1]]);
322 var result = [[1, 3, 2, 4], [2, 0, 1, 3], [5, 2, 2, 1]];
323 assertArrayEquals(result, m.appendColumns(b).toArray());
324 }
325
326
327 function testAppendRows() {
328 var m = new goog.math.Matrix([[1, 3, 2], [2, 0, 1], [5, 2, 2]]);
329 var b = new goog.math.Matrix([[4, 3, 1]]);
330 var result = [[1, 3, 2], [2, 0, 1], [5, 2, 2], [4, 3, 1]];
331 assertArrayEquals(result, m.appendRows(b).toArray());
332 }
333
334
335 function testSubmatrixByDeletion() {
336 var m = new goog.math.Matrix(
337 [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]);
338 var arr = [[1, 2, 3], [5, 6, 7], [13, 14, 15]];
339 assertArrayEquals(arr, m.getSubmatrixByDeletion_(2, 3).toArray());
340 }
341
342
343 function testMinor() {
344 var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
345 assertEquals(-3, m.getMinor_(0, 0));
346 }
347
348
349 function testCofactor() {
350 var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
351 assertEquals(6, m.getCofactor_(0, 1));
352 }
353
354
355 function testDeterminantForOneByOneMatrix() {
356 var m = new goog.math.Matrix([[3]]);
357 assertEquals(3, m.getDeterminant());
358 }
359
360
361 function testDeterminant() {
362 var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
363 assertEquals(0, m.getDeterminant());
364 }
365
366
367 function testGetSubmatrix() {
368 var m = new goog.math.Matrix(
369 [[2, -1, 0, 1, 0, 0], [-1, 2, -1, 0, 1, 0], [0, -1, 2, 0, 0, 1]]);
370 var sub1 = [[2, -1, 0], [-1, 2, -1], [0, -1, 2]];
371 assertArrayEquals(sub1, m.getSubmatrixByCoordinates_(0, 0, 2, 2).toArray());
372
373 var sub2 = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
374 assertArrayEquals(sub2, m.getSubmatrixByCoordinates_(0, 3).toArray());
375 }
376
377
378 function testGetReducedRowEchelonForm() {
379 var m = new goog.math.Matrix(
380 [[2, -1, 0, 1, 0, 0], [-1, 2, -1, 0, 1, 0], [0, -1, 2, 0, 0, 1]]);
381
382 var expected = new goog.math.Matrix(
383 [[1, 0, 0, .75, .5, .25], [0, 1, 0, .5, 1, .5], [0, 0, 1, .25, .5, .75]]);
384
385 assertTrue(expected.equals(m.getReducedRowEchelonForm()));
386 }
387
388
389 function testInverse() {
390 var m1 = new goog.math.Matrix([[2, -1, 0], [-1, 2, -1], [0, -1, 2]]);
391 var expected1 =
392 new goog.math.Matrix([[.75, .5, .25], [.5, 1, .5], [.25, .5, .75]]);
393 assertTrue(expected1.equals(m1.getInverse()));
394
395 var m2 = new goog.math.Matrix([[4, 8], [7, -2]]);
396 var expected2 = new goog.math.Matrix([[.03125, .125], [.10936, -.0625]]);
397 assertTrue(expected2.equals(m2.getInverse(), .0001));
398 var m3 = new goog.math.Matrix([[0, 0], [0, 0]]);
399 assertNull(m3.getInverse());
400 var m4 = new goog.math.Matrix([[2]]);
401 var expected4 = new goog.math.Matrix([[.5]]);
402 assertTrue(expected4.equals(m4.getInverse(), .0001));
403 var m5 = new goog.math.Matrix([[0]]);
404 assertNull(m5.getInverse());
405 }
406
407
408 function testEquals() {
409 var a1 = new goog.math.Matrix(
410 [[1, 0, 0, .75, .5, .25], [0, 1, 0, .5, 1, .5], [0, 0, 1, .25, .5, .75]]);
411
412 var a2 = new goog.math.Matrix(
413 [[1, 0, 0, .75, .5, .25], [0, 1, 0, .5, 1, .5], [0, 0, 1, .25, .5, .75]]);
414
415 var a3 = new goog.math.Matrix([
416 [1, 0, 0, .749, .5, .25], [0, 1, 0, .5, 1, .5], [0, 0, 1, .25, .5, .75]
417 ]);
418
419 assertTrue(a1.equals(a2));
420 assertTrue(a1.equals(a3, .01));
421 assertFalse(a1.equals(a3, .001));
422 }