math_parser library
Parse and evaluate mathematical expressions
A simple library done for tasks like function evaluation
Look at MathNodeExpression's fromString
method for more info about
how parsing works.
Parsing TL;DR
Predefined list of variables
import 'package:math_parser/math_parser.dart';
void main() {
final expression = MathNodeExpression.fromString(
'(2x)^(e^3 + 4) + y',
variableNames: {'x', 'y'},
).calc(
MathVariableValues({'x': 20, 'y': 10}),
);
}
Autodetect variables
Implicit multiplication (writing 2x
instead of 2*x
) is not supported for auto-detecting variables.
Trying to use auto-detection on expressions with implicit multiplication may cause a CantProcessExpressionException
during parsing or unexpected parsing results.
import 'dart:io';
import 'package:math_parser/math_parser.dart';
void main() {
final stringExpression = '(2*x)^(e^3 + 4) + y';
print('Expression: $stringExpression');
final definable = MathNodeExpression.getPotentialDefinable(
stringExpression,
hideBuiltIns: true,
);
final expression = MathNodeExpression.fromString(
stringExpression,
variableNames: definable.variables,
isImplicitMultiplication: false,
);
// Ask user to define variables
final variableValues = <String, double>{};
for (final variable in definable.variables) {
print('Enter value for $variable:');
final double value = double.parse(
stdin.readLineSync() as String,
);
variableValues[variable] = value;
}
final result = expression.calc(
MathVariableValues(variableValues),
);
print('Result: $result');
}
Classes
- MathComparison
- A parent class for comparisons
- MathComparisonEquation
- Equation
- MathComparisonGreater
- If Greater Comparison
- MathComparisonGreaterOrEquals
- If Greater or Equals Comparison
- MathComparisonLess
- If Less Comparison
- MathComparisonLessOrEquals
- If Less or Equals Comparison
- MathCustomFunctions
- Set of custom functions
- MathCustomFunctionsImplemented
- Set of custom implemented functions
- MathDefinitionFunctionFreeform
- Create a new freeform function
- MathDefinitionFunctionFreeformImplemented
- Create a new freeform function with implementation
- MathExpression
- Any Math-related object
- MathFunction
- Mathematical function
- MathFunctionAcos
- The acos Function
- MathFunctionAcot
- The acot Function
- MathFunctionAsin
- The asin Function
- MathFunctionAtan
- The atan Function
- MathFunctionCos
- The cos Function
- MathFunctionCot
- The cot Function
- MathFunctionE
- The Exponent in power constant
- MathFunctionFreeform
- Freeform function
- MathFunctionLn
- The ln Function
- MathFunctionLog
- The log Function
- MathFunctionNegative
- Negative value
- MathFunctionSin
- The sin Function
- MathFunctionTan
- The tan Function
- MathFunctionWithTwoArguments
- Mathematical function with two arguments
- MathNode
- Basic math expression unit
- MathOperator
- Mathematical operator
- MathOperatorAddition
- Addition operator (+)
- MathOperatorDivision
- Division operator (/)
- MathOperatorMultiplication
- Multiplication operator (*)
- MathOperatorPower
- The power operation
- MathOperatorSubtraction
- Subtraction operator (-)
- MathValue
- Constant value
- MathValuePi
- The Pi constant
- MathVariable
- A math variable
- MathVariableValues
- Variables dictionary
Extensions
- MathNodeExpression on MathExpression
- Math Expression Parser Extension
Exceptions / Errors
- BracketsNotClosedException
- Brackets Not Closed Exception
- CantProcessExpressionException
- Cant Process Expression Exception
- DuplicateDeclarationException
- Duplicate Declaration Exception
- InvalidFunctionArgumentsDeclaration
- Invalid Function Arguments Declaration
- InvalidFunctionNameException
- Invalid Function Name Exception
- InvalidVariableNameException
- Invalid Variable Name Exception
- MathException
- Math parsing error
- MathParseException
- Math Parse Exception
- MissingOperatorOperandException
- Missing Operator Operand Exception
- OutOfRangeFunctionArgumentListException
- Out Of Range Function Argument List Exception
- ParsingFailedException
- Parsing Failed Exception
- UndefinedFunctionException
- Undefined Function Exception
- UndefinedVariableException
- Undefined Variable Exception
- UnexpectedClosingBracketException
- Unexpected Closing Bracket Exception
- UnknownOperationException
- Unknown Operation Exception