Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added util.algorithm

  • Loading branch information...
commit 6e0cc6bdb7eb6d9621f777b7d56abb01dfebdc58 1 parent 7c28554
@fibo authored
View
5 index.js
@@ -41,8 +41,9 @@ exports.Octonion = Octonion;
var util = {};
-util.coerce = require('./lib/util/coerce.js');
-util.is = require('./lib/util/is.js');
+util.algorithm = require('./lib/util/algorithm.js');
+util.coerce = require('./lib/util/coerce.js');
+util.is = require('./lib/util/is.js');
exports.util = util;
View
8 lib/Real/Field.js
@@ -13,9 +13,13 @@ util.inherits(RealField, Field);
RealField.prototype.coerceToElement = coerce.toRealElement;
-RealField.prototype.getZero = function getZero() { return new RealElement(0); };
+function getZero() { return new RealElement(0); };
-RealField.prototype.getOne = function getOne() { return new RealElement(1); };
+RealField.prototype.getZero = getZero;
+
+function getOne() { return new RealElement(1); };
+
+RealField.prototype.getOne = getOne;
module.exports = RealField;
View
14 lib/Real/GeneralLinearGroup.js
@@ -13,6 +13,20 @@ function RealGeneralLinearGroup(order) {
arg.order = _order;
+ // Defaults to Identity.
+ 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());
+ }
+ }
+ }
+
+ for (var i in arg.elements) {
+ var element = new RealElement(arg.elements[i]);
+ _elements.push(element);
+ }
+
arg.elements = [];
for (var i in arguments) {
arg.elements.push(arguments[i]);
View
145 lib/Real/SquareMatrix.js
@@ -1,10 +1,9 @@
-var _ = require('underscore');
-
+var algorithm = require('../util/algorithm.js');
var RealElement = require('./Element.js');
-var RealField = require('./Field.js');
-var R = new RealField();
+var getAdjointElements = algorithm.getAdjointElements;
+var rowByColumnMultiplication = algorithm.rowByColumnMultiplication;
function index(i, j, numberOfColumns) {
return i * numberOfColumns + j;
@@ -13,8 +12,15 @@ function index(i, j, numberOfColumns) {
function RealSquareMatrix(arg) {
var self = this;
+//-----------------------------------------------------------------------------
+
var _order = arg.order;
- self.getOrder = function getOrder() { return _order; };
+
+ function getOrder() { return _order; };
+
+ self.getOrder = getOrder;
+
+//-----------------------------------------------------------------------------
var _elements = [];
@@ -22,33 +28,30 @@ function RealSquareMatrix(arg) {
self.getElements = getElements;
- // Defaults to Identity.
- 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());
- }
- }
- }
-
- for (var i in arg.elements) {
- var element = new RealElement(arg.elements[i]);
- _elements.push(element);
- }
+//-----------------------------------------------------------------------------
function getNumberOfColumns() { return _order; };
- self.numCols = self.getNumberOfColumns = getNumberOfColumns;
+ self.getNumberOfColumns = getNumberOfColumns;
+ self.numCols = getNumberOfColumns;
+
+//-----------------------------------------------------------------------------
function getNumberOfRows() { return _order; };
- self.numRows = self.getNumberOfRows = getNumberOfRows;
+ self.getNumberOfRows = getNumberOfRows;
+ self.numRows = getNumberOfRows;
+
+//-----------------------------------------------------------------------------
function getElementByIndexes(i, j) {
return _elements[index(i, j, self.numCols())];
};
- self.ij = self.getElementByIndexes = getElementByIndexes;
+ self.getElementByIndexes = getElementByIndexes;
+ self.ij = getElementByIndexes;
+
+//-----------------------------------------------------------------------------
function scalarMultiplication(scalar) {
@@ -65,20 +68,26 @@ function RealSquareMatrix(arg) {
// questo potrebbe essere usato sia da Vector che da Matrix che da Tensor
};
- self.scalar = self.scalarMultiplication = scalarMultiplication;
+ self.scalar = scalarMultiplication;
+ self.scalarMultiplication = scalarMultiplication;
+
+//-----------------------------------------------------------------------------
- // TODO wquesto modo di definire le funzioni sarebbe consigliabile
- // per non farle anomime e avere uno stack migliore.
function getRowByIndex(rowIndex) {
var row = [];
+
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.getRowByIndex = getRowByIndex;
+ self.row = getRowByIndex;
+
+//-----------------------------------------------------------------------------
function getColumnByIndex(colIndex) {
var col = [];
@@ -89,43 +98,20 @@ function RealSquareMatrix(arg) {
return col;
};
- self.col = self.getColumnByIndex = getColumnByIndex;
-
- function rowByColumnMultiplication(leftMatrix, rightMatrix) {
- var matricesCanNotBeMultiplied = (leftMatrix.getNumberOfColumns() !== rightMatrix.getNumberOfRows());
-
- var elements = [];
-
- // TODO dovrei controllare anche il Field o Ring del Gruppo di Matrici
- // per ora uso secco R
- var ring = R;
+ self.col = getColumnByIndex;
+ self.getColumnByIndex = getColumnByIndex;
- 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 = leftMatrix.ij(i, k);
- var leftElement = rightMatrix.ij(k, j);
- element.add(ring.mul(rightElement,leftElement));
-console.log(element.num());
- }
-
- elements.push(element);
- }
- }
-
- return elements;
- };
+//-----------------------------------------------------------------------------
function rightMultiplication(matrix) {
_elements = rowByColumnMultiplication(self, matrix);
};
- self.mul = self.rmul = self.rightMultiplication = rightMultiplication;
+ self.mul = rightMultiplication;
+ self.rightMultiplication = rightMultiplication;
+ self.rmul = rightMultiplication;
+
+//-----------------------------------------------------------------------------
function leftMultiplication(matrix) {
// TODO dovrei poter passare un array di matrici, cioè fare tipo
@@ -135,52 +121,19 @@ console.log(element.num());
// TODO rightMultiplication leftMultiplication in mxn matrix
- self.lmul = self.leftMultiplication = leftMultiplication;
-
- function getAdjointElements(rowIndex, columnIndex, numRows, numCols, elements) {
- var adjointElements = [];
- for (var i = 0; i < numRows; i++) {
- for (var j = 0; j < numCols; j++) {
- if (i != rowIndex && j != columnIndex) {
- adjointElements.push(elements[index(i, j, numCols)]);
- }
- }
- }
- return adjointElements;
- };
-
- function determinant(order, elements) {
- var det = R.getZero();
-
- if (order == 2) {
- det.add(elements[0].mul(elements[3])).sub(elements[2].mul(elements[1]));
- return det;
- }
-
- // TODO per ora scelgo sempre la prima riga.
- // TODO classe Collection con metodo countZeros o numberOfZeros()
- var rowIndex = 0;
+ self.leftMultiplication = leftMultiplication;
+ self.lmul = leftMultiplication;
- for (var columnIndex = 0; columnIndex < order; columnIndex++) {
- var adjointElements=getAdjointElements(rowIndex,columnIndex,order,order,elements);
- var adjointDeterminant=determinant(order - 1,adjointElements);
- var element = elements[index(rowIndex, columnIndex, order)];
- if (columnIndex % 2 == 0) {
- det.add(element);
- }
- else {
- det.sub(element);
- }
- det.mul(adjointDeterminant);
- }
- return det;
- }
+//-----------------------------------------------------------------------------
function realSquareMatrixDeterminant() {
return determinant(_order, _elements);
}
- self.det = self.determinant = realSquareMatrixDeterminant;
+ self.det = realSquareMatrixDeterminant;
+ self.determinant = realSquareMatrixDeterminant;
+
+//-----------------------------------------------------------------------------
self.toString = function () {
var str = '';
View
88 lib/util/algorithm.js
@@ -0,0 +1,88 @@
+
+var algorithm = {};
+
+var RealField = require('../Real/Field.js');
+
+var R = new RealField();
+
+//-----------------------------------------------------------------------------
+
+function determinant(order, elements) {
+ var det = R.getZero();
+
+ if (order == 2) {
+ det.add(elements[0].mul(elements[3])).sub(elements[2].mul(elements[1]));
+ return det;
+ }
+
+ // TODO per ora scelgo sempre la prima riga.
+ // TODO classe Collection con metodo countZeros o numberOfZeros()
+ var rowIndex = 0;
+
+ for (var columnIndex = 0; columnIndex < order; columnIndex++) {
+ var adjointElements=getAdjointElements(rowIndex,columnIndex,order,order,elements);
+ var adjointDeterminant=determinant(order - 1,adjointElements);
+ var element = elements[index(rowIndex, columnIndex, order)];
+ if (columnIndex % 2 == 0) {
+ det.add(element);
+ }
+ else {
+ det.sub(element);
+ }
+ det.mul(adjointDeterminant);
+ }
+ return det;
+}
+
+algorithm.determinant = determinant;
+
+//-----------------------------------------------------------------------------
+
+function getAdjointElements(rowIndex, columnIndex, numRows, numCols, elements) {
+ var adjointElements = [];
+ for (var i = 0; i < numRows; i++) {
+ for (var j = 0; j < numCols; j++) {
+ if (i != rowIndex && j != columnIndex) {
+ adjointElements.push(elements[index(i, j, numCols)]);
+ }
+ }
+ }
+ return adjointElements;
+};
+
+algorithm.getAdjointElements = getAdjointElements;
+
+//-----------------------------------------------------------------------------
+
+function rowByColumnMultiplication(leftMatrix, rightMatrix) {
+ var matricesCanNotBeMultiplied = (leftMatrix.getNumberOfColumns() !== rightMatrix.getNumberOfRows());
+
+ var elements = [];
+
+ // TODO dovrei controllare anche il Field o Ring del Gruppo di Matrici
+ // per ora uso secco R
+ var ring = R;
+
+ 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 = leftMatrix.ij(i, k);
+ var leftElement = rightMatrix.ij(k, j);
+ element.add(ring.mul(rightElement,leftElement));
+ }
+
+ elements.push(element);
+ }
+ }
+
+ return elements;
+};
+
+
+module.exports = algorithm;
+
View
13 test/Real/GeneralLinearGroup.js
@@ -24,6 +24,19 @@ describe('RealGeneralLinearGroup', function () {
it('throws Error', function () {
assert.throws(function () { new GL2.Matrix(1, 1, 2, 2); }, Error);
});
+
+ it('defaults to Indentity', function () {
+ var arg = {};
+ arg.order = 2;
+
+ var identity = new GL2.Matrix();
+
+ var elements = identity.getElements();
+ assert.ok(elements[0].isOne());
+ assert.ok(elements[1].isZero();
+ assert.ok(elements[2].isZero());
+ assert.ok(elements[3].isOne());
+ });
});
});
View
13 test/Real/SquareMatrix.js
@@ -23,19 +23,6 @@ describe('RealSquareMatrix', function () {
assert.ok(matrix1 instanceof RealSquareMatrix);
});
-
- it('defaults to Indentity', function () {
- var arg = {};
- arg.order = 2;
-
- var matrix1 = new RealSquareMatrix(arg);
-
- var elements1 = matrix1.getElements();
- assert.ok(elements1[0].eq(1));
- assert.ok(elements1[1].eq(0));
- assert.ok(elements1[2].eq(0));
- assert.ok(elements1[3].eq(1));
- });
});
describe('getOrder()', function () {
Please sign in to comment.
Something went wrong with that request. Please try again.