math_parser 1.1.0 math_parser: ^1.1.0 copied to clipboard
Process math expressions, convert them to machine-readable form, and calculate them.
Math Parser for Dart #
Process math expressions, convert them to machine-readable form, and calculate them.
Math Tree #
The library provides a family of MathNode
classes, most of
them have subnodes that are being calculated recursively.
There are such types of MathNode:
MathFunction
(andMathFunctionWithTwoArguments
subclass)MathValue
MathOperator
All the child classes names begin with the family they belong to.
Evaluation #
You can evaluate a MathNode and its subnodes recursively by calling
MathNode.calc(num x)
and passing custom x
variable value.
Example: Calculate x + 3
, where x = 5
.
MathOperatorAddition(
MathFunctionX(),
const MathValue(3),
).calc(5);
Parsing String to MathNode #
The library can parse general mathematical expressions strings
and return them as a machine-readable MathNode
using
MathNodeExpression.fromString
method.
- Set
isMinusNegativeFunction
totrue
to interpret minus operator as a sum of two values, right of which will be negative: X - Y turns to X + (-Y) - Set
isImplicitMultiplication
tofalse
to disable implicit multiplication
Parse priority: #
- Parentheses () []
- Variables: e, pi (π) and custom ones.
x
is being interpreted as a var by default, but you can override this behavior with the variableNames parameter. You can rewrite e and pi by defining it in variableNames and mentioning it during the calc call. First character must be a letter, others - letters, digits, or underscore. Letters may be latin or Greek, both lower or capital case. You can't use built-in function names like sin, cos, etc. Variable names are case-sensitive - Functions (case-sensitive):
- sin, cos, tan (tg), cot (ctg)
- sqrt (√) (interpreted as power of 1/2), complex numbers not supported
- ln (base=E), lg (base=2), log[base](x)
- asin (arcsin), acos (arccos), atan (arctg), acot (arcctg)
- Unary minus (-) at the beginning of a block
- Power (x^y)
- Implicit multiplication (two MathNodes put near without operator between)
- Division (/) & Multiplication (*)
- Subtraction (-) & Addition (+)
MathNode fromString(
/// The expression to convert
String expression, {
/// Converts all X - Y to X + (-Y)
bool isMinusNegativeFunction = false,
/// Allows skipping the multiplication (*) operator
bool isImplicitMultiplication = true,
/// Expressions which should be marked as variables
Set<String> variableNames = const {'x'},
});
Example for parsing a string and evaluating it with x = 20
:
final expression = MathNodeExpression.fromString(
'(2x)^(e^3 + 4)',
);
print(expression.calc(MathVariableValues.x(20)));
More complicated work with variables is shown off in example.
Other Features #
Numerical methods for Definite Integrals #
You can calculate a given node as a definite integral using
the MathNodeDefiniteIntegral
extension. All methods have
the same interface:
num definiteIntegralByLeftRectangles(
/// Precision
int n,
num lowerLimit,
num upperLimit,
);