<commit_before>var Fraction = require('./fractions');

var Expression = function(variable) {
    this.variable = variable;
    this.coefficient = new Fraction(1, 1);
    this.constant = new Fraction(0, 1);
};

Expression.prototype.copy = function() {
    var ex = new Expression(this.variable);
    ex.coefficient = this.coefficient;
    ex.constant = this.constant;
    return ex;
};

Expression.prototype.add = function(a) {
    var copy = this.copy();

    if (a instanceof Expression) {
        if (copy.variable == a.variable) {
            copy.coefficient = copy.coefficient.add(a.coefficient);
            copy.constant = copy.constant.add(a.constant);
        }
    } else {
        copy.constant = copy.constant.add(a);
    }

    return copy;
};

Expression.prototype.subtract = function(a) {
    var copy = this.copy();

    if (a instanceof Expression) {
        if (copy.variable == a.variable) {
            copy.coefficient = copy.coefficient.subtract(a.coefficient);
            copy.constant = copy.constant.subtract(a.constant);
        }
    } else {
        copy.constant = copy.constant.subtract(a);
    }

    return copy;
};

Expression.prototype.multiply = function(a) {
    var copy = this.copy();

    copy.coefficient = copy.coefficient.multiply(a);
    copy.constant = copy.constant.multiply(a);

    return copy;
};

Expression.prototype.divide = function(a) {
    if (a == 0) {
        return "stahp";
    }

    var copy = this.copy();

    copy.coefficient = copy.coefficient.divide(a);
    copy.constant = copy.constant.divide(a);
    return copy;
};

Expression.prototype.evaluateAt = function(constant) {
    var x = this.coefficient.multiply(constant);
    return x.add(this.constant);
};

Expression.prototype.print = function() {
    this.coefficient = this.coefficient.reduce();
    this.constant = this.constant.reduce();

    var s = "";

    if (this.coefficient.decimal() == 1) {
        s += this.variable;
    } else {
        s += this.coefficient.print() + this.variable;
    }

    if (this.constant.numer) {
        s += " " + (this.constant.numer > 0 ? "+ " : "- ") + this.constant.abs().print();
    }

    return s;
};

Expression.prototype.tex = function(type) {
    this.coefficient = this.coefficient.reduce();
    this.constant = this.constant.reduce();

    var s = "";

    if (this.coefficient.decimal() == 1) {
        s += this.variable;
    } else {
        s += this.coefficient.tex() + this.variable;<commit_msg>Leave off variable term in printing of expression if the coefficient is 0<commit_after>var Fraction = require('./fractions');

var Expression = function(variable) {
    this.variable = variable;
    this.coefficient = new Fraction(1, 1);
    this.constant = new Fraction(0, 1);
};

Expression.prototype.copy = function() {
    var ex = new Expression(this.variable);
    ex.coefficient = this.coefficient;
    ex.constant = this.constant;
    return ex;
};

Expression.prototype.add = function(a) {
    var copy = this.copy();

    if (a instanceof Expression) {
        if (copy.variable == a.variable) {
            copy.coefficient = copy.coefficient.add(a.coefficient);
            copy.constant = copy.constant.add(a.constant);
        }
    } else {
        copy.constant = copy.constant.add(a);
    }

    return copy;
};

Expression.prototype.subtract = function(a) {
    var copy = this.copy();

    if (a instanceof Expression) {
        if (copy.variable == a.variable) {
            copy.coefficient = copy.coefficient.subtract(a.coefficient);
            copy.constant = copy.constant.subtract(a.constant);
        }
    } else {
        copy.constant = copy.constant.subtract(a);
    }

    return copy;
};

Expression.prototype.multiply = function(a) {
    var copy = this.copy();

    copy.coefficient = copy.coefficient.multiply(a);
    copy.constant = copy.constant.multiply(a);

    return copy;
};

Expression.prototype.divide = function(a) {
    if (a == 0) {
        return "stahp";
    }

    var copy = this.copy();

    copy.coefficient = copy.coefficient.divide(a);
    copy.constant = copy.constant.divide(a);
    return copy;
};

Expression.prototype.evaluateAt = function(constant) {
    var x = this.coefficient.multiply(constant);
    return x.add(this.constant);
};

Expression.prototype.print = function() {
    var coefficient = this.coefficient.reduce();
    var constant = this.constant.reduce();

    if (coefficient.numer == 0 && constant.numer == 0) {
        return "0";
    }

    if (coefficient.numer == 0) {
        return constant.abs().print();
    } else {
        return (coefficient.numer < 0 ? "-" : "") +
               (coefficient.decimal() == 1 ? "" : coefficient.print()) +
               this.variable +
               (constant.numer > 0 ? " + " : " - ") + constant.abs().print();
    }
};

Expression.prototype.tex = function(type) {
    this.coefficient = this.coefficient.reduce();
    this.constant = this.constant.reduce();

    var s = "";

    if (this.coefficient.decimal() == 1) {
        s += this.variable;
    } else {
        s += this.coefficient.tex() + this.variable;
{'fraction_non_alphanumeric': 0.14871995309751807, 'fraction_numerical': 0.004299394176275161, 'mean_word_length': 2.5541666666666667, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5652966', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1549, 'n_words': 309}