Skip to content

Commit ffa0732

Browse files
committed
Fix docs
1 parent 9cadf9c commit ffa0732

File tree

6 files changed

+222
-96
lines changed

6 files changed

+222
-96
lines changed

CHANGELOG.md

+60-55
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,101 @@
1+
# 1.5.1
2+
3+
- Increased upper SDK constraint to declare support for Dart 3
4+
- Documentation fixes
5+
16
# 1.5.0
27

38
## Breaking changes
49

5-
- Now the `'` symbol is allowed for variable and function names so you can have variables like `y'`.
10+
- Now the `'` symbol is allowed for variable and function names so you can have variables like `y'`.
611

712
## Equations
813

9-
- Implemented `>=` and `<=`.
14+
- Implemented `>=` and `<=`.
1015

1116
# 1.4.0
1217

1318
## Custom Functions
1419

15-
- Define custom functions and redefine built-in functions when parsing
16-
an expression. See docs and example.
17-
- Use `MathNodeExpression.getPotentialFunctionNames()` to detect potentially
18-
used functions in a string.
19-
- Use period in the middle of custom variable and function names.
20-
- Under-hood, functions now support multiple comma separated arguments, so
21-
you can supply multiple arguments to your custom function.
22-
- Detect custom functions im math tree using
23-
`MathExpression.getUsedFreeformFunctions()`.
20+
- Define custom functions and redefine built-in functions when parsing
21+
an expression. See docs and example.
22+
- Use `MathNodeExpression.getPotentialFunctionNames()` to detect potentially
23+
used functions in a string.
24+
- Use period in the middle of custom variable and function names.
25+
- Under-hood, functions now support multiple comma separated arguments, so
26+
you can supply multiple arguments to your custom function.
27+
- Detect custom functions im math tree using
28+
`MathExpression.getUsedFreeformFunctions()`.
2429

2530
## Breaking Changes
2631

27-
- `MissingFunctionArgumentListException` renamed to
28-
`OutOfRangeFunctionArgumentListException`
29-
- `MathNodeExpression.fromString()` may throw other errors besides
30-
`MathException`
31-
- `MathNodeExpression.getPotentialVariableNames()` is replaced by
32-
`MathNodeExpression.getPotentialDefinable()`
33-
- Instead of `log[base](arg)`, you should pass `log(base, arg)` syntax now
34-
- Period is an allowed character in the middle of a variable name now
32+
- `MissingFunctionArgumentListException` renamed to
33+
`OutOfRangeFunctionArgumentListException`
34+
- `MathNodeExpression.fromString()` may throw other errors besides
35+
`MathException`
36+
- `MathNodeExpression.getPotentialVariableNames()` is replaced by
37+
`MathNodeExpression.getPotentialDefinable()`
38+
- Instead of `log[base](arg)`, you should pass `log(base, arg)` syntax now
39+
- Period is an allowed character in the middle of a variable name now
3540

3641
## Misc.
3742

38-
- `UnexpectedClosingBracketException` and `BracketsNotClosedException` can
39-
now tell where the problem probably happened.
40-
- New MathParseException's `InvalidFunctionNameException`,
41-
`DuplicateDeclarationException`, `InvalidFunctionArgumentsDeclaration`.
43+
- `UnexpectedClosingBracketException` and `BracketsNotClosedException` can
44+
now tell where the problem probably happened.
45+
- New MathParseException's `InvalidFunctionNameException`,
46+
`DuplicateDeclarationException`, `InvalidFunctionArgumentsDeclaration`.
4247

4348
# 1.3.1
4449

45-
- Variable validation fix
50+
- Variable validation fix
4651

4752
# 1.3.0
4853

4954
## Math Tree
5055

