Skip to content

Commit

Permalink
1.1.0 - 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sominemo committed Nov 25, 2021
1 parent 7f6b624 commit ff61b7a
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 113 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## 1.0.0
# 1.2.0

- Fix README
- Moved integrating features to a separate package library `math_parser_integrate`


# 1.1.0

- Custom variables support.
- `MathFunctionX`deprecated.
- `MathVariable` introduced.
- You need to pass an instance of `MathVariableValues` instead of a num to the `calc()` function now


# 1.0.0

- Initial version.
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ 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.
`MathNode.calc(MathVariableValues values)` and passing custom
variable values.

Example: Calculate `x + 3`, where `x = 5`.

```dart
MathOperatorAddition(
MathFunctionX(),
MathVariable('x'),
const MathValue(3),
).calc(5);
).calc(MathVariableValues.x(5));
```

## Parsing String to MathNode
Expand All @@ -43,8 +44,15 @@ and return them as a machine-readable `MathNode` using
### Parse priority:

1. Parentheses () []
2. Variables: x, e, pi (π)
3. Functions:
2. 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
3. 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\)
Expand All @@ -65,19 +73,25 @@ MathNode fromString(
/// 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`:
Example for parsing a string and evaluating it with `x = 20`
and `y = 5`:

```dart
final expression = MathNodeExpression.fromString(
'(2x)^(e^3 + 4)',
'(2x)^(e^3 + 4) + y',
).calc(
MathVariableValues({'x': 20, 'y': 5}),
);
print(expression.calc(20));
```

More complicated work with variables is shown off in example.

## Other Features

### Numerical methods for Definite Integrals
Expand Down
20 changes: 17 additions & 3 deletions example/math_parser_example.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import 'package:math_parser/math_parser.dart';
import 'package:math_parser/integrate.dart';

/// Example function to calculate an expression from string
void main() {
// Parsing string to a MathNode
final expression = MathNodeExpression.fromString(
'((2x)^(e^3 + 4) + cos(3)x) / log[x + 3^2e](2 + (3x)^2)^5 * (2 + x)(x^2 + 3) + arcctg(x)',
'((2x)^(e^3 + 4) + cos(3)x) / log[x_1*2 + 3^2e](2 + (3y)^2)^5 * (2 + y)(x^2 + 3) + arcctg(Θ)',
variableNames: {'x', 'y', 'Θ', 'x_1'},
);
// Display the parsed expression in human-readable form
print(expression);

// Evaluate the expression with `x = 20` and display result
print(expression.calc(20));
// Evaluate the expression with `x = 20`, `y = 2`, `theta = 1/2`
// and display result
print(expression.calc(
MathVariableValues({'x': 20, 'y': 2, 'Θ': 0.5, 'x_1': 3}),
));
}

/// Integrate library example
void integrate() {
print(
MathNodeExpression.fromString('cos(x)')
.definiteIntegralBySimpson(10, 0, 3.14),
);
}
11 changes: 11 additions & 0 deletions lib/integrate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Numerical methods for integrating extension
///
/// Following methods available:
/// - By rectangles (left, middle, right)
/// - Simpson's method
/// - By trapezoids
///
/// In order to use the functionality import the `integrate.dart` file
library math_parser_integrate;

export 'src/extensions/integrate.dart';
6 changes: 2 additions & 4 deletions lib/math_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
///
/// void main() {
/// final expression = MathNodeExpression.fromString(
/// '((2x)^(e^3 + 4)',
/// '(2x)^(e^3 + 4)',
/// );
/// print(expression.calc(20));
/// }
Expand All @@ -20,7 +20,5 @@ library math_parser;

export 'src/math_node.dart';
export 'src/parse.dart';
export 'src/math_errors.dart';
export 'src/parse_errors.dart';

// Extensions
export 'src/extensions/integrate.dart';
2 changes: 1 addition & 1 deletion lib/src/extensions/integrate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ Iterable<num> _stepsAtMiddle(int n, num lowerLimit, num upperLimit) sync* {
Iterable<num> _calculateMathNodeAtPoints(
MathNode expression, Iterable<num> points) sync* {
for (final point in points) {
yield expression.calc(point);
yield expression.calc(MathVariableValues.x(point));
}
}
25 changes: 25 additions & 0 deletions lib/src/math_errors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// Math parsing error
///
/// Errors must extends this class
abstract class MathException implements Exception {
/// Creates a new Math Exception
const MathException();
}

/// Undefined Variable Exception
///
/// Thrown when there's a variable referenced in calculations which wasn't passed
/// to the [MathNode.calc] function
class UndefinedVariableException extends MathException {
@override
String toString() {
return 'UndefinedVariableException: Variable "$name" was not defined in '
'the calc() function. Variable names are case-sensitive';
}

/// The missing variable
final String name;

/// Created a new Undefined Variable Exception
const UndefinedVariableException(this.name);
}
Loading

0 comments on commit ff61b7a

Please sign in to comment.