nicolewhite/algebra.js

Variable multiplication is not separed by *

CaselIT opened this issue · 6 comments

The omission of the multiplication symbol between the variables makes it impossible to reparse a expression

example: algebra.parse('x*y').toString() returns xy that is considered as a single variable of name xy instead of x*y if reparsed.

My use case: Save the simplified version of an expression written by an user.

Probably the best option is to add an optional config object to the toString() method to modify its behaviour.

To show the multiplications between the variables a boolean option implicit can be added. So when toString({implicit: true}) is called the * will be shown

This way it's also easy to add new options.
I can work on a PR if you approve

I am not sure I follow. While it is rendered as xy, they are still separate variables. Example:

var e = algebra.parse('x * y')
e.eval({x: 2}).toString();
"2y"
e.eval({y: 2}).toString();
"2x"

The problem arises if I try to re evaluate the toString output.
My use case is: The user inputs an expression, it is parsed and the parsed result is saved. Then I would like to be able to take the saved string at a later time and use it as input.

The problem is that since x * y is rendered as xy, the second time I parse xy this is considered as a single variable named xy.

In any case I've already solved this problem by writing a custom toString function, so if you like this can be closed.

Ohh, I see. If you have already written the function, feel free to send a pull request with the implicit arg.

Ok, thanks 👍

algebra-0.2.6.min.js is missing the support for options passed to Term.toString.
When I put algebra-0.2.6.min.js through unminify.com I get this

Term.prototype.toString = function() {
                var e = "";
                for (var t = 0; t < this.coefficients.length; t++) {
                    var r = this.coefficients[t];
                    if (r.abs().numer !== 1 || r.abs().denom !== 1) {
                        e += " * " + r.toString()
                    }
                }
                e = this.variables.reduce(function(e, t) {
                    return e.concat(t.toString())
                }, e);
                e = e.substring(0, 3) === " * " ? e.substring(3, e.length) : e;
                e = e.substring(0, 1) === "-" ? e.substring(1, e.length) : e;
                return e
            };

Whilst the original in expressions.js dated June 2017 is

Term.prototype.toString = function(options) {
    var implicit = options && options.implicit;
    var str = "";

    for (var i = 0; i < this.coefficients.length; i++) {
        var coef = this.coefficients[i];

        if (coef.abs().numer !== 1 || coef.abs().denom !== 1) {
            str += " * " + coef.toString();
        }
    }
    str = this.variables.reduce(function (p, c) {
        if (implicit && !!p) {
            var vStr = c.toString();
            return !!vStr ? p + "*" + vStr : p;
        } else
            return p.concat(c.toString());
    }, str);
    str = (str.substring(0, 3) === " * " ? str.substring(3, str.length) : str);
    str = (str.substring(0, 1) === "-" ? str.substring(1, str.length) : str);

    return str;
};

Is algebra-0.2.6.min.js based on older source?