Merge pull request #152 from itsvinayak/master

Javascript/linear-algebra-javascript
This commit is contained in:
Christian Clauss
2020-05-05 22:25:58 +02:00
committed by GitHub
4 changed files with 108 additions and 60 deletions

View File

@ -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

View File

@ -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
@ -51,12 +53,12 @@ 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++) {
@ -64,12 +66,12 @@ var LinearAlgebra;
}
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++) {
@ -77,20 +79,20 @@ var LinearAlgebra;
}
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
@ -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
}
@ -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
@ -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)
@ -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
@ -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++) {
@ -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) {

View File

@ -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)')
})
})
})
@ -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 () {
@ -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 () {
@ -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)')
})
})
})
@ -112,37 +112,65 @@ 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))
@ -150,20 +178,36 @@ describe('class Matrix', function () {
})
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))
})
})
})
})

View File

@ -1,4 +1,4 @@
const numRows = 5
const numRows = 5
var generate = function (numRows) {
const triangle = [[1], [1, 1]]