circuit-quantization 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,675 @@
1
+ # Vendored from sircuitenum/rational_check.py (GPL-3.0), sircuitenum commit b2c57e8.
2
+ import sympy as sym
3
+
4
+
5
+ def extract_numeric_part(expr, params):
6
+ """
7
+ Separates an expression into numeric and parametric parts.
8
+ For a sum like: 4a² - 180a - 707
9
+ Returns the constant term (numeric_part) and the rest.
10
+ """
11
+ expr = sym.simplify(expr).expand()
12
+
13
+ if not params:
14
+ return expr, sym.Integer(0)
15
+
16
+ try:
17
+ p = sym.Poly(expr, *params)
18
+ # Constant term (no parameters)
19
+ numeric_part = p.nth(*([0] * len(params)))
20
+ parametric_part = expr - numeric_part
21
+ return numeric_part, parametric_part
22
+ except Exception:
23
+ return None, expr
24
+
25
+
26
+ def is_perfect_power(n, k):
27
+ """Check if integer n is a perfect k-th power."""
28
+ if not isinstance(n, (int, sym.Integer)):
29
+ try:
30
+ n = int(n)
31
+ except (TypeError, ValueError):
32
+ return False
33
+
34
+ if n == 0:
35
+ return True
36
+
37
+ if n < 0:
38
+ if k % 2 == 0:
39
+ return False
40
+ n = abs(n)
41
+
42
+ root = round(abs(n) ** (1/k))
43
+ # Check root and neighbors due to floating point issues
44
+ for r in [root - 1, root, root + 1]:
45
+ if r > 0 and r ** k == abs(n):
46
+ return True
47
+ return False
48
+
49
+
50
+ def is_perfect_square(n):
51
+ """Check if n is a perfect square."""
52
+ return is_perfect_power(n, 2)
53
+
54
+
55
+ def is_perfect_cube(n):
56
+ """Check if n is a perfect cube."""
57
+ return is_perfect_power(n, 3)
58
+
59
+
60
+ def get_polynomial_params(poly):
61
+ """Extract free symbols from polynomial coefficients (the parameters)."""
62
+ params = set()
63
+ for coeff in poly.all_coeffs():
64
+ params.update(coeff.free_symbols)
65
+ return list(params)
66
+
67
+
68
+ def is_binomial(poly):
69
+ """
70
+ Check if polynomial is a binomial of form x^n + c or x^n - c.
71
+ Returns (True, n, c) if binomial, (False, None, None) otherwise.
72
+ """
73
+ # Get all terms with non-zero coefficients
74
+ terms = poly.as_dict()
75
+
76
+ if len(terms) != 2:
77
+ return False, None, None
78
+
79
+ n = sym.degree(poly)
80
+ gen = poly.gen
81
+
82
+ # Check for x^n term with coefficient 1
83
+ if (n,) not in terms or terms[(n,)] != 1:
84
+ return False, None, None
85
+
86
+ # Check for constant term
87
+ if (0,) not in terms:
88
+ return False, None, None
89
+
90
+ c = terms[(0,)]
91
+ return True, n, c
92
+
93
+
94
+ def is_even_powers_only(poly):
95
+ """
96
+ Check if polynomial contains only even powers of x.
97
+ Returns (True, reduced_poly) where reduced_poly is in terms of y = x².
98
+ """
99
+ terms = poly.as_dict()
100
+ gen = poly.gen
101
+
102
+ for (exp,) in terms.keys():
103
+ if exp % 2 != 0:
104
+ return False, None
105
+
106
+ # Create reduced polynomial in y = x²
107
+ y = sym.Symbol('_y_internal')
108
+ new_expr = sum(coeff * y**(exp // 2) for (exp,), coeff in terms.items())
109
+ reduced_poly = sym.Poly(new_expr, y)
110
+
111
+ return True, reduced_poly
112
+
113
+
114
+ def check_binomial_rationality(n, c, params):
115
+ """
116
+ Check if x^n - c has clean roots.
117
+ Clean if c is a perfect n-th power (for numeric c) or purely parametric.
118
+ """
119
+ if params:
120
+ numeric, parametric = extract_numeric_part(c, params)
121
+ if numeric is None:
122
+ return None, f"Could not analyze binomial constant {c}"
123
+ if parametric != 0:
124
+ # Mixed numeric and parametric
125
+ if numeric == 0:
126
+ return True, "Binomial with purely parametric constant"
127
+ # Check if numeric part is perfect n-th power
128
+ is_perf = is_perfect_power(abs(numeric), n)
129
+ return is_perf, f"Binomial x^{n} + {c}, numeric part {numeric} perfect {n}-th power: {is_perf}"
130
+ else:
131
+ # Pure numeric
132
+ is_perf = is_perfect_power(abs(c), n)
133
+ return is_perf, f"Binomial x^{n} + {c}, perfect {n}-th power: {is_perf}"
134
+ else:
135
+ # Pure numeric constant
136
+ try:
137
+ c_val = int(c)
138
+ is_perf = is_perfect_power(abs(c_val), n)
139
+ return is_perf, f"Binomial x^{n} + {c}, perfect {n}-th power: {is_perf}"
140
+ except (TypeError, ValueError):
141
+ return None, f"Could not evaluate binomial constant {c}"
142
+
143
+
144
+ def check_quadratic_rationality(poly):
145
+ """
146
+ For a quadratic polynomial, checks if roots are rational in parameters.
147
+
148
+ Condition: The discriminant Δ = b² - 4ac must have its numeric
149
+ (parameter-free) part be a perfect square (or zero).
150
+
151
+ Parameters:
152
+ poly: sympy Poly object, univariate quadratic
153
+
154
+ Returns: (is_rational, discriminant, numeric_part, explanation)
155
+ """
156
+ if sym.degree(poly) != 2:
157
+ raise ValueError(f"Expected degree 2, got {sym.degree(poly)}")
158
+
159
+ # Check for binomial x² + c
160
+ is_binom, n, c = is_binomial(poly)
161
+ if is_binom:
162
+ params = get_polynomial_params(poly)
163
+ result, explanation = check_binomial_rationality(n, -c, params)
164
+ return result, c, c, f"Binomial: {explanation}"
165
+
166
+ coeffs = poly.all_coeffs() # [a, b, c] for ax² + bx + c
167
+ a, b, c = coeffs
168
+
169
+ params = get_polynomial_params(poly)
170
+
171
+ disc = sym.simplify((b**2 - 4*a*c).expand())
172
+
173
+ numeric, parametric = extract_numeric_part(disc, params)
174
+
175
+ if numeric is None:
176
+ return None, disc, None, "Could not extract numeric part"
177
+
178
+ if parametric == 0:
179
+ is_rat = is_perfect_square(numeric)
180
+ return is_rat, disc, numeric, f"Pure numeric: {numeric}, perfect square: {is_rat}"
181
+
182
+ if numeric == 0:
183
+ return True, disc, sym.Integer(0), "No numeric constant in discriminant"
184
+
185
+ is_sq = is_perfect_square(abs(numeric))
186
+
187
+ explanation = f"Numeric part: {numeric}, is perfect square: {is_sq}"
188
+ if not is_sq:
189
+ explanation += " → roots will contain irrational constants"
190
+
191
+ return is_sq, disc, numeric, explanation
192
+
193
+
194
+ def check_cubic_rationality(poly):
195
+ """
196
+ For a cubic polynomial, checks if roots are rational in parameters.
197
+
198
+ Checks Cardano's D = (q/2)² + (p/3)³
199
+
200
+ Parameters:
201
+ poly: sympy Poly object, univariate cubic
202
+
203
+ Returns: (is_rational, D_expr, numeric_part, explanation)
204
+ """
205
+ if sym.degree(poly) != 3:
206
+ raise ValueError(f"Expected degree 3, got {sym.degree(poly)}")
207
+
208
+ # Check for binomial x³ + c
209
+ is_binom, n, c = is_binomial(poly)
210
+ if is_binom:
211
+ params = get_polynomial_params(poly)
212
+ result, explanation = check_binomial_rationality(n, -c, params)
213
+ return result, c, c, f"Binomial: {explanation}"
214
+
215
+ coeffs = poly.all_coeffs() # [a, b, c, d] for ax³ + bx² + cx + d
216
+ a, b, c, d = coeffs
217
+
218
+ params = get_polynomial_params(poly)
219
+
220
+ # Normalize to monic
221
+ b, c, d = b/a, c/a, d/a
222
+
223
+ # Depress: p and q for t³ + pt + q = 0
224
+ p = sym.simplify(c - b**2 / 3)
225
+ q = sym.simplify(d - b*c/3 + 2*b**3 / 27)
226
+
227
+ # Cardano's D
228
+ D = sym.simplify(((q/2)**2 + (p/3)**3).expand())
229
+
230
+ # Multiply by 108 to clear common denominators
231
+ D_scaled = sym.simplify((D * 108).expand())
232
+
233
+ numeric, parametric = extract_numeric_part(D_scaled, params)
234
+
235
+ if numeric is None:
236
+ return None, D, None, "Could not extract numeric part"
237
+
238
+ if parametric == 0:
239
+ is_sq = is_perfect_square(abs(numeric))
240
+ return is_sq, D, numeric, f"Pure numeric D*108: {numeric}"
241
+
242
+ if numeric == 0:
243
+ return True, D, sym.Integer(0), "No numeric constant in D → clean parametric roots"
244
+
245
+ is_sq = is_perfect_square(abs(numeric))
246
+
247
+ explanation = f"D*108 numeric part: {numeric}, is perfect square: {is_sq}"
248
+ if not is_sq:
249
+ explanation += " → roots will contain √(irrational)"
250
+
251
+ return is_sq, D, numeric, explanation
252
+
253
+
254
+ def check_quartic_rationality(poly):
255
+ """
256
+ For a quartic polynomial, checks if roots are rational in parameters.
257
+
258
+ Ferrari's method involves:
259
+ 1. A resolvent cubic
260
+ 2. Square roots of expressions involving the cubic's roots
261
+
262
+ We check the resolvent cubic's discriminant and the quartic's discriminant.
263
+
264
+ Also handles special cases:
265
+ - Binomials x⁴ + c
266
+ - Biquadratics (only even powers)
267
+
268
+ Parameters:
269
+ poly: sympy Poly object, univariate quartic
270
+
271
+ Returns: (is_rational, discriminant, numeric_part, explanation)
272
+ """
273
+ if sym.degree(poly) != 4:
274
+ raise ValueError(f"Expected degree 4, got {sym.degree(poly)}")
275
+
276
+ params = get_polynomial_params(poly)
277
+
278
+ # Check for binomial x⁴ + c
279
+ is_binom, n, c = is_binomial(poly)
280
+ if is_binom:
281
+ result, explanation = check_binomial_rationality(n, -c, params)
282
+ return result, c, c, f"Binomial: {explanation}"
283
+
284
+ # Check for biquadratic (only even powers)
285
+ is_even, reduced_poly = is_even_powers_only(poly)
286
+ if is_even:
287
+ # Reduce to quadratic in y = x²
288
+ reduced_degree = sym.degree(reduced_poly)
289
+ if reduced_degree == 2:
290
+ quad_result = check_quadratic_rationality(reduced_poly)
291
+ return quad_result[0], quad_result[1], quad_result[2], f"Biquadratic reduces to quadratic: {quad_result[3]}"
292
+ elif reduced_degree == 1:
293
+ return True, None, None, "Biquadratic reduces to linear"
294
+
295
+ coeffs = poly.all_coeffs()
296
+ a, b, c, d, e = coeffs # ax⁴ + bx³ + cx² + dx + e
297
+
298
+ # Normalize to monic
299
+ b, c, d, e = b/a, c/a, d/a, e/a
300
+
301
+ # Resolvent cubic: y³ - cy² + (bd - 4e)y - (b²e - 4ce + d²)
302
+ gen = poly.gen
303
+ rc_a = sym.Integer(1)
304
+ rc_b = -c
305
+ rc_c = b*d - 4*e
306
+ rc_d = -(b**2 * e - 4*c*e + d**2)
307
+
308
+ resolvent = sym.Poly(
309
+ rc_a * gen**3 + rc_b * gen**2 + rc_c * gen + rc_d,
310
+ gen
311
+ )
312
+
313
+ cubic_result = check_cubic_rationality(resolvent)
314
+
315
+ # Also compute quartic discriminant's numeric part
316
+ disc = sym.simplify((
317
+ 256*e**3 - 192*b*d*e**2 - 128*c**2*e**2 + 144*c*d**2*e
318
+ - 27*d**4 + 144*b*c**2*e - 6*b**2*d**2*e - 80*b*c*d**2
319
+ + 18*b*c*d**3 + 16*c**4 - 4*c**3*d**2 - 27*b**4*e**2
320
+ + 18*b**3*d*e - 4*b**2*c**3 + b**2*c**2*d**2
321
+ ).expand())
322
+
323
+ numeric, parametric = extract_numeric_part(disc, params)
324
+
325
+ if numeric is None:
326
+ return cubic_result[0], disc, None, f"Resolvent cubic check: {cubic_result[3]}"
327
+
328
+ if parametric == 0:
329
+ is_sq = is_perfect_square(abs(numeric))
330
+ return is_sq and cubic_result[0], disc, numeric, f"Pure numeric, resolvent: {cubic_result[0]}"
331
+
332
+ if numeric == 0:
333
+ return cubic_result[0], disc, sym.Integer(0), f"No numeric constant, resolvent: {cubic_result[3]}"
334
+
335
+ is_sq = is_perfect_square(abs(numeric))
336
+ combined = is_sq and cubic_result[0]
337
+
338
+ explanation = f"Disc numeric: {numeric}, perfect sq: {is_sq}, resolvent: {cubic_result[0]}"
339
+
340
+ return combined, disc, numeric, explanation
341
+
342
+
343
+ def check_higher_degree_rationality(poly):
344
+ """
345
+ For degree ≥ 5, we use a heuristic based on the discriminant.
346
+
347
+ The discriminant appears under radicals in any radical solution
348
+ (when one exists). If the numeric part isn't a perfect square,
349
+ the roots will contain irrational constants.
350
+
351
+ Also handles special cases:
352
+ - Binomials x^n + c
353
+ - Polynomials with only even powers
354
+
355
+ This is a necessary but NOT sufficient condition.
356
+ (Degree ≥ 5 polynomials may not even be solvable by radicals!)
357
+
358
+ Parameters:
359
+ poly: sympy Poly object
360
+
361
+ Returns: (passes_filter, discriminant, numeric_part, explanation)
362
+ """
363
+ n = sym.degree(poly)
364
+ params = get_polynomial_params(poly)
365
+
366
+ # Check for binomial x^n + c
367
+ is_binom, deg, c = is_binomial(poly)
368
+ if is_binom:
369
+ result, explanation = check_binomial_rationality(deg, -c, params)
370
+ return result, c, c, f"Binomial: {explanation}"
371
+
372
+ # Check for even powers only (reduces degree)
373
+ is_even, reduced_poly = is_even_powers_only(poly)
374
+ if is_even:
375
+ reduced_degree = sym.degree(reduced_poly)
376
+ if reduced_degree <= 4:
377
+ reduced_result, reduced_details = check_rationality(reduced_poly)
378
+ return reduced_result, None, None, f"Even powers only, reduces to degree {reduced_degree}: {reduced_details.get('explanation', '')}"
379
+ else:
380
+ # Recursively check the reduced polynomial
381
+ reduced_result = check_higher_degree_rationality(reduced_poly)
382
+ return reduced_result[0], reduced_result[1], reduced_result[2], f"Even powers only, reduces to degree {reduced_degree}: {reduced_result[3]}"
383
+
384
+ try:
385
+ disc = sym.simplify(sym.discriminant(poly).expand())
386
+ except Exception:
387
+ return None, None, None, "Could not compute discriminant"
388
+
389
+ numeric, parametric = extract_numeric_part(disc, params)
390
+
391
+ if numeric is None:
392
+ return None, disc, None, "Could not extract numeric part"
393
+
394
+ if parametric == 0:
395
+ is_sq = is_perfect_square(abs(numeric))
396
+ return is_sq, disc, numeric, f"Pure numeric discriminant, perfect square: {is_sq}"
397
+
398
+ if numeric == 0:
399
+ return True, disc, sym.Integer(0), "No numeric constant in discriminant (passes filter)"
400
+
401
+ is_sq = is_perfect_square(abs(numeric))
402
+
403
+ explanation = f"Degree {n}: Disc numeric part: {numeric}, perfect square: {is_sq}"
404
+ if not is_sq:
405
+ explanation += " → likely contains irrational constants"
406
+ else:
407
+ explanation += " → passes filter (but may still have irrationals)"
408
+
409
+ return is_sq, disc, numeric, explanation
410
+
411
+
412
+ def check_rationality(poly):
413
+ """
414
+ Main entry point: checks if polynomial roots are rational in parameters.
415
+
416
+ Parameters:
417
+ poly: sympy Poly object (univariate)
418
+
419
+ Returns: (passes_check, details_dict)
420
+ passes_check: True if roots likely contain only rational expressions
421
+ in (roots of) parameters
422
+ details_dict: Dictionary with discriminant, numeric_part, explanation
423
+ """
424
+ if not isinstance(poly, sym.Poly):
425
+ raise TypeError("Expected a sympy Poly object")
426
+
427
+ n = sym.degree(poly)
428
+
429
+ if n == 0:
430
+ return True, {"explanation": "Constant polynomial - no roots"}
431
+
432
+ if n == 1:
433
+ return True, {"explanation": "Linear polynomial - root is rational"}
434
+
435
+ if n == 2:
436
+ result, disc, numeric, explanation = check_quadratic_rationality(poly)
437
+ return result, {
438
+ "discriminant": disc,
439
+ "numeric_part": numeric,
440
+ "explanation": explanation
441
+ }
442
+
443
+ if n == 3:
444
+ result, D, numeric, explanation = check_cubic_rationality(poly)
445
+ return result, {
446
+ "cardano_D": D,
447
+ "numeric_part": numeric,
448
+ "explanation": explanation
449
+ }
450
+
451
+ if n == 4:
452
+ result, disc, numeric, explanation = check_quartic_rationality(poly)
453
+ return result, {
454
+ "discriminant": disc,
455
+ "numeric_part": numeric,
456
+ "explanation": explanation
457
+ }
458
+
459
+ # Degree 5+
460
+ result, disc, numeric, explanation = check_higher_degree_rationality(poly)
461
+ return result, {
462
+ "discriminant": disc,
463
+ "numeric_part": numeric,
464
+ "explanation": explanation,
465
+ "warning": "Degree ≥ 5: polynomial may not be solvable by radicals at all"
466
+ }
467
+
468
+
469
+ def has_clean_roots(poly):
470
+ """Simple boolean check - does the polynomial likely have clean roots?"""
471
+ result, _ = check_rationality(poly)
472
+ return result
473
+
474
+
475
+ def has_at_least_one_clean_root(poly):
476
+ """
477
+ Check if the polynomial has at least one "clean" root.
478
+
479
+ A clean root is one expressible as a rational function of
480
+ (roots of) the parameters, without irrational numeric constants.
481
+
482
+ Strategy:
483
+ 1. Try to factor the polynomial
484
+ 2. Check if any factor is linear (gives rational root)
485
+ 3. Check if any factor passes the all-roots-clean test
486
+
487
+ Parameters:
488
+ poly: sympy Poly object (univariate)
489
+
490
+ Returns: (has_clean_root, details_dict)
491
+ """
492
+ if not isinstance(poly, sym.Poly):
493
+ raise TypeError("Expected a sympy Poly object")
494
+
495
+ n = sym.degree(poly)
496
+
497
+ if n == 0:
498
+ return False, {"explanation": "Constant polynomial - no roots"}
499
+
500
+ if n == 1:
501
+ return True, {"explanation": "Linear polynomial - root is rational"}
502
+
503
+ params = get_polynomial_params(poly)
504
+ gen = poly.gen
505
+
506
+ # Try to factor the polynomial
507
+ try:
508
+ factors = sym.factor_list(poly.as_expr(), gen)
509
+ # factors is (content, [(factor1, mult1), (factor2, mult2), ...])
510
+ content, factor_list = factors
511
+
512
+ clean_factors = []
513
+
514
+ for factor_expr, multiplicity in factor_list:
515
+ factor_poly = sym.Poly(factor_expr, gen)
516
+ factor_degree = sym.degree(factor_poly)
517
+
518
+ # Linear factors always give clean roots
519
+ if factor_degree == 1:
520
+ # Extract the root from ax + b → x = -b/a
521
+ coeffs = factor_poly.all_coeffs()
522
+ a, b = coeffs
523
+ root = -b / a
524
+ clean_factors.append({
525
+ "factor": factor_expr,
526
+ "degree": 1,
527
+ "root": root,
528
+ "clean": True,
529
+ "multiplicity": multiplicity
530
+ })
531
+ continue
532
+
533
+ # For higher degree factors, check if all roots are clean
534
+ result, details = check_rationality(factor_poly)
535
+ clean_factors.append({
536
+ "factor": factor_expr,
537
+ "degree": factor_degree,
538
+ "clean": result,
539
+ "details": details,
540
+ "multiplicity": multiplicity
541
+ })
542
+
543
+ # Check if any factor has clean roots
544
+ has_clean = any(f["clean"] for f in clean_factors)
545
+
546
+ return has_clean, {
547
+ "explanation": "Factored polynomial",
548
+ "factors": clean_factors,
549
+ "num_clean_factors": sum(1 for f in clean_factors if f["clean"])
550
+ }
551
+
552
+ except Exception as e:
553
+ # Factoring failed, fall back to checking all roots
554
+ result, details = check_rationality(poly)
555
+ return result, {
556
+ "explanation": f"Could not factor, checked all roots: {details.get('explanation', '')}",
557
+ "factoring_error": str(e)
558
+ }
559
+
560
+
561
+ def get_clean_roots(poly):
562
+ """
563
+ Get the clean roots of a polynomial (if any).
564
+
565
+ Returns roots that are expressible as rational functions of
566
+ (roots of) the parameters.
567
+
568
+ Parameters:
569
+ poly: sympy Poly object (univariate)
570
+
571
+ Returns: list of clean roots (may be empty)
572
+ """
573
+ if not isinstance(poly, sym.Poly):
574
+ raise TypeError("Expected a sympy Poly object")
575
+
576
+ n = sym.degree(poly)
577
+ gen = poly.gen
578
+
579
+ if n == 0:
580
+ return []
581
+
582
+ if n == 1:
583
+ coeffs = poly.all_coeffs()
584
+ a, b = coeffs
585
+ return [sym.simplify(-b / a)]
586
+
587
+ clean_roots = []
588
+
589
+ # Try to factor the polynomial
590
+ try:
591
+ factors = sym.factor_list(poly.as_expr(), gen)
592
+ content, factor_list = factors
593
+
594
+ for factor_expr, multiplicity in factor_list:
595
+ factor_poly = sym.Poly(factor_expr, gen)
596
+ factor_degree = sym.degree(factor_poly)
597
+
598
+ # Linear factors give clean roots directly
599
+ if factor_degree == 1:
600
+ coeffs = factor_poly.all_coeffs()
601
+ a, b = coeffs
602
+ root = sym.simplify(-b / a)
603
+ clean_roots.extend([root] * multiplicity)
604
+ continue
605
+
606
+ # For higher degree factors, check if all roots are clean
607
+ result, _ = check_rationality(factor_poly)
608
+ if result:
609
+ # All roots of this factor are clean, solve it
610
+ try:
611
+ roots = sym.solve(factor_expr, gen)
612
+ clean_roots.extend(roots * multiplicity)
613
+ except Exception:
614
+ pass # Couldn't solve, skip
615
+
616
+ return clean_roots
617
+
618
+ except Exception:
619
+ # Factoring failed, try solving directly if all roots are clean
620
+ result, _ = check_rationality(poly)
621
+ if result:
622
+ try:
623
+ return sym.solve(poly.as_expr(), gen)
624
+ except Exception:
625
+ return []
626
+ return []
627
+
628
+
629
+ def count_clean_roots(poly):
630
+ """
631
+ Count how many clean roots the polynomial has (with multiplicity).
632
+
633
+ Parameters:
634
+ poly: sympy Poly object (univariate)
635
+
636
+ Returns: int
637
+ """
638
+ return len(get_clean_roots(poly))
639
+
640
+
641
+ def _has_rational_coeffs(expr, params):
642
+ """
643
+ Validates that 'expr' contains ONLY Rational numbers as coefficients.
644
+
645
+ Allowed:
646
+ - 1/2 * a
647
+ - sqrt(a) (Pow(a, 1/2) -> Base has params -> OK)
648
+ - (a + b)^(1/3) (Base has params -> OK)
649
+
650
+ Rejected:
651
+ - sqrt(2) * a (Pow(2, 1/2) -> Base is Number, not perfect square -> REJECT)
652
+ - pi * a (Transcendental number -> REJECT)
653
+ - 1.414 * a (Float -> REJECT, unless nsimplified first)
654
+ """
655
+ # Float Check
656
+ if expr.has(sym.Float):
657
+ return False
658
+
659
+ # Rejects Pi, E, any trig
660
+ if expr.has(sym.pi, sym.E, sym.sin, sym.cos, sym.tan):
661
+ return False
662
+
663
+ # Look for "Bad Powers" (sqrt(2), 5^(1/3))
664
+ # We look for Pow(b, e) where b is a Number.
665
+ for node in sym.preorder_traversal(expr):
666
+ if node.is_Pow:
667
+ node = sym.nsimplify(node)
668
+ base, exp = node.args
669
+ # Case A: Base is a Number (e.g., 2^(1/2))
670
+ if base.is_Number:
671
+ # If exponent is Integer, it's Rational (2^3 = 8). OK.
672
+ if not exp.is_Integer:
673
+ return False
674
+
675
+ return True