linalg.js
Version:
Linear Algebra Module
168 lines (149 loc) • 3.78 kB
JavaScript
function delta(i, j) {
return i == j ? 1 : 0;
}
function Complex(a, b) {
this.real = a = a || 0;
this.imag = b = b || 0;
this.toString = function() {
if (a == 0)
return b + 'i';
else if (b == 0)
return a.toString();
else
return '(' + a + '+' + b + 'i)';
}
}
function complexMult(a, b) {
return new Complex(a.real * b.real + a.imag * b.imag, a.real * b.imag + a.imag * b.real);
}
function complexExp(z, exp) {
//Exponentiation Rapide !!!
var z2 = z;
for (var i = 0; i < exp; i++) {
z2 = complexMult(z2, z);
}
return z2;
}
module.exports.Complex = Complex;
module.exports.complexMult = complexMult;
module.exports.complexExp = complexExp;
//Vector
function Vector(...values) {
return new Matrix(1, values.length, function(i, j) {
return values[i];
});
}
module.exports.Vector = Vector;
//Matrix
function Matrix(w, h, value = 0) {
this.width = w;
this.height = h;
var mat = new Array(h);
for (var i = 0; i < h; i++) {
mat[i] = new Array(w);
for (var j = 0; j < w; j++) {
mat[i][j] = typeof value === 'function' ? value(i, j) : value;
}
}
this.mat = mat;
this.get = function(i, j) {
return mat[i][j];
};
this.set = function(i, j, k) {
mat[i, j] = k;
}
this.transpose = function() {
return new Matrix(h, w, function(i, j) {
return mat[j][i];
});
}
this.toString = function() {
var str = '[';
for (var i = 0; i < h; i++) {
str += (i === 0 ? '' : ' ') + '[';
for (var j = 0; j < w; j++) {
var number = this.mat[i][j].toString();
str += number + ' '.repeat(8 - number.length);
}
str += ']' + (i === h - 1 ? '' : '\n');
}
str += ']\n';
return str;
}
}
function SquareMatrix(w, value = 0) {
return new Matrix(w, w, value);
}
function UpperTriMatrix(w, value = 0) {
return new Matrix(w, w, function(i, j) {
if (i < j)
return 0;
else return typeof value === 'function' ? value(i, j) : value;
})
}
function LowerTriMatrix(w, value = 0) {
return new Matrix(w, w, function(i, j) {
if (i > j)
return 0;
else return typeof value === 'function' ? value(i, j) : value;
})
}
function NullMatrix(w) {
return new Matrix(w, w, 0);
}
function IdentityMatrix(w) {
return new Matrix(w, w, function(i, j) {
return delta(i, j);
});
}
function matrixMult(matA, matB) {
var A = matA.mat;
var B = matB.mat;
console.log(matA.width + ' / ' + matB.height);
if (matA.width != matB.height) {
console.log('Error: Matrices Are Incompatible (%dx%d & %dx%d)', matA.height, matA.width, matB.height, matB.width);
return;
}
return new Matrix(matB.width, matA.height, function(i, k) {
var c = 0;
for (var j = 0; j < matA.width; j++) {
c += A[i][j] * B[j][k];
}
return c;
});
}
function matrixExp(mat, exp) {
//Exponentiation Rapide ...
if (mat.width != mat.height) return null;
if (exp < 0)
return null;
else if (exp == 0)
return new IdentityMatrix(mat.width);
else {
var newMat = mat;
for (var i = 1; i < exp; i++) {
newMat = matrixMult(newMat, mat);
}
return newMat;
}
}
module.exports.Matrix = Matrix;
module.exports.UpperTriMatrix = UpperTriMatrix;
module.exports.LowerTriMatrix = LowerTriMatrix;
module.exports.IdentityMatrix = IdentityMatrix;
module.exports.NullMatrix = NullMatrix;
module.exports.matrixMult = matrixMult;
module.exports.matrixExp = matrixExp;
function Polynom(...coeffs) {
this.polynom = coeffs;
this.toString = function() {
str = '';
for (var i = coeffs.length - 1; i >= 0; i--) {
str += coeffs[i].toString() + (i == 0 ? '' : 'X^' + i + ' + ');
}
return str;
}
this.evaluate = function(value) {
}
}
module.exports.Polynom = Polynom;