npx standard --fix

This commit is contained in:
cclauss
2020-05-03 09:05:12 +02:00
parent e62ad2f73e
commit 856dc2f63c
47 changed files with 2240 additions and 2371 deletions

View File

@@ -1,325 +1,307 @@
/*
author: Christian Bender
license: MIT-license
The namespace LinearAlgebra contains useful classes and functions for dealing with
linear algebra under JavaScript.
*/
var LinearAlgebra;
(function (LinearAlgebra) {
/*
/*
class: Vector
This class represents a vector of arbitrary size and operations on it.
*/
var Vector = /** @class */ (function () {
// constructor
function Vector(N, comps) {
if (comps === void 0) { comps = []; }
this.components = new Array(N);
if (comps.length == 0) {
for (var i = 0; i < N; i++) {
this.components[i] = 0.0;
}
}
else {
if (N == comps.length) {
this.components = comps;
}
else {
throw "Vector: invalide size!";
}
}
} // end of constructor
// returns the size of this vector.
// not the eulidean length!
Vector.prototype.size = function () {
return this.components.length;
};
// computes the eulidean length.
Vector.prototype.eulideanLength = function () {
var sum = 0;
for (var i = 0; i < this.components.length; i++) {
sum += this.components[i] * this.components[i];
}
return Math.sqrt(sum);
};
// getter for the components of the vector.
// returns a specified component (index)
Vector.prototype.component = function (index) {
return this.components[index];
};
// setter for a specified component of this vector.
Vector.prototype.changeComponent = function (index, value) {
if (index >= 0 && index < this.components.length) {
this.components[index] = value;
}
else {
throw "changeComponent: index out of bounds!";
}
};
// vector addition
Vector.prototype.add = function (other) {
if (this.size() == other.size()) {
var SIZE = this.size();
var ans = new Vector(SIZE);
for (var i = 0; i < SIZE; i++) {
ans.changeComponent(i, (this.components[i] + other.component(i)));
}
return ans;
}
else {
throw "add: vector must have same size!";
}
};
// vector subtraction
Vector.prototype.sub = function (other) {
if (this.size() == other.size()) {
var SIZE = this.size();
var ans = new Vector(SIZE);
for (var i = 0; i < SIZE; i++) {
ans.changeComponent(i, (this.components[i] - other.component(i)));
}
return ans;
}
else {
throw "add: vector must have same size!";
}
};
// dot-product
Vector.prototype.dot = function (other) {
var sum = 0;
if (other.size() == this.size()) {
var SIZE = other.size();
for (var i = 0; i < SIZE; i++) {
sum += this.components[i] * other.component(i);
}
return sum;
}
else {
throw "dot: vectors must have same size!";
}
};
// scalar multiplication
Vector.prototype.scalar = function (s) {
var SIZE = this.size();
var ans = new Vector(SIZE);
for (var i = 0; i < SIZE; i++) {
ans.changeComponent(i, (this.components[i] * s));
}
return ans;
};
// returns a string representation of this vector.
Vector.prototype.toString = function () {
var ans = "(";
var SIZE = this.components.length;
for (var i = 0; i < SIZE; i++) {
if (i < SIZE - 1) {
ans += this.components[i] + ",";
}
else {
ans += this.components[i] + ")";
}
}
return ans;
};
// converts this vector in a unit basis vector and returns it.
// the One is on position 'pos'
Vector.prototype.createUnitBasis = function (pos) {
if (pos >= 0 && pos < this.components.length) {
for (var i = 0; i < this.components.length; i++) {
if (i == pos) {
this.components[i] = 1.0;
}
else {
this.components[i] = 0.0;
}
}
}
else {
throw "createUnitBasis: index out of bounds";
}
return this;
};
// normalizes this vector and returns it.
Vector.prototype.norm = function () {
var SIZE = this.size();
var quotient = 1.0 / this.eulideanLength();
for (var i = 0; i < SIZE; i++) {
this.components[i] = this.components[i] * quotient;
}
return this;
};
// returns true if the vectors are equal otherwise false.
Vector.prototype.equal = function (other) {
var ans = true;
var SIZE = this.size();
var EPSILON = 0.001;
if (SIZE == other.size()) {
for (var i = 0; i < SIZE; i++) {
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
ans = false;
}
}
}
else {
ans = false;
}
return ans;
};
return Vector;
}()); // end of class Vector
LinearAlgebra.Vector = Vector;
// -------------- global functions ---------------------------------
// returns a unit basis vector of size N with a One on position 'pos'
function unitBasisVector(N, pos) {
var ans = new Vector(N);
var Vector = /** @class */ (function () {
// constructor
function Vector (N, comps) {
if (comps === void 0) { comps = [] }
this.components = new Array(N)
if (comps.length == 0) {
for (var i = 0; i < N; i++) {
if (i == pos) {
ans.changeComponent(i, 1.0);
}
else {
ans.changeComponent(i, 0);
}
this.components[i] = 0.0
}
return ans;
}
LinearAlgebra.unitBasisVector = unitBasisVector;
// returns a random vector with integer components (between 'a' and 'b') of size N.
function randomVectorInt(N, a, b) {
var ans = new Vector(N);
for (var i = 0; i < N; i++) {
ans.changeComponent(i, (Math.floor((Math.random() * b) + a)));
} else {
if (N == comps.length) {
this.components = comps
} else {
throw 'Vector: invalide size!'
}
return ans;
}
} // end of constructor
// returns the size of this vector.
// not the eulidean length!
Vector.prototype.size = function () {
return this.components.length
}
LinearAlgebra.randomVectorInt = randomVectorInt;
// returns a random vector with floating point components (between 'a' and 'b') of size N.
function randomVectorFloat(N, a, b) {
var ans = new Vector(N);
for (var i = 0; i < N; i++) {
ans.changeComponent(i, ((Math.random() * b) + a));
// computes the eulidean length.
Vector.prototype.eulideanLength = function () {
var sum = 0
for (var i = 0; i < this.components.length; i++) {
sum += this.components[i] * this.components[i]
}
return Math.sqrt(sum)
}
// getter for the components of the vector.
// returns a specified component (index)
Vector.prototype.component = function (index) {
return this.components[index]
}
// setter for a specified component of this vector.
Vector.prototype.changeComponent = function (index, value) {
if (index >= 0 && index < this.components.length) {
this.components[index] = value
} else {
throw 'changeComponent: index out of bounds!'
}
}
// vector addition
Vector.prototype.add = function (other) {
if (this.size() == other.size()) {
var SIZE = this.size()
var ans = new Vector(SIZE)
for (var i = 0; i < SIZE; i++) {
ans.changeComponent(i, (this.components[i] + other.component(i)))
}
return ans;
return ans
} else {
throw 'add: vector must have same size!'
}
}
LinearAlgebra.randomVectorFloat = randomVectorFloat;
// ------------------ end of global functions -----------------------------
/*
// vector subtraction
Vector.prototype.sub = function (other) {
if (this.size() == other.size()) {
var SIZE = this.size()
var ans = new Vector(SIZE)
for (var i = 0; i < SIZE; i++) {
ans.changeComponent(i, (this.components[i] - other.component(i)))
}
return ans
} else {
throw 'add: vector must have same size!'
}
}
// dot-product
Vector.prototype.dot = function (other) {
var sum = 0
if (other.size() == this.size()) {
var SIZE = other.size()
for (var i = 0; i < SIZE; i++) {
sum += this.components[i] * other.component(i)
}
return sum
} else {
throw 'dot: vectors must have same size!'
}
}
// scalar multiplication
Vector.prototype.scalar = function (s) {
var SIZE = this.size()
var ans = new Vector(SIZE)
for (var i = 0; i < SIZE; i++) {
ans.changeComponent(i, (this.components[i] * s))
}
return ans
}
// returns a string representation of this vector.
Vector.prototype.toString = function () {
var ans = '('
var SIZE = this.components.length
for (var i = 0; i < SIZE; i++) {
if (i < SIZE - 1) {
ans += this.components[i] + ','
} else {
ans += this.components[i] + ')'
}
}
return ans
}
// converts this vector in a unit basis vector and returns it.
// the One is on position 'pos'
Vector.prototype.createUnitBasis = function (pos) {
if (pos >= 0 && pos < this.components.length) {
for (var i = 0; i < this.components.length; i++) {
if (i == pos) {
this.components[i] = 1.0
} else {
this.components[i] = 0.0
}
}
} else {
throw 'createUnitBasis: index out of bounds'
}
return this
}
// normalizes this vector and returns it.
Vector.prototype.norm = function () {
var SIZE = this.size()
var quotient = 1.0 / this.eulideanLength()
for (var i = 0; i < SIZE; i++) {
this.components[i] = this.components[i] * quotient
}
return this
}
// returns true if the vectors are equal otherwise false.
Vector.prototype.equal = function (other) {
var ans = true
var SIZE = this.size()
var EPSILON = 0.001
if (SIZE == other.size()) {
for (var i = 0; i < SIZE; i++) {
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
ans = false
}
}
} else {
ans = false
}
return ans
}
return Vector
}()) // end of class Vector
LinearAlgebra.Vector = Vector
// -------------- global functions ---------------------------------
// returns a unit basis vector of size N with a One on position 'pos'
function unitBasisVector (N, pos) {
var ans = new Vector(N)
for (var i = 0; i < N; i++) {
if (i == pos) {
ans.changeComponent(i, 1.0)
} else {
ans.changeComponent(i, 0)
}
}
return ans
}
LinearAlgebra.unitBasisVector = unitBasisVector
// returns a random vector with integer components (between 'a' and 'b') of size N.
function randomVectorInt (N, a, b) {
var ans = new Vector(N)
for (var i = 0; i < N; i++) {
ans.changeComponent(i, (Math.floor((Math.random() * b) + a)))
}
return ans
}
LinearAlgebra.randomVectorInt = randomVectorInt
// returns a random vector with floating point components (between 'a' and 'b') of size N.
function randomVectorFloat (N, a, b) {
var ans = new Vector(N)
for (var i = 0; i < N; i++) {
ans.changeComponent(i, ((Math.random() * b) + a))
}
return ans
}
LinearAlgebra.randomVectorFloat = randomVectorFloat
// ------------------ end of global functions -----------------------------
/*
class: Matrix
This class represents a matrix of arbitrary size and operations on it.
*/
var Matrix = /** @class */ (function () {
// constructor for zero-matrix or fix number matrix.
function Matrix(row, col, comps) {
if (comps === void 0) { comps = []; }
if (comps.length == 0) {
this.matrix = new Array();
var rowVector = new Array();
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
rowVector[j] = 0;
}
this.matrix[i] = rowVector;
rowVector = new Array();
}
}
else {
this.matrix = comps;
}
this.rows = row;
this.cols = col;
var Matrix = /** @class */ (function () {
// constructor for zero-matrix or fix number matrix.
function Matrix (row, col, comps) {
if (comps === void 0) { comps = [] }
if (comps.length == 0) {
this.matrix = new Array()
var rowVector = new Array()
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
rowVector[j] = 0
}
this.matrix[i] = rowVector
rowVector = new Array()
}
// returns the specified component.
Matrix.prototype.component = function (x, y) {
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols) {
return this.matrix[x][y];
} else {
this.matrix = comps
}
this.rows = row
this.cols = col
}
// returns the specified component.
Matrix.prototype.component = function (x, y) {
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols) {
return this.matrix[x][y]
} else {
throw new Error('component: index out of bounds')
}
}
// changes the specified component with value 'value'.
Matrix.prototype.changeComponent = function (x, y, value) {
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols) {
this.matrix[x][y] = value
} else {
throw new Error('changeComponent: index out of bounds')
}
}
// returns a string representation of this matrix.
Matrix.prototype.toString = function () {
var ans = ''
for (var i = 0; i < this.rows; i++) {
ans += '|'
for (var j = 0; j < this.cols; j++) {
if (j < this.cols - 1) {
ans += this.matrix[i][j] + ','
} else {
if (i < this.rows - 1) {
ans += this.matrix[i][j] + '|\n'
} else {
ans += this.matrix[i][j] + '|'
}
else {
throw new Error("component: index out of bounds");
}
}
}
return ans
}
// returns the dimension rows x cols as number array
Matrix.prototype.dimension = function () {
var ans = new Array()
ans[0] = this.rows
ans[1] = this.cols
return ans
}
// matrix addition. returns the result.
Matrix.prototype.add = function (other) {
if (this.rows == other.dimension()[0] &&
this.cols == other.dimension()[1]) {
var ans = new Matrix(this.rows, this.cols)
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
ans.changeComponent(i, j, (this.matrix[i][j] + other.component(i, j)))
}
}
return ans
} else {
throw new Error('add: matrices must have same dimension!')
}
}
// returns true if the matrices are equal, otherwise false.
Matrix.prototype.equal = function (other) {
var ans = true
var EPSILON = 0.001
if (this.rows == other.dimension()[0] &&
this.cols == other.dimension()[1]) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
if (Math.abs(this.matrix[i][j] - other.component(i, j)) > EPSILON) {
ans = false
}
};
// changes the specified component with value 'value'.
Matrix.prototype.changeComponent = function (x, y, value) {
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols) {
this.matrix[x][y] = value;
}
else {
throw new Error("changeComponent: index out of bounds");
}
};
// returns a string representation of this matrix.
Matrix.prototype.toString = function () {
var ans = "";
for (var i = 0; i < this.rows; i++) {
ans += "|";
for (var j = 0; j < this.cols; j++) {
if (j < this.cols - 1) {
ans += this.matrix[i][j] + ",";
}
else {
if (i < this.rows - 1) {
ans += this.matrix[i][j] + "|\n";
}
else {
ans += this.matrix[i][j] + "|";
}
}
}
}
return ans;
};
// returns the dimension rows x cols as number array
Matrix.prototype.dimension = function () {
var ans = new Array();
ans[0] = this.rows;
ans[1] = this.cols;
return ans;
};
// matrix addition. returns the result.
Matrix.prototype.add = function (other) {
if (this.rows == other.dimension()[0]
&& this.cols == other.dimension()[1]) {
var ans = new Matrix(this.rows, this.cols);
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
ans.changeComponent(i, j, (this.matrix[i][j] + other.component(i, j)));
}
}
return ans;
}
else {
throw new Error("add: matrices must have same dimension!");
}
};
// returns true if the matrices are equal, otherwise false.
Matrix.prototype.equal = function (other) {
var ans = true;
var EPSILON = 0.001;
if (this.rows == other.dimension()[0]
&& this.cols == other.dimension()[1]) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
if (Math.abs(this.matrix[i][j] - other.component(i, j)) > EPSILON) {
ans = false;
}
}
}
}
else {
ans = false;
}
return ans;
};
// matrix-scalar multiplication
Matrix.prototype.scalar = function (c) {
var ans = new Matrix(this.rows, this.cols);
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
ans.changeComponent(i, j, (this.matrix[i][j] * c));
}
}
return ans;
};
return Matrix;
}()); // end of class Matrix
LinearAlgebra.Matrix = Matrix;
})(LinearAlgebra || (LinearAlgebra = {})); // end of namespace LinearAlgebra
}
}
} else {
ans = false
}
return ans
}
// matrix-scalar multiplication
Matrix.prototype.scalar = function (c) {
var ans = new Matrix(this.rows, this.cols)
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
ans.changeComponent(i, j, (this.matrix[i][j] * c))
}
}
return ans
}
return Matrix
}()) // end of class Matrix
LinearAlgebra.Matrix = Matrix
})(LinearAlgebra || (LinearAlgebra = {})) // end of namespace LinearAlgebra

