Permalink
Browse files

matrices are pretty good now, ready for version 0.1.3 ,)

  • Loading branch information...
1 parent 4a22e97 commit c419ceecb74d8174cd4443ebc2d1edf9c2692b39 @fibo committed Jan 20, 2013
Showing with 69 additions and 41 deletions.
  1. +36 −12 README.md
  2. +7 −18 lib/Real/SquareMatrix.js
  3. +2 −2 package.json
  4. +20 −8 test/Real/SquareMatrix.js
  5. +4 −1 test/quickStart.js
View
@@ -1,12 +1,25 @@
algebra
=======
-Vectors, Matrices and more
+Vectors, Matrices, Tensors.
+
+DISCLAIMER: this is a developer release, Tensors are not implemented yet. Every feedback is welcome!
# Installation
npm install algebra
+# Description
+
+I'm implementing matrices and vectors on few algebra fields
+(Reals, Complexes etc.) following Micheal Artin's "Algebra",
+which was my book at [Universita' Degli Studi di Genova] (http://www.dima.unige.it).
+
+My goal is to provide users with the feature of creating their own algebra field and building vector spaces and matrices on it.
+
+Suppose for example the set of strings with the concatenation operator,
+it could be extended to a group and maybe to a field and build "matrices of strings" or probably in the future ... "strings of matrices" (I had an intuition about a composition law for UTF-8 chars).
+
# Quick start
This is a 60 seconds tutorial to get your hands dirty with `algebra`.
@@ -56,33 +69,44 @@ This is a 60 seconds tutorial to get your hands dirty with `algebra`.
// GLnR is the Real General Linear Group of invertible matrices of order n.
var GLnR = algebra.Real.GeneralLinearGroup;
- // Create a 2x2 invertible matrix.
+ // Create the group of 2x2 invertible matrices.
var GL2R = new GLnR(2);
+ // Create two invertible matrices:
+ //
+ // | 1 2 | | -1 0 |
+ // | 3 4 | and | 0 1 |
+ //
var m1 = new GL2R.Matrix(1, 2,
3, 4);
+
var m2 = new GL2R.Matrix(-1, 0,
0, 1);
+ // Multiply m1 by m2 at right side.
m1.mul(m2);
- console.log(m1.toString());
+ console.log(m1.toString()); // | -1 2 |
+ // | -3 4 |
-# Description
+ // Check out m1 determinant, should be 6 = (1 * 4 - 3 * 2) * (-1 * 1 - 0 * 0)
+ console.log(m1.determinant().num()); // 6
-I'm implementing matrices and vectors on few algebra fields
-(Reals, Complexes etc.) following Micheal Artin's "Algebra",
-which was my book at [Universita' Degli Studi di Genova] (http://www.dima.unige.it).
-
-My goal is to provide users with the feature of creating their own algebra field and building vector spaces and matrices on it.
-
-Suppose for example the set of strings with the concatenation operator,
-it could be extended to a group and maybe to a field and build "matrices of strings" or probably in the future ... "strings of matrices" (I'd an intuition about a composition law for UTF-8 chars).
# Documentation
See the [Wiki] (https://github.com/fibo/algebra/wiki).
+# Support
+
+## Issues
+
+Please use [algebra's bug tracker](https://github.com/fibo/algebra/issues) on github: patches welcome.
+
+## Human contact
+
+My skype and gtalk is casati.gianluca, contact me if you can give me some advice about coding this lib or if you have passion for math. I'm located in Milan and Genoa, it could be possible to have a pizza or a beer :)
+
# Development
## Coding style
@@ -75,32 +75,26 @@ function RealSquareMatrix(arg) {
};
- self.rightMultiplication = function (matrix) {
+ self.mul = self.rmul = self.rightMultiplication = function rightMultiplication(matrix) {
var elements = [];
- var xelements=matrix.getElements();
- //for (var x in xelements) console.log(xelements[x].num());
for (var i = 0; i < _numRows; i++) {
for (var j = 0; j < _numCols; j++) {
var element = R.getZero();
for (var k = 0; k < _order; k++) {
// TODO funzione generica di somma sugli indici, tipo notazione di Einstein.
- var rightElement = self.ij(i, k);
- var leftElement = matrix.ij(k, j);
- rightElement.mul(leftElement);
- element.add(rightElement);
+ var rightElement = self.ij(i, k);
+ var leftElement = matrix.ij(k, j);
+ element.add(R.mul(rightElement,leftElement));
}
-
elements.push(element);
}
}
+
_elements = elements;
};
- self.mul = self.rightMultiplication;
- self.rmul = self.rightMultiplication;
-
self.leftMultiplication = function (matrix) {
};
@@ -148,8 +142,8 @@ function RealSquareMatrix(arg) {
return det;
}
- self.determinant = function () {
- return determinant(_order,_elements);
+ self.det = self.determinant = function () {
+ return determinant(_order, _elements);
}
self.toString = function () {
@@ -166,10 +160,5 @@ function RealSquareMatrix(arg) {
};
-RealSquareMatrix.inspect = function (depth) {
- var order = this.getOrder();
- return 'order' + order;
-};
-
module.exports = RealSquareMatrix;
View
@@ -1,7 +1,7 @@
{
"name": "algebra",
- "version": "0.1.2",
- "description": "Vectors, Matrices, Tensors ...",
+ "version": "0.1.3",
+ "description": "Vectors, Matrices, Tensors",
"main": "index.js",
"scripts": {
"test": "mocha --bail --recursive --reporter nyan"
@@ -37,6 +37,23 @@ describe('RealSquareMatrix', function () {
});
});
+ describe('det()', function () {
+ it('is an alias of determinant()', function () {
+ assert.ok(matrix.det === matrix.determinant);
+ });
+ });
+
+ describe('determinant()', function () {
+ it('Computes the determinant of the matrix', function () {
+ var arg = {};
+ arg.order = 2;
+ arg.elements = [1, 2, 2, 1];
+
+ var matrix1 = new RealSquareMatrix(arg);
+ assert.equal(matrix1.determinant().num(), -3);
+ });
+ });
+
describe('getColumnByIndex(<number>)', function () {
it('', function () {
var col = matrix.getColumnByIndex(0);
@@ -107,26 +124,20 @@ describe('RealSquareMatrix', function () {
matrix1.rightMultiplication(matrix2);
elements = matrix1.getElements();
- console.log(elements[0].num());
- console.log(elements[1].num());
- console.log(elements[2].num());
- console.log(elements[3].num());
assert.ok(elements[0].eq(-2));
assert.ok(elements[1].eq(0));
assert.ok(elements[2].eq(0));
- //assert.ok(elements[3].eq(-2));
+ assert.ok(elements[3].eq(-2));
arg.elements = [-0.5, 0, 0, -0.5];
var matrix3 = new RealSquareMatrix(arg);
+ matrix3.mul(matrix1);
elements = matrix3.getElements();
-/*
-TODO
assert.ok(elements[0].eq(1));
assert.ok(elements[1].eq(0));
assert.ok(elements[2].eq(0));
assert.ok(elements[3].eq(1));
-*/
});
});
@@ -161,6 +172,7 @@ TODO
assert.ok(matrix.ij(3,3).eq(6));
});
});
+
describe('mul()', function () {
it('is an alias of rightMultiplication', function () {
assert.ok(matrix.mul === matrix.rightMultiplication);
View
@@ -33,10 +33,13 @@ describe('Quick start:', function () {
var m1 = new GL2R.Matrix(1, 2,
3, 4);
+
var m2 = new GL2R.Matrix(-1, 0,
0, 1);
m1.mul(m2);
- //console.log(m1.toString());
+ assert.equal(m1.toString(),'| -1 2 |\n| -3 4 |\n');
+
+ assert.equal(m1.det().num(), 2);
});
});

0 comments on commit c419cee

Please sign in to comment.