101 lines
3.0 KiB
Plaintext
101 lines
3.0 KiB
Plaintext
var num = use('num');
|
|
|
|
return {
|
|
test_num_basic: function() {
|
|
// Test matrix creation and operations
|
|
var A = new num.Matrix([
|
|
[1, 2, 3],
|
|
[4, 5, 6],
|
|
[7, 8, 10]
|
|
]);
|
|
|
|
log.console("Matrix A:");
|
|
log.console(A.toArray());
|
|
|
|
// Test matrix inversion
|
|
var A_inv = A.inverse();
|
|
log.console("\nMatrix A inverse:");
|
|
log.console(A_inv.toArray());
|
|
|
|
// Verify A * A_inv = I (approximately)
|
|
var I = A.multiply(A_inv);
|
|
log.console("\nA * A_inv (should be identity):");
|
|
log.console(I.toArray());
|
|
|
|
// Test array creation
|
|
var v = new num.Array([1, 2, 3]);
|
|
log.console("\nVector v:");
|
|
log.console(v.toArray());
|
|
|
|
// Test matrix-vector multiplication
|
|
var result = A.multiply(v);
|
|
log.console("\nA * v:");
|
|
log.console(result.toArray());
|
|
|
|
// Test dot product
|
|
var u = new num.Array([4, 5, 6]);
|
|
var dot_product = v.dot(u);
|
|
log.console("\nv · u =", dot_product);
|
|
|
|
// Test norm
|
|
var v_norm = v.norm();
|
|
log.console("||v|| =", v_norm);
|
|
|
|
// Test matrix-matrix multiplication
|
|
var B = new num.Matrix([
|
|
[1, 0, 0],
|
|
[0, 2, 0],
|
|
[0, 0, 3]
|
|
]);
|
|
|
|
var C = A.multiply(B);
|
|
log.console("\nA * B:");
|
|
log.console(C.toArray());
|
|
},
|
|
|
|
test_num_property: function() {
|
|
// Create an array
|
|
var arr = new num.Array([10, 20, 30, 40, 50]);
|
|
|
|
if (arr[0] != 10) throw "arr[0] mismatch"
|
|
if (arr[1] != 20) throw "arr[1] mismatch"
|
|
if (arr[4] != 50) throw "arr[4] mismatch"
|
|
|
|
arr[0] = 15
|
|
if (arr[0] != 15) throw "arr[0] set failed"
|
|
|
|
// arr[10] should be null or undefined, check behavior
|
|
// log.console("arr[10] =", arr[10]);
|
|
|
|
if (arr.length != 5) throw "arr.length mismatch"
|
|
|
|
if (!(arr instanceof num.Array)) throw "instanceof check failed"
|
|
},
|
|
|
|
test_num_setter: function() {
|
|
// Create an array
|
|
var arr = new num.Array([1, 2, 3, 4, 5]);
|
|
|
|
// Test setting values
|
|
arr[0] = 100;
|
|
arr[1] = 200;
|
|
arr[2] = 300.5;
|
|
|
|
if (arr[0] != 100) throw "Setter failed index 0"
|
|
if (arr[1] != 200) throw "Setter failed index 1"
|
|
if (arr[2] != 300.5) throw "Setter failed index 2"
|
|
|
|
// Test setting with different types
|
|
arr[3] = "123.7"; // Should convert string to number
|
|
arr[4] = true; // Should convert boolean to number
|
|
|
|
// Loose comparison for converted values if needed, or check specific behavior
|
|
// Assuming implementation converts:
|
|
// log.console("arr[3] =", arr[3]);
|
|
// log.console("arr[4] =", arr[4]);
|
|
|
|
// Test bounds checking - this should fail silently or throw depending on impl
|
|
arr[10] = 999;
|
|
}
|
|
}
|