View File

@@ -6,165 +6,164 @@
The tests use javascript test-framework mocha
*/
var assert = require('assert');
var fs = require('fs');
var assert = require('assert')
var fs = require('fs')
// file is included here
eval(fs.readFileSync('src/la_lib.js') + '');
eval(fs.readFileSync('src/la_lib.js') + '')
// Tests goes here
// creating some vectors
describe('Create Vectors', function () {
describe('#toString()', function () {
it('should return a string representation', function () {
assert.equal((new LinearAlgebra.Vector(3, [1, 2, 3])).toString(), "(1,2,3)");
});
});
describe("#unitBasisVector()", function () {
it("should return a unit basis vector", function () {
assert.equal(LinearAlgebra.unitBasisVector(3, 1).toString(), "(0,1,0)");
});
});
});
describe('#toString()', function () {
it('should return a string representation', function () {
assert.equal((new LinearAlgebra.Vector(3, [1, 2, 3])).toString(), '(1,2,3)')
})
})
describe('#unitBasisVector()', function () {
it('should return a unit basis vector', function () {
assert.equal(LinearAlgebra.unitBasisVector(3, 1).toString(), '(0,1,0)')
})
})
})
// operations on it.
describe("Vector operations", function () {
describe("#add()", function () {
it("should return vector (2,4,6)", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3]);
var y = new LinearAlgebra.Vector(3, [1, 2, 3]);
assert.equal((x.add(y)).toString(), "(2,4,6)");
});
});
describe("#sub()", function () {
it("should return vector (0,0,0)", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3]);
var y = new LinearAlgebra.Vector(3, [1, 2, 3]);
assert.equal((x.sub(y)).toString(), "(0,0,0)");
});
});
describe("#dot()", function () {
it("should return the dot-product", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3]);
var y = new LinearAlgebra.Vector(3, [5, 6, 7]);
assert.equal(x.dot(y), 38);
});
});
describe("#scalar()", function () {
it("should return the scalar product", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3]);
assert.equal(x.scalar(2).toString(), "(2,4,6)");
});
});
describe("#norm()", function () {
it("should return the normalizes vector", function () {
var x = new LinearAlgebra.Vector(4, [9, 0, 3, 1]);
var y = x.norm();
assert.ok(Math.abs(y.component(0) - (9.0 / Math.sqrt(91))) <= 0.01);
});
});
describe("#eulideanLength()", function () {
it("should return the eulidean length of the vector", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2]);
assert.ok(Math.abs(x.eulideanLength() - 3) <= 0.001);
});
});
describe("#size()", function () {
it("should return the size (not eulidean length!) of the vector", function () {
var x = LinearAlgebra.randomVectorInt(10, 1, 5);
assert.equal(x.size(), 10);
});
});
describe("#equal()", function () {
it("should compares two vectors", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2]);
var y = new LinearAlgebra.Vector(3, [1, 2, 3]);
assert.ok(x.equal(x));
assert.ok(!x.equal(y));
});
});
});
describe("Methods on vectors", function () {
describe("#component()", function () {
it("should return the specified component", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2]);
assert.equal(x.component(1), 2);
});
});
describe("#changeComponent()", function () {
it("should return the changed vector", function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2]);
x.changeComponent(1, 5);
assert.equal(x.toString(), "(1,5,2)");
});
});
describe("#toString()", function () {
it("should return a string representation of the vector", function () {
var x = new LinearAlgebra.Vector(4, [9, 0, 3, 1]);
assert.equal(x.toString(), "(9,0,3,1)");
});
});
});
describe("class Matrix", function () {
describe("#component()", function () {
it("should return the specified component", function () {
var A = new LinearAlgebra.Matrix(2, 2);
assert.equal(A.component(0, 1), 0);
var B = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]]);
assert.equal(B.component(1, 0), 3);
});
});
describe("#toString()", function () {
it("should return a string representation of the matrix", function () {
var A = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]]);
assert.equal(A.toString(), "|1,2|\n|3,4|");
});
});
describe("#dimension()", function () {
it("should return the dimension of the matrix as number array", function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
assert.equal(A.dimension()[0], 3);
assert.equal(A.dimension()[1], 2);
});
});
describe("#changeComponent()", function () {
it("should change the specified component of the matrix", function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
A.changeComponent(1, 0, 5);
assert.equal(A.component(1, 0), 5);
});
});
describe("#equal()", function () {
it("should compares the matrices", function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
var B = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
var C = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]]);
var D = new LinearAlgebra.Matrix(2, 2, [[1, 2], [5, 4]]);
assert.ok(A.equal(B));
assert.ok(!A.equal(C));
assert.ok(!C.equal(D));
});
});
describe("#add()", function () {
it("should return the result of the matrix addition", function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
var B = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
var C = A.add(B);
assert.equal(C.component(1, 0), 6);
assert.equal(C.component(1, 1), 8);
assert.equal(C.component(0, 0), 2);
});
});
describe("#scalar()", function () {
it("should return the result of the matrix-scalar multiplication", function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
var B = A.scalar(2);
var C = new LinearAlgebra.Matrix(3, 2, [[2, 4], [6, 8], [10, 12]]);
assert.ok(B.equal(C));
});
describe('Vector operations', function () {
describe('#add()', function () {
it('should return vector (2,4,6)', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
assert.equal((x.add(y)).toString(), '(2,4,6)')
})
});
})
describe('#sub()', function () {
it('should return vector (0,0,0)', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
assert.equal((x.sub(y)).toString(), '(0,0,0)')
})
})
describe('#dot()', function () {
it('should return the dot-product', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
var y = new LinearAlgebra.Vector(3, [5, 6, 7])
assert.equal(x.dot(y), 38)
})
})
describe('#scalar()', function () {
it('should return the scalar product', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
assert.equal(x.scalar(2).toString(), '(2,4,6)')
})
})
describe('#norm()', function () {
it('should return the normalizes vector', function () {
var x = new LinearAlgebra.Vector(4, [9, 0, 3, 1])
var y = x.norm()
assert.ok(Math.abs(y.component(0) - (9.0 / Math.sqrt(91))) <= 0.01)
})
})
describe('#eulideanLength()', function () {
it('should return the eulidean length of the vector', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
assert.ok(Math.abs(x.eulideanLength() - 3) <= 0.001)
})
})
describe('#size()', function () {
it('should return the size (not eulidean length!) of the vector', function () {
var x = LinearAlgebra.randomVectorInt(10, 1, 5)
assert.equal(x.size(), 10)
})
})
describe('#equal()', function () {
it('should compares two vectors', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
assert.ok(x.equal(x))
assert.ok(!x.equal(y))
})
})
})
describe('Methods on vectors', function () {
describe('#component()', function () {
it('should return the specified component', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
assert.equal(x.component(1), 2)
})
})
describe('#changeComponent()', function () {
it('should return the changed vector', function () {
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
x.changeComponent(1, 5)
assert.equal(x.toString(), '(1,5,2)')
})
})
describe('#toString()', function () {
it('should return a string representation of the vector', function () {
var x = new LinearAlgebra.Vector(4, [9, 0, 3, 1])
assert.equal(x.toString(), '(9,0,3,1)')
})
})
})
describe('class Matrix', function () {
describe('#component()', function () {
it('should return the specified component', function () {
var A = new LinearAlgebra.Matrix(2, 2)
assert.equal(A.component(0, 1), 0)
var B = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]])
assert.equal(B.component(1, 0), 3)
})
})
describe('#toString()', function () {
it('should return a string representation of the matrix', function () {
var A = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]])
assert.equal(A.toString(), '|1,2|\n|3,4|')
})
})
describe('#dimension()', function () {
it('should return the dimension of the matrix as number array', function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
assert.equal(A.dimension()[0], 3)
assert.equal(A.dimension()[1], 2)
})
})
describe('#changeComponent()', function () {
it('should change the specified component of the matrix', function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
A.changeComponent(1, 0, 5)
assert.equal(A.component(1, 0), 5)
})
})
describe('#equal()', function () {
it('should compares the matrices', function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
var B = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
var C = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]])
var D = new LinearAlgebra.Matrix(2, 2, [[1, 2], [5, 4]])
assert.ok(A.equal(B))
assert.ok(!A.equal(C))
assert.ok(!C.equal(D))
})
})
describe('#add()', function () {
it('should return the result of the matrix addition', function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
var B = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
var C = A.add(B)
assert.equal(C.component(1, 0), 6)
assert.equal(C.component(1, 1), 8)
assert.equal(C.component(0, 0), 2)
})
})
describe('#scalar()', function () {
it('should return the result of the matrix-scalar multiplication', function () {
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
var B = A.scalar(2)
var C = new LinearAlgebra.Matrix(3, 2, [[2, 4], [6, 8], [10, 12]])
assert.ok(B.equal(C))
})
})
})