LinearAlgebra
ECMAScript modules for exactly manipulating vectors and matrices
of which elements are real or complex numbers of the form
(p / q)sqrt(b),
where p is an integer, q is a positive (non-zero) integer,
and b is a positive, square-free integer.
Installation
npm install @kkitahara/linear-algebra @kkitahara/complex-algebra @kkitahara/real-algebra
Examples
Complex linear algebra
import { ExactRealAlgebra as RealAlgebra } from '@kkitahara/real-algebra'
import { ComplexAlgebra } from '@kkitahara/complex-algebra'
import { LinearAlgebra } from '@kkitahara/linear-algebra'
let r = new RealAlgebra()
let c = new ComplexAlgebra(r)
let l = new LinearAlgebra(c)
let m1, m2, m3
Generate a new matrix (since v2.0.0)
m1 = l.$(1, 0, 0, 1)
m1.toString()
m1 = l.$(r.$(1, 2, 5), c.$(0, 1), c.$(0, -1), 1)
m1.toString()
m1 = l.$(r.$(1, 2, 5), c.$(0, 1), c.$(0, -1), 1)
m1.push(c.$(3))
m1.toString()
Set and get the dimension
m1 = l.$(1, 0, 0, 1)
m1.setDim(2, 2)
m1.getDim()[0]
m1.getDim()[1]
m1.toString()
m1.setDim(1, 4)
m1.getDim()[0]
m1.getDim()[1]
m1.toString()
m1.setDim(4, 1)
m1.getDim()[0]
m1.getDim()[1]
m1.toString()
m1.setDim(3, 1)
m1.getDim()
m1.setDim(2, 0)
m1.getDim()[0]
m1.getDim()[1]
m1.setDim(0, 1)
m1.getDim()[0]
m1.getDim()[1]
m1.setDim(0, 0)
m1.isAdaptive()
m1.getDim()
m1 = l.$(1, 0, 0, 1)
m1.isAdaptive()
:warning: matrix elements are stored in row-major order.
Copy (generate a new object)
m1 = l.$(1, 0, 0, 1).setDim(2, 2)
m2 = l.copy(m1)
m2.toString()
m2.getDim()[0]
m2.getDim()[1]
Equality
m1 = l.$(1, 0, 0, 1).setDim(2, 2)
m2 = l.$(1, 0, 0, 1).setDim(2, 2)
m3 = l.$(1, 0, 0, -1).setDim(2, 2)
l.eq(m1, m2)
l.eq(m1, m3)
m1 = l.$(1, 0, 0, 1).setDim(2, 2)
m2 = l.$(1, 0, 0, 1).setDim(1, 4)
l.eq(m1, m2)
m1 = l.$(1, 0, 0, 1)
m2 = l.$(1, 0, 0, 1).setDim(1, 4)
m3 = l.$(1, 0, 0, 1).setDim(4, 1)
l.eq(m1, m2)
l.eq(m1, m3)
l.eq(m2, m3)
Inequality
m1 = l.$(1, 0, 0, 1).setDim(2, 2)
m2 = l.$(1, 0, 0, 1).setDim(2, 2)
m3 = l.$(1, 0, 0, -1).setDim(2, 2)
l.ne(m1, m2)
l.ne(m1, m3)
m1 = l.$(1, 0, 0, 1).setDim(2, 2)
m2 = l.$(1, 0, 0, 1).setDim(1, 4)
l.ne(m1, m2)
m1 = l.$(1, 0, 0, 1)
m2 = l.$(1, 0, 0, 1).setDim(1, 4)
m3 = l.$(1, 0, 0, 1).setDim(4, 1)
l.ne(m1, m2)
l.ne(m1, m3)
l.ne(m2, m3)
isZero
m1 = l.$(1, 0, 0, 1).setDim(2, 2)
m2 = l.$(0, 0, 0, 0).setDim(2, 2)
l.isZero(m1)
l.isZero(m2)
isInteger (since v1.1.0)
m1 = l.$(1, r.$(4, 2), -3, 4).setDim(2, 2)
m2 = l.$(1, r.$(1, 2), -3, 4).setDim(2, 2)
l.isInteger(m1)
l.isInteger(m2)
Element-wise addition
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m3 = l.add(m1, m2)
m3.toString()
In-place element-wise addition
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m1 = l.iadd(m1, m2)
m1.toString()
Element-wise subtraction
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m3 = l.sub(m1, m2)
m3.toString()
In-place element-wise subtraction
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m1 = l.isub(m1, m2)
m1.toString()
Element-wise multiplication
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m3 = l.mul(m1, m2)
m3.toString()
In-place element-wise multiplication
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m1 = l.imul(m1, m2)
m1.toString()
Element-wise division
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m3 = l.div(m1, m2)
m3.toString()
In-place element-wise division
m1 = l.$(1, 2, 3, 4)
m2 = l.$(1, 3, 1, 3)
m1 = l.idiv(m1, m2)
m1.toString()
Scalar multiplication
m1 = l.$(1, 2, 3, 4)
m2 = l.smul(m1, r.$(1, 2))
m2.toString()
In-place scalar multiplication
m1 = l.$(1, 2, 3, 4)
m1 = l.ismul(m1, r.$(1, 2))
m1.toString()
Scalar multiplication by -1
m1 = l.$(1, 2, 3, 4)
m2 = l.neg(m1)
m2.toString()
In-place scalar multiplication by -1
m1 = l.$(1, 2, 3, 4)
m1 = l.ineg(m1)
m1.toString()
Scalar division (since v2.0.0)
m1 = l.$(1, 2, 3, 4)
m2 = l.sdiv(m1, 2)
m2.toString()
In-place scalar division (since v2.0.0)
m1 = l.$(1, 2, 3, 4)
m1 = l.isdiv(m1, 2)
m1.toString()
Complex conjugate
m1 = l.$(1, c.$(0, 2), 3, c.$(0, 4))
m2 = l.cjg(m1)
m2.toString()
In-place evaluation of the complex conjugate
m1 = l.$(1, c.$(0, 2), 3, c.$(0, 4))
m1 = l.icjg(m1)
m1.toString()
Transpose
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.transpose(m1)
m2.toString()
In-place evaluation of the transpose
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m1 = l.itranspose(m1)
m1.toString()
Conjugate transpose (Hermitian transpose)
m1 = l.$(1, c.$(0, 2), 3, c.$(0, 4)).setDim(2, 2)
m2 = l.cjgTranspose(m1)
m2.toString()
In-place evaluation of the conjugate transpose
m1 = l.$(1, c.$(0, 2), 3, c.$(0, 4)).setDim(2, 2)
m1 = l.icjgTranspose(m1)
m1.toString()
Dot product
m1 = l.$(1, c.$(0, 2))
m2 = l.$(c.$(0, 3), 2)
l.dot(m1, m2).toString()
Square of the absolute value (Frobenius norm)
m1 = l.$(1, c.$(0, 2))
let a = l.abs2(m1)
a.toString()
a.re
a.im
Matrix multiplication
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.$(1, 3, 1, 3).setDim(2, 2)
m3 = l.mmul(m1, m2)
m3.toString()
LU-factorisation
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.lup(m1)
In-place LU-factorisation
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m1 = l.ilup(m1)
Solving a linear equation
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.$(1, 2, 3, 4).setDim(2, 2)
m3 = l.solve(l.lup(m1), m2)
m3.toString()
m3 = l.solve(m2, l.lup(m1))
m3.toString()
Solving a linear equation in-place
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.isolve(l.lup(m1), m2)
m2.toString()
m2 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.isolve(m2, l.lup(m1))
m2.toString()
Determinant
m1 = l.$(1, 2, 3, 4).setDim(2, 2)
m2 = l.lup(m1)
let det = l.det(m2)
det.toString()
JSON (stringify and parse)
m1 = l.$(1, r.$(2, 3, 5), 3, c.$(0, r.$(4, 5, 3))).setDim(2, 2)
let str = JSON.stringify(m1)
m2 = JSON.parse(str, l.reviver)
l.eq(m1, m2)
Real linear algebra
If complex numbers are not necessary, you can use RealAlgebra
instead of ComplexAlgebra.
import { ExactRealAlgebra as RealAlgebra } from '@kkitahara/real-algebra'
import { LinearAlgebra } from '@kkitahara/linear-algebra'
let r = new RealAlgebra()
let l = new LinearAlgebra(r)
Numerical real/complex linear algebra
You can work with built-in numbers if you use
import { RealAlgebra } from '@kkitahara/real-algebra'
instead of ExactRealAlgebra.
See the documents of @kkitahara/real-algebra for more details.
ESDoc documents
For more examples, see ESDoc documents:
cd node_modules/@kkitahara/linear-algebra
npm install --only=dev
npm run doc
and open doc/index.html
in your browser.
LICENSE
© 2019 Koichi Kitahara
Apache 2.0