1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! Integration tests for polynomial system solving with irrational roots.
//!
//! These tests verify that `solve_system` (backed by Gröbner basis +
//! back-substitution) correctly finds irrational roots by falling back
//! to the general symbolic solver when the rational root theorem returns
//! empty.
use num_bigint::BigInt;
use num_rational::Ratio;
use symplex::multipoly::{GrevLex, MultiPoly};
use symplex::polysys::solve_polynomial_system;
// ─── helpers ───────────────────────────────────────────────────────────────
use symplex::prelude::*;
fn rat(n: i64) -> Ratio<BigInt> {
Ratio::from_integer(BigInt::from(n))
}
/// Evaluate an expression to f64, returning None on failure.
fn eval(e: &symplex::expr::Ex) -> Option<f64> {
e.eval_f64().ok()
}
/// Check that a value is approximately zero.
fn approx_zero(val: f64, tol: f64) -> bool {
val.abs() < tol
}
// ═══════════════════════════════════════════════════════════════════════════
// x² + y² = 3, x = y → should find ±√(3/2) solutions
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn solve_system_irrational_circle_diagonal() {
let ctx = Context::new();
let x = ctx.symbol("x");
let y = ctx.symbol("y");
let eq1 = &x.powi(2) + &y.powi(2) - 3;
let eq2 = &x - &y;
let solutions = symplex::polysys::solve_system_ex(&[eq1, eq2], &[x.clone(), y.clone()]);
match solutions {
Ok(sols) => {
assert!(
!sols.is_empty(),
"x²+y²=3, x=y should have solutions (irrational), got empty"
);
// Each solution should satisfy both equations numerically.
for sol in &sols {
assert_eq!(sol.len(), 2, "each solution should have 2 values");
let x_val = eval(&sol[0]);
let y_val = eval(&sol[1]);
if let (Some(xv), Some(yv)) = (x_val, y_val) {
// Check x = y
assert!(
approx_zero(xv - yv, 1e-10),
"x should equal y: x={xv}, y={yv}"
);
// Check x² + y² = 3
let lhs = xv * xv + yv * yv;
assert!(
approx_zero(lhs - 3.0, 1e-10),
"x²+y² should be 3, got {lhs}"
);
}
}
// Should have exactly 2 solutions (positive and negative √(3/2)).
assert_eq!(
sols.len(),
2,
"expected 2 solutions for x²+y²=3, x=y, got {}",
sols.len()
);
}
Err(e) => {
panic!("solve_system should succeed for x²+y²=3, x=y: {e}");
}
}
}
// ═══════════════════════════════════════════════════════════════════════════
// x² + y² = 5, x*y = 2 → rational solutions (1,2), (2,1), (-1,-2), (-2,-1)
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn solve_system_rational_two_conics() {
let ctx = Context::new();
let x = ctx.symbol("x");
let y = ctx.symbol("y");
let eq1 = &x.powi(2) + &y.powi(2) - 5;
let eq2 = &x * &y - 2;
let solutions = symplex::polysys::solve_system_ex(&[eq1, eq2], &[x.clone(), y.clone()]);
match solutions {
Ok(sols) => {
assert_eq!(
sols.len(),
4,
"expected 4 solutions for x²+y²=5, xy=2, got {}",
sols.len()
);
// Verify each solution numerically.
for sol in &sols {
let x_val = eval(&sol[0]);
let y_val = eval(&sol[1]);
if let (Some(xv), Some(yv)) = (x_val, y_val) {
let eq1_val = xv * xv + yv * yv - 5.0;
let eq2_val = xv * yv - 2.0;
assert!(
approx_zero(eq1_val, 1e-10),
"x²+y²-5 should ≈ 0, got {eq1_val} for x={xv}, y={yv}"
);
assert!(
approx_zero(eq2_val, 1e-10),
"xy-2 should ≈ 0, got {eq2_val} for x={xv}, y={yv}"
);
}
}
}
Err(e) => {
panic!("solve_system should succeed for x²+y²=5, xy=2: {e}");
}
}
}
// ═══════════════════════════════════════════════════════════════════════════
// x² = 2 (univariate via Gröbner) → √2, -√2
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn solve_system_univariate_irrational() {
let ctx = Context::new();
let x = ctx.symbol("x");
let eq = &x.powi(2) - 2;
let solutions = symplex::polysys::solve_system_ex(&[eq], std::slice::from_ref(&x));
match solutions {
Ok(sols) => {
assert!(
!sols.is_empty(),
"x²=2 should have solutions (±√2), got empty"
);
for sol in &sols {
let x_val = eval(&sol[0]);
if let Some(xv) = x_val {
let check = xv * xv - 2.0;
assert!(
approx_zero(check, 1e-10),
"x² should be 2, got x={xv}, x²={}",
xv * xv
);
}
}
assert_eq!(
sols.len(),
2,
"x²=2 should have 2 solutions, got {}",
sols.len()
);
}
Err(e) => {
panic!("solve_system should succeed for x²=2: {e}");
}
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Low-level: rational root theorem returns empty for irrational poly
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn polysys_rational_root_empty_for_x2_minus_2() {
// The rational-only solver should return empty for x²-2=0.
let x = MultiPoly::<GrevLex>::var(1, 0);
let p = &x * &x - MultiPoly::from_int(1, 2);
let sols = solve_polynomial_system(&[p]).unwrap();
// This returns empty because ±√2 are irrational.
assert!(
sols.is_empty(),
"rational-only solver should return empty for x²-2, got {:?}",
sols
);
}
// ═══════════════════════════════════════════════════════════════════════════
// Low-level: rational root theorem works for rational roots
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn polysys_rational_root_finds_rationals() {
// x² - 4 = 0 → x = ±2 (rational roots exist).
let x = MultiPoly::<GrevLex>::var(1, 0);
let p = &x * &x - MultiPoly::from_int(1, 4);
let sols = solve_polynomial_system(&[p]).unwrap();
assert_eq!(sols.len(), 2, "x²-4 should have 2 rational roots");
let mut root_vals: Vec<i64> = sols
.iter()
.map(|s| s[0].to_integer().try_into().unwrap())
.collect();
root_vals.sort();
assert_eq!(root_vals, vec![-2, 2]);
}
// ═══════════════════════════════════════════════════════════════════════════
// Verify no regression: circle-line system (rational solutions)
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn polysys_circle_line_no_regression() {
// x² + y² = 1, x + y = 1 → (0,1), (1,0)
let nv = 2;
let x = MultiPoly::<GrevLex>::var(nv, 0);
let y = MultiPoly::<GrevLex>::var(nv, 1);
let one = MultiPoly::from_int(nv, 1);
let circle = &(&x * &x) + &(&y * &y) - one.clone();
let line = &x + &y - one;
let sols = solve_polynomial_system(&[circle, line]).unwrap();
assert_eq!(
sols.len(),
2,
"circle-line should have 2 rational solutions"
);
// Verify each solution.
for sol in &sols {
let xv = &sol[0];
let yv = &sol[1];
// x² + y² = 1
let check = xv * xv + yv * yv;
assert!(check == rat(1), "x²+y²=1 check failed: got {check}");
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Verify symbolic fallback for 2-var system with irrational roots
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn solve_system_ex_irrational_symmetric() {
let ctx = Context::new();
// x² + y² = 2, x = y → x = y = ±1 (these are actually rational!)
// But this verifies the fallback path doesn't break rational-solution systems.
let x = ctx.symbol("x");
let y = ctx.symbol("y");
let eq1 = &x.powi(2) + &y.powi(2) - 2;
let eq2 = &x - &y;
let solutions = symplex::polysys::solve_system_ex(&[eq1, eq2], &[x.clone(), y.clone()]);
match solutions {
Ok(sols) => {
assert!(
!sols.is_empty(),
"x²+y²=2, x=y should have solutions, got empty"
);
for sol in &sols {
if let (Some(xv), Some(yv)) = (eval(&sol[0]), eval(&sol[1])) {
assert!(approx_zero(xv - yv, 1e-10), "x should equal y");
assert!(
approx_zero(xv * xv + yv * yv - 2.0, 1e-10),
"x²+y² should be 2"
);
}
}
}
Err(e) => {
panic!("solve_system should succeed: {e}");
}
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Edge case: system with no real solutions
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn solve_system_no_real_solutions() {
let ctx = Context::new();
// x² + 1 = 0 has no real solutions.
let x = ctx.symbol("x");
let eq = &x.powi(2) + 1;
let solutions = symplex::polysys::solve_system_ex(&[eq], std::slice::from_ref(&x));
match solutions {
Ok(sols) => {
// The solver may return complex solutions or empty.
// With the symbolic fallback, it might find ±i.
// Either way, any returned solution should satisfy the equation.
for sol in &sols {
let x_val = eval(&sol[0]);
// Complex solutions won't evaluate to real f64,
// so eval might fail — that's acceptable.
if let Some(xv) = x_val {
let check = xv * xv + 1.0;
assert!(
approx_zero(check, 1e-10),
"if a real root is returned, it must satisfy x²+1=0"
);
}
}
}
Err(_) => {
// Also acceptable: the system might be flagged as having
// no real solutions.
}
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Linear system: verify no regression
// ═══════════════════════════════════════════════════════════════════════════
#[test]
fn solve_system_linear_no_regression() {
let ctx = Context::new();
let x = ctx.symbol("x");
let y = ctx.symbol("y");
// x + y = 1, x - y = 0 → x = y = 1/2
let eq1 = &x + &y - 1;
let eq2 = &x - &y;
let solutions =
symplex::polysys::solve_system_ex(&[eq1, eq2], &[x.clone(), y.clone()]).unwrap();
assert_eq!(solutions.len(), 1, "linear system should have 1 solution");
let x_str = format!("{}", solutions[0][0]);
let y_str = format!("{}", solutions[0][1]);
assert_eq!(x_str, "1/2", "x should be 1/2, got {x_str}");
assert_eq!(y_str, "1/2", "y should be 1/2, got {y_str}");
}