You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3ec1708
Browse filesBrowse the repository at this point in the historyBrowse files
OPTIMIZE_FOR_SPEED T Algorithm<T, N>::Compute(T input)
366
+
{
367
+
// performance-critical implementation
368
+
}
369
+
```
370
+
371
+
The `#pragma GCC optimize` applies `-O3` and `-ffast-math` to the entire translation unit even in Debug builds, while `OPTIMIZE_FOR_SPEED` additionally forces inlining and marks functions as hot for the linker.
372
+
276
373
### Optimization Macros
277
374
278
-
The library provides compiler-specific optimization hints:
375
+
Defined in `numerical/math/CompilerOptimizations.hpp`:
279
376
280
377
- **OPTIMIZE_FOR_SPEED**: For performance-critical algorithms (PID, filters)
A[Input: Open-Loop Numerator N(s), Denominator D(s)] --> B[Find Open-Loop Poles and Zeros]
8
+
B --> C[Generate Logarithmic Gain Sweep]
9
+
C --> D[For Each Gain K]
10
+
D --> E[Form Characteristic Polynomial: D(s) + K·N(s)]
11
+
E --> F[Find Roots using Durand-Kerner]
12
+
F --> G[Store Roots as Locus Branches]
13
+
G --> H{More Gain Steps?}
14
+
H -->|Yes| D
15
+
H -->|No| I[Compute Closed-Loop Poles at Current Gain]
16
+
I --> J[Return Result]
17
+
```
18
+
19
+
## Mathematical Background
20
+
21
+
### Root Locus Definition
22
+
23
+
The root locus shows how the closed-loop poles of a feedback system move in the complex plane as a scalar gain $K$ varies. Given an open-loop transfer function:
24
+
25
+
$$G(s) = K \cdot \frac{N(s)}{D(s)}$$
26
+
27
+
the closed-loop characteristic equation is:
28
+
29
+
$$D(s) + K \cdot N(s) = 0$$
30
+
31
+
The root locus plots the solutions of this equation for $K \in [K_{\min}, K_{\max}]$.
32
+
33
+
### Properties
34
+
35
+
- **Starting points** ($K \to 0$): Roots converge to the open-loop poles (roots of $D(s)$)
36
+
- **Ending points** ($K \to \infty$): Roots converge to the open-loop zeros (roots of $N(s)$) or diverge to infinity
37
+
- **Number of branches**: Equal to the order of $D(s)$
38
+
- **Symmetry**: Complex roots appear in conjugate pairs for real-coefficient polynomials
39
+
40
+
### Gain Sweep
41
+
42
+
The gain is swept logarithmically to provide even resolution across decades:
- `denominator.size() >= numerator.size()` (proper or strictly proper transfer function)
96
+
- `MaxGainSteps > 1` (enforced at compile time)
97
+
98
+
### Result Structure
99
+
100
+
| Field | Type | Description |
101
+
|-------|------|-------------|
102
+
| `loci` | `array<LociBranch, MaxOrder>` | Root positions for each branch at each gain step |
103
+
| `activeBranches` | `size_t` | Number of active locus branches (= denominator order) |
104
+
| `gains` | `GainVector` | Gain values at each step |
105
+
| `openLoopPoles` | `RootVector` | Poles of the open-loop system |
106
+
| `openLoopZeros` | `RootVector` | Zeros of the open-loop system |
107
+
| `closedLoopPoles` | `RootVector` | Poles at `currentGain` |
108
+
| `currentGain` | `T` | The current gain value |
109
+
110
+
### Complexity
111
+
112
+
- **Time**: $O(N \cdot n^2 \cdot k)$ where $N$ = gain steps, $n$ = polynomial order, $k$ = Durand-Kerner iterations
113
+
- **Space**: $O(N \cdot n)$ for storing all locus branches
114
+
115
+
## Numerical Considerations
116
+
117
+
- **Logarithmic gain sweep**: Provides uniform resolution across gain decades; requires strictly positive gains
118
+
- **Branch tracking**: Roots at each gain step are sorted by Durand-Kerner's default ordering (real part ascending). For smooth branch visualization, the UI may need to apply additional nearest-neighbor matching between consecutive steps
119
+
- **Polynomial addition**: `PolyAdd` pads the shorter polynomial (numerator) with leading zeros to align with the longer one (denominator), requiring `denominator.size() >= numerator.size()`
120
+
- **Root finding accuracy**: Inherits convergence properties from the Durand-Kerner solver; see [DurandKerner.md](DurandKerner.md) for details
where the coefficients array is `[c₀, c₁, ..., cₙ]` with `c₀ ≠ 0` (leading coefficient).
28
+
29
+
### Durand-Kerner Method
30
+
31
+
The Durand-Kerner method (also known as Weierstrass iteration) simultaneously refines all roots of a polynomial. Given current root estimates $z_0, z_1, \ldots, z_{n-1}$, each root is updated:
This is equivalent to applying Newton's method but using the other root estimates to factor out known roots from the denominator, avoiding explicit polynomial deflation.
36
+
37
+
### Initial Root Placement
38
+
39
+
Roots are initialized uniformly on a circle of radius:
40
+
41
+
$$R = \left| \frac{c_n}{c_0} \right|^{1/n}$$
42
+
43
+
with an angular offset of 0.4 radians to break symmetry. If $R < 0.1$, it defaults to 1.0.
44
+
45
+
### Convergence
46
+
47
+
- **Quadratic convergence** when roots are well-separated
48
+
- **Linear convergence** near repeated roots
49
+
- Terminates when all corrections $|z_r^{(k+1)} - z_r^{(k)}| < \epsilon$ (default $\epsilon = 10^{-6}$)
50
+
- Maximum iteration limit (default 200) prevents infinite loops
- Leading coefficient `coefficients[0]` must be non-zero
84
+
- Polynomial order must not exceed `MaxOrder`
85
+
86
+
### Complexity
87
+
88
+
- **Time**: $O(n^2 \cdot k)$ where $n$ is polynomial order and $k$ is iteration count
89
+
- **Space**: $O(n)$ — roots stored in `BoundedVector`
90
+
91
+
## Numerical Considerations
92
+
93
+
- **Near-degenerate denominators**: When two root estimates are very close ($|z_r - z_j| < 10^{-15}$), the difference is excluded from the denominator product to avoid division by near-zero
94
+
- **Root sorting**: Results are sorted by real part (ascending), with imaginary part as tiebreaker
95
+
- **Fixed-point**: Only floating-point types (`float`, `double`) are supported due to the complex arithmetic requirements
96
+
- **Ill-conditioned polynomials**: Polynomials with clustered or repeated roots may converge slowly; increase `maxIterations` if needed
0 commit comments