File size: 15,266 Bytes
9c6594c |
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 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 459 460 461 462 463 |
"""Tests for solvers of systems of polynomial equations. """
from sympy.polys.domains import ZZ, QQ_I
from sympy.core.numbers import (I, Integer, Rational)
from sympy.core.singleton import S
from sympy.core.symbol import symbols
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.polys.domains.rationalfield import QQ
from sympy.polys.polyerrors import UnsolvableFactorError
from sympy.polys.polyoptions import Options
from sympy.polys.polytools import Poly
from sympy.polys.rootoftools import CRootOf
from sympy.solvers.solvers import solve
from sympy.utilities.iterables import flatten
from sympy.abc import a, b, c, x, y, z
from sympy.polys import PolynomialError
from sympy.solvers.polysys import (solve_poly_system,
solve_triangulated,
solve_biquadratic, SolveFailed,
solve_generic, factor_system_bool,
factor_system_cond, factor_system_poly,
factor_system, _factor_sets, _factor_sets_slow)
from sympy.polys.polytools import parallel_poly_from_expr
from sympy.testing.pytest import raises
from sympy.core.relational import Eq
from sympy.functions.elementary.trigonometric import sin, cos
from sympy.functions.elementary.exponential import exp
def test_solve_poly_system():
assert solve_poly_system([x - 1], x) == [(S.One,)]
assert solve_poly_system([y - x, y - x - 1], x, y) is None
assert solve_poly_system([y - x**2, y + x**2], x, y) == [(S.Zero, S.Zero)]
assert solve_poly_system([2*x - 3, y*Rational(3, 2) - 2*x, z - 5*y], x, y, z) == \
[(Rational(3, 2), Integer(2), Integer(10))]
assert solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y) == \
[(0, 0), (2, -sqrt(2)), (2, sqrt(2))]
assert solve_poly_system([y - x**2, y + x**2 + 1], x, y) == \
[(-I*sqrt(S.Half), Rational(-1, 2)), (I*sqrt(S.Half), Rational(-1, 2))]
f_1 = x**2 + y + z - 1
f_2 = x + y**2 + z - 1
f_3 = x + y + z**2 - 1
a, b = sqrt(2) - 1, -sqrt(2) - 1
assert solve_poly_system([f_1, f_2, f_3], x, y, z) == \
[(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
solution = [(1, -1), (1, 1)]
assert solve_poly_system([Poly(x**2 - y**2), Poly(x - 1)]) == solution
assert solve_poly_system([x**2 - y**2, x - 1], x, y) == solution
assert solve_poly_system([x**2 - y**2, x - 1]) == solution
assert solve_poly_system(
[x + x*y - 3, y + x*y - 4], x, y) == [(-3, -2), (1, 2)]
raises(NotImplementedError, lambda: solve_poly_system([x**3 - y**3], x, y))
raises(NotImplementedError, lambda: solve_poly_system(
[z, -2*x*y**2 + x + y**2*z, y**2*(-z - 4) + 2]))
raises(PolynomialError, lambda: solve_poly_system([1/x], x))
raises(NotImplementedError, lambda: solve_poly_system(
[x-1,], (x, y)))
raises(NotImplementedError, lambda: solve_poly_system(
[y-1,], (x, y)))
# solve_poly_system should ideally construct solutions using
# CRootOf for the following four tests
assert solve_poly_system([x**5 - x + 1], [x], strict=False) == []
raises(UnsolvableFactorError, lambda: solve_poly_system(
[x**5 - x + 1], [x], strict=True))
assert solve_poly_system([(x - 1)*(x**5 - x + 1), y**2 - 1], [x, y],
strict=False) == [(1, -1), (1, 1)]
raises(UnsolvableFactorError,
lambda: solve_poly_system([(x - 1)*(x**5 - x + 1), y**2-1],
[x, y], strict=True))
def test_solve_generic():
NewOption = Options((x, y), {'domain': 'ZZ'})
assert solve_generic([x**2 - 2*y**2, y**2 - y + 1], NewOption) == \
[(-sqrt(-1 - sqrt(3)*I), Rational(1, 2) - sqrt(3)*I/2),
(sqrt(-1 - sqrt(3)*I), Rational(1, 2) - sqrt(3)*I/2),
(-sqrt(-1 + sqrt(3)*I), Rational(1, 2) + sqrt(3)*I/2),
(sqrt(-1 + sqrt(3)*I), Rational(1, 2) + sqrt(3)*I/2)]
# solve_generic should ideally construct solutions using
# CRootOf for the following two tests
assert solve_generic(
[2*x - y, (y - 1)*(y**5 - y + 1)], NewOption, strict=False) == \
[(Rational(1, 2), 1)]
raises(UnsolvableFactorError, lambda: solve_generic(
[2*x - y, (y - 1)*(y**5 - y + 1)], NewOption, strict=True))
def test_solve_biquadratic():
x0, y0, x1, y1, r = symbols('x0 y0 x1 y1 r')
f_1 = (x - 1)**2 + (y - 1)**2 - r**2
f_2 = (x - 2)**2 + (y - 2)**2 - r**2
s = sqrt(2*r**2 - 1)
a = (3 - s)/2
b = (3 + s)/2
assert solve_poly_system([f_1, f_2], x, y) == [(a, b), (b, a)]
f_1 = (x - 1)**2 + (y - 2)**2 - r**2
f_2 = (x - 1)**2 + (y - 1)**2 - r**2
assert solve_poly_system([f_1, f_2], x, y) == \
[(1 - sqrt((2*r - 1)*(2*r + 1))/2, Rational(3, 2)),
(1 + sqrt((2*r - 1)*(2*r + 1))/2, Rational(3, 2))]
query = lambda expr: expr.is_Pow and expr.exp is S.Half
f_1 = (x - 1 )**2 + (y - 2)**2 - r**2
f_2 = (x - x1)**2 + (y - 1)**2 - r**2
result = solve_poly_system([f_1, f_2], x, y)
assert len(result) == 2 and all(len(r) == 2 for r in result)
assert all(r.count(query) == 1 for r in flatten(result))
f_1 = (x - x0)**2 + (y - y0)**2 - r**2
f_2 = (x - x1)**2 + (y - y1)**2 - r**2
result = solve_poly_system([f_1, f_2], x, y)
assert len(result) == 2 and all(len(r) == 2 for r in result)
assert all(len(r.find(query)) == 1 for r in flatten(result))
s1 = (x*y - y, x**2 - x)
assert solve(s1) == [{x: 1}, {x: 0, y: 0}]
s2 = (x*y - x, y**2 - y)
assert solve(s2) == [{y: 1}, {x: 0, y: 0}]
gens = (x, y)
for seq in (s1, s2):
(f, g), opt = parallel_poly_from_expr(seq, *gens)
raises(SolveFailed, lambda: solve_biquadratic(f, g, opt))
seq = (x**2 + y**2 - 2, y**2 - 1)
(f, g), opt = parallel_poly_from_expr(seq, *gens)
assert solve_biquadratic(f, g, opt) == [
(-1, -1), (-1, 1), (1, -1), (1, 1)]
ans = [(0, -1), (0, 1)]
seq = (x**2 + y**2 - 1, y**2 - 1)
(f, g), opt = parallel_poly_from_expr(seq, *gens)
assert solve_biquadratic(f, g, opt) == ans
seq = (x**2 + y**2 - 1, x**2 - x + y**2 - 1)
(f, g), opt = parallel_poly_from_expr(seq, *gens)
assert solve_biquadratic(f, g, opt) == ans
def test_solve_triangulated():
f_1 = x**2 + y + z - 1
f_2 = x + y**2 + z - 1
f_3 = x + y + z**2 - 1
a, b = sqrt(2) - 1, -sqrt(2) - 1
assert solve_triangulated([f_1, f_2, f_3], x, y, z) == \
[(0, 0, 1), (0, 1, 0), (1, 0, 0)]
dom = QQ.algebraic_field(sqrt(2))
assert solve_triangulated([f_1, f_2, f_3], x, y, z, domain=dom) == \
[(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
a, b = CRootOf(z**2 + 2*z - 1, 0), CRootOf(z**2 + 2*z - 1, 1)
assert solve_triangulated([f_1, f_2, f_3], x, y, z, extension=True) == \
[(0, 0, 1), (0, 1, 0), (1, 0, 0), (a, a, a), (b, b, b)]
def test_solve_issue_3686():
roots = solve_poly_system([((x - 5)**2/250000 + (y - Rational(5, 10))**2/250000) - 1, x], x, y)
assert roots == [(0, S.Half - 15*sqrt(1111)), (0, S.Half + 15*sqrt(1111))]
roots = solve_poly_system([((x - 5)**2/250000 + (y - 5.0/10)**2/250000) - 1, x], x, y)
# TODO: does this really have to be so complicated?!
assert len(roots) == 2
assert roots[0][0] == 0
assert roots[0][1].epsilon_eq(-499.474999374969, 1e12)
assert roots[1][0] == 0
assert roots[1][1].epsilon_eq(500.474999374969, 1e12)
def test_factor_system():
assert factor_system([x**2 + 2*x + 1]) == [[x + 1]]
assert factor_system([x**2 + 2*x + 1, y**2 + 2*y + 1]) == [[x + 1, y + 1]]
assert factor_system([x**2 + 1]) == [[x**2 + 1]]
assert factor_system([]) == [[]]
assert factor_system([x**2 + y**2 + 2*x*y, x**2 - 2], extension=sqrt(2)) == [
[x + y, x + sqrt(2)],
[x + y, x - sqrt(2)],
]
assert factor_system([x**2 + 1, y**2 + 1], gaussian=True) == [
[x + I, y + I],
[x + I, y - I],
[x - I, y + I],
[x - I, y - I],
]
assert factor_system([x**2 + 1, y**2 + 1], domain=QQ_I) == [
[x + I, y + I],
[x + I, y - I],
[x - I, y + I],
[x - I, y - I],
]
assert factor_system([0]) == [[]]
assert factor_system([1]) == []
assert factor_system([0 , x]) == [[x]]
assert factor_system([1, 0, x]) == []
assert factor_system([x**4 - 1, y**6 - 1]) == [
[x**2 + 1, y**2 + y + 1],
[x**2 + 1, y**2 - y + 1],
[x**2 + 1, y + 1],
[x**2 + 1, y - 1],
[x + 1, y**2 + y + 1],
[x + 1, y**2 - y + 1],
[x - 1, y**2 + y + 1],
[x - 1, y**2 - y + 1],
[x + 1, y + 1],
[x + 1, y - 1],
[x - 1, y + 1],
[x - 1, y - 1],
]
assert factor_system([(x - 1)*(y - 2), (y - 2)*(z - 3)]) == [
[x - 1, z - 3],
[y - 2]
]
assert factor_system([sin(x)**2 + cos(x)**2 - 1, x]) == [
[x, sin(x)**2 + cos(x)**2 - 1],
]
assert factor_system([sin(x)**2 + cos(x)**2 - 1]) == [
[sin(x)**2 + cos(x)**2 - 1]
]
assert factor_system([sin(x)**2 + cos(x)**2]) == [
[sin(x)**2 + cos(x)**2]
]
assert factor_system([a*x, y, a]) == [[y, a]]
assert factor_system([a*x, y, a], [x, y]) == []
assert factor_system([a ** 2 * x, y], [x, y]) == [[x, y]]
assert factor_system([a*x*(x - 1), b*y, c], [x, y]) == []
assert factor_system([a*x*(x - 1), b*y, c], [x, y, c]) == [
[x - 1, y, c],
[x, y, c],
]
assert factor_system([a*x*(x - 1), b*y, c]) == [
[x - 1, y, c],
[x, y, c],
[x - 1, b, c],
[x, b, c],
[y, a, c],
[a, b, c],
]
assert factor_system([x**2 - 2], [y]) == []
assert factor_system([x**2 - 2], [x]) == [[x**2 - 2]]
assert factor_system([cos(x)**2 - sin(x)**2, cos(x)**2 + sin(x)**2 - 1]) == [
[sin(x)**2 + cos(x)**2 - 1, sin(x) + cos(x)],
[sin(x)**2 + cos(x)**2 - 1, -sin(x) + cos(x)],
]
assert factor_system([(cos(x) + sin(x))**2 - 1, cos(x)**2 - sin(x)**2 - cos(2*x)]) == [
[sin(x)**2 - cos(x)**2 + cos(2*x), sin(x) + cos(x) + 1],
[sin(x)**2 - cos(x)**2 + cos(2*x), sin(x) + cos(x) - 1],
]
assert factor_system([(cos(x) + sin(x))*exp(y) - 1, (cos(x) - sin(x))*exp(y) - 1]) == [
[exp(y)*sin(x) + exp(y)*cos(x) - 1, -exp(y)*sin(x) + exp(y)*cos(x) - 1]
]
def test_factor_system_poly():
px = lambda e: Poly(e, x)
pxab = lambda e: Poly(e, x, domain=ZZ[a, b])
pxI = lambda e: Poly(e, x, domain=QQ_I)
pxyz = lambda e: Poly(e, (x, y, z))
assert factor_system_poly([px(x**2 - 1), px(x**2 - 4)]) == [
[px(x + 2), px(x + 1)],
[px(x + 2), px(x - 1)],
[px(x + 1), px(x - 2)],
[px(x - 1), px(x - 2)],
]
assert factor_system_poly([px(x**2 - 1)]) == [[px(x + 1)], [px(x - 1)]]
assert factor_system_poly([pxyz(x**2*y - y), pxyz(x**2*z - z)]) == [
[pxyz(x + 1)],
[pxyz(x - 1)],
[pxyz(y), pxyz(z)],
]
assert factor_system_poly([px(x**2*(x - 1)**2), px(x*(x - 1))]) == [
[px(x)],
[px(x - 1)],
]
assert factor_system_poly([pxyz(x**2 + y*x), pxyz(x**2 + z*x)]) == [
[pxyz(x + y), pxyz(x + z)],
[pxyz(x)],
]
assert factor_system_poly([pxab((a - 1)*(x - 2)), pxab((b - 3)*(x - 2))]) == [
[pxab(x - 2)],
[pxab(a - 1), pxab(b - 3)],
]
assert factor_system_poly([pxI(x**2 + 1)]) == [[pxI(x + I)], [pxI(x - I)]]
assert factor_system_poly([]) == [[]]
assert factor_system_poly([px(1)]) == []
assert factor_system_poly([px(0), px(x)]) == [[px(x)]]
def test_factor_system_cond():
assert factor_system_cond([x ** 2 - 1, x ** 2 - 4]) == [
[x + 2, x + 1],
[x + 2, x - 1],
[x + 1, x - 2],
[x - 1, x - 2],
]
assert factor_system_cond([1]) == []
assert factor_system_cond([0]) == [[]]
assert factor_system_cond([1, x]) == []
assert factor_system_cond([0, x]) == [[x]]
assert factor_system_cond([]) == [[]]
assert factor_system_cond([x**2 + y*x]) == [[x + y], [x]]
assert factor_system_cond([(a - 1)*(x - 2), (b - 3)*(x - 2)], [x]) == [
[x - 2],
[a - 1, b - 3],
]
assert factor_system_cond([a * (x - 1), b], [x]) == [[x - 1, b], [a, b]]
assert factor_system_cond([a*x*(x-1), b*y, c], [x, y]) == [
[x - 1, y, c],
[x, y, c],
[x - 1, b, c],
[x, b, c],
[y, a, c],
[a, b, c],
]
assert factor_system_cond([x*(x-1), y], [x, y]) == [[x - 1, y], [x, y]]
assert factor_system_cond([a*x, y, a], [x, y]) == [[y, a]]
assert factor_system_cond([a*x, b*x], [x, y]) == [[x], [a, b]]
assert factor_system_cond([a*b*x, y], [x, y]) == [[x, y], [y, a*b]]
assert factor_system_cond([a*b*x, y]) == [[x, y], [y, a], [y, b]]
assert factor_system_cond([a**2*x, y], [x, y]) == [[x, y], [y, a]]
def test_factor_system_bool():
eqs = [a*(x - 1)*(y - 1), b*(x - 2)*(y - 1)*(y - 2)]
assert factor_system_bool(eqs, [x, y]) == (
Eq(y - 1, 0)
| (Eq(a, 0) & Eq(b, 0))
| (Eq(a, 0) & Eq(x - 2, 0))
| (Eq(a, 0) & Eq(y - 2, 0))
| (Eq(b, 0) & Eq(x - 1, 0))
| (Eq(x - 2, 0) & Eq(x - 1, 0))
| (Eq(x - 1, 0) & Eq(y - 2, 0))
)
assert factor_system_bool([x - 1], [x]) == Eq(x - 1, 0)
assert factor_system_bool([(x - 1)*(x - 2)], [x]) == Eq(x - 2, 0) | Eq(x - 1, 0)
assert factor_system_bool([], [x]) == True
assert factor_system_bool([0], [x]) == True
assert factor_system_bool([1], [x]) == False
assert factor_system_bool([a], [x]) == Eq(a, 0)
assert factor_system_bool([a * x, y, a], [x, y]) == Eq(a, 0) & Eq(y, 0)
assert (factor_system_bool([a*x, b*y*x, a], [x, y]) == (
Eq(a, 0) & Eq(b, 0))
| (Eq(a, 0) & Eq(x, 0))
| (Eq(a, 0) & Eq(y, 0)))
assert (factor_system_bool([a*x, b*x], [x, y]) == Eq(x, 0) |
(Eq(a, 0) & Eq(b, 0)))
assert (factor_system_bool([a*b*x, y], [x, y]) == (
Eq(x, 0) & Eq(y, 0)) |
(Eq(y, 0) & Eq(a*b, 0)))
assert (factor_system_bool([a**2*x, y], [x, y]) == (
Eq(a, 0) & Eq(y, 0)) |
(Eq(x, 0) & Eq(y, 0)))
assert factor_system_bool([a*x*y, b*y*z], [x, y, z]) == (
Eq(y, 0)
| (Eq(a, 0) & Eq(b, 0))
| (Eq(a, 0) & Eq(z, 0))
| (Eq(b, 0) & Eq(x, 0))
| (Eq(x, 0) & Eq(z, 0))
)
assert factor_system_bool([a*(x - 1), b], [x]) == (
(Eq(a, 0) & Eq(b, 0))
| (Eq(x - 1, 0) & Eq(b, 0))
)
def test_factor_sets():
#
from random import randint
def generate_random_system(n_eqs=3, n_factors=2, max_val=10):
return [
[randint(0, max_val) for _ in range(randint(1, n_factors))]
for _ in range(n_eqs)
]
test_cases = [
[[1, 2], [1, 3]],
[[1, 2], [3, 4]],
[[1], [1, 2], [2]],
]
for case in test_cases:
assert _factor_sets(case) == _factor_sets_slow(case)
for _ in range(100):
system = generate_random_system()
assert _factor_sets(system) == _factor_sets_slow(system)
|