Skip to content

Commit

Permalink
Fixed substitution method.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewJA committed Jun 24, 2014
1 parent 59dcb02 commit 2d95dae
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
33 changes: 29 additions & 4 deletions coffeequate/build/coffeequate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2652,7 +2652,7 @@ define("lib/almond", function(){});
evaluateSymbolicConstants = false;
}
for (variable in substitutions) {
if (!(substitutions[variable] instanceof terminals.Terminal || substitutions[variable] instanceof nodes.BasicNode)) {
if (substitutions[variable].copy == null) {
substitutions[variable] = new terminals.Constant(substitutions[variable]);
}
}
Expand Down Expand Up @@ -3355,7 +3355,7 @@ define("lib/almond", function(){});
evaluateSymbolicConstants = false;
}
for (variable in substitutions) {
if (!(substitutions[variable] instanceof terminals.Terminal || substitutions[variable] instanceof nodes.BasicNode)) {
if (substitutions[variable].copy == null) {
substitutions[variable] = new terminals.Constant(substitutions[variable]);
}
}
Expand Down Expand Up @@ -3956,7 +3956,7 @@ define("lib/almond", function(){});
evaluateSymbolicConstants = false;
}
for (variable in substitutions) {
if (!(substitutions[variable] instanceof terminals.Terminal || substitutions[variable] instanceof nodes.BasicNode)) {
if (substitutions[variable].copy == null) {
substitutions[variable] = new terminals.Constant(substitutions[variable]);
}
}
Expand Down Expand Up @@ -4253,7 +4253,7 @@ define("lib/almond", function(){});
function Expression(val) {
if (val instanceof String || typeof val === "string") {
this.expr = parse.stringToExpression(val);
} else if (val instanceof nodes.BasicNode) {
} else if (val.copy != null) {
this.expr = val.copy();
} else {
throw new Error("Unknown argument: `" + val + "'.");
Expand All @@ -4264,6 +4264,31 @@ define("lib/almond", function(){});
return this.expr.toString();
};

Expression.prototype.solve = function(variable) {
return this.expr.solve(variable);
};

Expression.prototype.sub = function(substitutions) {
var key, newsubs;
newsubs = {};
for (key in substitutions) {
if (substitutions[key] instanceof Expression) {
newsubs[key] = substitutions[key].expr;
} else {
newsubs[key] = substitutions[key];
}
}
return this.expr.sub(newsubs, null, null).simplify();
};

Expression.prototype.copy = function() {
return new Expression(this.expr.copy());
};

Expression.prototype.simplify = function() {
return this.expr.simplify();
};

return Expression;

})();
Expand Down
4 changes: 2 additions & 2 deletions coffeequate/build/coffeequate.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion coffeequate/compile.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coffee -c .
node ./src/lib/r.js -o ./src/build.js
node ./src/lib/r.js -o ./src/build-min.js
node ./src/lib/r.js -o ./src/build-min.js
16 changes: 14 additions & 2 deletions coffeequate/src/Expression.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define ["parse", "nodes"], (parse, nodes) ->
if val instanceof String or typeof val == "string"
# The string we pass in is just a representation to parse.
@expr = parse.stringToExpression(val)
else if val instanceof nodes.BasicNode
else if val.copy?
@expr = val.copy()
else
throw new Error("Unknown argument: `#{val}'.")
Expand All @@ -22,7 +22,19 @@ define ["parse", "nodes"], (parse, nodes) ->
sub: (substitutions) ->
# TODO: Uncertainties, equivalencies, options.
# TODO: Seems that the way I implemented substituting expressions was different last time for no real reason. Fix.
@expr.sub(substitutions, null, null).simplify()

# If there are any Expressions in here, we should remove them.
newsubs = {}
for key of substitutions
if substitutions[key] instanceof Expression
newsubs[key] = substitutions[key].expr
else
newsubs[key] = substitutions[key]

return @expr.sub(newsubs, null, null).simplify()

copy: ->
new Expression(@expr.copy())

simplify: ->
@expr.simplify()
Expand Down
2 changes: 1 addition & 1 deletion coffeequate/src/operators/Add.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ define [

# Interpret substitutions.
for variable of substitutions
unless substitutions[variable] instanceof terminals.Terminal or substitutions[variable] instanceof nodes.BasicNode
unless substitutions[variable].copy? # All nodes and terminals should implement this.
substitutions[variable] = new terminals.Constant(substitutions[variable])

unless equivalencies?
Expand Down
2 changes: 1 addition & 1 deletion coffeequate/src/operators/Mul.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ define [

# Interpret substitutions.
for variable of substitutions
unless substitutions[variable] instanceof terminals.Terminal or substitutions[variable] instanceof nodes.BasicNode
unless substitutions[variable].copy?
substitutions[variable] = new terminals.Constant(substitutions[variable])

unless equivalencies?
Expand Down
2 changes: 1 addition & 1 deletion coffeequate/src/operators/Pow.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ define [

# Interpret substitutions.
for variable of substitutions
unless substitutions[variable] instanceof terminals.Terminal or substitutions[variable] instanceof nodes.BasicNode
unless substitutions[variable].copy?
substitutions[variable] = new terminals.Constant(substitutions[variable])

unless equivalencies?
Expand Down

0 comments on commit 2d95dae

Please sign in to comment.