51-
- Important change: `MathNode` is now a class of `MathExpression` interface.
52-
Compared to MathNode, MathExpression may return null in `calc()` method.
53-
- New: `getUsedVariables()` method for `MathExpression` and `MathNode`.
54-
This method goes down the math tree to find any uses of `MathVariable`
55-
and returns names of all variables.
56-
- New: `MathExpression` object family - `MathComparison`:
57-
- `MathComparisonEquation` (=)
58-
- `MathComparisonGreater` (>)
59-
- `MathComparisonLess` (<)
56+
- Important change: `MathNode` is now a class of `MathExpression` interface.
57+
Compared to MathNode, MathExpression may return null in `calc()` method.
58+
- New: `getUsedVariables()` method for `MathExpression` and `MathNode`.
59+
This method goes down the math tree to find any uses of `MathVariable`
60+
and returns names of all variables.
61+
- New: `MathExpression` object family - `MathComparison`:
62+
- `MathComparisonEquation` (=)
63+
- `MathComparisonGreater` (>)
64+
- `MathComparisonLess` (<)
6065

6166
## Parsing
6267

63-
- New: `MathNodeExpression.fromStringExtended()` method allows you to
64-
interpret equations and comparisons. Compared to `fromString`,
65-
it returns `MathExpression` instead of `MathNode`, since comparisons
66-
can't guarantee result.
67-
- New: `MathNodeExpression.getPotentialVariableNames()` analyzes given
68-
math expression string for possible use of variables. Refer to
69-
documentation for rough edges before using it.
70-
- New: `MathNodeExpression.builtInVariables` gives a list of built-in
71-
predefined variable names.
72-
- New: `MathNodeExpression.isVariableNameValid()` lets you check if
73-
the parser can work with a given name.
68+
- New: `MathNodeExpression.fromStringExtended()` method allows you to
69+
interpret equations and comparisons. Compared to `fromString`,
70+
it returns `MathExpression` instead of `MathNode`, since comparisons
71+
can't guarantee result.
72+
- New: `MathNodeExpression.getPotentialVariableNames()` analyzes given
73+
math expression string for possible use of variables. Refer to
74+
documentation for rough edges before using it.
75+
- New: `MathNodeExpression.builtInVariables` gives a list of built-in
76+
predefined variable names.
77+
- New: `MathNodeExpression.isVariableNameValid()` lets you check if
78+
the parser can work with a given name.
7479

7580
## Misc.
7681

77-
- Changed input parameters type for `CantProcessExpressionException`.
78-
- Small documentation fixes.
82+
- Changed input parameters type for `CantProcessExpressionException`.
83+
- Small documentation fixes.
7984

8085
# 1.2.0
8186

82-
- Fix README.
83-
- Moved integrating features to a separate package library
84-
`math_parser_integrate`.
87+
- Fix README.
88+
- Moved integrating features to a separate package library
89+
`math_parser_integrate`.
8590

8691
# 1.1.0
8792

88-
- Custom variables support.
89-
- `MathFunctionX`deprecated.
90-
- `MathVariable` introduced.
91-
- You need to pass an instance of `MathVariableValues` instead of a num
92-
to the `calc()` function now.
93+
- Custom variables support.
94+
- `MathFunctionX`deprecated.
95+
- `MathVariable` introduced.
96+
- You need to pass an instance of `MathVariableValues` instead of a num
97+
to the `calc()` function now.
9398

9499
# 1.0.0
95100

96-
- Initial version.
101+
- Initial version.

README.md

+83-23
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,98 @@ parts of equations and other forms of simple math
88
expressions in your projects. This package supports custom
99
variables too.
1010

