Skip to content
Browse files

added matrix multiplication

  • Loading branch information...
1 parent 7532c16 commit cb90a543769c73afd541151a5c089da7f03b6a13 @fibo committed
Showing with 119 additions and 4 deletions.
  1. +7 −1 README.md
  2. +77 −0 lib/Real/SquareMatrix.js
  3. +30 −0 test/Real/SquareMatrix.js
  4. +5 −3 test/quickStart.js
View
8 README.md
@@ -59,8 +59,14 @@ This is a 60 seconds tutorial to get your hands dirty with `algebra`.
// Create a 2x2 invertible matrix.
var GL2R = new GLnR(2);
- var m1 = new GL2R.Matrix(1, 2, 3, 4);
+ 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());
# Description
View
77 lib/Real/SquareMatrix.js
@@ -1,9 +1,86 @@
+var _ = require('underscore');
+
+var RealField = require('./Field.js');
+
+var R = new RealField();
+
function RealSquareMatrix(arg) {
var self = this;
var _elements = arg.elements;
self.getElements = function () { return _elements };
+
+ var _order = arg.order;
+
+ var _numRows = _order;
+ var _numCols = _order;
+
+ function index(i ,j) {
+ return i * _numCols + j;
+ };
+
+ self.ij = function (i, j) {
+ return _elements[index(i, j)];
+ };
+
+ self.row = function (rowIndex) {
+ var row = [];
+ for (var j = 0; j < _numCols; j++) {
+ var element = self.ij(rowIndex, j);
+ row.push(element);
+ }
+ return row;
+ };
+
+ self.col = function (colIndex) {
+ var col = [];
+ for (var i = 0; i < _numRows; i++) {
+ var element = self.ij(i, colIndex);
+ col.push(element);
+ }
+ return col;
+ };
+
+ self.rightMultiplication = function (matrix) {
+ for (var i = 0; i < _numRows; i++) {
+ var row = self.row(i);
+
+ for (var j = 0; j < _numCols; j++) {
+ var col = matrix.col(j);
+
+ var element = R.getZero();
+ for (var k = 0; k < _order; k++) {
+ element.add(row[k]).mul(col[k]);
+ }
+ _elements[index(i, j)] = element;
+ }
+ }
+ };
+
+ self.mul = self.rightMultiplication;
+ self.rmul = self.rightMultiplication;
+
+ self.leftMultiplication = function (matrix) {
+
+ };
+
+ // TODO rightMultiplication leftMultiplication in mxn matrix
+
+ self.lmul = self.leftMultiplication;
+
+ self.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;
+ };
+
};
module.exports = RealSquareMatrix;
View
30 test/Real/SquareMatrix.js
@@ -16,5 +16,35 @@ describe('RealSquareMatrix', function () {
assert.ok(matrix instanceof RealSquareMatrix);
});
});
+
+ describe('getElements()', function () {
+ it('', function () {
+ });
+ });
+
+ describe('rightMultiplication()', function () {
+ it('', function () {
+ });
+ });
+
+ describe('leftMultiplication()', function () {
+ it('', function () {
+ });
+ });
+
+ describe('mul()', function () {
+ it('', function () {
+ });
+ });
+
+ describe('lmul()', function () {
+ it('', function () {
+ });
+ });
+
+ describe('rmul()', function () {
+ it('', function () {
+ });
+ });
});
View
8 test/quickStart.js
@@ -31,9 +31,11 @@ describe('Quick start:', function () {
var GLnR = algebra.Real.GeneralLinearGroup;
var GL2R = new GLnR(2);
- var m1 = new GL2R.Matrix(1, 2, 3, 4);
-
- //m1.mul(m2);
+ 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());
});

0 comments on commit cb90a54

Please sign in to comment.
Something went wrong with that request. Please try again.