panchi.symbolic
from panchi.symbolic import QuadraticNumber, sqrt, Polynomial, exact_roots, quadratic_roots
Exact numbers
panchi.symbolic.QuadraticNumber
An exact number of the form a + b√d with rational a and b.
The radicand d is a squarefree integer other than 0 and 1. A negative
radicand makes the number complex: d = -1 is the imaginary unit i, so
QuadraticNumber(3, -2, -1) is 3 - 2i. These numbers are closed under
+, -, × and ÷ as long as every operand shares the same radicand, which
is exactly what the eigenvalues of a 2×2 or 3×3 matrix need.
Arithmetic results whose √d part cancels collapse back to a plain int or Fraction, so exact computations stay as simple as possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rational
|
int | Fraction
|
The rational part a. |
required |
coefficient
|
int | Fraction
|
The coefficient b of √d. |
required |
radicand
|
int
|
The integer d under the square root. It is reduced to squarefree
form, so |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or b is not an int or Fraction, or d is not an int. |
ValueError
|
If d is a perfect square (including 0 and 1), since √d would then be rational. |
Examples:
>>> golden = QuadraticNumber(Fraction(1, 2), Fraction(1, 2), 5)
>>> print(golden)
(1 + √5)/2
>>> golden * golden == golden + 1
True
>>> print(QuadraticNumber(1, 1, -1) * QuadraticNumber(1, -1, -1))
2
Source code in panchi/symbolic/quadratic_number.py
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
imag
property
The imaginary part, exactly (so the imaginary part of 1 + i√2 is √2).
is_real
property
True when the number has no imaginary part.
real
property
The real part, exactly.
__abs__()
Return the exact absolute value (the modulus, for complex numbers).
For a complex number this is √(a² - d·b²), which may involve a different square root than the number itself.
Source code in panchi/symbolic/quadratic_number.py
367 368 369 370 371 372 373 374 375 376 | |
__str__()
Return the number in standard mathematical form.
Examples:
>>> print(QuadraticNumber(Fraction(1, 2), Fraction(1, 2), 5))
(1 + √5)/2
>>> print(QuadraticNumber(1, -2, -3))
1 - 2i√3
Source code in panchi/symbolic/quadratic_number.py
433 434 435 436 437 438 439 440 441 442 443 444 | |
conjugate()
Return the conjugate a - b√d.
For a complex number this is the usual complex conjugate.
Source code in panchi/symbolic/quadratic_number.py
227 228 229 230 231 232 233 | |
norm()
Return the field norm (a + b√d)(a - b√d) = a² - d·b².
The norm is always rational. For a complex number it is |z|².
Source code in panchi/symbolic/quadratic_number.py
219 220 221 222 223 224 225 | |
panchi.symbolic.sqrt(value)
Return the exact square root of a rational number.
Perfect squares give an int or Fraction. Anything else gives a
QuadraticNumber in simplest radical form, so sqrt(12) is 2√3.
Negative values give imaginary results, so sqrt(-4) is 2i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | Fraction
|
The rational number whose square root to take. |
required |
Returns:
| Type | Description |
|---|---|
int | Fraction | QuadraticNumber
|
The exact square root. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If value is not an int or Fraction. |
Examples:
>>> sqrt(9)
3
>>> print(sqrt(8))
2√2
>>> print(sqrt(Fraction(1, 2)))
√2/2
>>> print(sqrt(-1))
i
Source code in panchi/symbolic/quadratic_number.py
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 | |
Polynomials
panchi.symbolic.Polynomial
A polynomial in one variable with exact coefficients.
Coefficients are listed from the constant term upwards, so
Polynomial([6, -5, 1]) is λ² - 5λ + 6. Coefficients may be ints,
Fractions or QuadraticNumbers, and trailing zero coefficients are
dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefficients
|
list[int | Fraction | QuadraticNumber]
|
The coefficients, constant term first. |
required |
variable
|
str
|
The name of the variable used when printing. Defaults to "λ". |
'λ'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If any coefficient is not an exact number. |
Examples:
>>> p = Polynomial([6, -5, 1])
>>> print(p)
λ² - 5λ + 6
>>> p(2)
0
Source code in panchi/symbolic/polynomial.py
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 | |
degree
property
The highest power with a non-zero coefficient (0 for constants).
leading_coefficient
property
The coefficient of the highest power.
__call__(value)
Evaluate the polynomial at a value using Horner's method.
Examples:
>>> Polynomial([6, -5, 1])(3)
0
Source code in panchi/symbolic/polynomial.py
135 136 137 138 139 140 141 142 143 144 145 146 147 | |
__str__()
Return the polynomial written from the highest power down.
Examples:
>>> print(Polynomial([-2, 0, 0, 1]))
λ³ - 2
Source code in panchi/symbolic/polynomial.py
291 292 293 294 295 296 297 298 299 300 | |
divide_by_root(root)
Divide by (λ - root) using synthetic division.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
int | Fraction | QuadraticNumber
|
The value r in the divisor (λ - r). |
required |
Returns:
| Type | Description |
|---|---|
tuple[Polynomial, Scalar]
|
The quotient and the remainder. The remainder is zero exactly when root is a root of the polynomial. |
Examples:
>>> quotient, remainder = Polynomial([6, -5, 1]).divide_by_root(2)
>>> print(quotient)
λ - 3
>>> remainder
0
Source code in panchi/symbolic/polynomial.py
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 | |
rational_roots()
Find every rational root using the rational root theorem.
After clearing denominators, any rational root p/q in lowest terms has p dividing the constant term and q dividing the leading coefficient, so only finitely many candidates need checking.
Returns:
| Type | Description |
|---|---|
list[int | Fraction]
|
The distinct rational roots, in increasing order. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any coefficient is irrational or complex. |
Examples:
>>> Polynomial([-6, 11, -6, 1]).rational_roots()
[1, 2, 3]
Source code in panchi/symbolic/polynomial.py
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 | |
Root finding
panchi.symbolic.exact_roots(polynomial)
Find every root of a polynomial exactly, with multiplicities.
Rational roots are found with the rational root theorem and divided out by synthetic division, repeatedly, so repeated roots are counted. Whatever remains must have degree at most 2, which is then solved with the quadratic formula. This is the by-hand method for characteristic polynomials of 2×2 and 3×3 matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polynomial
|
Polynomial
|
A non-constant polynomial with rational coefficients. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[Scalar, int]]
|
The distinct roots paired with their multiplicities, real roots first in increasing order, then complex ones. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the polynomial is constant, or if after removing rational roots a factor of degree 3 or more remains (for example λ³ - 2, whose roots involve cube roots and cannot be written as a + b√d). |
Examples:
>>> [(str(r), m) for r, m in exact_roots(Polynomial([-4, 8, -5, 1]))]
[('1', 1), ('2', 2)]
Source code in panchi/symbolic/roots.py
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 | |
panchi.symbolic.quadratic_roots(polynomial)
Solve a quadratic exactly with the quadratic formula.
For aλ² + bλ + c the roots are (-b ± √(b² - 4ac)) / 2a. The discriminant b² - 4ac decides their kind: a perfect square gives rational roots, any other positive value gives irrational ones such as 1 ± √2, a negative value gives a complex conjugate pair, and zero gives a single repeated root.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polynomial
|
Polynomial
|
A degree-2 polynomial with rational coefficients. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[Scalar, int]]
|
The distinct roots paired with their multiplicities. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the polynomial is not quadratic. |
Examples:
>>> [(str(r), m) for r, m in quadratic_roots(Polynomial([1, -2, 1]))]
[('1', 2)]
>>> [str(r) for r, _ in quadratic_roots(Polynomial([2, -2, 1]))]
['1 - i', '1 + i']
Source code in panchi/symbolic/roots.py
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 | |