11+
## TL;DR How to parse and calculate an expression
12+
13+
### Predefined list of variables
14+
15+
```dart
16+
import 'package:math_parser/math_parser.dart';
17+
18+
void main() {
19+
final expression = MathNodeExpression.fromString(
20+
'(2x)^(e^3 + 4) + y',
21+
variableNames: {'x', 'y'},
22+
).calc(
23+
MathVariableValues({'x': 20, 'y': 10}),
24+
);
25+
}
26+
```
27+
28+
### Autodetect variables
29+
30+
Implicit multiplication (writing `2x` instead of `2*x`) is not supported for auto-detecting variables.
31+
Trying to use auto-detection on expressions with implicit multiplication may cause a `CantProcessExpressionException` during parsing or unexpected parsing results.
32+
33+
```dart
34+
import 'dart:io';
35+
import 'package:math_parser/math_parser.dart';
36+
37+
void main() {
38+
final stringExpression = '(2*x)^(e^3 + 4) + y';
39+
print('Expression: $stringExpression');
40+
41+
final definable = MathNodeExpression.getPotentialDefinable(
42+
stringExpression,
43+
hideBuiltIns: true,
44+
);
45+
46+
final expression = MathNodeExpression.fromString(
47+
stringExpression,
48+
variableNames: definable.variables,
49+
isImplicitMultiplication: false,
50+
);
51+
52+
// Ask user to define variables
53+
final variableValues = <String, double>{};
54+
for (final variable in definable.variables) {
55+
print('Enter value for $variable:');
56+
final double value = double.parse(
57+
stdin.readLineSync() as String,
58+
);
59+
variableValues[variable] = value;
60+
}
61+
62+
final result = expression.calc(
63+
MathVariableValues(variableValues),
64+
);
65+
66+
print('Result: $result');
67+
}
68+
```
69+
1170
## Features: In Short
12-
For more details about these features, refer to documentation,
13-
this readme or example file. All public API elements are
71+
72+
For more details about these features, refer to documentation,
73+
this readme or example file. All public API elements are
1474
documented.
1575

16-
- Parse mathematical expressions using
17-
`MathNodeExpression.fromString` or equations using
18-
`MathNodeExpression.fromStringExtended`.
19-
- Define custom variables and functions by passing
20-
`variableNames` and `customFunctions` parameters. To define
21-
a custom function, you'll have to implement the
22-
`MathDefinitionFunctionFreeformImplemented` interface for
23-
each such function.
24-
- Automatically detect possible variable and function names used in
25-
an expression, but this works reliably only with implicit
26-
multiplication off.
76+
- Parse mathematical expressions using
77+
`MathNodeExpression.fromString` or equations using
78+
`MathNodeExpression.fromStringExtended`.
79+
- Define custom variables and functions by passing
80+
`variableNames` and `customFunctions` parameters. To define
81+
a custom function, you'll have to implement the
82+
`MathDefinitionFunctionFreeformImplemented` interface for
83+
each such function.
84+
- Automatically detect possible variable and function names used in
85+
an expression, but this works reliably only with implicit
86+
multiplication off.
2787

28-
## Math Tree
88+
## Advanced use: Math Tree
2989

3090
The library provides a family of `MathExpression` and
3191
`MathNode` classes, most of them have subnodes that are being
3292
calculated recursively.
3393

3494
There are such types of MathNode:
3595

36-
- `MathFunction` (and `MathFunctionWithTwoArguments` subclass)
37-
- `MathValue`
38-
- `MathOperator`
96+
- `MathFunction` (and `MathFunctionWithTwoArguments` subclass)
97+
- `MathValue`
98+
- `MathOperator`
3999

40100
Types of `MathExpression`:
41101

42-
- `MathComparison`
102+
- `MathComparison`
43103

44104
All the child classes names begin with the family they belong to.
45105

@@ -90,11 +150,11 @@ you don't need to redeclare the function in `MathExpression.calc`.
90150
functions have the same requirements, except they can override built-in
91151
functions.
92152
3. Functions (case-sensitive):
93-
- Custom functions
94-
- sin, cos, tan (tg), cot (ctg)
95-
- sqrt (√) (interpreted as power of 1/2), complex numbers not supported
96-
- ln (base=E), lg (base=2), log\[base\]\(x\)
97-
- asin (arcsin), acos (arccos), atan (arctg), acot (arcctg)
153+
- Custom functions
154+
- sin, cos, tan (tg), cot (ctg)
155+
- sqrt (√) (interpreted as power of 1/2), complex numbers not supported
156+
- ln (base=E), lg (base=2), log\[base\]\(x\)
157+
- asin (arcsin), acos (arccos), atan (arctg), acot (arcctg)
98158
4. Unary minus (-) at the beginning of a block
99159
5. Power (x^y)
100160
6. Implicit multiplication (two MathNodes put near without operator between)

analysis_options.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
include: package:lints/recommended.yaml
22
analyzer:
3-
strong-mode:
4-
implicit-casts: false
5-
implicit-dynamic: false
63
language:
74
strict-raw-types: true
5+
strict-inference: true
6+
strict-casts: true
87
linter:
98
rules:
109
always_declare_return_types: true
1110
prefer_single_quotes: true
1211
unawaited_futures: true
1312
package_api_docs: true
13+
public_member_api_docs: true
14+
type_annotate_public_apis: true
1415
prefer_final_in_for_each: true
1516
prefer_final_locals: true

lib/math_parser.dart

+56-10
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,66 @@
22
///
33
/// A simple library done for tasks like function evaluation
44
///
5-
/// Example for parsing a string and calculating it with x = 20.
65
/// Look at [MathNodeExpression]'s `fromString` method for more info about
76
/// how parsing works.
87
///
9-
/// ```dart
10-
/// import 'package:math_parser/math_parser.dart';
8+
///## Parsing TL;DR
9+
///### Predefined list of variables
1110
///
12-
/// void main() {
13-
/// final expression = MathNodeExpression.fromString(
14-
/// '(2x)^(e^3 + 4)',
15-
/// );
16-
/// print(expression.calc(20));
17-
/// }
18-
/// ```
11+
///```dart
12+
///import 'package:math_parser/math_parser.dart';
13+
///
14+
///void main() {
15+
/// final expression = MathNodeExpression.fromString(
16+
/// '(2x)^(e^3 + 4) + y',
17+
/// variableNames: {'x', 'y'},
18+
/// ).calc(
19+
/// MathVariableValues({'x': 20, 'y': 10}),
20+
/// );
21+
///}
22+
///```
23+
///
24+
///### Autodetect variables
25+
///
26+
///Implicit multiplication (writing `2x` instead of `2*x`) is not supported for auto-detecting variables.
27+
///Trying to use auto-detection on expressions with implicit multiplication may cause a `CantProcessExpressionException` during parsing or unexpected parsing results.
28+
///
29+
///```dart
30+
///import 'dart:io';
31+
///import 'package:math_parser/math_parser.dart';
32+
///
33+
///void main() {
34+
/// final stringExpression = '(2*x)^(e^3 + 4) + y';
35+
/// print('Expression: $stringExpression');
36+
///
37+
/// final definable = MathNodeExpression.getPotentialDefinable(
38+
/// stringExpression,
39+
/// hideBuiltIns: true,
40+
/// );
41+
///
42+
/// final expression = MathNodeExpression.fromString(
43+
/// stringExpression,
44+
/// variableNames: definable.variables,
45+
/// isImplicitMultiplication: false,
46+
/// );
47+
///
48+
/// // Ask user to define variables
49+
/// final variableValues = <String, double>{};
50+
/// for (final variable in definable.variables) {
51+
/// print('Enter value for $variable:');
52+
/// final double value = double.parse(
53+
/// stdin.readLineSync() as String,
54+
/// );
55+
/// variableValues[variable] = value;
56+
/// }
57+
///
58+
/// final result = expression.calc(
59+
/// MathVariableValues(variableValues),
60+
/// );
61+
///
62+
/// print('Result: $result');
63+
///}
64+
///```
1965
library math_parser;
2066

2167
export 'src/math_node.dart';

0 commit comments

Comments
 (0)