fmcubic 0.1.0__tar.gz
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.
- fmcubic-0.1.0/LICENSE +22 -0
- fmcubic-0.1.0/PKG-INFO +152 -0
- fmcubic-0.1.0/README.md +141 -0
- fmcubic-0.1.0/fmcubic/__init__.py +36 -0
- fmcubic-0.1.0/fmcubic/benchmark.py +292 -0
- fmcubic-0.1.0/fmcubic/core.py +497 -0
- fmcubic-0.1.0/fmcubic.egg-info/PKG-INFO +152 -0
- fmcubic-0.1.0/fmcubic.egg-info/SOURCES.txt +12 -0
- fmcubic-0.1.0/fmcubic.egg-info/dependency_links.txt +1 -0
- fmcubic-0.1.0/fmcubic.egg-info/top_level.txt +1 -0
- fmcubic-0.1.0/pyproject.toml +23 -0
- fmcubic-0.1.0/setup.cfg +4 -0
- fmcubic-0.1.0/tests/test_patel_teja.py +55 -0
- fmcubic-0.1.0/tests/test_solver.py +110 -0
fmcubic-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ramón A. Fernández Molina, Leonardo Di G. Sigalotti,
|
|
4
|
+
Otto Rendón, Antonio J. Mejías
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
fmcubic-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fmcubic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Reference implementation of the Fernández Molina series method for solving cubic equations (AIP Advances 12, 045002, 2022).
|
|
5
|
+
Author: Ramón A. Fernández Molina, Leonardo Di G. Sigalotti, Otto Rendón, Antonio J. Mejías
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# fmcubic
|
|
13
|
+
|
|
14
|
+
A serious, honest Python implementation of the **Fernández Molina** series
|
|
15
|
+
method for cubic equations.
|
|
16
|
+
|
|
17
|
+
> **Reference:** Fernández Molina, R. A., Sigalotti, L. Di G., Rendón, O.,
|
|
18
|
+
> & Mejías, A. J. (2022). *A rapidly convergent method for solving
|
|
19
|
+
> third-order polynomials.* AIP Advances **12**, 045002.
|
|
20
|
+
> https://doi.org/10.1063/5.0073851
|
|
21
|
+
|
|
22
|
+
**Authors of the mathematical method:**
|
|
23
|
+
Ramón A. Fernández Molina · Leonardo Di G. Sigalotti · Otto Rendón · Antonio J. Mejías
|
|
24
|
+
|
|
25
|
+
Reference Python implementation of the method. Explicit about what is
|
|
26
|
+
faithful to the paper and what is a practical fallback in IEEE-754 double
|
|
27
|
+
precision.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install -e .
|
|
35
|
+
# or just drop the `fmcubic/` folder next to your code
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Requires Python ≥ 3.11 (uses `math.cbrt`).
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from fmcubic import solve_cubic
|
|
44
|
+
|
|
45
|
+
res = solve_cubic(1, -6, 11, -6)
|
|
46
|
+
print(res.method) # → "Fernández Molina (trigonometric fallback, Δ<0)"
|
|
47
|
+
print(res.branch) # → "trig-viete"
|
|
48
|
+
print(res.iterations) # → 1
|
|
49
|
+
for r in res.roots:
|
|
50
|
+
print(r) # → 1, 2, 3
|
|
51
|
+
print(res.notes)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`solve_cubic` returns a `SolveResult` with:
|
|
55
|
+
|
|
56
|
+
| field | meaning |
|
|
57
|
+
|----------------------|--------------------------------------------------|
|
|
58
|
+
| `roots` | three `Complex(re, im)` roots |
|
|
59
|
+
| `method` | human-readable branch label |
|
|
60
|
+
| `branch` | machine tag (`series-A-Eq17`, `trig-viete`, …) |
|
|
61
|
+
| `iterations` | series terms or Newton iterations |
|
|
62
|
+
| `convergence_trace` | per-step approximation of x₁ |
|
|
63
|
+
| `p`, `q`, `delta` | depressed-cubic invariants |
|
|
64
|
+
| `ratio` | `q²/(4Δ)` — the series convergence ratio |
|
|
65
|
+
| `notes` | free-form caveats |
|
|
66
|
+
|
|
67
|
+
## Branch tree
|
|
68
|
+
|
|
69
|
+
| Tag | Condition | Origin in paper | Status |
|
|
70
|
+
|-----------------|--------------------------|------------------------|-----------------------|
|
|
71
|
+
| `triple-root` | p≈0 ∧ q≈0 | trivial | **paper-faithful** |
|
|
72
|
+
| `p-axis-Eq57` | p≈0, q≠0 | Eq. (57) | **paper-faithful** |
|
|
73
|
+
| `delta-zero` | Δ≈0 | closed form | **paper-faithful** |
|
|
74
|
+
| `curve-Eq37-58` | p³ + 27q²/2 ≈ 0 | Eq. (37) + (58) | **paper-faithful** |
|
|
75
|
+
| `series-A-Eq17` | Δ>0, |q²/(4Δ)| < 1 | Eq. (17) | **paper-faithful** |
|
|
76
|
+
| `series-B-Eq30` | Δ>0, |q²/(4Δ)| > 1 | Eq. (30) | **paper-faithful** |
|
|
77
|
+
| `buffer-newton` | ||ratio|−1| < 0.05 | — | practical fallback |
|
|
78
|
+
| `trig-viete` | Δ < 0 | Viète | numerical equivalent |
|
|
79
|
+
|
|
80
|
+
The complex-branch series proposed in the paper for **Δ<0** is *not*
|
|
81
|
+
implemented faithfully — we use Viète's trigonometric form, which is
|
|
82
|
+
mathematically exact but is a substitution, not a faithful execution.
|
|
83
|
+
|
|
84
|
+
## Numerical honesty
|
|
85
|
+
|
|
86
|
+
The `solve_cubic` docstring spells this out, and the Patel-Teja benchmark
|
|
87
|
+
demonstrates it:
|
|
88
|
+
|
|
89
|
+
* `Δ = q²/4 + p³/27` suffers **catastrophic cancellation** when q²/4 ≈
|
|
90
|
+
−p³/27 (e.g. Patel-Teja case V: ~10⁵¹ + ~10⁵¹ → ~10³⁷). In double
|
|
91
|
+
precision this leaves ~2 significant digits of Δ.
|
|
92
|
+
* The series method consumes the *ratio* 4Δ/q², which is far less
|
|
93
|
+
sensitive — but any caller using Δ directly should be aware.
|
|
94
|
+
* Some polynomials evaluate to `f(x) == 0.0` exactly in IEEE-754 at a
|
|
95
|
+
point that **does not satisfy Vieta's relations** (spurious zero).
|
|
96
|
+
Use `vieta_residuals(A, B, C, D, roots)` to detect this.
|
|
97
|
+
|
|
98
|
+
## Patel-Teja benchmark
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from fmcubic import run_patel_teja_validation
|
|
102
|
+
|
|
103
|
+
report = run_patel_teja_validation()
|
|
104
|
+
print("OK?" , report.overall_pass)
|
|
105
|
+
for cr in report.cases:
|
|
106
|
+
print(cr.case.id, "Δ=", cr.delta, "pass=", cr.pass_overall)
|
|
107
|
+
for m in cr.methods:
|
|
108
|
+
print(" ", m.method, m.picked, "vieta-sum=", m.vieta["sum"])
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
| Case | Conditioning | Validation criterion |
|
|
112
|
+
|------|-------------------|------------------------------------|
|
|
113
|
+
| Z | severe (Δ ≈ 10⁻³⁴) | FM↔Cardano consensus |
|
|
114
|
+
| ρ | well conditioned | FM matches paper to <1e-7 % |
|
|
115
|
+
| V | extreme (10⁵¹+10⁵¹)| FM↔Cardano consensus + Vieta check |
|
|
116
|
+
|
|
117
|
+
## Tests
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
python -m pip install pytest
|
|
121
|
+
python -m pytest -q
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Covers: triple root, p=0/q≠0, Δ=0, three real roots, complex conjugates,
|
|
125
|
+
extreme coefficients, buffer zone, Patel-Teja Z/ρ/V, and Vieta consistency.
|
|
126
|
+
|
|
127
|
+
## What's faithful and what isn't (one-page summary)
|
|
128
|
+
|
|
129
|
+
**Faithful to the paper:**
|
|
130
|
+
- Depression to `y³ + py + q = 0` and shift recovery.
|
|
131
|
+
- All four closed-form special cases (B1-B4) including the special
|
|
132
|
+
curve `p³ + 27q²/2 = 0` (Eqs. 37 + 58).
|
|
133
|
+
- Series A (Eq. 17) and Series B (Eq. 30) with **incremental binomial
|
|
134
|
+
recurrence** `c_{i+1} = c_i · (1/3 − i)/(i+1)`.
|
|
135
|
+
- Stabilized Eq. (51)-(53) deflation Γ for the other two roots.
|
|
136
|
+
|
|
137
|
+
**Practical / equivalent:**
|
|
138
|
+
- Trigonometric (Viète) form for **Δ<0** — paper proposes a complex
|
|
139
|
+
series; we substitute the closed form.
|
|
140
|
+
- A small **buffer zone** around |ratio|=1 that switches to
|
|
141
|
+
Newton-Raphson because the series convergence is impractically slow
|
|
142
|
+
there.
|
|
143
|
+
|
|
144
|
+
**Not yet implemented:**
|
|
145
|
+
- The complex-branch series for Δ<0.
|
|
146
|
+
- Extended-precision (≥128-bit) mode required to reproduce Patel-Teja
|
|
147
|
+
cases Z and V exactly.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
Mathematical method © Fernández Molina, Sigalotti, Rendón & Mejías (2022).
|
|
152
|
+
Please cite: AIP Advances 12, 045002. https://doi.org/10.1063/5.0073851
|
fmcubic-0.1.0/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# fmcubic
|
|
2
|
+
|
|
3
|
+
A serious, honest Python implementation of the **Fernández Molina** series
|
|
4
|
+
method for cubic equations.
|
|
5
|
+
|
|
6
|
+
> **Reference:** Fernández Molina, R. A., Sigalotti, L. Di G., Rendón, O.,
|
|
7
|
+
> & Mejías, A. J. (2022). *A rapidly convergent method for solving
|
|
8
|
+
> third-order polynomials.* AIP Advances **12**, 045002.
|
|
9
|
+
> https://doi.org/10.1063/5.0073851
|
|
10
|
+
|
|
11
|
+
**Authors of the mathematical method:**
|
|
12
|
+
Ramón A. Fernández Molina · Leonardo Di G. Sigalotti · Otto Rendón · Antonio J. Mejías
|
|
13
|
+
|
|
14
|
+
Reference Python implementation of the method. Explicit about what is
|
|
15
|
+
faithful to the paper and what is a practical fallback in IEEE-754 double
|
|
16
|
+
precision.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install -e .
|
|
24
|
+
# or just drop the `fmcubic/` folder next to your code
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Requires Python ≥ 3.11 (uses `math.cbrt`).
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from fmcubic import solve_cubic
|
|
33
|
+
|
|
34
|
+
res = solve_cubic(1, -6, 11, -6)
|
|
35
|
+
print(res.method) # → "Fernández Molina (trigonometric fallback, Δ<0)"
|
|
36
|
+
print(res.branch) # → "trig-viete"
|
|
37
|
+
print(res.iterations) # → 1
|
|
38
|
+
for r in res.roots:
|
|
39
|
+
print(r) # → 1, 2, 3
|
|
40
|
+
print(res.notes)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`solve_cubic` returns a `SolveResult` with:
|
|
44
|
+
|
|
45
|
+
| field | meaning |
|
|
46
|
+
|----------------------|--------------------------------------------------|
|
|
47
|
+
| `roots` | three `Complex(re, im)` roots |
|
|
48
|
+
| `method` | human-readable branch label |
|
|
49
|
+
| `branch` | machine tag (`series-A-Eq17`, `trig-viete`, …) |
|
|
50
|
+
| `iterations` | series terms or Newton iterations |
|
|
51
|
+
| `convergence_trace` | per-step approximation of x₁ |
|
|
52
|
+
| `p`, `q`, `delta` | depressed-cubic invariants |
|
|
53
|
+
| `ratio` | `q²/(4Δ)` — the series convergence ratio |
|
|
54
|
+
| `notes` | free-form caveats |
|
|
55
|
+
|
|
56
|
+
## Branch tree
|
|
57
|
+
|
|
58
|
+
| Tag | Condition | Origin in paper | Status |
|
|
59
|
+
|-----------------|--------------------------|------------------------|-----------------------|
|
|
60
|
+
| `triple-root` | p≈0 ∧ q≈0 | trivial | **paper-faithful** |
|
|
61
|
+
| `p-axis-Eq57` | p≈0, q≠0 | Eq. (57) | **paper-faithful** |
|
|
62
|
+
| `delta-zero` | Δ≈0 | closed form | **paper-faithful** |
|
|
63
|
+
| `curve-Eq37-58` | p³ + 27q²/2 ≈ 0 | Eq. (37) + (58) | **paper-faithful** |
|
|
64
|
+
| `series-A-Eq17` | Δ>0, |q²/(4Δ)| < 1 | Eq. (17) | **paper-faithful** |
|
|
65
|
+
| `series-B-Eq30` | Δ>0, |q²/(4Δ)| > 1 | Eq. (30) | **paper-faithful** |
|
|
66
|
+
| `buffer-newton` | ||ratio|−1| < 0.05 | — | practical fallback |
|
|
67
|
+
| `trig-viete` | Δ < 0 | Viète | numerical equivalent |
|
|
68
|
+
|
|
69
|
+
The complex-branch series proposed in the paper for **Δ<0** is *not*
|
|
70
|
+
implemented faithfully — we use Viète's trigonometric form, which is
|
|
71
|
+
mathematically exact but is a substitution, not a faithful execution.
|
|
72
|
+
|
|
73
|
+
## Numerical honesty
|
|
74
|
+
|
|
75
|
+
The `solve_cubic` docstring spells this out, and the Patel-Teja benchmark
|
|
76
|
+
demonstrates it:
|
|
77
|
+
|
|
78
|
+
* `Δ = q²/4 + p³/27` suffers **catastrophic cancellation** when q²/4 ≈
|
|
79
|
+
−p³/27 (e.g. Patel-Teja case V: ~10⁵¹ + ~10⁵¹ → ~10³⁷). In double
|
|
80
|
+
precision this leaves ~2 significant digits of Δ.
|
|
81
|
+
* The series method consumes the *ratio* 4Δ/q², which is far less
|
|
82
|
+
sensitive — but any caller using Δ directly should be aware.
|
|
83
|
+
* Some polynomials evaluate to `f(x) == 0.0` exactly in IEEE-754 at a
|
|
84
|
+
point that **does not satisfy Vieta's relations** (spurious zero).
|
|
85
|
+
Use `vieta_residuals(A, B, C, D, roots)` to detect this.
|
|
86
|
+
|
|
87
|
+
## Patel-Teja benchmark
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from fmcubic import run_patel_teja_validation
|
|
91
|
+
|
|
92
|
+
report = run_patel_teja_validation()
|
|
93
|
+
print("OK?" , report.overall_pass)
|
|
94
|
+
for cr in report.cases:
|
|
95
|
+
print(cr.case.id, "Δ=", cr.delta, "pass=", cr.pass_overall)
|
|
96
|
+
for m in cr.methods:
|
|
97
|
+
print(" ", m.method, m.picked, "vieta-sum=", m.vieta["sum"])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
| Case | Conditioning | Validation criterion |
|
|
101
|
+
|------|-------------------|------------------------------------|
|
|
102
|
+
| Z | severe (Δ ≈ 10⁻³⁴) | FM↔Cardano consensus |
|
|
103
|
+
| ρ | well conditioned | FM matches paper to <1e-7 % |
|
|
104
|
+
| V | extreme (10⁵¹+10⁵¹)| FM↔Cardano consensus + Vieta check |
|
|
105
|
+
|
|
106
|
+
## Tests
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
python -m pip install pytest
|
|
110
|
+
python -m pytest -q
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Covers: triple root, p=0/q≠0, Δ=0, three real roots, complex conjugates,
|
|
114
|
+
extreme coefficients, buffer zone, Patel-Teja Z/ρ/V, and Vieta consistency.
|
|
115
|
+
|
|
116
|
+
## What's faithful and what isn't (one-page summary)
|
|
117
|
+
|
|
118
|
+
**Faithful to the paper:**
|
|
119
|
+
- Depression to `y³ + py + q = 0` and shift recovery.
|
|
120
|
+
- All four closed-form special cases (B1-B4) including the special
|
|
121
|
+
curve `p³ + 27q²/2 = 0` (Eqs. 37 + 58).
|
|
122
|
+
- Series A (Eq. 17) and Series B (Eq. 30) with **incremental binomial
|
|
123
|
+
recurrence** `c_{i+1} = c_i · (1/3 − i)/(i+1)`.
|
|
124
|
+
- Stabilized Eq. (51)-(53) deflation Γ for the other two roots.
|
|
125
|
+
|
|
126
|
+
**Practical / equivalent:**
|
|
127
|
+
- Trigonometric (Viète) form for **Δ<0** — paper proposes a complex
|
|
128
|
+
series; we substitute the closed form.
|
|
129
|
+
- A small **buffer zone** around |ratio|=1 that switches to
|
|
130
|
+
Newton-Raphson because the series convergence is impractically slow
|
|
131
|
+
there.
|
|
132
|
+
|
|
133
|
+
**Not yet implemented:**
|
|
134
|
+
- The complex-branch series for Δ<0.
|
|
135
|
+
- Extended-precision (≥128-bit) mode required to reproduce Patel-Teja
|
|
136
|
+
cases Z and V exactly.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
Mathematical method © Fernández Molina, Sigalotti, Rendón & Mejías (2022).
|
|
141
|
+
Please cite: AIP Advances 12, 045002. https://doi.org/10.1063/5.0073851
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
fmcubic — Fernández Molina cubic equation solver.
|
|
3
|
+
|
|
4
|
+
Reference:
|
|
5
|
+
Fernández Molina, J. M., et al. (2022).
|
|
6
|
+
"A series-based analytical solution to the cubic equation."
|
|
7
|
+
AIP Advances 12, 045002.
|
|
8
|
+
|
|
9
|
+
Public API:
|
|
10
|
+
solve_cubic(A, B, C, D, tol=1e-12, max_terms=200) -> SolveResult
|
|
11
|
+
"""
|
|
12
|
+
from .core import (
|
|
13
|
+
solve_cubic,
|
|
14
|
+
SolveResult,
|
|
15
|
+
Complex,
|
|
16
|
+
depress,
|
|
17
|
+
classify_stability,
|
|
18
|
+
vieta_residuals,
|
|
19
|
+
)
|
|
20
|
+
from .benchmark import (
|
|
21
|
+
PATEL_TEJA_CASES,
|
|
22
|
+
run_patel_teja_validation,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"solve_cubic",
|
|
27
|
+
"SolveResult",
|
|
28
|
+
"Complex",
|
|
29
|
+
"depress",
|
|
30
|
+
"classify_stability",
|
|
31
|
+
"vieta_residuals",
|
|
32
|
+
"PATEL_TEJA_CASES",
|
|
33
|
+
"run_patel_teja_validation",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"""
|
|
2
|
+
fmcubic.benchmark
|
|
3
|
+
=================
|
|
4
|
+
|
|
5
|
+
Patel-Teja / propylene benchmark from
|
|
6
|
+
Fernández Molina et al. (2022), Tables I-II.
|
|
7
|
+
|
|
8
|
+
This module is intentionally *honest* about double-precision limitations:
|
|
9
|
+
|
|
10
|
+
* Case Z and Case V have q²/4 and p³/27 of magnitude ~10⁵¹ that nearly
|
|
11
|
+
cancel — the resulting Δ retains only ~2 significant digits in IEEE-754
|
|
12
|
+
double precision. Any solver that needs Δ directly is degraded; the FM
|
|
13
|
+
series is partially shielded because it consumes the *ratio* 4Δ/q².
|
|
14
|
+
* For these two cases we therefore validate by **internal consensus**
|
|
15
|
+
between FM and Cardano (trigonometric branch), not by paper fidelity.
|
|
16
|
+
* Case ρ is well conditioned and reproduces the paper to <1e-7 % rel. err.
|
|
17
|
+
|
|
18
|
+
Newton-Raphson can land on a *spurious* root for case V because f(x)
|
|
19
|
+
evaluates exactly to 0.0 in IEEE-754 around x≈1.0626 even though the
|
|
20
|
+
triplet fails Vieta — see notes in core.py.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
from dataclasses import dataclass, field, asdict
|
|
26
|
+
from typing import List, Optional
|
|
27
|
+
|
|
28
|
+
from .core import (
|
|
29
|
+
solve_cubic,
|
|
30
|
+
SolveResult,
|
|
31
|
+
Complex,
|
|
32
|
+
vieta_residuals,
|
|
33
|
+
depress,
|
|
34
|
+
)
|
|
35
|
+
from math import isfinite
|
|
36
|
+
|
|
37
|
+
# Tolerances used by the benchmark — mirror the TS demo.
|
|
38
|
+
FM_TOLERANCE_PCT = 1e-4 # FM vs paper (where paper fidelity is claimed)
|
|
39
|
+
CONSENSUS_TOLERANCE_PCT = 1e-3 # FM vs Cardano (intra-double consensus)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True)
|
|
43
|
+
class PatelTejaCase:
|
|
44
|
+
id: str
|
|
45
|
+
label: str
|
|
46
|
+
A: float
|
|
47
|
+
B: float
|
|
48
|
+
C: float
|
|
49
|
+
D: float
|
|
50
|
+
paper_root: float
|
|
51
|
+
cardano_fails_in_paper: bool = False
|
|
52
|
+
double_precision_limited: bool = False
|
|
53
|
+
notes: str = ""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# Paper coefficients (Tables I-II). The "(n)" suffix in the paper means ×10ⁿ.
|
|
57
|
+
PATEL_TEJA_CASES: List[PatelTejaCase] = [
|
|
58
|
+
PatelTejaCase(
|
|
59
|
+
id="Z",
|
|
60
|
+
label="Compressibility factor Z (Cardano fails in paper)",
|
|
61
|
+
A=1.0,
|
|
62
|
+
B=-0.9999999995630439,
|
|
63
|
+
C= 2.8044233900191200e-8,
|
|
64
|
+
D=-2.3813809751410260e-17,
|
|
65
|
+
paper_root=8.765491016939650e-10, # xl(Exp) from Table I
|
|
66
|
+
cardano_fails_in_paper=True,
|
|
67
|
+
double_precision_limited=True,
|
|
68
|
+
notes=(
|
|
69
|
+
"Coefficients differ by 8-9 orders of magnitude. "
|
|
70
|
+
"Cardano gives ~9.477e-8 (wrong by 10^4%). "
|
|
71
|
+
"FM paper result: 8.765491017508983e-10. "
|
|
72
|
+
"In double, D ≈ 2.4e-17 is near machine epsilon × |C|, "
|
|
73
|
+
"causing ~2e-7% residual error — consistent with paper Table II."
|
|
74
|
+
),
|
|
75
|
+
),
|
|
76
|
+
PatelTejaCase(
|
|
77
|
+
id="rho",
|
|
78
|
+
label="Density ρ (well conditioned — Cardano also works)",
|
|
79
|
+
A=1.0,
|
|
80
|
+
B=-0.9714266137223527,
|
|
81
|
+
C= 2.857338622910670e-2,
|
|
82
|
+
D=-2.356986025368972e-11,
|
|
83
|
+
paper_root=0.9410637561882021, # xl(Exp) from Table I
|
|
84
|
+
notes=(
|
|
85
|
+
"Well conditioned: |Δ| ~ 1e-6, 4Δ/q² ≈ -7.29e-3. "
|
|
86
|
+
"FM paper result: 0.9410637546022087 (error 1.685e-7% vs experimental). "
|
|
87
|
+
"Cardano also correct here. FM reproduces paper to <1e-7% in double."
|
|
88
|
+
),
|
|
89
|
+
),
|
|
90
|
+
PatelTejaCase(
|
|
91
|
+
id="V",
|
|
92
|
+
label="Molar volume V (Cardano fails — extreme magnitudes)",
|
|
93
|
+
A=1.0,
|
|
94
|
+
B=-1.212284923269059e9, # note: (9) suffix in Table I means ×10^9
|
|
95
|
+
C= 4.121478037063378e10,
|
|
96
|
+
D=-4.242705529596227e10,
|
|
97
|
+
paper_root=1.0626272592311, # xl(Exp) from Table I
|
|
98
|
+
cardano_fails_in_paper=True,
|
|
99
|
+
double_precision_limited=True,
|
|
100
|
+
notes=(
|
|
101
|
+
"Coefficients span ~10 orders of magnitude; |Δ| ~ 10^37. "
|
|
102
|
+
"q²/4 ≈ p³/27 ≈ 4.354e51 — subtraction loses ~14 digits (catastrophic "
|
|
103
|
+
"cancellation). FM paper result: 1.062627261021974. "
|
|
104
|
+
"Cardano gives ~115.97 (wrong by 10^4%). "
|
|
105
|
+
"In double: FM/Cardano return a Vieta-consistent but artifactual triplet; "
|
|
106
|
+
"Newton finds a spurious zero where f(x)=0.0 exact in IEEE-754. "
|
|
107
|
+
"Requires ≥128-bit arithmetic for faithful reproduction."
|
|
108
|
+
),
|
|
109
|
+
),
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# ---------------------------------------------------------------------------
|
|
114
|
+
# Reference implementations of Cardano and Newton (kept here so the benchmark
|
|
115
|
+
# is self-contained and we can show the documented Cardano failure).
|
|
116
|
+
# ---------------------------------------------------------------------------
|
|
117
|
+
from math import sqrt, acos, cos, pi, copysign, cbrt as _cbrt
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _signed_cbrt(x: float) -> float:
|
|
121
|
+
if x == 0.0:
|
|
122
|
+
return 0.0
|
|
123
|
+
return copysign(_cbrt(abs(x)), x)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def solve_cardano(A: float, B: float, C: float, D: float) -> List[Complex]:
|
|
127
|
+
p, q, delta, shift = depress(A, B, C, D)
|
|
128
|
+
if delta > 0:
|
|
129
|
+
sq = sqrt(delta)
|
|
130
|
+
u = _signed_cbrt(-q / 2 + sq)
|
|
131
|
+
v = _signed_cbrt(-q / 2 - sq)
|
|
132
|
+
y1 = u + v
|
|
133
|
+
re = -(u + v) / 2
|
|
134
|
+
im = (sqrt(3) / 2) * (u - v)
|
|
135
|
+
return [
|
|
136
|
+
Complex(y1 - shift, 0.0),
|
|
137
|
+
Complex(re - shift, im),
|
|
138
|
+
Complex(re - shift, -im),
|
|
139
|
+
]
|
|
140
|
+
if abs(delta) < 1e-14:
|
|
141
|
+
u = _signed_cbrt(-q / 2)
|
|
142
|
+
return [Complex(2 * u - shift), Complex(-u - shift), Complex(-u - shift)]
|
|
143
|
+
# delta < 0 — trigonometric (Viète)
|
|
144
|
+
r = sqrt(-(p ** 3) / 27)
|
|
145
|
+
arg = max(-1.0, min(1.0, -q / (2 * r)))
|
|
146
|
+
phi = acos(arg)
|
|
147
|
+
m = 2 * _cbrt(r)
|
|
148
|
+
return [Complex(m * cos((phi + 2 * pi * k) / 3) - shift) for k in range(3)]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def solve_newton(A: float, B: float, C: float, D: float,
|
|
152
|
+
tol: float = 1e-12, max_iter: int = 200) -> List[Complex]:
|
|
153
|
+
f = lambda x: ((A * x + B) * x + C) * x + D
|
|
154
|
+
fp = lambda x: (3 * A * x + 2 * B) * x + C
|
|
155
|
+
x = -B / (3.0 * A)
|
|
156
|
+
for _ in range(max_iter):
|
|
157
|
+
fx, fpx = f(x), fp(x)
|
|
158
|
+
if abs(fx) < tol:
|
|
159
|
+
break
|
|
160
|
+
if abs(fpx) < 1e-18:
|
|
161
|
+
x += 1e-6; continue
|
|
162
|
+
nxt = x - fx / fpx
|
|
163
|
+
if abs(nxt - x) < tol:
|
|
164
|
+
x = nxt; break
|
|
165
|
+
x = nxt
|
|
166
|
+
b1 = B + A * x
|
|
167
|
+
c1 = C + b1 * x
|
|
168
|
+
disc = b1 * b1 - 4 * A * c1
|
|
169
|
+
out = [Complex(x)]
|
|
170
|
+
if disc >= 0:
|
|
171
|
+
s = sqrt(disc)
|
|
172
|
+
out.append(Complex((-b1 + s) / (2 * A)))
|
|
173
|
+
out.append(Complex((-b1 - s) / (2 * A)))
|
|
174
|
+
else:
|
|
175
|
+
s = sqrt(-disc)
|
|
176
|
+
out.append(Complex(-b1 / (2 * A), s / (2 * A)))
|
|
177
|
+
out.append(Complex(-b1 / (2 * A), -s / (2 * A)))
|
|
178
|
+
return out
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
# ---------------------------------------------------------------------------
|
|
182
|
+
# Picking the "physical" root (closest real root to the paper value)
|
|
183
|
+
# ---------------------------------------------------------------------------
|
|
184
|
+
def _pick_real(roots: List[Complex], paper: float) -> Complex:
|
|
185
|
+
real_roots = [r for r in roots if r.is_real(1e-6)]
|
|
186
|
+
pool = real_roots if real_roots else roots
|
|
187
|
+
return min(pool, key=lambda r: abs(r.re - paper))
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _rel_pct(value: float, target: float) -> float:
|
|
191
|
+
if not isfinite(value) or not isfinite(target) or target == 0:
|
|
192
|
+
return float("inf")
|
|
193
|
+
return 100.0 * abs(value - target) / abs(target)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@dataclass
|
|
197
|
+
class MethodOutcome:
|
|
198
|
+
method: str
|
|
199
|
+
picked: float
|
|
200
|
+
is_real: bool
|
|
201
|
+
error_vs_paper_pct: float
|
|
202
|
+
error_vs_consensus_pct: float
|
|
203
|
+
fail_expected: bool = False
|
|
204
|
+
iterations: int = 0
|
|
205
|
+
branch: Optional[str] = None
|
|
206
|
+
notes: str = ""
|
|
207
|
+
vieta: Optional[dict] = None
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@dataclass
|
|
211
|
+
class CaseReport:
|
|
212
|
+
case: PatelTejaCase
|
|
213
|
+
p: float
|
|
214
|
+
q: float
|
|
215
|
+
delta: float
|
|
216
|
+
ratio_4d_q2: float
|
|
217
|
+
methods: List[MethodOutcome]
|
|
218
|
+
pass_overall: bool
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@dataclass
|
|
222
|
+
class BenchmarkReport:
|
|
223
|
+
cases: List[CaseReport]
|
|
224
|
+
overall_pass: bool
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def run_patel_teja_validation() -> BenchmarkReport:
|
|
228
|
+
"""Run the three Patel-Teja cases through FM, Cardano and Newton."""
|
|
229
|
+
case_reports: List[CaseReport] = []
|
|
230
|
+
for tc in PATEL_TEJA_CASES:
|
|
231
|
+
fm_res = solve_cubic(tc.A, tc.B, tc.C, tc.D)
|
|
232
|
+
cd_roots = solve_cardano(tc.A, tc.B, tc.C, tc.D)
|
|
233
|
+
nw_roots = solve_newton(tc.A, tc.B, tc.C, tc.D)
|
|
234
|
+
|
|
235
|
+
fm_pick = _pick_real(fm_res.roots, tc.paper_root)
|
|
236
|
+
cd_pick = _pick_real(cd_roots, tc.paper_root)
|
|
237
|
+
nw_pick = _pick_real(nw_roots, tc.paper_root)
|
|
238
|
+
|
|
239
|
+
# Consensus = FM↔Cardano agreement (both run in the same precision).
|
|
240
|
+
consensus = (fm_pick.re + cd_pick.re) / 2.0
|
|
241
|
+
|
|
242
|
+
outcomes = [
|
|
243
|
+
MethodOutcome(
|
|
244
|
+
method="Fernández Molina",
|
|
245
|
+
picked=fm_pick.re,
|
|
246
|
+
is_real=fm_pick.is_real(1e-6),
|
|
247
|
+
error_vs_paper_pct =_rel_pct(fm_pick.re, tc.paper_root),
|
|
248
|
+
error_vs_consensus_pct=_rel_pct(fm_pick.re, consensus),
|
|
249
|
+
iterations=fm_res.iterations,
|
|
250
|
+
branch=fm_res.branch,
|
|
251
|
+
notes=fm_res.notes,
|
|
252
|
+
vieta=vieta_residuals(tc.A, tc.B, tc.C, tc.D, fm_res.roots),
|
|
253
|
+
),
|
|
254
|
+
MethodOutcome(
|
|
255
|
+
method="Cardano",
|
|
256
|
+
picked=cd_pick.re,
|
|
257
|
+
is_real=cd_pick.is_real(1e-6),
|
|
258
|
+
error_vs_paper_pct =_rel_pct(cd_pick.re, tc.paper_root),
|
|
259
|
+
error_vs_consensus_pct=_rel_pct(cd_pick.re, consensus),
|
|
260
|
+
fail_expected=tc.cardano_fails_in_paper,
|
|
261
|
+
vieta=vieta_residuals(tc.A, tc.B, tc.C, tc.D, cd_roots),
|
|
262
|
+
),
|
|
263
|
+
MethodOutcome(
|
|
264
|
+
method="Newton-Raphson",
|
|
265
|
+
picked=nw_pick.re,
|
|
266
|
+
is_real=nw_pick.is_real(1e-6),
|
|
267
|
+
error_vs_paper_pct =_rel_pct(nw_pick.re, tc.paper_root),
|
|
268
|
+
error_vs_consensus_pct=_rel_pct(nw_pick.re, consensus),
|
|
269
|
+
vieta=vieta_residuals(tc.A, tc.B, tc.C, tc.D, nw_roots),
|
|
270
|
+
),
|
|
271
|
+
]
|
|
272
|
+
|
|
273
|
+
# Pass criterion:
|
|
274
|
+
# - well conditioned cases → FM must reproduce paper.
|
|
275
|
+
# - double-limited cases → FM must agree with Cardano.
|
|
276
|
+
if tc.double_precision_limited:
|
|
277
|
+
case_pass = outcomes[0].error_vs_consensus_pct < CONSENSUS_TOLERANCE_PCT
|
|
278
|
+
else:
|
|
279
|
+
case_pass = outcomes[0].error_vs_paper_pct < FM_TOLERANCE_PCT
|
|
280
|
+
|
|
281
|
+
p, q, delta, _shift = depress(tc.A, tc.B, tc.C, tc.D)
|
|
282
|
+
ratio = (4 * delta) / (q * q) if q != 0 else float("nan")
|
|
283
|
+
|
|
284
|
+
case_reports.append(CaseReport(
|
|
285
|
+
case=tc, p=p, q=q, delta=delta, ratio_4d_q2=ratio,
|
|
286
|
+
methods=outcomes, pass_overall=case_pass,
|
|
287
|
+
))
|
|
288
|
+
|
|
289
|
+
return BenchmarkReport(
|
|
290
|
+
cases=case_reports,
|
|
291
|
+
overall_pass=all(c.pass_overall for c in case_reports),
|
|
292
|
+
)
|