UNPKG

qminer

Version:

A C++ based data analytics platform for processing large-scale real-time streams containing structured and unstructured data

1,298 lines (1,115 loc) 187 kB
/** * Copyright (c) 2015, Jozef Stefan Institute, Quintelligence d.o.o. and contributors * All rights reserved. * * This source code is licensed under the FreeBSD license found in the * LICENSE file in the root directory of this source tree. */ // console.log(__filename) // // Sample unit test using standard assert JS library // var assert = require("../../src/nodejs/scripts/assert.js") var la = require('../../index.js').la; describe('Import test', function () { it('if import of qminer.node succeeds, return true', function () { assert.strictEqual(1, 1); }) }) ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function tests for Vector ///////////////////////////////////////////////////////////////////////////////////////////////////////////// var v = new la.Vector(); function FVector() { this.vec = new la.Vector([1, 2, 3]); } describe('Vector Tests', function () { ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Property tests ///////////////////////////////////////////////////////////////////////////////////////////////////////// describe('Property Tests', function () { describe('Length Test', function () { var v = new FVector(); it('should return 0 for vector v', function () { assert.strictEqual(v.vec.length, 3); }) }) }); ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Function tests ///////////////////////////////////////////////////////////////////////////////////////////////////////// describe('Functions Tests', function () { describe('Push Test', function () { var v = new FVector(); it('pushing 3.1 and 4 into a vector v, v.length should return 2', function () { v.vec.push(3.2); v.vec.push(4); assert.strictEqual(v.vec.length, 5); }) }); describe('At Test', function () { var v = new FVector(); it('returns element in with indexes 0 (3.2) and 1 (4)', function () { assert.strictEqual(1, v.vec.at(0)); assert.strictEqual(2, v.vec.at(1)); }) }); describe('At "Out of Bound" Test', function () { var v = new FVector(); it('should give an error for accessing an element out of bound -1', function () { assert.throws(function () { var n = v.vec.at(-1); }) }) it('should give an error for accessing an element out of bound 3', function () { assert.throws(function () { var n = v.vec.at(4); }) }) }); describe('Constructor "Array" Test', function () { it('takes an array [1, 2, 3] and it should create a vector', function () { var vec = new la.Vector([1, 2, 3]); assert.strictEqual(vec.length, 3); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), i + 1); } }) }); describe('Constructor "String Array" Test', function () { it('takes an array ["a", "b", "c"] and it should return an exception', function () { assert.throws(function () { var vec = new la.Vector(["a", "b", "c"]); assert.strictEqual(vec.length, 3); assert.strictEqual(vec.at(0), "a"); assert.strictEqual(vec.at(1), "b"); assert.strictEqual(vec.at(2), "c"); }) }) }); describe('Contructor "Empty Array" Test', function () { it('takes an empty array [] and it should create an empty vector', function () { var vec = new la.Vector([]); assert.strictEqual(vec.length, 0); assert.throws(function () { var n = vec.at(0); }) }) }); describe('Constructor "Dictionary" Test', function () { it('takes {"vals":5, "mxvals": 5} and creates a vector with 5 zeros', function () { var vec = new la.Vector({ "vals": 5, "mxvals": 5 }); assert.strictEqual(vec.length, 5); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), 0); } }) }); describe('Contructor "Invalid Dictionary" Test', function () { it('takes an invalid dictionary {"vals": "a", "maxvals": 5} and it should give an exception', function () { assert.throws(function () { var vec = new la.Vector({ "vals": "a", "mxvals": 5 }); }) }) }); describe('Copy Constructor Test', function () { var v = new FVector(); it('should copy the vector v and save it in vec', function () { var vec = new la.Vector(v.vec); assert.strictEqual(vec.length, v.vec.length); for (var i = 0; i < v.length; i++) { assert.strictEqual(vec.at(i), v.at(i)); } }) it('should copy vector v and while changed it doesn\'t change v', function () { var vec = new la.Vector(v.vec); vec.put(1, -100); assert.notEqual(vec.at(1), v.vec.at(1)); }) }); describe('Sum Test', function () { var v = new FVector(); it('should return a sum 1 + 2 + 3', function () { assert.strictEqual(6, v.vec.sum()); }) }); describe('Sum "Zero" Test', function () { it('the sum returned should be zero', function () { var vec = la.Vector([3.14 * 0.0001, 3.14 * -0.0001]); assert.strictEqual(vec.sum(), 0); }) }); describe('getMaxIdx Test', function () { var v = new FVector(); it('should return index of last element in vector, 1.', function () { assert.strictEqual(v.vec.length - 1, v.vec.getMaxIdx()); }) }); describe('getMaxIdx "All Elements Same" Test', function () { var v = new FVector(); it('if all elements are the same, it should return the first max index', function () { var vec = new la.Vector([1, 1, 1, 1]); assert.strictEqual(vec.getMaxIdx(), 0); }) }); describe('Ones Test', function () { it('should return a 5-dimensional vector whose entries are set to 1.0', function () { var n = 5; var w = la.ones(n); for (var i = 0; i < w.length; i++) { assert.strictEqual(w.at(i), 1); } }) }); describe('Ones "Parameter" Tests', function () { it('should return an empty vector for parameter zero', function () { var w = la.ones(0); assert.strictEqual(w.length, 0); }) it('should throw an exception for parameters less than 0', function () { assert.throws(function () { var w = la.ones(-1); }) }) it('should throw an exception for floating number parameters', function () { assert.throws(function () { var w = la.ones(2.5); }) }) }); // not implemented //describe('Square test', function () { // it('should square all values of vector v', function () { // var array = [1, 2, 3, 4]; // var w = new la.Vector(array); la.square(w); // for (var i = 0; i < w.length; i++) { // assert.strictEqual(w.at(i), array[i] * array[i]); // } // }) //}); describe('Sort Tests', function () { it('should sort vector in ascending order [0.11, 0.12, 3.5, 4]', function () { var array = [0.11, 0.12, 3.5, 4]; var vec = new la.Vector([3.5, 0.12, 4, 0.11]); var vec = vec.sort(); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), array[i]); } }) it('should sort vector in descending order [4, 3.5, 0.12, 0.11]', function () { var array = [4, 3.5, 0.12, 0.11]; var vec = new la.Vector([3.5, 0.12, 4, 0.11]); var vec = vec.sort(false); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), array[i]); } }) }); describe('Plus Test', function () { it('should sum the vectors', function () { var vec1 = new la.Vector([1, 3, 4, 8]); var vec2 = new la.Vector([4, 3, 8, 2.1]); var vec = vec1.plus(vec2); var controlVec = new la.Vector([5, 6, 12, 10.1]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('Plus "Problem" Test', function () { it('should throw an exception for different length vectors', function () { var vec1 = new la.Vector([1, 3, 4, 8]); var vec2 = new la.Vector([4, 3, 8]); assert.throws(function () { var vec = vec1.plus(vec2); }) }) }); describe('Minus Test', function () { it('should substract the vectors', function () { var vec1 = new la.Vector([1, 3, 4, 8]); var vec2 = new la.Vector([4, 3, 8, 2.1]); var vec = vec1.minus(vec2); var controlVec = new la.Vector([-3, 0, -4, 5.9]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('Multiply Test', function () { it('should multiply vector vec with scalar 3.14', function () { var vec = new la.Vector([3, 0, -12, 0.0001]); var vec2 = vec.multiply(3.14); var controlVec = new la.Vector([9.42, 0, -37.68, 0.000314]); assert.deepEqual(vec2, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.eqtol(vec2.at(i), controlVec.at(i)); } }) }); describe('Multiply "Small scalar" Test', function () { it('should multiply vector vec with scalar 10^(-5)', function () { var vec = new la.Vector([3, 0, 1, 0.0001]); var vec2 = vec.multiply(Math.pow(10, -5)); var controlVec = new la.Vector([0.00003, 0, 0.00001, 0.000000001]); assert.deepEqual(vec2, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.eqtol(vec2.at(i), controlVec.at(i)); } }) }); describe('Subvector Test', function () { it('should return the subvector of vector vec', function () { var vec = new la.Vector([3, -51, 22, 19]); var indVec = new la.IntVector([1, 3, 2, 0]); vec = vec.subVec(indVec); var controlVec = new la.Vector([-51, 19, 22, 3]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('Subvector "Big Index" Test', function () { it('should throw an exception for indVec having a value > vec.length', function () { var vec = new la.Vector([3, -51, 22, 19]); var indVec = new la.IntVector([1, 4, 2, 0]); assert.throws(function () { vec = vec.subVec(indVec); }) }) }); describe('At [] Tests', function () { var v = new FVector(); it('should return elements with indices 0 (3.2) and 1 (4)', function () { assert.strictEqual(1, v.vec[0]); assert.strictEqual(2, v.vec[1]); }) it('should save new value at index 0 (12)', function () { v.vec[0] = 12; assert.strictEqual(12, v.vec[0]); }) }); describe('At [] "Out of Bound" Test', function () { var v = new FVector(); it('should return an exception for out of bound indices', function () { assert.throws(function () { v.vec[4] = 12; }) }) }); describe('Put Test', function () { var v = new FVector(); it('should put the value -21 at index 1', function () { v.vec.put(1, -21); assert.strictEqual(-21, v.vec[1]); }) }); describe('Put "Parameter" Tests', function () { var v = new FVector(); it('should throw exception for putting an element out of bounds', function () { assert.throws(function () { v.vec.put(4, 100); }) }) it('should throw exception for putting an element in position 0.5', function () { assert.throws(function () { v.vec.put(0.5, 100); }) }) }); describe('Diag Test', function () { var v = new FVector(); it('should return a matrix with the vector v on it\'s diagonal', function () { var mat = v.vec.diag(); for (var i = 0; i < mat.rows; i++) { for (var j = 0; j < mat.cols; j++) { if (i == j) { assert.strictEqual(mat.at(i, j), v.vec[i]); } else { assert.strictEqual(mat.at(i, j), 0); } } } }) }); describe('PushV Test', function () { var v = new FVector(); it('should return v with appended vector [1, 2, 3]', function () { var w = new la.Vector([1, 2, 3]); v.vec.pushV(w); assert.strictEqual(v.vec.length, 6); var controlVec = new la.Vector([1, 2, 3, 1, 2, 3]); assert.deepEqual(v.vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(v.vec.at(i), controlVec.at(i)); } }) }); describe('Norm Test', function () { it('should return the norm of vec: 5', function () { var vec = new la.Vector([3, 4]); var n = vec.norm(); assert.eqtol(n, 5); }) }); describe('Norm "Empty Vector" Test', function () { it('should throw exception for an empty vector', function () { var vec = new la.Vector(); var n = vec.norm(); assert.strictEqual(n, 0); }) }); describe('Normalize Test', function () { it('should normalize vec and return vector [3/5, 4/5]', function () { var vec = new la.Vector([3, 4]); vec.normalize(); var controlVec = new la.Vector([3 / 5, 4 / 5]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.eqtol(vec.at(i), controlVec.at(i)); } }) }); describe('Normalize "Zer0 Vector" Test', function () { it('should return the same vector', function () { var vec = new la.Vector([0, 0, 0, 0]); vec.normalize(); var controlVec = new la.Vector([0, 0, 0, 0]); assert.deepEqual(vec, controlVec); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('Normalize "Empty Vector" Test', function () { it('should throw an exception for empty vector', function () { var vec = new la.Vector(); assert.throws(function () { vec.normalize(); }) }) }); describe('ToMat Test', function () { var v = new FVector() it('should return matrix with a single column that equals vec', function () { var mat = v.vec.toMat(); for (var i = 0; i < v.vec.length; i++) { assert.strictEqual(mat.at(i, 0), v.vec.at(i)); } }) }); describe('ToMat "Empty Vector" Test', function () { it('should return an empty matrix', function () { var vec = new la.Vector(); var mat = vec.toMat(); assert.strictEqual(mat.rows, 0); }) }); describe('Sparse Test', function () { var v = new FVector(); it('should return the sparse vector of vec', function () { var spV = v.vec.sparse(); var controlVec = new la.SparseVector([[0, 1], [1, 2], [2, 3]]); for (var i = 0; i < controlVec.dim; i++) { assert.eqtol(spV.at(i), controlVec.at(i)); } }) }); describe('Unshift Test', function () { var v = new FVector(); it('should insert the value 10 at the beginning of vector v', function () { v.vec.unshift(10); assert.strictEqual(v.vec[0], 10); }) }); describe('Trunc Test', function () { var v = new FVector(); it('should cut off the last 1 values in vector v', function () { v.vec.trunc(2); assert.strictEqual(v.vec.length, 2); }) }); describe('Trunc "Parameter" Tests', function () { var v = new FVector(); it('should return the same vector for parameter 3', function () { v.vec.trunc(4); assert.strictEqual(v.vec.length, 3); }) it('should throw an exception for parameter < 0', function () { assert.throws(function () { v.vec.trunc(-1); }) }) }); describe('Outer Test', function () { var v = new FVector(); it('should return a matrix v * v^T', function () { var mat = v.vec.outer(v.vec); var controlMat = new la.Matrix([[1, 2, 3], [2, 4, 6], [3, 6, 9]]); assert.deepEqual(mat, controlMat); for (var i = 0; i < controlMat.rows; i++) { for (var j = 0; j < controlMat.cols; j++) { assert.eqtol(mat.at(i, j), controlMat.at(i, j)); } } }) }); describe('Inner Test', function () { var v = new FVector(); it('should return the scalar product of v and [1, 2, 3]', function () { var n = v.vec.inner(new la.Vector([1, 2, 3])); assert.strictEqual(n, 1 + 4 + 9); }) }); describe('Inner "Different Length" Test', function () { var v = new FVector(); it('should return an exception for vectors having different lengths', function () { assert.throws(function () { var n = v.vec.inner(new la.Vector([1, 2])); }) }) }); describe('SpDiag Test', function () { var v = new FVector(); it('should return a sparse matrix with v on the diagonal', function () { var spMat = v.vec.spDiag(); var controlspMat = new la.SparseMatrix([[[0, 1]], [[1, 2]], [[2, 3]]], { "rows": 3 }); for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { if (i == j) { assert.strictEqual(spMat.at(i, j), controlspMat.at(i, j)) } else { assert.strictEqual(spMat.at(i, j), 0) } } } }) }); }); }); // // Function tests for IntVector // function IVector() { this.intV = new la.IntVector([-1, 6]); } describe('IntVector Test', function () { describe('Property Tests', function () { var vec = new IVector(); describe('Length Test', function () { it('should return 0 as the length of intV', function () { assert.strictEqual(vec.intV.length, 2); }) }); }); describe('Functions Tests', function () { describe('Push Test', function () { var vec = new IVector(); it('pushing values -1 and 6 into intV, length should return 2', function () { vec.intV.push(-1); vec.intV.push(6); assert.strictEqual(vec.intV.length, 4); }) }); describe('At Test', function () { var vec = new IVector(); it('should return the values at position 0 (-1) and at position 1 (6)', function () { assert.strictEqual(vec.intV.at(0), -1); assert.strictEqual(vec.intV.at(1), 6); }) }); describe('At "Out of Bound" Test', function () { var vec = new IVector(); it('should give an error for accessing an element out of bound', function () { assert.throws(function () { var n = vec.intV.at(-1); }) }) }); describe('Constructor "Array" Test', function () { it('takes an array [1, 2, 3] and creates a vector with same values', function () { var vec = new la.IntVector([1, 2, 3]); assert.strictEqual(vec.length, 3); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), i + 1); } }) }); describe('Constructor "Empty Array" Test', function () { it('should take an empty array and creates an empty vector', function () { var vec = new la.IntVector([]); assert.strictEqual(vec.length, 0); assert.throws(function () { var n = vec.at(-1); }) }) }); describe('Constructor "Dictionary" Test', function () { it('takes {"vals":5, "mxvals": 5} and creates a vector with 5 zeros', function () { var vec = new la.IntVector({ "vals": 5, "mxvals": 5 }); assert.strictEqual(vec.length, 5); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), 0); } }) }); describe('Copy Constructor Test', function () { var vec = new IVector(); it('should copy the vector v and save it in vec', function () { var vec2 = new la.IntVector(vec.intV); assert.strictEqual(vec2.length, vec.intV.length); for (var i = 0; i < vec.intV.length; i++) { assert.strictEqual(vec2.at(i), vec.intV.at(i)); } }) }); describe('Sum Test', function () { var vec = new IVector(); it('should return a sum of -1 and 6', function () { assert.strictEqual(vec.intV.sum(), 5); }) }); describe('Sum "Empty Vector" Test', function () { it('should throw an exception for empty vector', function () { var vec = new la.IntVector(); assert.throws(function () { var n = vec.norm(); }) }) }); describe('getMaxIdx Test', function () { var vec = new IVector(); it('should return index of last element in vector, 1.', function () { assert.strictEqual(vec.intV.getMaxIdx(), 1); }) }); describe('getMaxIdx "All Elements Same" Test', function () { it('if all elements are the same, it should return the first max index', function () { var vec = new la.IntVector([1, 1, 1, 1]); assert.strictEqual(vec.getMaxIdx(), 0); }) }); describe('Sort Test', function () { it('should sort vector in ascending order [-3, 0, 2, 15]', function () { var array = [-3, 0, 2, 15]; var sortedVec = new la.IntVector(array); var vec = new la.IntVector([2, 0, 15, -3]); var vec = vec.sort(); assert.deepEqual(vec, sortedVec); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), sortedVec.at(i)); } }) it('should sort vector in descending order [15, 2, 0, -3]', function () { var array = [15, 2, 0, -3]; var vec = new la.IntVector([2, 0, 15, -3]); var vec = vec.sort(false); for (var i = 0; i < vec.length; i++) { assert.strictEqual(vec.at(i), array[i]); } }) }); // not implemented //describe('Plus Test', function () { // it('should sum the vectors', function () { // var vec1 = new la.IntVector([1, 3, 4, 8]); // var vec2 = new la.IntVector([4, 3, 8, 2]); // var vec = vec1.plus(vec2); // var controlVec = new la.IntVector([5, 6, 12, 10]); // assert.deepEqual(vec, controlVec); // for (var i = 0; i < controlVec.length; i++) { // assert.strictEqual(vec.at(i), controlVec.at(i)); // } // }) //}); // not implemented //describe('Minus Test', function () { // it('should substract the vectors', function () { // var vec1 = new la.IntVector([1, 3, 4, 8]); // var vec2 = new la.IntVector([4, 3, 8, 2]); // var vec = vec1.minus(vec2); // var controlVec = new la.IntVector([-3, 0, -4, 6]); // assert.deepEqual(vec, controlVec); // for (var i = 0; i < controlVec.length; i++) { // assert.strictEqual(vec.at(i), controlVec.at(i)); // } // }) //}); // not implemented //describe('Multiply Test', function () { // it('should multiply vector vec with scalar 3.14', function () { // var vec = new la.IntVector([3, 0, -12, 0]); // var vec2 = vec.multiply(3); // var controlVec = new la.IntVector([9, 0, -36, 0]); // assert.deepEqual(vec2, controlVec); // for (var i = 0; i < controlVec.length; i++) { // assert.eqtol(vec2.at(i), controlVec.at(i)); // } // }) //}); describe('Subvector Test', function () { it('should return the subvector of vector vec', function () { var vec = new la.IntVector([3, -51, 22, 19]); var indVec = new la.IntVector([1, 3, 2, 0]); vec = vec.subVec(indVec); var controlVec = new la.IntVector([-51, 19, 22, 3]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('Subvector "Big Index" Test', function () { it('should throw an exception for indVec having a value > vec.length', function () { var vec = new la.IntVector([3, -51, 22, 19]); var indVec = new la.IntVector([1, 4, 2, 0]); assert.throws(function () { vec = vec.subVec(indVec); }) }) }); describe('At [] Test', function () { var vec = new IVector(); it('should return elements with indexes 0 (-1) and 1 (6)', function () { assert.strictEqual(vec.intV[0], -1); assert.strictEqual(vec.intV[1], 6); }) it('should save new value at index 0 (12)', function () { vec.intV[0] = 12; assert.strictEqual(vec.intV[0], 12); }) }); describe('At [] "Out of Bound" Test', function () { var vec = new IVector(); it('should return an exception for out of bound indices', function () { assert.throws(function () { vec.intV[4] = 12; }) }) }); describe('Put Test', function () { var vec = new IVector(); it('should put the value -21 at index 1', function () { vec.intV.put(1, -21); assert.strictEqual(vec.intV[1], -21); }) }); //// not implemented (low priority) //describe('Diag Test', function () { // var vec = new IVector(); // it('should return a matrix with the vector v on it\'s diagonal', function () { // var mat = vec.intV.diag(); // for (var i = 0; i < mat.rows; i++) { // for (var j = 0; j < mat.cols; j++) { // if (i == j) { assert.strictEqual(mat.at(i, j), vec.intV[i]); } // else { assert.strictEqual(mat.at(i, j), 0); } // } // } // }) //}); describe('PushV Test', function () { var vec = new IVector(); it('should return v with appended vector [1, 2, 3]', function () { var intW = new la.IntVector([1, 2, 3]); vec.intV.pushV(intW); assert.strictEqual(vec.intV.length, 5); var controlVec = new la.IntVector([-1, 6, 1, 2, 3]); assert.deepEqual(vec.intV, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.intV.at(i), controlVec.at(i)); } }) }); // not implemented (low priority) //describe('Norm Test', function () { // it('should return the norm of vec 5', function () { // var vec = new la.IntVector([3, 4]); // var n = vec.norm(); // assert.eqtol(n, 5); // }) //}); // not implemented (low priority) //describe('Normalize Test', function () { // it('should normalize vec and return vector [3/5, 4/5]', function () { // var vec = new la.IntVector([3, 4]); // vec.normalize(); // var controlVec = new la.IntVector([3 / 5, 4 / 5]); // assert.deepEqual(vec, controlVec); // for (var i = 0; i < controlVec.length; i++) { // assert.eqtol(vec.at(i), controlVec.at(i)); // } // }) //}); // not implemented (low priority) //describe('Normalize "Zer0 Vector" Test', function () { // it('should return the same vector', function () { // var vec = new la.IntVector([0, 0, 0, 0]); // vec.normalize(); // var controlVec = new la.IntVector([0, 0, 0, 0]); // assert.deepEqual(vec, controlVec); // for (var i = 0; i < vec.length; i++) { // assert.strictEqual(vec.at(i), controlVec.at(i)); // } // }) //}); // not implemented (low priority) //describe('ToMat Test', function () { // var vec = new IVector(); // it('should return matrix with a single column that equals intV', function () { // var mat = vec.intV.toMat(); // for (var i = 0; i < vec.intV.length; i++) { // assert.strictEqual(mat.at(i, 0), vec.intV.at(i)); // } // }) //}); // not implemented (low priority) //describe('Sparse Test', function () { // var vec = new IVector(); // it('should return the sparse vector of intV', function () { // var spV = vec.intV.sparse(); // var controlVec = new la.SparseVector([[0, -1], [1, 6]]); // //assert.deepEqual(spV, controlVec); // spV.dim = -1 instead of 5 ?? // for (var i = 0; i < controlVec.dim; i++) { // assert.eqtol(spV.at(i), controlVec.at(i)); // } // }) //}); describe('Unshift Test', function () { var vec = new IVector(); it('should insert the value 10 at the beginning of vector intV', function () { vec.intV.unshift(10); assert.strictEqual(vec.intV[0], 10); }) }); // not implemented (low priority) //describe('RangeVec Test', function () { // it('should return a integer vector with elements from 3 to 8', function () { // var intV2 = la.rangeVec(3, 8); // assert.strictEqual(intV2.length, 6); // for (var i = 0; i < intV2.length; i++) { // assert.strictEqual(intV2.at(i), i + 3); // } // }) //}); }); }); ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function tests for Matrix ///////////////////////////////////////////////////////////////////////////////////////////////////////////// var mat2 = new la.Matrix([[3, -1], [8, -2]]); function DMatrix() { this.dMat = new la.Matrix([[1, 2], [3, 4]]); } describe('Matrix Test', function () { ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Property Tests ///////////////////////////////////////////////////////////////////////////////////////////////////////// describe('Property Tests', function () { describe('Row Test', function () { var mat = new DMatrix(); it('should return 0 for matrix mat', function () { assert.strictEqual(mat.dMat.rows, 2); }) }); describe('Col Test', function () { var mat = new DMatrix(); it('should return 0 for matrix mat', function () { assert.strictEqual(mat.dMat.cols, 2); }) }); }); ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Function Tests ///////////////////////////////////////////////////////////////////////////////////////////////////////// describe('Functions Test', function () { describe('At Test', function () { it('should throw error, mat has no elements (assert catches error)', function () { var mat = new la.Matrix(); assert.throws(function () { var k = mat.at(0, 0); }) }) }); describe('At Test', function () { var mat = new DMatrix(); it('should return the element at (0, 1): 2', function () { assert.strictEqual(mat.dMat.at(0, 1), 2); }) }); describe('Contructor "Nested Array" Test', function () { it('takes a nested array and it should return a dense matrix object', function () { var array = [[1, 2], [3, 4]]; var dMat = new la.Matrix(array); assert.strictEqual(dMat.rows, 2); assert.strictEqual(dMat.cols, 2); for (var i = 0; i < dMat.rows; i++) { for (var j = 0; j < dMat.cols; j++) { assert.strictEqual(dMat.at(i, j), array[i][j]); } } }) }); describe('Constructor "Empty Array" Test', function () { it('takes an empty array and it should return an empty matrix', function () { var dMat = new la.Matrix([]); assert.strictEqual(dMat.rows, 0); assert.strictEqual(dMat.cols, 0); }) }); describe('Contructor "Dictionary" Test', function () { it('takes a dictionary of rows, columns and random and return a matrix', function () { var dMat = new la.Matrix({ "rows": 3, "cols": 3, "random": false }); assert.strictEqual(dMat.rows, 3); assert.strictEqual(dMat.cols, 3); for (var i = 0; i < dMat.rows; i++) { for (var j = 0; j < dMat.cols; j++) { assert.strictEqual(dMat.at(i, j), 0); } } }) }); describe('Copy Constructor Test', function () { var mat = new DMatrix(); it('should make the same matrix as mat', function () { var mat2 = new la.Matrix(mat.dMat); assert.deepEqual(mat.dMat, mat2); for (var i = 0; i < mat2.rows; i++) { for (var j = 0; j < mat2.cols; j++) { assert.eqtol(mat2.at(i, j), mat.dMat.at(i, j)); } } }) }); describe('Put Test', function () { var mat = new DMatrix(); it('should put value 10 at (1, 1)', function () { mat.dMat.put(1, 1, 10); assert.strictEqual(mat.dMat.at(1, 1), 10); }) }); describe('Put "Out of Bound" Test', function () { var mat = new DMatrix(); it('should throw an exception for index (3, 3)', function () { assert.throws(function () { matdMat.put(3, 3, 100); }) }) }); describe('Multiply "Scalar" Test', function () { var mat = new DMatrix(); it('should multiply matrix with scalar 10', function () { var mat3 = mat.dMat.multiply(10); for (var i = 0; i < mat.rows; i++) { for (var j = 0; j < mat.cols; j++) { assert.strictEqual(mat3.at(i, j), 10 * mat.at(i, j)); } } }) }); describe('Multiply "Vector" Test', function () { var mat = new DMatrix(); it('should multiply mat with vector [1, 2]', function () { var vec = mat.dMat.multiply(new la.Vector([1, 2])); var controlVec = new la.Vector([5, 11]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('Multiply "Different Dimension Vector" Test', function () { var mat = new DMatrix(); it('should throw exception for vector with length different of mat.cols', function () { assert.throws(function () { var vec = mat.dMat.multiply(new la.Vector([1])); }) }) }); describe('Multiply "Sparse Vector" Test', function () { var mat = new DMatrix(); it('should multiply mat with sparse vector [1, 2]', function () { var vec = mat.dMat.multiply(new la.SparseVector([[0, 1], [1, 2]])); var controlVec = new la.Vector([5, 11]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('Multiply "Different Dimension Sparse Vector" Test', function () { var mat = new DMatrix(); it('should throw exception for sparse vector with length different of mat.cols', function () { assert.throws(function () { var vec = mat.dMat.multiply(new la.SparseVector([[0, 1], [1, 2], [2, 3]])); }) }) }); describe('Multiply "Matrix" Test', function () { var mat = new DMatrix(); it('should multiply mat with [[3, -1],[8, -2]]', function () { var mat3 = mat.dMat.multiply(mat2); var controlMat = new la.Matrix([[19, -5], [41, -11]]); for (var i = 0; i < controlMat.rows; i++) { for (var j = 0; j < controlMat.cols; j++) { assert.strictEqual(mat3.at(i, j), controlMat.at(i, j)); } } }) }); describe('Multiply "Sparse Matrix" Test', function () { var mat = new DMatrix(); it('should multiply mat with sparse matrix [[3, -1],[8, -2]]', function () { var mat3 = mat.dMat.multiply(new la.SparseMatrix([[[0, 3], [1, 8]], [[0, -1], [1, -2]]])); var controlMat = new la.Matrix([[19, -5], [41, -11]]); for (var i = 0; i < controlMat.rows; i++) { for (var j = 0; j < controlMat.cols; j++) { assert.strictEqual(mat3.at(i, j), controlMat.at(i, j)); } } }) }); describe('MultiplyT "Scalar" Test', function () { var mat = new DMatrix(); it('should transpose mat and multiply with scalar 10', function () { var mat3 = mat.dMat.multiplyT(10); for (var i = 0; i < mat.rows; i++) { for (var j = 0; j < mat.cols; j++) { assert.strictEqual(mat3.at(j, i), 10 * mat.at(i, j)); } } }) }); describe('MultiplyT "Vector" Test', function () { var mat = new DMatrix(); it('should transpose mat and multiply with vector [1, 2]', function () { var vec = mat.dMat.multiplyT(new la.Vector([1, 2])); var controlVec = new la.Vector([7, 10]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('MultiplyT "Sparse Vector" Test', function () { var mat = new DMatrix(); it('should transpose mat and multiply with sparse vector [1, 2]', function () { var vec = mat.dMat.multiplyT(new la.SparseVector([[0, 1], [1, 2]])); var controlVec = new la.Vector([7, 10]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.strictEqual(vec.at(i), controlVec.at(i)); } }) }); describe('MultiplyT "Matrix" Test', function () { var mat = new DMatrix(); it('should tranpose mat multiply with matrix [[3, -1],[8, -2]]', function () { var mat3 = mat.dMat.multiplyT(mat2); var controlMat = new la.Matrix([[27, -7], [38, -10]]); for (var i = 0; i < controlMat.rows; i++) { for (var j = 0; j < controlMat.cols; j++) { assert.strictEqual(mat3.at(i, j), controlMat.at(i, j)); } } }) }); describe('MultiplyT "Sparse Matrix" Test', function () { var mat = new DMatrix(); it('should tranpose mat multiply with matrix [[3, -1],[8, -2]]', function () { var mat3 = mat.dMat.multiplyT(new la.SparseMatrix([[[0, 3], [1, 8]], [[0, -1], [1, -2]]])); var controlMat = new la.Matrix([[27, -7], [38, -10]]); for (var i = 0; i < controlMat.rows; i++) { for (var j = 0; j < controlMat.cols; j++) { assert.strictEqual(mat3.at(i, j), controlMat.at(i, j)); } } }) }); describe('Plus Test', function () { var mat = new DMatrix(); it('should add mat and mat2', function () { var mat3 = mat.dMat.plus(mat2); assert.deepEqual(mat3, mat.dMat); for (var i = 0; i < mat.dMat.rows; i++) { for (var j = 0; j < mat.dMat.cols; j++) { assert.strictEqual(mat3.at(i, j), mat.dMat.at(i, j) + mat2.at(i, j)); } } }) }); describe('Sparse Test', function () { var mat = new DMatrix(); it('should return the sparse matrix of mat', function () { var spMat = mat.dMat.sparse(); var controlspMat = new la.SparseMatrix([[[0, 1], [1, 3]], [[0, 2], [1, 4]]]); for (var i = 0; i < mat.dMat.rows; i++) { for (var j = 0; j < mat.dMat.cols; j++) { assert.eqtol(spMat.at(i, j), controlspMat.at(i, j)); } } }) }); describe('Minus Test', function () { var mat = new DMatrix(); it('should substract mat2 from mat', function () { var mat3 = mat.dMat.minus(mat2); assert.deepEqual(mat3, mat.dMat); for (var i = 0; i < mat.dMat.rows; i++) { for (var j = 0; j < mat.dMat.cols; j++) { assert.strictEqual(mat3.at(i, j), mat.dMat.at(i, j) - mat2.at(i, j)); } } }) }); describe('Transpose Test', function () { var mat = new DMatrix(); it('should transpose mat', function () { var mat3 = mat.dMat.transpose(); assert.deepEqual(mat3, mat.dMat); for (var i = 0; i < mat.dMat.rows; i++) { for (var j = 0; j < mat.dMat.cols; j++) { assert.strictEqual(mat3.at(j, i), mat.dMat.at(i, j)); } } }) }); describe('Solve Test', function () { var mat = new DMatrix(); it('should solve linear system A*x = y', function () { var vec = mat.dMat.solve(new la.Vector([5, 11])); var solution = new la.Vector([1, 2]); assert.deepEqual(vec, solution); for (var i = 0; i < solution.length; i++) { assert.strictEqual(vec.at(i), solution.at(i)); } }) }); describe('RowNorms Test', function () { var mat = new DMatrix(); it('should return vector with norm of i-th row as i-th element', function () { var vec = mat.dMat.rowNorms(); var controlVec = new la.Vector([Math.sqrt(5), Math.sqrt(25)]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.eqtol(vec.at(i), controlVec.at(i)); } }) }); describe('ColNorms Test', function () { var mat = new DMatrix(); it('should return vector with norm of i-th column as i-th element', function () { var vec = mat.dMat.colNorms(); var controlVec = new la.Vector([Math.sqrt(10), Math.sqrt(20)]); assert.deepEqual(vec, controlVec); for (var i = 0; i < controlVec.length; i++) { assert.eqtol(vec.at(i), controlVec.at(i)); } }) }); describe('NormalizeCols Test', function () { var mat = new DMatrix(); it('should normalize the columns of matrix mat', function () { mat.dMat.normalizeCols(); var controlMat = new la.Matrix([[1 / Math.sqrt(10), 2 / Math.sqrt(20)], [3 / Math.sqrt(10), 4 / Math.sqrt(20)]]); assert.deepEqual(mat.dMat, controlMat); for (var i = 0; i < controlMat.rows; i++) { for (var j = 0; j < controlMat.cols; j++) { as