Skip to content
Browse files

added rowByColumnMultiplication

  • Loading branch information...
1 parent f0b40ed commit cf27c98a7cf03e16967b0ce279950afe7ab243da @fibo committed Jan 21, 2013
Showing with 51 additions and 26 deletions.
  1. +47 −23 lib/Real/SquareMatrix.js
  2. +4 −3 test/quickStart.js
View
70 lib/Real/SquareMatrix.js
@@ -1,7 +1,8 @@
+var _ = require('underscore');
+
var RealElement = require('./Element.js');
var RealField = require('./Field.js');
-var is = require('../util/is.js');
var R = new RealField();
@@ -22,7 +23,7 @@ function RealSquareMatrix(arg) {
self.getElements = getElements;
// Defaults to Identity.
- if (is.notDefined(arg.elements) {
+ if (! _.isArray(arg.elements)) {
for (var i = 0; i < _order; i++) {
for (var j = 0; j < _order; j++) {
i == j ? _elements.push(R.getOne()) : _elements.push(R.getZero());
@@ -35,12 +36,16 @@ function RealSquareMatrix(arg) {
_elements.push(element);
}
+ function getNumberOfColumns() { return _order; };
+
+ self.numCols = self.getNumberOfColumns = getNumberOfColumns;
+
+ function getNumberOfRows() { return _order; };
- var _numRows = _order;
- var _numCols = _order;
+ self.numRows = self.getNumberOfRows = getNumberOfRows;
function getElementByIndexes(i, j) {
- return _elements[index(i, j, _numCols)];
+ return _elements[index(i, j, self.numCols())];
};
self.ij = self.getElementByIndexes = getElementByIndexes;
@@ -64,54 +69,73 @@ function RealSquareMatrix(arg) {
// TODO wquesto modo di definire le funzioni sarebbe consigliabile
// per non farle anomime e avere uno stack migliore.
- self.row = self.getRowByIndex = function getRowByIndex(rowIndex) {
+ function getRowByIndex(rowIndex) {
var row = [];
- for (var j = 0; j < _numCols; j++) {
+ for (var j = 0; j < self.numCols(); j++) {
var element = self.ij(rowIndex, j);
row.push(element);
}
return row;
};
+ self.row = self.getRowByIndex = getRowByIndex;
-
- self.col = self.getColumnByIndex = function getColumnByIndex(colIndex) {
+ function getColumnByIndex(colIndex) {
var col = [];
- for (var i = 0; i < _numRows; i++) {
+ for (var i = 0; i < self.numRows(); i++) {
var element = self.ij(i, colIndex);
col.push(element);
}
return col;
};
+ self.col = self.getColumnByIndex = getColumnByIndex;
+
+ function rowByColumnMultiplication(leftMatrix, rightMatrix) {
+ var matricesCanNotBeMultiplied = (leftMatrix.getNumberOfColumns() !== rightMatrix.getNumberOfRows());
- self.mul = self.rmul = self.rightMultiplication = function rightMultiplication(matrix) {
var elements = [];
- for (var i = 0; i < _numRows; i++) {
- for (var j = 0; j < _numCols; j++) {
- var element = R.getZero();
+ // TODO dovrei controllare anche il Field o Ring del Gruppo di Matrici
+ // per ora uso secco R
+ var ring = R;
- for (var k = 0; k < _order; k++) {
+ if (matricesCanNotBeMultiplied) throw new Error();
+
+ for (var i = 0; i < leftMatrix.getNumberOfRows(); i++) {
+ for (var j = 0; j < rightMatrix.getNumberOfColumns(); j++) {
+ var element = ring.getZero();
+
+ for (var k = 0; k < leftMatrix.getNumberOfColumns(); k++) {
// TODO funzione generica di somma sugli indici, tipo notazione di Einstein.
- var rightElement = self.ij(i, k);
- var leftElement = matrix.ij(k, j);
- element.add(R.mul(rightElement,leftElement));
+ var rightElement = leftMatrix.ij(i, k);
+ var leftElement = rightMatrix.ij(k, j);
+ element.add(ring.mul(rightElement,leftElement));
+console.log(element.num());
}
+
elements.push(element);
}
}
- _elements = elements;
+ return elements;
+ };
+
+ function rightMultiplication(matrix) {
+ _elements = rowByColumnMultiplication(self, matrix);
};
- self.leftMultiplication = function (matrix) {
+ self.mul = self.rmul = self.rightMultiplication = rightMultiplication;
+ function leftMultiplication(matrix) {
+ // TODO dovrei poter passare un array di matrici, cioè fare tipo
+ // for (var i in arguments) _elements = rowByColumnMultiplication(arguments[i], self);
+ _elements = rowByColumnMultiplication(matrix, self);
};
// TODO rightMultiplication leftMultiplication in mxn matrix
- self.lmul = self.leftMultiplication;
+ self.lmul = self.leftMultiplication = leftMultiplication;
function getAdjointElements(rowIndex, columnIndex, numRows, numCols, elements) {
var adjointElements = [];
@@ -160,9 +184,9 @@ function RealSquareMatrix(arg) {
self.toString = function () {
var str = '';
- for (var i = 0; i < _numRows; i++) {
+ for (var i = 0; i < self.numRows(); i++) {
str += '|';
- for (var j = 0; j < _numCols; j++) {
+ for (var j = 0; j < self.numCols(); j++) {
str += ' ' + self.ij(i, j).num() + ' ';
}
str += '|\n';
View
7 test/quickStart.js
@@ -36,10 +36,11 @@ describe('Quick start:', function () {
var m2 = new GL2R.Matrix(-1, 0,
0, 1);
- //m1.mul(m2);
- //assert.equal(m1.toString(),'| -1 2 |\n| -3 4 |\n');
+ m1.mul(m2);
- //assert.equal(m1.det().num(), 2);
+ assert.equal(m1.toString(),'| -1 2 |\n| -3 4 |\n');
+
+ assert.equal(m1.det().num(), 2);
});
});

0 comments on commit cf27c98

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