"examples/vscode:/vscode.git/clone" did not exist on "82703dff7395dbc80d320af2d101d3ea530d2a25"
Unverified Commit df52ff73 authored by peastman's avatar peastman Committed by GitHub
Browse files

Merge pull request #2312 from peastman/sqrt

Algebraic optimizations
parents 5a244b5c 0bf7b063
...@@ -271,6 +271,16 @@ ExpressionTreeNode ParsedExpression::substituteSimplerExpression(const Expressio ...@@ -271,6 +271,16 @@ ExpressionTreeNode ParsedExpression::substituteSimplerExpression(const Expressio
return ExpressionTreeNode(new Operation::MultiplyConstant(-dynamic_cast<const Operation::MultiplyConstant*>(&node.getOperation())->getValue()), children[0].getChildren()[0]); return ExpressionTreeNode(new Operation::MultiplyConstant(-dynamic_cast<const Operation::MultiplyConstant*>(&node.getOperation())->getValue()), children[0].getChildren()[0]);
break; break;
} }
case Operation::SQRT:
{
if (children[0].getOperation().getId() == Operation::SQUARE) // sqrt(square(x)) = abs(x)
return ExpressionTreeNode(new Operation::Abs(), children[0].getChildren()[0]);
}
case Operation::SQUARE:
{
if (children[0].getOperation().getId() == Operation::SQRT) // square(sqrt(x)) = x
return children[0].getChildren()[0];
}
default: default:
{ {
// If operation ID is not one of the above, // If operation ID is not one of the above,
......
...@@ -266,6 +266,8 @@ int main() { ...@@ -266,6 +266,8 @@ int main() {
verifyEvaluation("select(x, 1.0, y)", 0.3, 2.0, 1.0); verifyEvaluation("select(x, 1.0, y)", 0.3, 2.0, 1.0);
verifyEvaluation("select(x, 1.0, y)", 0.0, 2.0, 2.0); verifyEvaluation("select(x, 1.0, y)", 0.0, 2.0, 2.0);
verifyEvaluation("atan2(x, y)", 3.0, 1.5, std::atan(2.0)); verifyEvaluation("atan2(x, y)", 3.0, 1.5, std::atan(2.0));
verifyEvaluation("sqrt(x^2)", -2.2, 0.0, 2.2);
verifyEvaluation("sqrt(x)^2", 2.2, 0.0, 2.2);
verifyInvalidExpression("1..2"); verifyInvalidExpression("1..2");
verifyInvalidExpression("1*(2+3"); verifyInvalidExpression("1*(2+3");
verifyInvalidExpression("5++4"); verifyInvalidExpression("5++4");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment