math_expressions
A library for parsing and evaluating mathematical expressions, supporting real numbers, vectors, and basic interval arithmetic.
A library for parsing and evaluating mathematical expressions, supporting real numbers, vectors, and basic interval arithmetic.
^7.0.0^2.1.3^6.0.0^1.25.7English project snapshot. Visit GitHub for the latest content.
A library for parsing and evaluating mathematical expressions.
math_expressions is inspired by mathExpr for Java and distributed under the MIT license.
This package contains a very simple command-line interpreter for real numbers:
dart pub run math_expressions:interpreter
Suggestions and pull requests are always welcome!
Below are two basic examples of how to use this library. There also is some additional example code available.
This example shows how to evaluate
$\frac{(x^2+\cos y)}{3}$
for $x=2, y=\pi$
You can either create an mathematical expression programmatically or parse a string.
var x = Variable('x'), y = Variable('y');
var xSquare = Power(x, 2);
var yCos = Cos(y);
var three = Number(3.0);
Expression exp = (xSquare + yCos) / three;
ExpressionParser p = GrammarParser();
Expression exp = p.parse("(x^2 + cos(y)) / 3");
// Bind variables:
var context = ContextModel()
..bindVariableName('x', Number(2.0));
..bindVariableName('y', Number(math.pi));
// Evaluate expression:
var evaluator = RealEvaluator(context);
num eval = evaluator.evaluate(exp);
print(eval) // = 1.0
This example shows how to simplify and differentiate
$x \cdot 1 - (-5)$
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
Here are some other Dart libraries that implement similar functionality to math_expression: parsing and evaluating mathematical expressions.
To the author's knowledge math_expressions is currently the only library supporting interval arithmetics.