use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class TestParser method testCommutativeOperators.
/**
* Tests commutative operators
*/
@Test
public void testCommutativeOperators() {
ExpressionParser parser = new ExpressionParser();
Expression expression = parse(parser, "1 + 2 + 3");
assertTrue(expression.evaluate(new TestingContext()) == 6.0);
expression = parse(parser, "1 + 1 + 1 + (3 * 4)");
assertTrue(expression.evaluate(new TestingContext()) == 15.0);
expression = parse(parser, "1 * 1 * 2 * 3 * (1 + 1)");
assertTrue(expression.evaluate(new TestingContext()) == 12.0);
}
use of edu.cmu.tetrad.calculator.parser.ExpressionParser in project tetrad by cmu-phil.
the class TestParser method testBasicExpressions.
/**
* Tests expressions without variables (mainly used while writing the parser)
*/
@Test
public void testBasicExpressions() {
ExpressionParser parser = new ExpressionParser();
Expression expression = parse(parser, "+(1,1)");
assertTrue(expression.evaluate(new TestingContext()) == 2.0);
expression = parse(parser, "*(+(1,2), 5)");
assertTrue(expression.evaluate(new TestingContext()) == 15.0);
expression = parse(parser, "1 + 2.5");
assertTrue(expression.evaluate(new TestingContext()) == 3.5);
expression = parse(parser, "(2 + 3)");
assertTrue(expression.evaluate(new TestingContext()) == 5.0);
expression = parse(parser, "1 + (3 + 4)");
assertTrue(expression.evaluate(new TestingContext()) == 8.0);
expression = parse(parser, "1 + 2 + 5");
assertTrue(expression.evaluate(new TestingContext()) == 8.0);
expression = parse(parser, "1 + (2 * 3)");
assertTrue(expression.evaluate(new TestingContext()) == 7.0);
expression = parse(parser, "1 + (2 + (3 * 4))");
assertTrue(expression.evaluate(new TestingContext()) == 15.0);
expression = parse(parser, "(2 * 3) + (4 * 5)");
assertTrue(expression.evaluate(new TestingContext()) == 26.0);
expression = parse(parser, "((2 * 3) + (1 + (2 + (3 * 4))))");
assertTrue(expression.evaluate(new TestingContext()) == 21.0);
expression = parse(parser, "pow(2,3)");
assertTrue(expression.evaluate(new TestingContext()) == 8.0);
expression = parse(parser, "sqrt(pow(2,2))");
assertTrue(expression.evaluate(new TestingContext()) == 2.0);
expression = parse(parser, ConstantExpression.E.getName());
assertTrue(expression.evaluate(new TestingContext()) == ConstantExpression.E.evaluate(null));
expression = parse(parser, ConstantExpression.PI.getName());
assertTrue(expression.evaluate(new TestingContext()) == ConstantExpression.PI.evaluate(null));
expression = parse(parser, ConstantExpression.PI.getName() + "+ 2");
assertTrue(expression.evaluate(new TestingContext()) == Math.PI + 2);
}
Aggregations