# fibo/algebra

1 parent b5be06a commit 7e1fac2fa59519ea9e0de3a50bb2d88140370351 committed
 @@ -53,12 +53,14 @@ This is a 60 seconds tutorial to get your hands dirty with `algebra`. var algebra = require('algebra'); - var MnR = algebra.Real.MatrixRing; - var M2R = new MnR(2); + // GLnR is the Real General Linear Group of invertible matrices of order n. + var GLnR = algebra.Real.GeneralLinearGroup; - var m1 = new M2R.Matrix(1, 2, 3, 4); + // Create a 2x2 invertible matrix. + var GL2R = new GLnR(2); + + var m1 = new GL2R.Matrix(1, 2, 3, 4); - // coming soon :P # Description
1 examples/RealGeneralLinearGroup.js
 @@ -4,7 +4,6 @@ var albegra = require('algebra'); var GLRn = algebra.Real.GeneralLinearGroup; -//var GLRn = algebra.Real.GeneralLinearGroup; var GLR2 = new GLRn(2);
7 examples/RealMatrix.js
 @@ -1,7 +0,0 @@ - -var algebra = require('algebra'); - -var MnR = algebra.Real.MatrixRing; - -var M2R = new MnR(2); -
32 examples/matrix.js
 @@ -1,32 +0,0 @@ - -var algebra = require('../index.js'); - -var GLn = algebra.Real.GeneralLinearGroup; -var Matrix = algebra.Matrix; -var RealField = algebra.Real.Field; - -var GL2 = new GLn(2); -var GL3 = new GLn(3); - -var matrix2 = new GL2.Matrix(0,1,-1,0); -console.log(matrix2.toString()); - -matrix2.tr(); -console.log(matrix2.toString()); - -var matrix3 = new GL3.Matrix(0,1,1,1,0,-1,0,-1,0); -console.log(matrix3.toString()); - -/* -var R = new RealField(); -var matrix2x3 = new Matrix({ - elements: [0, 1, 2, 3, 4, 5], - field: R, - numRows: 2, - numCols: 3 -}); -console.log(matrix2x3.toString()); -matrix2x3.tr(); -console.log(matrix2x3.toString()); -*/ -
15 index.js
 @@ -1,8 +1,5 @@ exports.Field = require('./lib/Field.js'); -exports.Matrix = require('./lib/Matrix.js'); -exports.MatrixRing = require('./lib/MatrixRing.js'); -exports.SquareMatrix = require('./lib/SquareMatrix.js'); exports.Group = require('./lib/Group.js'); exports.Vector = require('./lib/Vector.js'); exports.VectorSpace = require('./lib/VectorSpace.js'); @@ -10,11 +7,12 @@ exports.VectorSpace = require('./lib/VectorSpace.js'); var Real = {}; -Real.Element = require('./lib/Real/Element.js'); -Real.Field = require('./lib/Real/Field.js'); -Real.Vector = require('./lib/Real/Vector.js'); -Real.VectorSpace = require('./lib/Real/VectorSpace.js'); -Real.MatrixRing = require('./lib/Real/MatrixRing.js'); +Real.Element = require('./lib/Real/Element.js'); +Real.Field = require('./lib/Real/Field.js'); +Real.GeneralLinearGroup = require('./lib/Real/GeneralLinearGroup.js'); +Real.InvertibleMatrix = require('./lib/Real/InvertibleMatrix.js'); +Real.Vector = require('./lib/Real/Vector.js'); +Real.VectorSpace = require('./lib/Real/VectorSpace.js'); exports.Real = Real; @@ -24,7 +22,6 @@ Complex.Element = require('./lib/Complex/Element.js'); Complex.Field = require('./lib/Complex/Field.js'); //Complex.Vector = require('./lib/Complex/Vector.js'); //Complex.VectorSpace = require('./lib/Complex/VectorSpace.js'); -//Complex.MatrixRing = require('./lib/Complex/MatrixRing.js'); exports.Complex = Complex;
