algebra quick start

node v4.9.1
version: 1.0.0
endpointsharetweet
First of all, import algebra package. Use the Real numbers as scalars. Use static addition operator to add three numbers.
var algebra = require('algebra') var R = algebra.Real R.add(1, 2, 3)
Create two real number objects: x = 2, y = -2
var x = new R(2)
var y = new R(-2)
The value r is the result of x multiplied by y
var r = x.mul(y)
x and y are not changed
x.data
y.data
Raw numbers are coerced, operators can be chained, when it makes sense. Of course you can reassign x
x = x.add(3).mul(2).inv()
Comparison operators equal and notEqual are available, but they cannot be chained.
x.equal(0.1)
x.notEqual(Math.PI)
You can also play with Complexes.
var C = algebra.Complex var z1 = new C([1, 2]) var z2 = new C([3, 4]) z1 = z1.mul(z2)
z1 = z1.conj().mul([2, 0])
Vectors Create vector space of dimension 2 over Reals. Create two vectors and add them.
var R2 = algebra.VectorSpace(R)(2) var v1 = new R2([0, 1]) var v2 = new R2([1, -2]) v1 = v1.add(v2)
Matrices Create space of matrices 3 x 2 over Reals. Create a matrix.
var R3x2 = algebra.MatrixSpace(R)(3, 2) var m1 = new R3x2([1, 1, 0, 1, 1, 0])
Multiply m1 by v1, the result is a vector v3 with dimension 3. In fact we are multiplying a 3 x 2 matrix by a 2 dimensional vector, but v1 is traited as a column vector so it is like a 2 x 1 matrix. Then, following the row by column multiplication law we have
var v3 = m1.mul(v1)
Let's try with two square matrices 2 x 2.
var R2x2 = algebra.MatrixSpace(R)(2, 2) var m2 = new R2x2([1, 0, 0, 2]) var m3 = new R2x2([0, -1, 1, 0]) m2 = m2.mul(m3)
Since m2 is a square matrix we can calculate its determinant.
m2.determinant
Loading…

no comments

    sign in to comment