algebra

Vectors, Matrices, Real, Complex, Quaternion

algebra

Vectors, Matrices; Real, Complex, Quaternion; custom groups and rings for Node.js

algebra is under development, but API should not change until version 1.0.

I am currently adding more tests and examples to achieve a stable version.

Many functionalities of previous versions are now in separated atomic packages:

  • Real, Complex, Quaternion, Octonion numbers.
  • Vector and Matrix spaces over any field (included Real numbers, of course :).
  • Expressive syntax.
  • Immutable objects.
  • Written in ES6; a transpiled to ES5 version is available for client side usage.
  • math blog with articles explaining algebra concepts and practical examples. I started blogging about math hoping it can help other people learning about the Queen of Science.

With npm do

$ npm install algebra

With bower do

$ bower install algebra

This is a 60 seconds tutorial to get your hands dirty with algebra.

All code in the examples below is intended to be contained into a single file.

First of all, import algebra package.

var algebra = require('algebra');

Use the Real numbers as scalars.

var R = algebra.Real;

Every operator is implemented both as a static function and as an object method.

Static operators return raw data, while class methods return object instances.

Use static addition operator to add three numbers.

console.log(R.add(1, 2, 3)); // 1 + 2 + 3 = 6

Create two real number objects: x = 2, y = -2

var x = new R(2),
    y = new R(-2);

The value r is the result of x multiplied by y.

var r = x.mul(y);
console.log(r.data); // 2 * (-2) = -4
console.log(x.data); // still 2
console.log(y.data); // still -2

Raw numbers are coerced, operators can be chained when it makes sense. Of course you can reassign x, for example, x value will be 0.1: x -> x + 3 -> x * 2 -> x ^-1

x = x.add(3).mul(2).inv();
console.log(x.data); // ((2 + 3) * 2)^(-1) = 0.1

Comparison operators equal and notEqual are available, but they cannot be chained.

x.equal(0.1) // true
x.notEqual(Math.PI) // true

Create vector space of dimension 2 over Reals.

var R2 = algebra.VectorSpace(R)(2);

Create two vectors and add them.

var v1 = new R2([0, 1]);
var v2 = new R2([1, -2]);
 
// v1 -> v1 + v2 -> [0, 1] + [1, -2] = [1, -1]
v1 = v1.add(v2);
 
console.log(v1.data); // [1, -1]

Create space of matrices 3 x 2 over Reals.

var R3x2 = algebra.MatrixSpace(R)(3, 2);

Create a matrix.

//       | 1 1 |
//  m1 = | 0 1 |
//       | 1 0 |
//
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

//  3 x 2  by  2 x 1  which gives a   3 x 1
//      ↑      ↑
//      +------+----→  by removing the middle indices.
//
//                   | 1 1 |
//    v3 = m1 * v1 = | 0 1 | * [1 , -1] = [0, -1, 1]
//                   | 1 0 |
 
var v3 = m1.mul(v1);
 
console.log(v3.data); // [0, -1, 1]

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]),
    m3 = new R2x2([0, -1,
                   1, 0]);
 
m2 = m2.mul(m3);
 
console.log(m2.data); // [0, -1,
                      //  2,  0]

Since m2 is a square matrix we can calculate its determinant.

console.log(m2.determinant.data); // 2

MIT