Permalink
Browse files

lot of mess around :)

  • Loading branch information...
1 parent c00f55e commit f0b40ed6882cef8d75a1d01bfbe634b9aadc7cbc @fibo committed Jan 20, 2013
View
@@ -10,7 +10,6 @@ var Real = {};
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.SquareMatrix = require('./lib/Real/SquareMatrix.js');
Real.Vector = require('./lib/Real/Vector.js');
Real.VectorSpace = require('./lib/Real/VectorSpace.js');
@@ -40,3 +39,10 @@ var Octonion = {};
exports.Octonion = Octonion;
+var util = {};
+
+util.coerce = require('./lib/util/coerce.js');
+util.is = require('./lib/util/is.js');
+
+exports.util = util;
+
@@ -10,13 +10,9 @@ var ComplexField = function () {
util.inherits(ComplexField, Field);
-ComplexField.prototype.getZero = function () {
- return new Complex(0);
-};
+ComplexField.prototype.getZero = function () { return new Complex(0); };
-ComplexField.prototype.getOne = function () {
- return new Complex(1);
-};
+ComplexField.prototype.getOne = function () { return new Complex(1); };
ComplexField.prototype.getMinusOne = function () {
return new Complex(-1);
View
@@ -1,5 +1,5 @@
-var RealElement = function (arg) {
+function RealElement(arg) {
var self = this;
var _num = 0;
@@ -9,7 +9,7 @@ var RealElement = function (arg) {
self.num = function () { return _num; };
- var coerce = function (x) {
+ function coerce(x) {
if (typeof x == 'number') {
return x;
}
@@ -18,20 +18,40 @@ var RealElement = function (arg) {
}
};
- self.eq = function (x) {
+ function equals(x) {
var num = coerce(x);
return (num == _num);
};
- self.isOne = function () {
- return self.eq(1);
+ self.eq = self.equals = equals;
+
+ function notEquals(x) {
+ var num = coerce(x);
+
+ return (num != _num);
};
- self.isZero = function () {
- return self.eq(0);
+ self.ne = self.notEquals = notEquals;
+
+ function isZero() {
+ return equals(0);
};
+ self.isZero = isZero;
+
+ function isOne() {
+ return equals(1);
+ };
+
+ self.isOne = isOne;
+
+ function isNotZero() {
+ return notEquals(0);
+ };
+
+ self.isNotZero = isNotZero;
+
self.neg = function () {
_num = 0 - _num;
View
@@ -2,29 +2,20 @@
var util = require('util');
var Field = require('../Field.js');
-var Real = require('./Element.js');
+var RealElement = require('./Element.js');
+var coerce = require('../util/coerce.js');
-var RealField = function () {
+function RealField() {
var self = this;
};
util.inherits(RealField, Field);
-RealField.prototype.coerceToElement = function (arg) {
-// TODO sistema bene, mancano tutte le casistiche, i test ecc
-// poi considera che voglio fare i numei trascendenti, le radici dei polinomi, ecc
-// gli potrei passare dei numeri complessi, ecc
- if( typeof arg == 'number' ) {
- return new Real(arg);
- }
- else {
- return arg;
- }
-};
+RealField.prototype.coerceToElement = coerce.toRealElement;
-RealField.prototype.getZero = function () { return new Real(0); };
+RealField.prototype.getZero = function getZero() { return new RealElement(0); };
-RealField.prototype.getOne = function () { return new Real(1); };
+RealField.prototype.getOne = function getOne() { return new RealElement(1); };
module.exports = RealField;
@@ -1,12 +1,12 @@
-var RealInvertibleMatrix = require('./InvertibleMatrix.js');
+var RealSquareMatrix = require('./SquareMatrix.js');
-function GeneralLinearGroup(order) {
+function RealGeneralLinearGroup(order) {
var self = this;
var _order = order;
- self.Matrix = function () {
+ self.Matrix = function Matrix() {
var arg = {};
arg.order = _order;
@@ -15,11 +15,13 @@ function GeneralLinearGroup(order) {
arg.elements.push(arguments[i]);
}
- RealInvertibleMatrix.call(this, arg);
+ RealSquareMatrix.call(this, arg);
+
+ if (this.determinant().isZero()) throw new Error();
};
- // TODO self.Id = self.Matrix;
+ self.Id = self.Identity = function Identity() {return new Matrix()};
};
-module.exports = GeneralLinearGroup;
+module.exports = RealGeneralLinearGroup;
@@ -1,41 +0,0 @@
-
-var _ = require('underscore');
-var util = require('util');
-
-var RealElement = require('./Element.js');
-var RealField = require('./Field.js');
-var RealSquareMatrix = require('./SquareMatrix.js');
-
-var R = new RealField();
-
-function RealInvertibleMatrix(arg) {
- var self = this;
-
- var _order = arg.order;
- var _elements = [];
-
- if (_.isArray(arg.elements)) _elements = arg.elements;
-
- // Matrix defaults to Identity.
- for (var i = 0; i < _order; i++) {
- for (var j = 0; j < _order; j++) {
- var index = i * _order + j;
-
- var element = _elements[index];
-
- if (_.isUndefined(element)) {
- i == j ? _elements.push(R.getOne()) : _elements.push(R.getZero());
- }
- }
- }
-
- arg.elements = _elements;
- RealSquareMatrix.call(self, arg);
-};
-
-RealInvertibleMatrix.inspect = RealSquareMatrix.inspect;
-
-util.inherits(RealInvertibleMatrix, RealSquareMatrix);
-
-module.exports = RealInvertibleMatrix;
-
@@ -1,8 +1,7 @@
-var _ = require('underscore');
-
var RealElement = require('./Element.js');
var RealField = require('./Field.js');
+var is = require('../util/is.js');
var R = new RealField();
@@ -13,31 +12,40 @@ function index(i, j, numberOfColumns) {
function RealSquareMatrix(arg) {
var self = this;
+ var _order = arg.order;
+ self.getOrder = function getOrder() { return _order; };
+
var _elements = [];
- for (var i in arg.elements) {
- if (typeof arg.elements[i] == 'number') {
- var element = new RealElement(arg.elements[i]);
- _elements.push(element);
- }
- else {
- _elements.push(arg.elements[i]);
+
+ function getElements() { return _elements };
+
+ self.getElements = getElements;
+
+ // Defaults to Identity.
+ if (is.notDefined(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());
+ }
}
}
- self.getElements = function getElements() { return _elements };
- // TODO order ricavato in automatico in base al numero di elementi
- // se non si tratta di un quadrato pero?
- var _order = arg.order;
- self.getOrder = function getOrder() { return _order; };
+ for (var i in arg.elements) {
+ var element = new RealElement(arg.elements[i]);
+ _elements.push(element);
+ }
+
var _numRows = _order;
var _numCols = _order;
- self.ij = self.getElementByIndexes = function getElementByIndexes(i, j) {
+ function getElementByIndexes(i, j) {
return _elements[index(i, j, _numCols)];
};
- self.scalar = self.scalarMultiplication = function scalarMultiplication(scalar) {
+ self.ij = self.getElementByIndexes = getElementByIndexes;
+
+ function scalarMultiplication(scalar) {
// TOOD funzione comune a tutti coerceToRealElement
if (typeof scalar == 'number') {
@@ -52,6 +60,8 @@ function RealSquareMatrix(arg) {
// questo potrebbe essere usato sia da Vector che da Matrix che da Tensor
};
+ self.scalar = self.scalarMultiplication = scalarMultiplication;
+
// 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) {
@@ -142,10 +152,12 @@ function RealSquareMatrix(arg) {
return det;
}
- self.det = self.determinant = function () {
+ function realSquareMatrixDeterminant() {
return determinant(_order, _elements);
}
+ self.det = self.determinant = realSquareMatrixDeterminant;
+
self.toString = function () {
var str = '';
for (var i = 0; i < _numRows; i++) {
@@ -1,12 +1,12 @@
+var RealElement = require('./Element.js');
var RealField = require('./Field.js');
-var R = new RealField();
-
+var RealVector = require('./Vector.js');
var VectorSpace = require('../VectorSpace.js');
-var RealVector = require('./Vector.js');
-var Real = require('./Element.js');
-var RealVectorSpace = function (dim) {
+var R = new RealField();
+
+function RealVectorSpace(dim) {
var self = this;
var arg = {};
@@ -16,14 +16,14 @@ var RealVectorSpace = function (dim) {
VectorSpace.call(self, arg);
- self.Vector = function () {
+ self.Vector = function Vector() {
var arg = {};
arg.elements = [];
arg.space = self;
for (var i = 0; i < self.getDim(); i++) {
var num = arguments[i] || 0;
- var element = new Real(num);
+ var element = new RealElement(num);
arg.elements.push(element);
}
View
@@ -0,0 +1,21 @@
+
+var RealElement = require('../Real/Element.js');
+
+var coerce = {};
+
+function coerceToRealElement(arg) {
+// TODO sistema bene, mancano tutte le casistiche, i test ecc
+// poi considera che voglio fare i numei trascendenti, le radici dei polinomi, ecc
+// gli potrei passare dei numeri complessi, ecc
+ if( typeof arg == 'number' ) {
+ return new RealElement(arg);
+ }
+ else {
+ return arg;
+ }
+};
+
+coerce.toRealElement = coerceToRealElement;
+
+module.exports = coerce;
+
View
@@ -0,0 +1,13 @@
+
+var is = {};
+
+function isDefined(arg) {
+ if (arguments.lenght != 1) throw new Error();
+
+ return typeof arguments[0] === 'undefined';
+}
+
+is.defined = isDefined;
+
+module.exports = is;
+
Oops, something went wrong.

0 comments on commit f0b40ed

Please sign in to comment.