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 commandline interpreter for real numbers:
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
for
You can either create an mathematical expression programmatically or parse a string.
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;
Parser p = new Parser();
Expression exp = p.parse("(x^2 + cos(y)) / 3");
// Bind variables:
ContextModel cm = new ContextModel();
cm.bindVariable(x, new Number(2.0));
cm.bindVariable(y, new Number(Math.PI));
// Evaluate expression:
double eval = exp.evaluate(EvaluationType.REAL, cm);
print(eval) // = 1.0
This example shows how to simplify and differentiate
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.
vector_math
2.0.0 or newerPoint3D
to Point3
Point3
now is a subtype of Vector3
derive
vector_math
1.4.4 or greatertest
instead of unittest
packagevector_math
as latest pub release is severely outdatede(x)
or e^x
. Consequently, removed support for exp(x)
.5
works now. (by markhats)e^x
additional to exp(x)
. (by markhats)&
to conveniently create a CompositeFunction from two existing
functions: expr = f & g;
2*5
use 2*_5
.example/main.dart
import 'dart:math' as math;
import 'package:math_expressions/math_expressions.dart';
/**
* This file contains three examples:
*  Example 1: Expression creation and evaluation
* (through the Parser and programmatically)
*  Example 2: Expression simplification and differentiation
*  Example 3: Custom function definition and use
*/
void main() {
_expression_creation_and_evaluation();
_expression_simplification_and_differentiation();
_custom_function_definition_and_use();
}
/**
* Example 1: Expression creation and evaluation
*
* How to create an expression (a) via the Parser, (b) programmatically,
* and how to evaluate an expression given a context.
*/
void _expression_creation_and_evaluation() {
print('\nExample 1: Expression creation and evaluation\n');
// You can either create an mathematical expression programmatically or parse
// a string.
// (1a) Parse expression:
Parser p = new Parser();
Expression exp = p.parse('(x^2 + cos(y)) / 3');
// (1b) Build expression: (x^2 + cos(y)) / 3
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);
exp = (xSquare + yCos) / three;
// Bind variables and evaluate the expression as real number.
// (2) Bind variables:
ContextModel cm = new ContextModel()
..bindVariable(x, new Number(2.0))
..bindVariable(y, new Number(math.pi));
// (3) Evaluate expression:
double eval = exp.evaluate(EvaluationType.REAL, cm);
print('Expression: $exp');
print('Evaluated expression: $eval\n (with context: $cm)'); // = 1
}
/**
* Example 2: Expression simplification and differentiation
*
* How to simplify an expression, and how to differentiate it with respect
* to a given variable.
*/
void _expression_simplification_and_differentiation() {
print('\nExample 2: Expression simplification and differentiation\n');
// (1) Parse expression:
Parser p = new Parser();
Expression exp = p.parse('x*1  (5)');
// (2) Simplify expression:
print('Expression: $exp'); // = ((x * 1.0)  (5.0))
print('Simplified expression: ${exp.simplify()}\n'); // = (x + 5.0)
// (2) Differentiate expression with respect to variable 'x':
Expression expDerived = exp.derive('x');
print(
'Differentiated expression: $expDerived'); // = (((x * 0.0) + (1.0 * 1.0))  (0.0))
print(
'Simplified differentiated expression: ${expDerived.simplify()}'); // = 1.0
}
/**
* Example 3: Custom function definition and use
*
* How to create an arbitrary custom function expression and evaluate it.
*/
void _custom_function_definition_and_use() {
print('\nExample 3: Custom function definition and use\n');
// (1) Create and evaluate custom function: DOUBLEUP (R > R)
ContextModel cm = ContextModel();
Variable x = Variable('x');
CustomFunction doubleup = CustomFunction('doubleup', [x], x * new Number(2));
cm.bindVariable(x, Number(0.5));
print('$doubleup = ${doubleup.expression}');
print(
'doubleup(${cm.getExpression('x')}) = ${doubleup.evaluate(EvaluationType.REAL, cm)}\n');
// (1) Create and evaluate custom function: LEFTSHIFT (R² > R)
// Shifting to the left makes the number larger, effectively multiplying the
// number by pow(2, shiftIndex). Custom implementation of x << i.
Variable shiftIndex = Variable('i');
CustomFunction leftshift = new CustomFunction(
'leftshift', [x, shiftIndex], x * new Power(2, shiftIndex));
cm.bindVariable(x, Number(250));
cm.bindVariable(shiftIndex, Number(8));
print('$leftshift = ${leftshift.expression}');
print(
'leftshift(${cm.getExpression('x')}, ${cm.getExpression('i')}) = ${leftshift.evaluate(EvaluationType.REAL, cm)}');
}
Add this to your package's pubspec.yaml file:
dependencies:
math_expressions: ^1.1.1
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter pub get
Alternatively, your editor might support pub get
or flutter pub get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:math_expressions/math_expressions.dart';
Popularity:
Describes how popular the package is relative to other packages.
[more]

83

Health:
Code health derived from static analysis.
[more]

95

Maintenance:
Reflects how tidy and uptodate the package is.
[more]

100

Overall:
Weighted score of the above.
[more]

90

We analyzed this package on Jul 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:math_expressions/math_expressions.dart
.
Fix lib/src/algebra.dart
. (3.93 points)
Analysis of lib/src/algebra.dart
reported 8 hints, including:
line 117 col 7: DO use curly braces for all flow control structures.
line 119 col 7: DO use curly braces for all flow control structures.
line 127 col 7: DO use curly braces for all flow control structures.
line 129 col 7: DO use curly braces for all flow control structures.
line 137 col 7: DO use curly braces for all flow control structures.
Fix lib/src/parser.dart
. (1.49 points)
Analysis of lib/src/parser.dart
reported 3 hints:
line 539 col 28: Use =
to separate a named parameter from its default value.
line 539 col 49: Use =
to separate a named parameter from its default value.
line 539 col 71: Use =
to separate a named parameter from its default value.
Package  Constraint  Resolved  Available 

Direct dependencies  
Dart SDK  >=2.0.0 <3.0.0  
vector_math  ^2.0.0  2.0.8  
Dev dependencies  
pedantic  ^1.4.0  
test  ^1.3.0 