@itwin/core-geometry 4.3.0-dev.11 → 4.3.0-dev.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/numerics/Newton.d.ts +115 -60
- package/lib/cjs/numerics/Newton.d.ts.map +1 -1
- package/lib/cjs/numerics/Newton.js +118 -61
- package/lib/cjs/numerics/Newton.js.map +1 -1
- package/lib/cjs/numerics/Polynomials.d.ts +2 -2
- package/lib/cjs/numerics/Polynomials.d.ts.map +1 -1
- package/lib/cjs/numerics/Polynomials.js +2 -2
- package/lib/cjs/numerics/Polynomials.js.map +1 -1
- package/lib/esm/numerics/Newton.d.ts +115 -60
- package/lib/esm/numerics/Newton.d.ts.map +1 -1
- package/lib/esm/numerics/Newton.js +118 -61
- package/lib/esm/numerics/Newton.js.map +1 -1
- package/lib/esm/numerics/Polynomials.d.ts +2 -2
- package/lib/esm/numerics/Polynomials.d.ts.map +1 -1
- package/lib/esm/numerics/Polynomials.js +2 -2
- package/lib/esm/numerics/Polynomials.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,186 +1,241 @@
|
|
|
1
1
|
import { Plane3dByOriginAndVectors } from "../geometry3d/Plane3dByOriginAndVectors";
|
|
2
|
-
/**
|
|
3
|
-
*
|
|
4
|
-
* from this base class.
|
|
2
|
+
/**
|
|
3
|
+
* Base class for Newton iterations in various dimensions.
|
|
4
|
+
* Dimension-specific classes carry all dimension-related data and answer generalized queries from this base class.
|
|
5
5
|
* @internal
|
|
6
6
|
*/
|
|
7
7
|
export declare abstract class AbstractNewtonIterator {
|
|
8
|
-
/** Compute a step.
|
|
8
|
+
/** Compute a step. The current x and function values must be retained for use in later method calls. */
|
|
9
9
|
abstract computeStep(): boolean;
|
|
10
|
-
/**
|
|
11
|
-
*
|
|
10
|
+
/**
|
|
11
|
+
* Return the current step size, scaled for use in tolerance tests.
|
|
12
|
+
* * This is a single number, typically the max of various per-dimension `dx/(1+x)` for the x and dx of that dimension.
|
|
12
13
|
*/
|
|
13
14
|
abstract currentStepSize(): number;
|
|
14
15
|
/**
|
|
15
|
-
* Apply the current step (in all dimensions)
|
|
16
|
+
* Apply the current step (in all dimensions).
|
|
16
17
|
* @param isFinalStep true if this is a final step.
|
|
17
18
|
*/
|
|
18
19
|
abstract applyCurrentStep(isFinalStep: boolean): boolean;
|
|
19
20
|
/**
|
|
21
|
+
* The constructor.
|
|
20
22
|
* @param stepSizeTarget tolerance to consider a single step converged.
|
|
21
|
-
* This number should be "moderately" strict.
|
|
23
|
+
* This number should be "moderately" strict. Because 2 successive convergences are required,
|
|
22
24
|
* it is expected that a first "accept" for (say) 10 to 14 digit step will be followed by another
|
|
23
|
-
* iteration.
|
|
24
|
-
* 20 to 28.
|
|
25
|
+
* iteration. A well behaved newton would then hypothetically double the number of digits to
|
|
26
|
+
* 20 to 28. Since the IEEE double only carries 16 digits, this second-convergence step will
|
|
25
27
|
* typically achieve full precision.
|
|
26
28
|
* @param successiveConvergenceTarget number of successive convergences required for acceptance.
|
|
27
|
-
* @param maxIterations max number of iterations.
|
|
28
|
-
*
|
|
29
|
+
* @param maxIterations max number of iterations. A typical newton step converges in 3 to 6 iterations.
|
|
30
|
+
* Allow 15 to 20 to catch difficult cases.
|
|
29
31
|
*/
|
|
30
32
|
protected constructor(stepSizeTolerance?: number, successiveConvergenceTarget?: number, maxIterations?: number);
|
|
31
|
-
/** Number of consecutive steps which passed convergence condition */
|
|
33
|
+
/** Number of consecutive steps which passed convergence condition. */
|
|
32
34
|
protected _numAccepted: number;
|
|
33
|
-
/** Target number of successive convergences */
|
|
35
|
+
/** Target number of successive convergences. */
|
|
34
36
|
protected _successiveConvergenceTarget: number;
|
|
35
|
-
/**
|
|
37
|
+
/** Convergence target (the implementation-specific currentStepSize is compared to this). */
|
|
36
38
|
protected _stepSizeTolerance: number;
|
|
37
|
-
/** Max iterations allowed */
|
|
39
|
+
/** Max iterations allowed. */
|
|
38
40
|
protected _maxIterations: number;
|
|
39
|
-
/**
|
|
41
|
+
/** Number of iterations (incremented at each step). */
|
|
40
42
|
numIterations: number;
|
|
41
43
|
/**
|
|
42
44
|
* Test if a step is converged.
|
|
43
|
-
* * Convergence is accepted with enough (_successiveConvergenceTarget) small steps (according to _stepSizeTolerance)
|
|
44
|
-
*
|
|
45
|
+
* * Convergence is accepted with enough (_successiveConvergenceTarget) small steps (according to _stepSizeTolerance)
|
|
46
|
+
* occur in succession.
|
|
47
|
+
* @param delta step size as reported by currentStepSize.
|
|
45
48
|
*/
|
|
46
49
|
testConvergence(delta: number): boolean;
|
|
47
50
|
/**
|
|
48
51
|
* Run iterations, calling various methods from base and derived classes:
|
|
49
52
|
* * computeStep -- typically evaluate derivatives and solve linear system.
|
|
50
|
-
* * currentStepSize -- return numeric measure of the step just computed by computeStep
|
|
53
|
+
* * currentStepSize -- return numeric measure of the step just computed by computeStep.
|
|
51
54
|
* * testConvergence -- test if the step from currentStepSize (along with recent steps) is converged.
|
|
52
|
-
* * applyCurrentStep -- apply the step to the independent variables
|
|
55
|
+
* * applyCurrentStep -- apply the step to the independent variables.
|
|
53
56
|
*/
|
|
54
57
|
runIterations(): boolean;
|
|
55
58
|
}
|
|
56
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* Object to evaluate a newton function. The object must retain most-recent function and derivative
|
|
57
61
|
* values for immediate query.
|
|
58
62
|
* @internal
|
|
59
63
|
*/
|
|
60
64
|
export declare abstract class NewtonEvaluatorRtoRD {
|
|
61
|
-
/**
|
|
65
|
+
/** Evaluate the function and its derivative at x. */
|
|
62
66
|
abstract evaluate(x: number): boolean;
|
|
63
|
-
/**
|
|
67
|
+
/** Most recent function value, i.e., f(x_n). */
|
|
64
68
|
currentF: number;
|
|
65
|
-
/**
|
|
69
|
+
/** Most recent evaluated derivative, i.e., f'(x_n). */
|
|
66
70
|
currentdFdX: number;
|
|
67
71
|
}
|
|
68
72
|
/**
|
|
69
73
|
* Newton iterator for use when both function and derivative can be evaluated.
|
|
74
|
+
* To solve `f(x) = 0`, the Newton iteration is `x_{n+1} = x_n - dx = x_n - f(x_n)/f'(x_n)`.
|
|
75
|
+
* To solve `f(x) = target` which is equivalent to solving `g(x) = f(x) - target = 0`, the Newton iteration is
|
|
76
|
+
* `x_{n+1} = x_n - dx = x_n - g(x_n)/g'(x_n) = x_n - (f(x_n)-target)/f'(x_n)`.
|
|
70
77
|
* @internal
|
|
71
78
|
*/
|
|
72
79
|
export declare class Newton1dUnbounded extends AbstractNewtonIterator {
|
|
73
80
|
private _func;
|
|
81
|
+
/** Current step is dx. */
|
|
74
82
|
private _currentStep;
|
|
83
|
+
/** Current X is x_n. */
|
|
75
84
|
private _currentX;
|
|
85
|
+
/** The target */
|
|
76
86
|
private _target;
|
|
77
87
|
/**
|
|
78
|
-
* Constructor for 1D newton iteration with
|
|
79
|
-
* @param func function that returns both function and derivative.
|
|
88
|
+
* Constructor for 1D newton iteration with derivatives.
|
|
89
|
+
* @param func function that returns both function value and derivative.
|
|
80
90
|
*/
|
|
81
91
|
constructor(func: NewtonEvaluatorRtoRD);
|
|
82
|
-
/** Set the independent variable */
|
|
92
|
+
/** Set the independent variable, i.e., x_n. */
|
|
83
93
|
setX(x: number): boolean;
|
|
84
|
-
/** Get the independent variable */
|
|
94
|
+
/** Get the independent variable, i.e., x_n. */
|
|
85
95
|
getX(): number;
|
|
86
|
-
/** Set the target function value */
|
|
96
|
+
/** Set the target function value. */
|
|
87
97
|
setTarget(y: number): void;
|
|
88
|
-
/**
|
|
98
|
+
/** Move the current X by the just-computed step, i.e., `x_n - dx`. */
|
|
89
99
|
applyCurrentStep(): boolean;
|
|
90
100
|
/** Compute the univariate newton step. */
|
|
91
101
|
computeStep(): boolean;
|
|
92
|
-
/** Return the current step size as a relative number. */
|
|
102
|
+
/** Return the current step size as a relative number, i.e., `|dx / (1 + |x_n|)|`. */
|
|
93
103
|
currentStepSize(): number;
|
|
94
104
|
}
|
|
95
|
-
/**
|
|
105
|
+
/**
|
|
106
|
+
* Object to evaluate a newton function (without derivative). The object must retain most-recent function value.
|
|
96
107
|
* @internal
|
|
97
108
|
*/
|
|
98
109
|
export declare abstract class NewtonEvaluatorRtoR {
|
|
99
110
|
/** Evaluate function value into member currentF */
|
|
100
111
|
abstract evaluate(x: number): boolean;
|
|
101
|
-
/** Most recent function evaluation. */
|
|
112
|
+
/** Most recent function evaluation, i.e., f(x_n). */
|
|
102
113
|
currentF: number;
|
|
103
114
|
}
|
|
104
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* Newton iteration for a univariate function, using approximate derivatives.
|
|
117
|
+
* To approximate the derivatives we use a small step `h`, i.e., `f'(x_n) = (f(x_n + h) - f(x_n)) / h`.
|
|
118
|
+
* Therefore, to solve `f(x) = 0`, the iteration is
|
|
119
|
+
* `x_{n+1} = x_n - dx = x_n - f(x_n)/f'(x_n) = x_n - f(x_n) * h / (f(x_n + h) - f(x_n))`.
|
|
105
120
|
* @internal
|
|
106
121
|
*/
|
|
107
122
|
export declare class Newton1dUnboundedApproximateDerivative extends AbstractNewtonIterator {
|
|
108
123
|
private _func;
|
|
124
|
+
/** Current step is dx. */
|
|
109
125
|
private _currentStep;
|
|
126
|
+
/** Current X is x_n. */
|
|
110
127
|
private _currentX;
|
|
111
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* Step size for approximate derivative for the iteration.
|
|
112
130
|
* * Initialized to 1e-8, which is appropriate for iteration in fraction space.
|
|
113
131
|
* * Should be larger for iteration with real distance as x.
|
|
114
132
|
*/
|
|
115
133
|
derivativeH: number;
|
|
116
134
|
/**
|
|
117
135
|
* Constructor for 1D newton iteration with approximate derivatives.
|
|
118
|
-
* @param func function that returns
|
|
136
|
+
* @param func function that only returns function value (and not derivative).
|
|
119
137
|
*/
|
|
120
138
|
constructor(func: NewtonEvaluatorRtoR);
|
|
121
|
-
/** Set the
|
|
139
|
+
/** Set the independent variable, i.e., x_n. */
|
|
122
140
|
setX(x: number): boolean;
|
|
123
|
-
/** Get the independent variable */
|
|
141
|
+
/** Get the independent variable, i.e., x_n. */
|
|
124
142
|
getX(): number;
|
|
125
|
-
/**
|
|
143
|
+
/** Move the current X by the just-computed step, i.e., `x_n - dx`. */
|
|
126
144
|
applyCurrentStep(): boolean;
|
|
127
|
-
/** Univariate newton step computed with
|
|
145
|
+
/** Univariate newton step computed with approximate derivative. */
|
|
128
146
|
computeStep(): boolean;
|
|
129
|
-
/** Return the current step size as a relative number. */
|
|
147
|
+
/** Return the current step size as a relative number, i.e., `|dx / (1 + |x_n|)|`. */
|
|
130
148
|
currentStepSize(): number;
|
|
131
149
|
}
|
|
132
|
-
/**
|
|
150
|
+
/**
|
|
151
|
+
* Object to evaluate a 2-parameter newton function with derivatives.
|
|
133
152
|
* @internal
|
|
134
153
|
*/
|
|
135
154
|
export declare abstract class NewtonEvaluatorRRtoRRD {
|
|
136
|
-
/**
|
|
155
|
+
/**
|
|
156
|
+
* Iteration controller calls this to ask for evaluation of the function and its two partial derivatives.
|
|
137
157
|
* * The implementation returns true, it must set the currentF object.
|
|
138
158
|
*/
|
|
139
159
|
abstract evaluate(x: number, y: number): boolean;
|
|
140
|
-
/**
|
|
160
|
+
/**
|
|
161
|
+
* Most recent function evaluation as parts of the plane.
|
|
162
|
+
* * See doc of [[Newton2dUnboundedWithDerivative]] class for info on 2d newton method.
|
|
163
|
+
* * For current value (u,v) of the independent variable, and `F(u,v) := (x(u,v), y(u,v)), the returned plane has:
|
|
164
|
+
* * `origin.x` = x(u,v)
|
|
165
|
+
* * `origin.y` = y(u,v)
|
|
166
|
+
* * `vectorU.x` = dx/du
|
|
167
|
+
* * `vectorU.y` = dy/du
|
|
168
|
+
* * `vectorV.x` = dx/dv
|
|
169
|
+
* * `vectorV.y` = dy/dv
|
|
170
|
+
* * In other words, the plane stores the columns of the Jacobian matrix J of F: `vectorU` stores the partials
|
|
171
|
+
* of F with respect to u (the first column of J), and `vectorV` stores the partials of F with respect to v
|
|
172
|
+
* (the second column of J):
|
|
173
|
+
*
|
|
174
|
+
* `[vectorU.x vectorV.x]`
|
|
175
|
+
*
|
|
176
|
+
* `[vectorU.y vectorV.y]`
|
|
177
|
+
*/
|
|
141
178
|
currentF: Plane3dByOriginAndVectors;
|
|
142
179
|
/**
|
|
143
|
-
*
|
|
180
|
+
* Constructor.
|
|
144
181
|
* * This creates a currentF object to (repeatedly) receive function and derivatives.
|
|
145
182
|
*/
|
|
146
183
|
constructor();
|
|
147
184
|
}
|
|
148
185
|
/**
|
|
149
186
|
* Implement evaluation steps for newton iteration in 2 dimensions, using caller supplied NewtonEvaluatorRRtoRRD object.
|
|
187
|
+
* * Suppose we want to find the roots of `F(u,v) := (x(u,v), y(u,v))`. Writing `X := (u,v)` and `F(X)` as column vectors,
|
|
188
|
+
* the 2D Newton's iteration to find a root of `F` is given by:
|
|
189
|
+
* `X_{n+1} = X_n - dX = X_n - JInv(X_n)F(X_n)`, where `JInv` is the inverse of the Jacobian matrix `J`, and `J` is
|
|
190
|
+
* defined as:
|
|
191
|
+
*
|
|
192
|
+
* `[dx/du dx/dv]`
|
|
193
|
+
*
|
|
194
|
+
* `[dy/du dy/dv]`
|
|
150
195
|
* @internal
|
|
151
196
|
*/
|
|
152
197
|
export declare class Newton2dUnboundedWithDerivative extends AbstractNewtonIterator {
|
|
153
198
|
private _func;
|
|
199
|
+
/** Current step, or dX = (du, dv). */
|
|
154
200
|
private _currentStep;
|
|
201
|
+
/** Current uv parameters, or X_n = (u_n, v_n). */
|
|
155
202
|
private _currentUV;
|
|
203
|
+
/**
|
|
204
|
+
* Constructor for 2D newton iteration with derivatives.
|
|
205
|
+
* @param func function that returns both function value and derivative.
|
|
206
|
+
*/
|
|
156
207
|
constructor(func: NewtonEvaluatorRRtoRRD);
|
|
157
|
-
/** Set the current uv
|
|
158
|
-
setUV(
|
|
159
|
-
/** Get the current u
|
|
208
|
+
/** Set the current uv parameters, i.e., `X_n = (u_n, v_n)`. */
|
|
209
|
+
setUV(u: number, v: number): boolean;
|
|
210
|
+
/** Get the current u parameter of X_n, i.e., u_n. */
|
|
160
211
|
getU(): number;
|
|
161
|
-
/** Get the current v
|
|
212
|
+
/** Get the current v parameter of X_n, i.e., v_n. */
|
|
162
213
|
getV(): number;
|
|
163
|
-
/**
|
|
214
|
+
/** Update the current uv parameter by currentStep, i.e., compute `X_{n+1} := X_n - dX = (u_n - du, v_n - dv)`. */
|
|
164
215
|
applyCurrentStep(): boolean;
|
|
165
|
-
/**
|
|
166
|
-
*
|
|
216
|
+
/**
|
|
217
|
+
* Evaluate the functions and derivatives at `X_n = (u_n, v_n)`, and solve the Jacobian matrix equation to
|
|
218
|
+
* compute `dX = (du, dv)`.
|
|
167
219
|
*/
|
|
168
220
|
computeStep(): boolean;
|
|
169
221
|
/**
|
|
170
|
-
* Return the
|
|
222
|
+
* Return the current relative step size, i.e., the larger absolute component of `dX / (1 + |X_n|)`
|
|
171
223
|
*/
|
|
172
224
|
currentStepSize(): number;
|
|
173
225
|
}
|
|
174
226
|
/**
|
|
175
|
-
* SimpleNewton has static methods for newton methods with evaluated functions presented as immediate arguments
|
|
227
|
+
* SimpleNewton has static methods for newton methods with evaluated functions presented as immediate arguments
|
|
228
|
+
* (not function object).
|
|
176
229
|
* @internal
|
|
177
230
|
*/
|
|
178
231
|
export declare class SimpleNewton {
|
|
179
|
-
/**
|
|
180
|
-
*
|
|
181
|
-
* *
|
|
182
|
-
*
|
|
183
|
-
*
|
|
232
|
+
/**
|
|
233
|
+
* Run a one-dimensional newton iteration with separate functions for function and derivative.
|
|
234
|
+
* * Completion is at 2 (TWO) successive passes at `absoluteTolerance + relTol * abs(x)`, where relTol is
|
|
235
|
+
* chosen internally.
|
|
236
|
+
* * `absoluteTolerance` is usually aggressively tight -- should come into play only for x near zero.
|
|
237
|
+
* * The `relTol` is fluffy (for instance around 1e-11) but in properly converging cases the extra pass after
|
|
238
|
+
* first success normally moves to full machine precision.
|
|
184
239
|
* * This is an open-loop newton -- it just runs, and returns undefined if anything bad happens.
|
|
185
240
|
*/
|
|
186
241
|
static runNewton1D(x: number, func: (x: number) => number | undefined, derivative: (x: number) => number | undefined, absoluteTolerance?: number): number | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Newton.d.ts","sourceRoot":"","sources":["../../../src/numerics/Newton.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"Newton.d.ts","sourceRoot":"","sources":["../../../src/numerics/Newton.ts"],"names":[],"mappings":"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"}
|
|
@@ -10,27 +10,28 @@ import { Plane3dByOriginAndVectors } from "../geometry3d/Plane3dByOriginAndVecto
|
|
|
10
10
|
import { Point2d, Vector2d } from "../geometry3d/Point2dVector2d";
|
|
11
11
|
import { SmallSystem } from "./Polynomials";
|
|
12
12
|
// cspell:word currentdFdX
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* from this base class.
|
|
13
|
+
/**
|
|
14
|
+
* Base class for Newton iterations in various dimensions.
|
|
15
|
+
* Dimension-specific classes carry all dimension-related data and answer generalized queries from this base class.
|
|
16
16
|
* @internal
|
|
17
17
|
*/
|
|
18
18
|
export class AbstractNewtonIterator {
|
|
19
19
|
/**
|
|
20
|
+
* The constructor.
|
|
20
21
|
* @param stepSizeTarget tolerance to consider a single step converged.
|
|
21
|
-
* This number should be "moderately" strict.
|
|
22
|
+
* This number should be "moderately" strict. Because 2 successive convergences are required,
|
|
22
23
|
* it is expected that a first "accept" for (say) 10 to 14 digit step will be followed by another
|
|
23
|
-
* iteration.
|
|
24
|
-
* 20 to 28.
|
|
24
|
+
* iteration. A well behaved newton would then hypothetically double the number of digits to
|
|
25
|
+
* 20 to 28. Since the IEEE double only carries 16 digits, this second-convergence step will
|
|
25
26
|
* typically achieve full precision.
|
|
26
27
|
* @param successiveConvergenceTarget number of successive convergences required for acceptance.
|
|
27
|
-
* @param maxIterations max number of iterations.
|
|
28
|
-
*
|
|
28
|
+
* @param maxIterations max number of iterations. A typical newton step converges in 3 to 6 iterations.
|
|
29
|
+
* Allow 15 to 20 to catch difficult cases.
|
|
29
30
|
*/
|
|
30
31
|
constructor(stepSizeTolerance = 1.0e-11, successiveConvergenceTarget = 2, maxIterations = 15) {
|
|
31
|
-
/** Number of consecutive steps which passed convergence condition */
|
|
32
|
+
/** Number of consecutive steps which passed convergence condition. */
|
|
32
33
|
this._numAccepted = 0;
|
|
33
|
-
/**
|
|
34
|
+
/** Number of iterations (incremented at each step). */
|
|
34
35
|
this.numIterations = 0;
|
|
35
36
|
this._stepSizeTolerance = stepSizeTolerance;
|
|
36
37
|
this._successiveConvergenceTarget = successiveConvergenceTarget;
|
|
@@ -38,8 +39,9 @@ export class AbstractNewtonIterator {
|
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
40
41
|
* Test if a step is converged.
|
|
41
|
-
* * Convergence is accepted with enough (_successiveConvergenceTarget) small steps (according to _stepSizeTolerance)
|
|
42
|
-
*
|
|
42
|
+
* * Convergence is accepted with enough (_successiveConvergenceTarget) small steps (according to _stepSizeTolerance)
|
|
43
|
+
* occur in succession.
|
|
44
|
+
* @param delta step size as reported by currentStepSize.
|
|
43
45
|
*/
|
|
44
46
|
testConvergence(delta) {
|
|
45
47
|
if (Math.abs(delta) < this._stepSizeTolerance) {
|
|
@@ -52,16 +54,15 @@ export class AbstractNewtonIterator {
|
|
|
52
54
|
/**
|
|
53
55
|
* Run iterations, calling various methods from base and derived classes:
|
|
54
56
|
* * computeStep -- typically evaluate derivatives and solve linear system.
|
|
55
|
-
* * currentStepSize -- return numeric measure of the step just computed by computeStep
|
|
57
|
+
* * currentStepSize -- return numeric measure of the step just computed by computeStep.
|
|
56
58
|
* * testConvergence -- test if the step from currentStepSize (along with recent steps) is converged.
|
|
57
|
-
* * applyCurrentStep -- apply the step to the independent variables
|
|
59
|
+
* * applyCurrentStep -- apply the step to the independent variables.
|
|
58
60
|
*/
|
|
59
61
|
runIterations() {
|
|
60
62
|
this._numAccepted = 0;
|
|
61
63
|
this.numIterations = 0;
|
|
62
64
|
while (this.numIterations++ < this._maxIterations && this.computeStep()) {
|
|
63
|
-
if (this.testConvergence(this.currentStepSize())
|
|
64
|
-
&& this.applyCurrentStep(true)) {
|
|
65
|
+
if (this.testConvergence(this.currentStepSize()) && this.applyCurrentStep(true)) {
|
|
65
66
|
return true;
|
|
66
67
|
}
|
|
67
68
|
this.applyCurrentStep(false);
|
|
@@ -69,7 +70,8 @@ export class AbstractNewtonIterator {
|
|
|
69
70
|
return false;
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* Object to evaluate a newton function. The object must retain most-recent function and derivative
|
|
73
75
|
* values for immediate query.
|
|
74
76
|
* @internal
|
|
75
77
|
*/
|
|
@@ -77,26 +79,39 @@ export class NewtonEvaluatorRtoRD {
|
|
|
77
79
|
}
|
|
78
80
|
/**
|
|
79
81
|
* Newton iterator for use when both function and derivative can be evaluated.
|
|
82
|
+
* To solve `f(x) = 0`, the Newton iteration is `x_{n+1} = x_n - dx = x_n - f(x_n)/f'(x_n)`.
|
|
83
|
+
* To solve `f(x) = target` which is equivalent to solving `g(x) = f(x) - target = 0`, the Newton iteration is
|
|
84
|
+
* `x_{n+1} = x_n - dx = x_n - g(x_n)/g'(x_n) = x_n - (f(x_n)-target)/f'(x_n)`.
|
|
80
85
|
* @internal
|
|
81
86
|
*/
|
|
82
87
|
export class Newton1dUnbounded extends AbstractNewtonIterator {
|
|
83
88
|
/**
|
|
84
|
-
* Constructor for 1D newton iteration with
|
|
85
|
-
* @param func function that returns both function and derivative.
|
|
89
|
+
* Constructor for 1D newton iteration with derivatives.
|
|
90
|
+
* @param func function that returns both function value and derivative.
|
|
86
91
|
*/
|
|
87
92
|
constructor(func) {
|
|
88
93
|
super();
|
|
89
94
|
this._func = func;
|
|
90
95
|
this.setTarget(0);
|
|
91
96
|
}
|
|
92
|
-
/** Set the independent variable */
|
|
93
|
-
setX(x) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
/** Set the independent variable, i.e., x_n. */
|
|
98
|
+
setX(x) {
|
|
99
|
+
this._currentX = x;
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/** Get the independent variable, i.e., x_n. */
|
|
103
|
+
getX() {
|
|
104
|
+
return this._currentX;
|
|
105
|
+
}
|
|
106
|
+
/** Set the target function value. */
|
|
107
|
+
setTarget(y) {
|
|
108
|
+
this._target = y;
|
|
109
|
+
}
|
|
110
|
+
/** Move the current X by the just-computed step, i.e., `x_n - dx`. */
|
|
111
|
+
applyCurrentStep() {
|
|
112
|
+
// console.log(this._currentX - this._currentStep); // print approximations for debug
|
|
113
|
+
return this.setX(this._currentX - this._currentStep);
|
|
114
|
+
}
|
|
100
115
|
/** Compute the univariate newton step. */
|
|
101
116
|
computeStep() {
|
|
102
117
|
if (this._func.evaluate(this._currentX)) {
|
|
@@ -108,41 +123,54 @@ export class Newton1dUnbounded extends AbstractNewtonIterator {
|
|
|
108
123
|
}
|
|
109
124
|
return false;
|
|
110
125
|
}
|
|
111
|
-
/** Return the current step size as a relative number. */
|
|
126
|
+
/** Return the current step size as a relative number, i.e., `|dx / (1 + |x_n|)|`. */
|
|
112
127
|
currentStepSize() {
|
|
113
128
|
return Math.abs(this._currentStep / (1.0 + Math.abs(this._currentX)));
|
|
114
129
|
}
|
|
115
130
|
}
|
|
116
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Object to evaluate a newton function (without derivative). The object must retain most-recent function value.
|
|
117
133
|
* @internal
|
|
118
134
|
*/
|
|
119
135
|
export class NewtonEvaluatorRtoR {
|
|
120
136
|
}
|
|
121
|
-
/**
|
|
137
|
+
/**
|
|
138
|
+
* Newton iteration for a univariate function, using approximate derivatives.
|
|
139
|
+
* To approximate the derivatives we use a small step `h`, i.e., `f'(x_n) = (f(x_n + h) - f(x_n)) / h`.
|
|
140
|
+
* Therefore, to solve `f(x) = 0`, the iteration is
|
|
141
|
+
* `x_{n+1} = x_n - dx = x_n - f(x_n)/f'(x_n) = x_n - f(x_n) * h / (f(x_n + h) - f(x_n))`.
|
|
122
142
|
* @internal
|
|
123
143
|
*/
|
|
124
144
|
export class Newton1dUnboundedApproximateDerivative extends AbstractNewtonIterator {
|
|
125
145
|
/**
|
|
126
146
|
* Constructor for 1D newton iteration with approximate derivatives.
|
|
127
|
-
* @param func function that returns
|
|
147
|
+
* @param func function that only returns function value (and not derivative).
|
|
128
148
|
*/
|
|
129
149
|
constructor(func) {
|
|
130
150
|
super();
|
|
131
151
|
this._func = func;
|
|
132
152
|
this.derivativeH = 1.0e-8;
|
|
133
153
|
}
|
|
134
|
-
/** Set the
|
|
135
|
-
setX(x) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
154
|
+
/** Set the independent variable, i.e., x_n. */
|
|
155
|
+
setX(x) {
|
|
156
|
+
this._currentX = x;
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
/** Get the independent variable, i.e., x_n. */
|
|
160
|
+
getX() {
|
|
161
|
+
return this._currentX;
|
|
162
|
+
}
|
|
163
|
+
/** Move the current X by the just-computed step, i.e., `x_n - dx`. */
|
|
164
|
+
applyCurrentStep() {
|
|
165
|
+
// console.log(this._currentX - this._currentStep); // print approximations for debug
|
|
166
|
+
return this.setX(this._currentX - this._currentStep);
|
|
167
|
+
}
|
|
168
|
+
/** Univariate newton step computed with approximate derivative. */
|
|
141
169
|
computeStep() {
|
|
142
170
|
if (this._func.evaluate(this._currentX)) {
|
|
143
|
-
const fA = this._func.currentF;
|
|
171
|
+
const fA = this._func.currentF; // f(x_n)
|
|
144
172
|
if (this._func.evaluate(this._currentX + this.derivativeH)) {
|
|
145
|
-
const fB = this._func.currentF;
|
|
173
|
+
const fB = this._func.currentF; // f(x_n + h)
|
|
146
174
|
const dx = Geometry.conditionalDivideFraction(fA, (fB - fA) / this.derivativeH);
|
|
147
175
|
if (dx !== undefined) {
|
|
148
176
|
this._currentStep = dx;
|
|
@@ -152,17 +180,18 @@ export class Newton1dUnboundedApproximateDerivative extends AbstractNewtonIterat
|
|
|
152
180
|
}
|
|
153
181
|
return false;
|
|
154
182
|
}
|
|
155
|
-
/** Return the current step size as a relative number. */
|
|
183
|
+
/** Return the current step size as a relative number, i.e., `|dx / (1 + |x_n|)|`. */
|
|
156
184
|
currentStepSize() {
|
|
157
185
|
return Math.abs(this._currentStep / (1.0 + Math.abs(this._currentX)));
|
|
158
186
|
}
|
|
159
187
|
}
|
|
160
|
-
/**
|
|
188
|
+
/**
|
|
189
|
+
* Object to evaluate a 2-parameter newton function with derivatives.
|
|
161
190
|
* @internal
|
|
162
191
|
*/
|
|
163
192
|
export class NewtonEvaluatorRRtoRRD {
|
|
164
193
|
/**
|
|
165
|
-
*
|
|
194
|
+
* Constructor.
|
|
166
195
|
* * This creates a currentF object to (repeatedly) receive function and derivatives.
|
|
167
196
|
*/
|
|
168
197
|
constructor() {
|
|
@@ -171,25 +200,49 @@ export class NewtonEvaluatorRRtoRRD {
|
|
|
171
200
|
}
|
|
172
201
|
/**
|
|
173
202
|
* Implement evaluation steps for newton iteration in 2 dimensions, using caller supplied NewtonEvaluatorRRtoRRD object.
|
|
203
|
+
* * Suppose we want to find the roots of `F(u,v) := (x(u,v), y(u,v))`. Writing `X := (u,v)` and `F(X)` as column vectors,
|
|
204
|
+
* the 2D Newton's iteration to find a root of `F` is given by:
|
|
205
|
+
* `X_{n+1} = X_n - dX = X_n - JInv(X_n)F(X_n)`, where `JInv` is the inverse of the Jacobian matrix `J`, and `J` is
|
|
206
|
+
* defined as:
|
|
207
|
+
*
|
|
208
|
+
* `[dx/du dx/dv]`
|
|
209
|
+
*
|
|
210
|
+
* `[dy/du dy/dv]`
|
|
174
211
|
* @internal
|
|
175
212
|
*/
|
|
176
213
|
export class Newton2dUnboundedWithDerivative extends AbstractNewtonIterator {
|
|
214
|
+
/**
|
|
215
|
+
* Constructor for 2D newton iteration with derivatives.
|
|
216
|
+
* @param func function that returns both function value and derivative.
|
|
217
|
+
*/
|
|
177
218
|
constructor(func) {
|
|
178
219
|
super();
|
|
179
220
|
this._func = func;
|
|
180
221
|
this._currentStep = Vector2d.createZero();
|
|
181
222
|
this._currentUV = Point2d.createZero();
|
|
182
223
|
}
|
|
183
|
-
/** Set the current uv
|
|
184
|
-
setUV(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
224
|
+
/** Set the current uv parameters, i.e., `X_n = (u_n, v_n)`. */
|
|
225
|
+
setUV(u, v) {
|
|
226
|
+
this._currentUV.set(u, v);
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
/** Get the current u parameter of X_n, i.e., u_n. */
|
|
230
|
+
getU() {
|
|
231
|
+
return this._currentUV.x;
|
|
232
|
+
}
|
|
233
|
+
/** Get the current v parameter of X_n, i.e., v_n. */
|
|
234
|
+
getV() {
|
|
235
|
+
return this._currentUV.y;
|
|
236
|
+
}
|
|
237
|
+
/** Update the current uv parameter by currentStep, i.e., compute `X_{n+1} := X_n - dX = (u_n - du, v_n - dv)`. */
|
|
238
|
+
applyCurrentStep() {
|
|
239
|
+
// print approximations for debug
|
|
240
|
+
// console.log("(" + (this._currentUV.x - this._currentStep.x) + "," + (this._currentUV.y - this._currentStep.y) + ")");
|
|
241
|
+
return this.setUV(this._currentUV.x - this._currentStep.x, this._currentUV.y - this._currentStep.y);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Evaluate the functions and derivatives at `X_n = (u_n, v_n)`, and solve the Jacobian matrix equation to
|
|
245
|
+
* compute `dX = (du, dv)`.
|
|
193
246
|
*/
|
|
194
247
|
computeStep() {
|
|
195
248
|
if (this._func.evaluate(this._currentUV.x, this._currentUV.y)) {
|
|
@@ -200,22 +253,25 @@ export class Newton2dUnboundedWithDerivative extends AbstractNewtonIterator {
|
|
|
200
253
|
return false;
|
|
201
254
|
}
|
|
202
255
|
/**
|
|
203
|
-
* Return the
|
|
256
|
+
* Return the current relative step size, i.e., the larger absolute component of `dX / (1 + |X_n|)`
|
|
204
257
|
*/
|
|
205
258
|
currentStepSize() {
|
|
206
259
|
return Geometry.maxAbsXY(this._currentStep.x / (1.0 + Math.abs(this._currentUV.x)), this._currentStep.y / (1.0 + Math.abs(this._currentUV.y)));
|
|
207
260
|
}
|
|
208
261
|
}
|
|
209
262
|
/**
|
|
210
|
-
* SimpleNewton has static methods for newton methods with evaluated functions presented as immediate arguments
|
|
263
|
+
* SimpleNewton has static methods for newton methods with evaluated functions presented as immediate arguments
|
|
264
|
+
* (not function object).
|
|
211
265
|
* @internal
|
|
212
266
|
*/
|
|
213
267
|
export class SimpleNewton {
|
|
214
|
-
/**
|
|
215
|
-
*
|
|
216
|
-
* *
|
|
217
|
-
*
|
|
218
|
-
*
|
|
268
|
+
/**
|
|
269
|
+
* Run a one-dimensional newton iteration with separate functions for function and derivative.
|
|
270
|
+
* * Completion is at 2 (TWO) successive passes at `absoluteTolerance + relTol * abs(x)`, where relTol is
|
|
271
|
+
* chosen internally.
|
|
272
|
+
* * `absoluteTolerance` is usually aggressively tight -- should come into play only for x near zero.
|
|
273
|
+
* * The `relTol` is fluffy (for instance around 1e-11) but in properly converging cases the extra pass after
|
|
274
|
+
* first success normally moves to full machine precision.
|
|
219
275
|
* * This is an open-loop newton -- it just runs, and returns undefined if anything bad happens.
|
|
220
276
|
*/
|
|
221
277
|
static runNewton1D(x, func, derivative, absoluteTolerance = Geometry.smallFloatingPoint) {
|
|
@@ -230,10 +286,11 @@ export class SimpleNewton {
|
|
|
230
286
|
if (dx === undefined)
|
|
231
287
|
return undefined;
|
|
232
288
|
x -= dx;
|
|
289
|
+
// console.log(x); // print approximations for debug
|
|
233
290
|
tolerance = absoluteTolerance + Math.abs(x) * relTol;
|
|
234
291
|
if (Math.abs(dx) < tolerance) {
|
|
235
292
|
numConverged++;
|
|
236
|
-
if (dx === 0.0 || numConverged > 1) // bypass convergence count on true 0 dx
|
|
293
|
+
if (dx === 0.0 || numConverged > 1) // bypass convergence count on true 0 dx
|
|
237
294
|
return x;
|
|
238
295
|
}
|
|
239
296
|
else {
|