nicolewhite / algebra.js

Build, display, and solve algebraic equations.
http://algebra.js.org
MIT License
1.34k stars 111 forks source link

featurerequest: vectors and matrices #6

Open jmishra17 opened 9 years ago

jmishra17 commented 9 years ago

Was about to start writing some features for vectors and after looking at some of the code, thought that Vectors should operate on array of expressions as their data type:

For instance,

var Vector = function(args){
    this.vec = [];
    if (Array.isArray(args)){
        args.forEach(function (arg){
            var exp = new Expression(arg);
            this.vec.push(exp);
        });
    } else if(typeof(args) !== "undefined"){
        throw "Expected array of stirngs";
    } 
};

Vector.prototype.add = function(args){
    var otherVec = args;
    if (!(args instanceof Vector)){
        otherVec = new Vector(args);
    }

    if (this.vec.length !== otherVec.vec.length) {
        throw "Unequal lengths";
    }
    var thisVec = this.copy();
    var resultVec = new Vector();
    for (var i = 0; i<thisVec.length; i++){
        var addedExp = thisVec.vec[i].add(otherVec.vec[i]);
        resultVec.push(addedExp);
    }
    return resultVec;
};

Following this logic, nd-vectors or Martrices would operate on the same logic, keeping an array of vectors as their internal data structure.

Any suggestions?

nicolewhite commented 9 years ago

I think it makes perfect sense to maintain a Vector as an array of Expressions. That'll make basic operations pretty simple.

nicolewhite commented 9 years ago

How are you doing with this? I've been playing around with some code locally but I don't want to step on your toes if you're still working on it.

jmishra17 commented 9 years ago

Actually, you can go ahead and write the feature for this. I have been busy with some other stuff. I am actually gonna go ahead and add plotting feature for all fractions, equations and expressions.

jmishra17 commented 9 years ago

To add to that, I had a few framework adjustment ideas to suggest if you wanna talk this in detail. We can definitely open a Gitter Chat for this if you like

nicolewhite commented 9 years ago

Sure, we're over here: https://gitter.im/nicolewhite/algebra.js