More test for formula solver
This commit is contained in:
parent
7b5194d04c
commit
1757d13b6e
2 changed files with 27 additions and 5 deletions
|
|
@ -415,6 +415,7 @@ Number functionSolver(
|
|||
|
||||
while (iter < maxNewtonIters) {
|
||||
final Number y = f(x);
|
||||
print( "iter: $iter x: $x y: $y");
|
||||
if (y == 0 || y.abs() <= maxDelta) {
|
||||
return x;
|
||||
}
|
||||
|
|
@ -446,9 +447,16 @@ Number functionSolver(
|
|||
|
||||
try {
|
||||
return searchNewton();
|
||||
} catch (e) {
|
||||
var approx = searchApproximately(hint, hint + step);
|
||||
return binarySearch(approx[0], approx[1]);
|
||||
} catch (e1) {
|
||||
try {
|
||||
var approx = searchApproximately(hint, hint + step);
|
||||
return binarySearch(approx[0], approx[1]);
|
||||
}
|
||||
catch( e2 ){
|
||||
errorHandler.notify(e1);
|
||||
errorHandler.notify(e2);
|
||||
throw NoSolutionException("Failed to find a root using both Newton-Raphson and approximate search: $e1 -- $e2");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import 'dart:math' as Math;
|
||||
|
||||
import 'package:d4rt_formulas/formula_evaluator.dart';
|
||||
import 'package:d4rt_formulas/formula_models.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'dart:math' as Math;
|
||||
|
||||
|
||||
void main() {
|
||||
|
|
@ -22,6 +23,20 @@ void main() {
|
|||
expect( solution, closeTo(5, 1e-10));
|
||||
});
|
||||
|
||||
|
||||
test("Solve x formula", () {
|
||||
final formula = Formula(
|
||||
name: 'Test x',
|
||||
input: [
|
||||
VariableSpec(name: 'x', unit: 'scalar'),
|
||||
],
|
||||
output: VariableSpec(name: 'y', unit: 'scalar'),
|
||||
d4rtCode: 'y = x;',
|
||||
);
|
||||
|
||||
var solution = formulaSolver(formula, "x", {"y": 123456789}, maxDelta: 1e-10);
|
||||
expect(solution, closeTo(123456789, 1e-10));
|
||||
});
|
||||
});
|
||||
|
||||
group('Native functions', () {
|
||||
|
|
@ -75,5 +90,4 @@ void main() {
|
|||
expect(root, closeTo(Math.log(2), 0.01));
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue