mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
style: Fixed most styles (according to standardjs)
This commit is contained in:
@@ -5,13 +5,13 @@
|
||||
The namespace LinearAlgebra contains useful classes and functions for dealing with
|
||||
linear algebra under JavaScript.
|
||||
*/
|
||||
var LinearAlgebra;
|
||||
let LinearAlgebra;
|
||||
(function (LinearAlgebra) {
|
||||
/*
|
||||
class: Vector
|
||||
This class represents a vector of arbitrary size and operations on it.
|
||||
*/
|
||||
var Vector = /** @class */ (function () {
|
||||
const Vector = /** @class */ (function () {
|
||||
// constructor
|
||||
function Vector (N, comps) {
|
||||
if (comps === undefined) {
|
||||
@@ -19,7 +19,7 @@ var LinearAlgebra;
|
||||
}
|
||||
this.components = new Array(N)
|
||||
if (comps.length === 0) {
|
||||
for (var i = 0; i < N; i++) {
|
||||
for (let i = 0; i < N; i++) {
|
||||
this.components[i] = 0.0
|
||||
}
|
||||
} else {
|
||||
@@ -37,8 +37,8 @@ var LinearAlgebra;
|
||||
}
|
||||
// computes the eulidean length.
|
||||
Vector.prototype.eulideanLength = function () {
|
||||
var sum = 0
|
||||
for (var i = 0; i < this.components.length; i++) {
|
||||
let sum = 0
|
||||
for (let i = 0; i < this.components.length; i++) {
|
||||
sum += this.components[i] * this.components[i]
|
||||
}
|
||||
return Math.sqrt(sum)
|
||||
@@ -59,9 +59,9 @@ var LinearAlgebra;
|
||||
// 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++) {
|
||||
const SIZE = this.size()
|
||||
const ans = new Vector(SIZE)
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
ans.changeComponent(i, (this.components[i] + other.component(i)))
|
||||
}
|
||||
return ans
|
||||
@@ -72,9 +72,9 @@ var LinearAlgebra;
|
||||
// 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++) {
|
||||
const SIZE = this.size()
|
||||
const ans = new Vector(SIZE)
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
ans.changeComponent(i, (this.components[i] - other.component(i)))
|
||||
}
|
||||
return ans
|
||||
@@ -84,10 +84,10 @@ var LinearAlgebra;
|
||||
}
|
||||
// dot-product
|
||||
Vector.prototype.dot = function (other) {
|
||||
var sum = 0
|
||||
let sum = 0
|
||||
if (other.size() === this.size()) {
|
||||
var SIZE = other.size()
|
||||
for (var i = 0; i < SIZE; i++) {
|
||||
const SIZE = other.size()
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
sum += this.components[i] * other.component(i)
|
||||
}
|
||||
return sum
|
||||
@@ -97,18 +97,18 @@ var LinearAlgebra;
|
||||
}
|
||||
// scalar multiplication
|
||||
Vector.prototype.scalar = function (s) {
|
||||
var SIZE = this.size()
|
||||
var ans = new Vector(SIZE)
|
||||
for (var i = 0; i < SIZE; i++) {
|
||||
const SIZE = this.size()
|
||||
const ans = new Vector(SIZE)
|
||||
for (let 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++) {
|
||||
let ans = '('
|
||||
const SIZE = this.components.length
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
if (i < SIZE - 1) {
|
||||
ans += this.components[i] + ','
|
||||
} else {
|
||||
@@ -121,7 +121,7 @@ var LinearAlgebra;
|
||||
// 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++) {
|
||||
for (let i = 0; i < this.components.length; i++) {
|
||||
if (i === pos) {
|
||||
this.components[i] = 1.0
|
||||
} else {
|
||||
@@ -135,20 +135,20 @@ var LinearAlgebra;
|
||||
}
|
||||
// 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++) {
|
||||
const SIZE = this.size()
|
||||
const quotient = 1.0 / this.eulideanLength()
|
||||
for (let 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
|
||||
let ans = true
|
||||
const SIZE = this.size()
|
||||
const EPSILON = 0.001
|
||||
if (SIZE === other.size()) {
|
||||
for (var i = 0; i < SIZE; i++) {
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
|
||||
ans = false
|
||||
}
|
||||
@@ -164,8 +164,8 @@ var LinearAlgebra;
|
||||
// -------------- 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++) {
|
||||
const ans = new Vector(N)
|
||||
for (let i = 0; i < N; i++) {
|
||||
if (i === pos) {
|
||||
ans.changeComponent(i, 1.0)
|
||||
} else {
|
||||
@@ -177,8 +177,8 @@ var LinearAlgebra;
|
||||
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++) {
|
||||
const ans = new Vector(N)
|
||||
for (let i = 0; i < N; i++) {
|
||||
ans.changeComponent(i, (Math.floor((Math.random() * b) + a)))
|
||||
}
|
||||
return ans
|
||||
@@ -186,8 +186,8 @@ var LinearAlgebra;
|
||||
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++) {
|
||||
const ans = new Vector(N)
|
||||
for (let i = 0; i < N; i++) {
|
||||
ans.changeComponent(i, ((Math.random() * b) + a))
|
||||
}
|
||||
return ans
|
||||
@@ -198,7 +198,7 @@ var LinearAlgebra;
|
||||
class: Matrix
|
||||
This class represents a matrix of arbitrary size and operations on it.
|
||||
*/
|
||||
var Matrix = /** @class */ (function () {
|
||||
const Matrix = /** @class */ (function () {
|
||||
// constructor for zero-matrix or fix number matrix.
|
||||
function Matrix (row, col, comps) {
|
||||
if (comps === undefined) {
|
||||
@@ -206,9 +206,9 @@ var LinearAlgebra;
|
||||
}
|
||||
if (comps.length === 0) {
|
||||
this.matrix = []
|
||||
var rowVector = []
|
||||
for (var i = 0; i < row; i++) {
|
||||
for (var j = 0; j < col; j++) {
|
||||
let rowVector = []
|
||||
for (let i = 0; i < row; i++) {
|
||||
for (let j = 0; j < col; j++) {
|
||||
rowVector[j] = 0
|
||||
}
|
||||
this.matrix[i] = rowVector
|
||||
@@ -238,10 +238,10 @@ var LinearAlgebra;
|
||||
}
|
||||
// returns a string representation of this matrix.
|
||||
Matrix.prototype.toString = function () {
|
||||
var ans = ''
|
||||
for (var i = 0; i < this.rows; i++) {
|
||||
let ans = ''
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
ans += '|'
|
||||
for (var j = 0; j < this.cols; j++) {
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
if (j < this.cols - 1) {
|
||||
ans += this.matrix[i][j] + ','
|
||||
} else {
|
||||
@@ -257,7 +257,7 @@ var LinearAlgebra;
|
||||
}
|
||||
// returns the dimension rows x cols as number array
|
||||
Matrix.prototype.dimension = function () {
|
||||
var ans = []
|
||||
const ans = []
|
||||
ans[0] = this.rows
|
||||
ans[1] = this.cols
|
||||
return ans
|
||||
@@ -266,9 +266,9 @@ var LinearAlgebra;
|
||||
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++) {
|
||||
const ans = new Matrix(this.rows, this.cols)
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
ans.changeComponent(i, j, (this.matrix[i][j] + other.component(i, j)))
|
||||
}
|
||||
}
|
||||
@@ -279,12 +279,12 @@ var LinearAlgebra;
|
||||
}
|
||||
// returns true if the matrices are equal, otherwise false.
|
||||
Matrix.prototype.equal = function (other) {
|
||||
var ans = true
|
||||
var EPSILON = 0.001
|
||||
let ans = true
|
||||
const 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++) {
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
if (Math.abs(this.matrix[i][j] - other.component(i, j)) > EPSILON) {
|
||||
ans = false
|
||||
}
|
||||
@@ -297,9 +297,9 @@ var LinearAlgebra;
|
||||
}
|
||||
// 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++) {
|
||||
const ans = new Matrix(this.rows, this.cols)
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
ans.changeComponent(i, j, (this.matrix[i][j] * c))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user