Skip to content

Evaluate math expressions to double or apache commons-math Complex, Dfp of BigFraction numbers.

License

Notifications You must be signed in to change notification settings

axkr/symja-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Apache commons-math parser

Implementation of math expressions parsers for Java double values or Apache Commons Math Complex, Dfp or BigFraction numbers.

Evaluate a math expression string

Evaluate a math expression string to a Java double value, with a syntax which doesn't distinguish between upper- and lowercase function names:

	public void testDoubleEvaluatorRelaxed() {
		try {
			DoubleEvaluator engine = new DoubleEvaluator(true);
			double d = engine.evaluate("sin(pi/2*cOs(pi))");
			assertEquals(Double.toString(d), "-1.0");
		} catch (Exception e) {
			e.printStackTrace();
			assertEquals("", e.getMessage());
		}
	}

Evaluate a math expression string to a Java double value, with a syntax which distinguishes between upper- and lowercase function names (syntax is similar to Mathematica):

	public void testDoubleEvaluatorMMA() {
		try {
			DoubleEvaluator engine = new DoubleEvaluator();
			double d = engine.evaluate("Sin[Pi/2*Cos[Pi]]");
			assertEquals(Double.toString(d), "-1.0");
		} catch (Exception e) {
			e.printStackTrace();
			assertEquals("", e.getMessage());
		}
	}

Evaluate a math expression string to a commons-math Complex value, with a syntax which distinguishes between upper- and lowercase function names (syntax is similar to Mathematica):

	public void testComplexEvaluatorMMA() {
		try {
			ComplexEvaluator engine = new ComplexEvaluator();
			Complex c = engine.evaluate("Sin[Pi/2*Cos[Pi]]");
			assertEquals(ComplexEvaluator.toString(c), "-1.0");
		} catch (Exception e) {
			e.printStackTrace();
			assertEquals("", e.getMessage());
		}
	}

Evaluate a math expression string to a commons-math Dfp value, with a syntax which doesn't distinguish between upper- and lowercase function names:

	public void testDfpEvaluatorRelaxed() {
		try {
			DfpEvaluator engine = new DfpEvaluator(50, true);
			Dfp d = engine.evaluate("sin(pi/2*cOs(pi))");
			assertEquals(d.toString(), "-1.");
		} catch (Exception e) {
			e.printStackTrace();
			assertEquals("", e.getMessage());
		}
	}

Define variables

An example for defining variables for assigning multiple values:

	public void testDefineVariables() {
		try {
			IDoubleValue vd = new DoubleVariable(3.0);
			DoubleEvaluator engine = new DoubleEvaluator(true);
			engine.defineVariable("X", vd);
			double d = engine.evaluate("X^2+3");
			assertEquals(Double.valueOf(d).toString(), "12.0");
			vd.setValue(4);
			d = engine.evaluate();
			assertEquals(Double.valueOf(d).toString(), "19.0");
		} catch (Exception e) {
			e.printStackTrace();
			assertEquals("", e.getMessage());
		}
	}

Parser AST type hierarchy

Here is a hierarchy overview of the classes, which are generated by the parser to create the internal abstract syntax tree (AST) representation:

java.lang.Object
   |--- org.matheclipse.parser.client.ast.ASTNode
           |--- org.matheclipse.parser.client.ast.FunctionNode
           |--- org.matheclipse.parser.client.ast.NumberNode
           |       |--- org.matheclipse.parser.client.ast.FloatNode
           |       |--- org.matheclipse.parser.client.ast.FractionNode
           |       |--- org.matheclipse.parser.client.ast.IntegerNode
           |--- org.matheclipse.parser.client.ast.PatternNode
           |       |--- org.matheclipse.parser.client.ast.Pattern2Node
           |       |--- org.matheclipse.parser.client.ast.Pattern3Node
           |--- org.matheclipse.parser.client.ast.StringNode
           |--- org.matheclipse.parser.client.ast.SymbolNode
           |--- org.matheclipse.parser.client.eval.ComplexNode
           |--- org.matheclipse.parser.client.eval.DoubleNode
           |--- org.matheclipse.parser.client.eval.bigfraction.BigFractionNode
           |--- org.matheclipse.parser.client.eval.dfp.DfpNode

License

Copyright © 2016 Axel Kramer

Distributed under the Apache License.

About

Evaluate math expressions to double or apache commons-math Complex, Dfp of BigFraction numbers.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published