math_expressions 0.0.4 math_expressions: ^0.0.4 copied to clipboard
A math library for parsing and evaluating expressions in real, interval and vector contexts. Also supports simplification and differentiation.
math_expressions #
A math library for parsing and evaluating expressions in real, interval and vector contexts. Also supports simplification and differentiation.
It is partly inspired by mathExpr for Java.
Note: This library is still in an early state, the test coverage is not perfect, the performance is not optimized and some features are still unimplemented. Basic REAL and INTERVAL evaluations should work though. Suggestions and pull requests are always welcome!
Features #
- Parsing, simplification and differantiation of mathematical expressions.
- Evaluation of expressions in various modes (Real, Vector, Interval).
- Supporting most basic functions out of the box.
- Well documented.
What's not working yet? #
- Evaluations in vector space.
- Composite functions.
- Parser is a little rough.
Documentation #
See DartDoc.
Examples #
1. Expression creation and evaluation #
This example shows how to evaluate
for
Build the expression
You can either create an mathematical expression programmatically or parse a string.
- Create the expression programmatically:
Variable x = new Variable('x'), y = new Variable('y');
Power xSquare = new Power(x, 2);
Cos yCos = new Cos(y);
Number three = new Number(3.0);
Expression exp = (xSquare + yCos) / three;
- Create the expression via the parser:
Parser p = new Parser();
Expression exp = p.parse("(x^2 + cos(y)) / 3");
Evaluate the expression
- Bind variables and evaluate the expression as real number:
// Bind variables:
ContextModel cm = new ContextModel();
cm.bindGlobalVariable(x, new Number(2.0));
cm.bindGlobalVariable(y, new Number(Math.PI));
// Evaluate expression:
double eval = exp.evaluate(EvaluationType.REAL, cm);
print(eval) // = 1.0
2. Expression simplification and differentiation #
This example shows how to simplify and differentiate
- Expressions can be simplified and differentiated with respect to a given variable:
Expression exp = p.parse("x*1 - (_5)");
print(exp); // = ((x * 1.0) - -(5.0))
print(exp.simplify()); // = (x + 5.0)
Expression expDerived = exp.derive('x');
print(expDerived); // = (((x * 0.0) + (1.0 * 1.0)) - -(0.0))
print(expDerived.simplify()); // = 1.0
For a CLI evaluator, see cli_evaluator.dart.
For more code, see example.dart.
math_expressions is distributed under the MIT license as described in the LICENSE file.