7 lib/Complex/Element.js
 @@ -1,7 +1,7 @@ -var MnR = require('../Real/MatrixRing.js'); +var GLnR = require('../Real/GeneralLinearGroup.js'); -var M2R = new MnR(2); +var GL2R = new GLnR(2); var ComplexElement = function (a, b) { var self = this; @@ -145,7 +145,8 @@ ComplexElement.prototype.toMatrix = function () { var im = this.im(); var re = this.re(); - return new M2R.Matrix(re, -im, im, re); + // TODO if is zero return new M2R.Matrix(re, -im, im, re); + return new GL2R.Matrix(re, -im, im, re); }; ComplexElement.prototype.toString = function () {
91 lib/Matrix.js
 @@ -1,91 +0,0 @@ -function Matrix(arg) { - var self = this; - - var field = arg.field; - this.getField = function () { return field; }; - - var numRows = arg.numRows; - this.getNumRows = function () { return numRows; }; - - var numCols = arg.numCols; - this.getNumCols = function () { return numCols; }; - //console.log(numCols); - - var elements = arg.elements; - this.elems = this.getElements = function () { return elements; }; - - var index = function(i ,j) { - return i * numCols + j; - }; - - this.ij = function (i, j) { - return elements[index(i, j)]; - }; - - this.row = function (rowIndex) { - var row = []; - for (var j = 0; j < numCols; j++) { - var element = self.ij(rowIndex, j); - row.push(element); - } - return row; - }; - - this.col = function (colIndex) { - var col = []; - for (var i = 0; i < numRows; i++) { - var element = self.ij(i, colIndex); - col.push(element); - } - return col; - }; - - this.toString = function () { - var str = ''; - for (var i = 0; i < numRows; i++ ) { - str += '|'; - for (var j = 0; j < numCols; j++ ) { - str += ' ' + self.ij(i, j) + ' '; - } - str += '|\n'; - } - return str; - }; - - this.transpose = this.tr = function () { - var swap; - - for(var i = 0; i < numRows; i++) { - for(var j = 0; j < numCols; j++) { - if ( i != j ) { - var origIndex = index(i, j); - var newIndex = index(j, i); - - swap = elements[origIndex]; - elements[origIndex] = elements[newIndex]; - elements[newIndex] = swap; - } - } - } - - swap = numRows; - numCols = numRows; - numRows = swap; - - return self; - }; -}; - -Matrix.prototype = { - clone: function () { - return new Matrix({ - field: this.getField(), - numCols: this.getNumCols(), - numRows: this.getNumRows(), - elements: this.getElements() - }); - } -}; - -module.exports = Matrix; -
57 lib/MatrixRing.js
 @@ -1,57 +0,0 @@ - -var SquareMatrix = require('./SquareMatrix.js'); - -function MatrixRing(arg) { - var self = this; - - var _field = arg.field; - this.getField = function () { return _field; }; - - var _order = arg.order; - this.getOrder = function () { return _order; }; - - var zeroElement = _field.getZero(); - var oneElement = _field.getOne(); - - self.getOne = function () { - var arg = {}; - arg.field = _field; - arg.order = _order; - - var elements = []; - for (var i = 0; i < _order; i++) { - for (var j = 0; j < _order; j++) { - if (i == j) { - elements.push(oneElement.clone()); - } - else { - elements.push(zeroElement.clone()); - } - } - } - arg.elements = elements; - - var one = new SquareMatrix(arg); - - return one; - }; - - self.getZero = function () { - var arg = {}; - arg.field = _field; - arg.order = _order; - - var elements = []; - for (var i = 0; i < _order * _order; i++) { - elements.push(zeroElement.clone()); - } - arg.elements = elements; - - var zero = new SquareMatrix(arg); - - return zero; - }; -} - -module.exports = MatrixRing; -
21 lib/Real/GeneralLinearGroup.js
 @@ -0,0 +1,21 @@ + +var RealInvertibleMatrix = require('./InvertibleMatrix.js'); + +function GeneralLinearGroup(order) { + var self = this; + + var _order = order; + + self.Matrix = function () { + var arg = {}; + + arg.order = _order; + + RealInvertibleMatrix.call(this, arg); + }; + + // TODO self.Id = self.Matrix; +}; + +module.exports = GeneralLinearGroup; +
22 lib/Real/InvertibleMatrix.js
 @@ -0,0 +1,22 @@ + +function RealInvertibleMatrix(arg) { + var self = this; + + var _order = arg.order; + +/* TODO + // Matrix defaults to big Id. + for (var i = 0; i < o; i++) { + for (var j = 0; j < o; j++) { + var _default = 0; + if (i == j) _default = 1; + var num = arguments[i*o+j] || _default; + var element = new Real(num); + arg.elements.push(element); + } + } + */ +}; + +module.exports = RealInvertibleMatrix; +
46 lib/Real/MatrixRing.js
 @@ -1,46 +0,0 @@ - -var util = require('util'); - -var MatrixRing = require('../MatrixRing.js'); -var RealField = require('./Field.js'); -var Real = require('./Element.js'); -var SquareMatrix = require('../Matrix.js'); - -var R = new RealField(); - -var RealMatrixRing = function (order) { - var self = this; - - var arg = {}; - arg.order = order; - arg.field = R; - - MatrixRing.call(self, arg); - - self.Matrix = function () { - var arg = {}; - arg.elements = []; - arg.field = self.getField(); - var o = arg.order = self.getOrder(); - - // Matrix defaults to big Id. - for (var i = 0; i < o; i++) { - for (var j = 0; j < o; j++) { - var _default = 0; - if (i == j) _default = 1; - var num = arguments[i*o+j] || _default; - var element = new Real(num); - arg.elements.push(element); - } - } - - SquareMatrix.call(this, arg); - }; - - self.Id = self.Matrix; -}; - -util.inherits(RealMatrixRing, MatrixRing); - -module.exports = RealMatrixRing; -
31 lib/SquareMatrix.js
 @@ -1,31 +0,0 @@ - -var util = require('util'); - -var Matrix = require('./Matrix.js'); - -function SquareMatrix(arg) { - var order = arg.order; - this.getOrder = function () { return order; }; - - //console.log('xxx'+order); - arg.numRows = order; - arg.numCols = order; - - //trace: function() { // TODO somma degli elementi della diagonale. }, - //adjoint: function() { // TODO matrice aggiunta. }; - - Matrix.call(this, arg); -}; - -// TODO fai il metodo clone -// da vedere se qua posso chiamare getField(), getNumRows(), ecc che stanno nella classe padre. -//SquareMatrix.prototype = { -// clone: function() { -// return new SquareMatrix(); -// } -//} - -util.inherits(SquareMatrix, Matrix); - -module.exports = SquareMatrix; -
1 test/Complex/Element.js
 @@ -310,6 +310,7 @@ describe('ComplexElement', function () { describe('toMatrix()', function () { it('...', function () { + // TODO caso C = 0 ritorna matrice nulla, altrimenti e' una RealInvertibleMatrix }); }); });
133 test/Matrix.js
 @@ -1,133 +0,0 @@ -var assert = require('assert'); -var algebra = require('../index.js'); - -var Matrix = algebra.Matrix; -var RealField = algebra.Real.Field; -var ComplexField = algebra.Complex.Field; - -var C = new ComplexField(); -var R = new RealField(); - -var elements1 = [1, 0, 2, -5, -1, 6]; -var m1 = new Matrix({ - numRows: 2, - numCols: 3, - field: R, - elements: elements1 -}); - -describe('Matrix', function () { - describe('constructor', function () { - it('requires: numRows, numCols, order', function () { - var m = new Matrix({ - numRows: 2, - numCols: 3, - field: R - }); - assert.ok(m instanceof Matrix); - }); - - it('elements arg defaults to field.getZero()', function () { - /* - var m = new Matrix({ - numCols: 2, - numRows: 2, - field: C, - elements: [z1, z2] - }); - */ - }); - }); - - describe('ij(number, number)', function () { - it('returns the i,j-elem', function () { - assert.equal(m1.ij(0, 0), elements1[0]); - assert.equal(m1.ij(0, 1), elements1[1]); - assert.equal(m1.ij(0, 2), elements1[2]); - assert.equal(m1.ij(1, 0), elements1[3]); - assert.equal(m1.ij(1, 1), elements1[4]); - assert.equal(m1.ij(1, 2), elements1[5]); - }); - }); - - describe('row()', function () { - it('returns the i-th row', function () { - assert.deepEqual(m1.row(0), [1, 0, 2]); - assert.deepEqual(m1.row(1), [-5, -1, 6]); - }); - }); - - describe('col()', function () { - it('returns the j-th column', function () { - assert.deepEqual(m1.col(0), [1, -5]); - assert.deepEqual(m1.col(1), [0, -1]); - assert.deepEqual(m1.col(2), [2, 6]); - }); - }); - - describe('getElements()', function () { - it('returns the elements', function() { - assert.deepEqual(m1.getElements(), elements1); - }); - - it('has an elems() alias', function () { - assert.ok(m1.getElements === m1.elems); - }); - }); - - describe('transpose()', function () { - var elements1 = [1, 2, 3, 4, -1, -2, -3, -4]; - var m1 = new Matrix({ - numRows: 2, - numCols: 4, - field: R, - elements: elements1 - }); - - var m2 = m1.clone(); - m2.transpose(); - - // TODO si dice involutive in inglese? - // voglio dire che e' un involuzione cioe che se la applico due - // volte torno all' elemento di partenza. - it('is involutive', function () { - assert.deepEqual(m1.tr().tr().elems(), elements1); - }); - - it('swaps the i,j-th element with the i,j-th one', function () { - //assert.equal(m1.ij(0, 1), m2.ij(1, 0)); - //assert.equal(m1.ij(0, 2), m2.ij(3, 0)); - //assert.equal(m1.ij(0, 3), m2.ij(4, 0)); - //assert.equal(m1.ij(1, 0), m2.ij(0, 1)); - // ... - }); - - it('swaps the number of rows with the number of columns', function () { - assert.equal(m1.getNumCols(), m2.getNumRows()); - assert.equal(m1.getNumRows(), m2.getNumCols()); - }); - - it('has a tr() alias', function () { - assert.ok(m1.transpose === m1.tr); - }); - }); - - describe('getNumCols()', function () { - it('returns the number of cols', function() { - assert.equal(m1.getNumCols(), 3); - }); - }); - - describe('getNumRows()', function () { - it('returns the number of rows', function() { - assert.equal(m1.getNumRows(), 2); - }); - }); - - describe('r4c()', function () { - it('implements rows for columns right multiplication by a matrix or a vector', function() { - }); - }); -}); - -
33 test/MatrixRing.js
 @@ -1,33 +0,0 @@ - -var assert = require('assert'); -var algebra = require('../index.js'); - -var MatrixRing = algebra.MatrixRing; - -describe('MatrixRing', function () { - describe('constructor:', function () { - it('', function () { - }); - }); - - describe('getField()', function () { - it('', function () { - }); - }); - - describe('getOrder()', function () { - it('', function () { - }); - }); - - describe('getOne()', function () { - it('returns the identity matrix', function () { - }); - }); - - describe('getZero()', function () { - it('returns the zero matrix', function () { - }); - }); -}); -
14 test/Real/GeneralLinearGroup.js
 @@ -0,0 +1,14 @@ + +var assert = require('assert'); +var algebra = require('../../index.js'); + +var GLnR = algebra.Real.GeneralLinearGroup; + +describe('RealGeneralLinearGroup', function () { + describe('constructor:', function () { + it('', function () { + }); + + }); +}); +
15 test/Real/InvertibleMatrix.js
 @@ -0,0 +1,15 @@ + +var assert = require('assert'); +var algebra = require('../../index.js'); + +var Matrix = algebra.Real.InvertibleMatrix; + +describe('RealInvertibleMatrix', function () { + describe('constructor:', function () { + it('', function () { + }); + + }); +}); + +
44 test/Real/MatrixRing.js
 @@ -1,44 +0,0 @@ - -var assert = require('assert'); -var algebra = require('../../index.js'); - -var MatrixRing = algebra.MatrixRing; -var MnR = algebra.Real.MatrixRing; -var RealField = algebra.Real.Field; -var SquareMatrix = algebra.SquareMatrix; - -var M2R = new MnR(2); - -describe('RealMatrixRing', function () { - describe('constructor:', function () { - it('', function () { - }); - }); - - describe('inheritance:', function () { - it('is a MatrixRing', function () { - assert.ok(M2R instanceof MatrixRing); - }); - }); - - describe('getField()', function () { - it('returns the Real field', function () { - assert.ok(M2R.getField() instanceof RealField); - }); - }); - - describe('getOrder()', function () { - it('returns the order', function () { - assert.equal(M2R.getOrder(), 2); - }); - }); - - describe('Matrix()', function () { - it('returns a SquareMatrix constructor', function () { - var matrix = new M2R.Matrix(1,2,3,4); - assert.ok(typeof matrix == 'object'); - //console.log(matrix.ij(0,0)); - }); - }); -}); -
27 test/SquareMatrix.js
 @@ -1,27 +0,0 @@ - -var assert = require('assert'); -var algebra = require('../index.js'); - -var Matrix = algebra.Matrix; -var RealField = algebra.Real.Field; -var ComplexField = algebra.Complex.Field; -var SquareMatrix = algebra.SquareMatrix; - -describe('SquareMatrix', function () { - describe('constructor', function () { - it('requires: field, order', function () { - var R = new RealField(); - var m = new SquareMatrix({order: 4, field: R}); - assert.ok(m instanceof SquareMatrix); - }); - }); - - describe('inherits', function () { - it('from Matrix', function () { - var C = new ComplexField(); - var m = new SquareMatrix({order: 2, field: C}); - assert.ok(m instanceof Matrix); - }); - }); -}); -
9 test/Vector.js
 @@ -3,21 +3,12 @@ var assert = require('assert'); var algebra = require('../index.js'); var ComplexField = algebra.Complex.Field; -var Matrix = algebra.Matrix; var RealField = algebra.Real.Field; var VectorSpace = algebra.VectorSpace; var C = new ComplexField(); var R = new RealField(); -var elements1 = [1, 0, 2, -5, -1, 6]; -var m1 = new Matrix({ - numRows: 2, - numCols: 3, - field: R, - elements: elements1 -}); - describe('Vector', function () { describe('constructor:', function () { it('requires: space', function() {
7 test/quickStart.js
 @@ -28,11 +28,10 @@ describe('Quick start:', function () { }); it('has a working Matrices example', function () { - var MnR = algebra.Real.MatrixRing; - var M2R = new MnR(2); + var GLnR = algebra.Real.GeneralLinearGroup; + var GL2R = new GLnR(2); - var m1 = new M2R.Matrix(1, 2, 3, 4); - var m2 = new M2R.Id(-1, 0, 0, 1); + var m1 = new GL2R.Matrix(1, 2, 3, 4); //m1.mul(m2);