Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: npm install, build, and test
run: |
npm install standard --save-dev
npx standard || true # several files are still on compliant
npx standard
cd linear-algebra-javascript
npm ci
npm run build --if-present
Expand Down
54 changes: 29 additions & 25 deletions linear-algebra-javascript/src/la_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ var LinearAlgebra;
var Vector = /** @class */ (function () {
// constructor
function Vector (N, comps) {
if (comps === void 0) { comps = [] }
if (comps === undefined) {
comps = []
}
this.components = new Array(N)
if (comps.length == 0) {
if (comps.length === 0) {
for (var i = 0; i < N; i++) {
this.components[i] = 0.0
}
} else {
if (N == comps.length) {
if (N === comps.length) {
this.components = comps
} else {
throw 'Vector: invalide size!'
throw new Error('Vector: invalide size!')
}
}
} // end of constructor
Expand All @@ -51,46 +53,46 @@ var LinearAlgebra;
if (index >= 0 && index < this.components.length) {
this.components[index] = value
} else {
throw 'changeComponent: index out of bounds!'
throw new Error('changeComponent: index out of bounds!')
}
}
// vector addition
Vector.prototype.add = function (other) {
if (this.size() == other.size()) {
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!'
throw new Error('add: vector must have same size!')
}
}
// vector subtraction
Vector.prototype.sub = function (other) {
if (this.size() == other.size()) {
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!'
throw new Error('add: vector must have same size!')
}
}
// dot-product
Vector.prototype.dot = function (other) {
var sum = 0
if (other.size() == this.size()) {
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!'
throw new Error('dot: vectors must have same size!')
}
}
// scalar multiplication
Expand Down Expand Up @@ -120,14 +122,14 @@ var LinearAlgebra;
Vector.prototype.createUnitBasis = function (pos) {
if (pos >= 0 && pos < this.components.length) {
for (var i = 0; i < this.components.length; i++) {
if (i == pos) {
if (i === pos) {
this.components[i] = 1.0
} else {
this.components[i] = 0.0
}
}
} else {
throw 'createUnitBasis: index out of bounds'
throw new Error('createUnitBasis: index out of bounds')
}
return this
}
Expand All @@ -145,7 +147,7 @@ var LinearAlgebra;
var ans = true
var SIZE = this.size()
var EPSILON = 0.001
if (SIZE == other.size()) {
if (SIZE === other.size()) {
for (var i = 0; i < SIZE; i++) {
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
ans = false
Expand All @@ -164,7 +166,7 @@ var LinearAlgebra;
function unitBasisVector (N, pos) {
var ans = new Vector(N)
for (var i = 0; i < N; i++) {
if (i == pos) {
if (i === pos) {
ans.changeComponent(i, 1.0)
} else {
ans.changeComponent(i, 0)
Expand Down Expand Up @@ -199,16 +201,18 @@ var LinearAlgebra;
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()
if (comps === undefined) {
comps = []
}
if (comps.length === 0) {
this.matrix = []
var rowVector = []
for (var i = 0; i < row; i++) {
for (var j = 0; j < col; j++) {
rowVector[j] = 0
}
this.matrix[i] = rowVector
rowVector = new Array()
rowVector = []
}
} else {
this.matrix = comps
Expand Down Expand Up @@ -253,15 +257,15 @@ var LinearAlgebra;
}
// returns the dimension rows x cols as number array
Matrix.prototype.dimension = function () {
var ans = new Array()
var ans = []
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]) {
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++) {
Expand All @@ -277,8 +281,8 @@ var LinearAlgebra;
Matrix.prototype.equal = function (other) {
var ans = true
var EPSILON = 0.001
if (this.rows == other.dimension()[0] &&
this.cols == other.dimension()[1]) {
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) {
Expand Down
110 changes: 77 additions & 33 deletions linear-algebra-javascript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
This file contains the test-suite for the linear algebra library.
The tests use javascript test-framework mocha
*/
/* eslint-disable */

var assert = require('assert')
var fs = require('fs')

// file is included here
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)')
assert.strictEqual((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)')
assert.strictEqual(LinearAlgebra.unitBasisVector(3, 1).toString(), '(0,1,0)')
})
})
})
Expand All @@ -34,27 +34,27 @@ describe('Vector operations', 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)')
assert.strictEqual((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)')
assert.strictEqual((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)
assert.strictEqual(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)')
assert.strictEqual(x.scalar(2).toString(), '(2,4,6)')
})
})
describe('#norm()', function () {
Expand All @@ -73,7 +73,7 @@ describe('Vector operations', function () {
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)
assert.strictEqual(x.size(), 10)
})
})
describe('#equal()', function () {
Expand All @@ -90,20 +90,20 @@ 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)
assert.strictEqual(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)')
assert.strictEqual(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)')
assert.strictEqual(x.toString(), '(9,0,3,1)')
})
})
})
Expand All @@ -112,58 +112,102 @@ 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)
assert.strictEqual(A.component(0, 1), 0)
var B = new LinearAlgebra.Matrix(2, 2, [
[1, 2],
[3, 4]
])
assert.strictEqual(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|')
var A = new LinearAlgebra.Matrix(2, 2, [
[1, 2],
[3, 4]
])
assert.strictEqual(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)
var A = new LinearAlgebra.Matrix(3, 2, [
[1, 2],
[3, 4],
[5, 6]
])
assert.strictEqual(A.dimension()[0], 3)
assert.strictEqual(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]])
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)
assert.strictEqual(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]])
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 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)
assert.strictEqual(C.component(1, 0), 6)
assert.strictEqual(C.component(1, 1), 8)
assert.strictEqual(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 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]])
var C = new LinearAlgebra.Matrix(3, 2, [
[2, 4],
[6, 8],
[10, 12]
])
assert.ok(B.equal(C))
})
})
})
})
Loading