Skip to content

Commit c3532ca

Browse files
committed
Javascript/linear-algebra-javascript
1 parent d37af25 commit c3532ca

File tree

3 files changed

+107
-59
lines changed

3 files changed

+107
-59
lines changed

linear-algebra-javascript/src/la_lib.js

+29-25
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ var LinearAlgebra;
1414
var Vector = /** @class */ (function () {
1515
// constructor
1616
function Vector (N, comps) {
17-
if (comps === void 0) { comps = [] }
17+
if (comps === undefined) {
18+
comps = []
19+
}
1820
this.components = new Array(N)
19-
if (comps.length == 0) {
21+
if (comps.length === 0) {
2022
for (var i = 0; i < N; i++) {
2123
this.components[i] = 0.0
2224
}
2325
} else {
24-
if (N == comps.length) {
26+
if (N === comps.length) {
2527
this.components = comps
2628
} else {
27-
throw 'Vector: invalide size!'
29+
throw new Error('Vector: invalide size!')
2830
}
2931
}
3032
} // end of constructor
@@ -51,46 +53,46 @@ var LinearAlgebra;
5153
if (index >= 0 && index < this.components.length) {
5254
this.components[index] = value
5355
} else {
54-
throw 'changeComponent: index out of bounds!'
56+
throw new Error('changeComponent: index out of bounds!')
5557
}
5658
}
5759
// vector addition
5860
Vector.prototype.add = function (other) {
59-
if (this.size() == other.size()) {
61+
if (this.size() === other.size()) {
6062
var SIZE = this.size()
6163
var ans = new Vector(SIZE)
6264
for (var i = 0; i < SIZE; i++) {
6365
ans.changeComponent(i, (this.components[i] + other.component(i)))
6466
}
6567
return ans
6668
} else {
67-
throw 'add: vector must have same size!'
69+
throw new Error('add: vector must have same size!')
6870
}
6971
}
7072
// vector subtraction
7173
Vector.prototype.sub = function (other) {
72-
if (this.size() == other.size()) {
74+
if (this.size() === other.size()) {
7375
var SIZE = this.size()
7476
var ans = new Vector(SIZE)
7577
for (var i = 0; i < SIZE; i++) {
7678
ans.changeComponent(i, (this.components[i] - other.component(i)))
7779
}
7880
return ans
7981
} else {
80-
throw 'add: vector must have same size!'
82+
throw new Error('add: vector must have same size!')
8183
}
8284
}
8385
// dot-product
8486
Vector.prototype.dot = function (other) {
8587
var sum = 0
86-
if (other.size() == this.size()) {
88+
if (other.size() === this.size()) {
8789
var SIZE = other.size()
8890
for (var i = 0; i < SIZE; i++) {
8991
sum += this.components[i] * other.component(i)
9092
}
9193
return sum
9294
} else {
93-
throw 'dot: vectors must have same size!'
95+
throw new Error('dot: vectors must have same size!')
9496
}
9597
}
9698
// scalar multiplication
@@ -120,14 +122,14 @@ var LinearAlgebra;
120122
Vector.prototype.createUnitBasis = function (pos) {
121123
if (pos >= 0 && pos < this.components.length) {
122124
for (var i = 0; i < this.components.length; i++) {
123-
if (i == pos) {
125+
if (i === pos) {
124126
this.components[i] = 1.0
125127
} else {
126128
this.components[i] = 0.0
127129
}
128130
}
129131
} else {
130-
throw 'createUnitBasis: index out of bounds'
132+
throw new Error('createUnitBasis: index out of bounds')
131133
}
132134
return this
133135
}
@@ -145,7 +147,7 @@ var LinearAlgebra;
145147
var ans = true
146148
var SIZE = this.size()
147149
var EPSILON = 0.001
148-
if (SIZE == other.size()) {
150+
if (SIZE === other.size()) {
149151
for (var i = 0; i < SIZE; i++) {
150152
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
151153
ans = false
@@ -164,7 +166,7 @@ var LinearAlgebra;
164166
function unitBasisVector (N, pos) {
165167
var ans = new Vector(N)
166168
for (var i = 0; i < N; i++) {
167-
if (i == pos) {
169+
if (i === pos) {
168170
ans.changeComponent(i, 1.0)
169171
} else {
170172
ans.changeComponent(i, 0)
@@ -199,16 +201,18 @@ var LinearAlgebra;
199201
var Matrix = /** @class */ (function () {
200202
// constructor for zero-matrix or fix number matrix.
201203
function Matrix (row, col, comps) {
202-
if (comps === void 0) { comps = [] }
203-
if (comps.length == 0) {
204-
this.matrix = new Array()
205-
var rowVector = new Array()
204+
if (comps === undefined) {
205+
comps = []
206+
}
207+
if (comps.length === 0) {
208+
this.matrix = []
209+
var rowVector = []
206210
for (var i = 0; i < row; i++) {
207211
for (var j = 0; j < col; j++) {
208212
rowVector[j] = 0
209213
}
210214
this.matrix[i] = rowVector
211-
rowVector = new Array()
215+
rowVector = []
212216
}
213217
} else {
214218
this.matrix = comps
@@ -253,15 +257,15 @@ var LinearAlgebra;
253257
}
254258
// returns the dimension rows x cols as number array
255259
Matrix.prototype.dimension = function () {
256-
var ans = new Array()
260+
var ans = []
257261
ans[0] = this.rows
258262
ans[1] = this.cols
259263
return ans
260264
}
261265
// matrix addition. returns the result.
262266
Matrix.prototype.add = function (other) {
263-
if (this.rows == other.dimension()[0] &&
264-
this.cols == other.dimension()[1]) {
267+
if (this.rows === other.dimension()[0] &&
268+
this.cols === other.dimension()[1]) {
265269
var ans = new Matrix(this.rows, this.cols)
266270
for (var i = 0; i < this.rows; i++) {
267271
for (var j = 0; j < this.cols; j++) {
@@ -277,8 +281,8 @@ var LinearAlgebra;
277281
Matrix.prototype.equal = function (other) {
278282
var ans = true
279283
var EPSILON = 0.001
280-
if (this.rows == other.dimension()[0] &&
281-
this.cols == other.dimension()[1]) {
284+
if (this.rows === other.dimension()[0] &&
285+
this.cols === other.dimension()[1]) {
282286
for (var i = 0; i < this.rows; i++) {
283287
for (var j = 0; j < this.cols; j++) {
284288
if (Math.abs(this.matrix[i][j] - other.component(i, j)) > EPSILON) {

linear-algebra-javascript/test/test.js

+77-33
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
This file contains the test-suite for the linear algebra library.
66
The tests use javascript test-framework mocha
77
*/
8+
/* eslint-disable */
89

910
var assert = require('assert')
1011
var fs = require('fs')
1112

1213
// file is included here
1314
eval(fs.readFileSync('src/la_lib.js') + '')
14-
1515
// Tests goes here
1616

1717
// creating some vectors
1818
describe('Create Vectors', function () {
1919
describe('#toString()', function () {
2020
it('should return a string representation', function () {
21-
assert.equal((new LinearAlgebra.Vector(3, [1, 2, 3])).toString(), '(1,2,3)')
21+
assert.strictEqual((new LinearAlgebra.Vector(3, [1, 2, 3])).toString(), '(1,2,3)')
2222
})
2323
})
2424
describe('#unitBasisVector()', function () {
2525
it('should return a unit basis vector', function () {
26-
assert.equal(LinearAlgebra.unitBasisVector(3, 1).toString(), '(0,1,0)')
26+
assert.strictEqual(LinearAlgebra.unitBasisVector(3, 1).toString(), '(0,1,0)')
2727
})
2828
})
2929
})
@@ -34,27 +34,27 @@ describe('Vector operations', function () {
3434
it('should return vector (2,4,6)', function () {
3535
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
3636
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
37-
assert.equal((x.add(y)).toString(), '(2,4,6)')
37+
assert.strictEqual((x.add(y)).toString(), '(2,4,6)')
3838
})
3939
})
4040
describe('#sub()', function () {
4141
it('should return vector (0,0,0)', function () {
4242
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
4343
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
44-
assert.equal((x.sub(y)).toString(), '(0,0,0)')
44+
assert.strictEqual((x.sub(y)).toString(), '(0,0,0)')
4545
})
4646
})
4747
describe('#dot()', function () {
4848
it('should return the dot-product', function () {
4949
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
5050
var y = new LinearAlgebra.Vector(3, [5, 6, 7])
51-
assert.equal(x.dot(y), 38)
51+
assert.strictEqual(x.dot(y), 38)
5252
})
5353
})
5454
describe('#scalar()', function () {
5555
it('should return the scalar product', function () {
5656
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
57-
assert.equal(x.scalar(2).toString(), '(2,4,6)')
57+
assert.strictEqual(x.scalar(2).toString(), '(2,4,6)')
5858
})
5959
})
6060
describe('#norm()', function () {
@@ -73,7 +73,7 @@ describe('Vector operations', function () {
7373
describe('#size()', function () {
7474
it('should return the size (not eulidean length!) of the vector', function () {
7575
var x = LinearAlgebra.randomVectorInt(10, 1, 5)
76-
assert.equal(x.size(), 10)
76+
assert.strictEqual(x.size(), 10)
7777
})
7878
})
7979
describe('#equal()', function () {
@@ -90,20 +90,20 @@ describe('Methods on vectors', function () {
9090
describe('#component()', function () {
9191
it('should return the specified component', function () {
9292
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
93-
assert.equal(x.component(1), 2)
93+
assert.strictEqual(x.component(1), 2)
9494
})
9595
})
9696
describe('#changeComponent()', function () {
9797
it('should return the changed vector', function () {
9898
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
9999
x.changeComponent(1, 5)
100-
assert.equal(x.toString(), '(1,5,2)')
100+
assert.strictEqual(x.toString(), '(1,5,2)')
101101
})
102102
})
103103
describe('#toString()', function () {
104104
it('should return a string representation of the vector', function () {
105105
var x = new LinearAlgebra.Vector(4, [9, 0, 3, 1])
106-
assert.equal(x.toString(), '(9,0,3,1)')
106+
assert.strictEqual(x.toString(), '(9,0,3,1)')
107107
})
108108
})
109109
})
@@ -112,58 +112,102 @@ describe('class Matrix', function () {
112112
describe('#component()', function () {
113113
it('should return the specified component', function () {
114114
var A = new LinearAlgebra.Matrix(2, 2)
115-
assert.equal(A.component(0, 1), 0)
116-
var B = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]])
117-
assert.equal(B.component(1, 0), 3)
115+
assert.strictEqual(A.component(0, 1), 0)
116+
var B = new LinearAlgebra.Matrix(2, 2, [
117+
[1, 2],
118+
[3, 4]
119+
])
120+
assert.strictEqual(B.component(1, 0), 3)
118121
})
119122
})
120123
describe('#toString()', function () {
121124
it('should return a string representation of the matrix', function () {
122-
var A = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]])
123-
assert.equal(A.toString(), '|1,2|\n|3,4|')
125+
var A = new LinearAlgebra.Matrix(2, 2, [
126+
[1, 2],
127+
[3, 4]
128+
])
129+
assert.strictEqual(A.toString(), '|1,2|\n|3,4|')
124130
})
125131
})
126132
describe('#dimension()', function () {
127133
it('should return the dimension of the matrix as number array', function () {
128-
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
129-
assert.equal(A.dimension()[0], 3)
130-
assert.equal(A.dimension()[1], 2)
134+
var A = new LinearAlgebra.Matrix(3, 2, [
135+
[1, 2],
136+
[3, 4],
137+
[5, 6]
138+
])
139+
assert.strictEqual(A.dimension()[0], 3)
140+
assert.strictEqual(A.dimension()[1], 2)
131141
})
132142
})
133143
describe('#changeComponent()', function () {
134144
it('should change the specified component of the matrix', function () {
135-
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
145+
var A = new LinearAlgebra.Matrix(3, 2, [
146+
[1, 2],
147+
[3, 4],
148+
[5, 6]
149+
])
136150
A.changeComponent(1, 0, 5)
137-
assert.equal(A.component(1, 0), 5)
151+
assert.strictEqual(A.component(1, 0), 5)
138152
})
139153
})
140154
describe('#equal()', function () {
141155
it('should compares the matrices', function () {
142-
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
143-
var B = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
144-
var C = new LinearAlgebra.Matrix(2, 2, [[1, 2], [3, 4]])
145-
var D = new LinearAlgebra.Matrix(2, 2, [[1, 2], [5, 4]])
156+
var A = new LinearAlgebra.Matrix(3, 2, [
157+
[1, 2],
158+
[3, 4],
159+
[5, 6]
160+
])
161+
var B = new LinearAlgebra.Matrix(3, 2, [
162+
[1, 2],
163+
[3, 4],
164+
[5, 6]
165+
])
166+
var C = new LinearAlgebra.Matrix(2, 2, [
167+
[1, 2],
168+
[3, 4]
169+
])
170+
var D = new LinearAlgebra.Matrix(2, 2, [
171+
[1, 2],
172+
[5, 4]
173+
])
146174
assert.ok(A.equal(B))
147175
assert.ok(!A.equal(C))
148176
assert.ok(!C.equal(D))
149177
})
150178
})
151179
describe('#add()', function () {
152180
it('should return the result of the matrix addition', function () {
153-
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
154-
var B = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
181+
var A = new LinearAlgebra.Matrix(3, 2, [
182+
[1, 2],
183+
[3, 4],
184+
[5, 6]
185+
])
186+
var B = new LinearAlgebra.Matrix(3, 2, [
187+
[1, 2],
188+
[3, 4],
189+
[5, 6]
190+
])
155191
var C = A.add(B)
156-
assert.equal(C.component(1, 0), 6)
157-
assert.equal(C.component(1, 1), 8)
158-
assert.equal(C.component(0, 0), 2)
192+
assert.strictEqual(C.component(1, 0), 6)
193+
assert.strictEqual(C.component(1, 1), 8)
194+
assert.strictEqual(C.component(0, 0), 2)
159195
})
160196
})
161197
describe('#scalar()', function () {
162198
it('should return the result of the matrix-scalar multiplication', function () {
163-
var A = new LinearAlgebra.Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
199+
var A = new LinearAlgebra.Matrix(3, 2, [
200+
[1, 2],
201+
[3, 4],
202+
[5, 6]
203+
])
164204
var B = A.scalar(2)
165-
var C = new LinearAlgebra.Matrix(3, 2, [[2, 4], [6, 8], [10, 12]])
205+
var C = new LinearAlgebra.Matrix(3, 2, [
206+
[2, 4],
207+
[6, 8],
208+
[10, 12]
209+
])
166210
assert.ok(B.equal(C))
167211
})
168212
})
169-
})
213+
})

maths/pascalTriangle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const numRows = 5
1+
const numRows = 5
22

33
var generate = function (numRows) {
44
const triangle = [[1], [1, 1]]

0 commit comments

Comments
 (0)