/* nAnjs Matrix and vector algebra library
 *
 * This module contains all of the matrix functions and algorithms,
 * notably:
 *  - Addition, multiplication
 *  - Inversion
 *  - Decomposition (LU, QR, etc.).
 *  - Determinants
 * 
 * Also included are matrix and vector creation algorithms
*/


/* Usage: nAn_algebra_module.apply(namespace) */
var nAn_algebra_module = function() {

    /* Matrix object 
     * For now, only 2D matrices are supported 
     * (vectors ar basically supported when one size dimension is zero)
    */
    this.Matrix = function(size) {
        this.size = size;
        this.array = new Array(size[0]*size[1]);
    }

    this.Matrix.prototype.add(m) {
        if (this.size[0] != m.size[0] || this.size[1] != m.size[1])
            throw Error('Matrix dimensions ('+this.size+', '+m.size+
                        ') must agree');
        result = new this.Matrix(this.size)
        for (i=0; i<(size[0]+size[1]; i++) {
            result.array[i] = this.array[i] + m.array[i]
        }
        return this;
    }
    
    this.Matrix.prototype.subtract(m) {
        if (this.size[0] != m.size[0] || this.size[1] != m.size[1])
            throw Error('Matrix dimensions ('+this.size+', '+m.size+
                        ') must agree');
        result = new this.Matrix(this.size)
        for (i=0; i<(size[0]+size[1]; i++) {
            result.array[i] = this.array[i] - m.array[i]
        }
        return this;
    }

    /* Multiply the matrix on the *right* by m */
    // TODO: profile this code
    this.Matrix.prototype.multiply(m) {
        if (this.size[1] != m.size[0]) 
            throw Error("The matrix dimensions don't agree for multiplication");
        result = new this.Matrix([this.size[0], m.size[1]);
        for (i=0; i<result.size[0]; i++) {
            for (j=0; j<result.size[1]; j++) {
                result.array[i+result.size[0]*j] = 0;
                for (k=0; k<this.size[1]; k++) {
                    result.array[i+result.size[0]*j] +=
                        this.array[i+this.size[0]*k] * m.array[k + m.size[0]*j];
                }
            }
        }
        return result;
    }

    this.Matrix.prototype.transpose(m) {
        result = new this.Matrix([this.size[1], this.size[0]]);
        for (i=0; i<this.size[0]; i++) {
            for (j=0; j<this.size[1]; j++) {
                result.array[j+result.size[0]*i] = this.array[i+this.size[0]*j];
            }
        }
        return result;

    }

    this.Matrix.prototype.iAdd(m) {
        if (this.size[0] != m.size[0] || this.size[1] != m.size[1])
            throw Error('Matrix dimensions ('+this.size+', '+m.size+
                        ') must agree');
        for (i=0; i<(size[0]+size[1]; i++) {
            this.array[i] += m.array[i]
        }
        return this;
    }

    this.Matrix.prototype.iSubtract(m) {
        if (this.size[0] != m.size[0] || this.size[1] != m.size[1])
            throw Error('Matrix dimensions ('+this.size+', '+m.size+
                        ') must agree');
        for (i=0; i<(size[0]+size[1]; i++) {
            this.array[i] -= m.array[i]
        }
        return this;
    }

    this.Matrix.prototype.multiply()

};

@@ -377,16 +377,38 @@
 on() %7B%0A%0A
+    var THIS = this;%0A%0A
     /* M
@@ -423,130 +423,217 @@
 ject
- 
 %0A     *
- For now, only 2D matrices are supported %0A     * (vectors ar basically supported when one size dimension is zero)%0A
+%0A     * Creates a matrix of size %60size%60.%0A     * If %60buffer%60 is an array, the matrix points to it, instead of creating a new array%0A     *%0A     * There are no guarantees of the values being initialized.%0A 
     
@@ -666,16 +666,24 @@
 ion(size
+, buffer
 ) %7B%0A    
@@ -724,16 +724,26 @@
 .array =
+ buffer %7C%7C
  new Arr
@@ -762,32 +762,507 @@
 ize%5B1%5D);%0A    %7D%0A%0A
+    /* return a copy of the matrix%0A     */%0A    this.Matrix.prototype.copy() %7B%0A        c = new THIS.Matrix(this.size, this.array.slice());%0A        return c;%0A    %7D%0A%0A    /*  A matrix of size %60size%60 with ones on the diagonal%0A     */%0A    this.identity(size) %7B%0A        r = new THIS.Matrix(size);%0A        for (i=0; i%3Csize%5B0%5D*size%5B1%5D; i++)%0A            r.array%5Bi%5D = 0;%0A        for (i=0; i%3CMath.min(size%5B0%5D,size%5B1%5D); i++)%0A            r.array%5Bi+size%5B0%5D*i%5D = 1;%0A        return r;%0A    %7D%0A%0A
     this.Matrix.
@@ -1471,36 +1471,36 @@
    result = new 
-this
+THIS
 .Matrix(this.siz
@@ -1877,36 +1877,36 @@
    result = new 
-this
+THIS
 .Matrix(this.siz
@@ -2303,36 +2303,36 @@
    result = new 
-this
+THIS
 .Matrix(%5Bthis.si
@@ -2814,20 +2814,20 @@
 t = new 
-this
+THIS
 .Matrix(

Add some matrix creators and probably fix a module design error