fromString static method
Parse MathNode from String
Returns a single MathNode. Throws MathException if parsing fails.
- Set
isMinusNegativeFunctiontotrueto interpret minus operator as a sum of two values, right of which will be negative: X - Y turns to X + (-Y) - Set
isImplicitMultiplicationtofalseto disable implicit multiplication
Define custom variables with variableNames parameter. Don't forget to
define the variable value in MathExpression.calc when calling it.
Define custom functions using customFunctions argument. You can use either
MathCustomFunctions class, which plainly declares the functions, or
MathCustomFunctionsImplemented, which also requires to implement the
function. When you use MathCustomFunctionsImplemented during parsing,
you don't need to redeclare the function in MathExpression.calc.
Parse priority:
- Parentheses () []
- Variables: e, pi (π) and custom ones.
xis 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 or _, others - letters, digits, period,' or underscore. Last symbol can't be a period. 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. Custom functions have the same requirements, except they can override built-in functions. - Functions (case-sensitive):
- Custom functions
- 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 (+)
Implementation
static 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'},
/// Expressions which should be marked as functions
MathCustomFunctions customFunctions = const MathCustomFunctions({}),
}) {
for (final e in variableNames) {
_checkVariableName(e);
}
for (final e in customFunctions.definitions) {
_checkVariableName(e.name, isFunction: true);
if (e.minArgumentsCount < 0 ||
e.maxArgumentsCount < e.minArgumentsCount) {
throw InvalidFunctionArgumentsDeclaration(e.toString());
}
if (variableNames.contains(e.name)) {
throw DuplicateDeclarationException(e.toString());
}
}
final variableNamesList = [
...customFunctions.definitions.map<String>((e) => e.name),
..._variableBuiltIns,
..._bracketFuncs,
...variableNames,
];
variableNamesList.sort((a, b) => b.length.compareTo(a.length));
String variableNamesRegexPart =
variableNamesList.map((e) => RegExp.escape(e)).join('|');
if (variableNamesRegexPart.isNotEmpty) {
variableNamesRegexPart += '|';
}
return _parseMathString(
expression,
isMinusNegativeFunction: isMinusNegativeFunction,
isImplicitMultiplication: isImplicitMultiplication,
variableNames: variableNames,
customFunctions: customFunctions,
regexp: RegExp(
r'(' + variableNamesRegexPart + r'(\d+(\.\d+)?)|\+|-|\^|/|\*)',
),
);
}