Eldar Sultanow
Eldar Sultanow

Reputation: 247

How can I solve a diophantine equation x^2-2w^2+2y^2-z^2-constant using SymPy

I have a triplet, for example (1806336, 1849600, 93636) and I need to solve the diophantine equation:

x^2-2w^2+2y^2-z^2-constant=0

Here the constant is 1806336 + 1849600 - 93636.

What I tried is:

from sympy.solvers.diophantine.diophantine import diop_general_pythagorean
from sympy.abc import x, w, y, z
tuple = [1806336, 1849600, 93636]
s = tuple[0]
t = tuple[1]
u = tuple[2]
diop_general_pythagorean(x**2 - 2*w**2 + 2*y**2 - z**2 - s-t+u)

which yields the exception

"This equation is not yet recognized or else has not been simplified sufficiently to put it in a form recognized by diop_classify()."

Without success, I also tried to avoid using the constant as a variable and wrote directly:

diop_general_pythagorean(x**2 - 2*w**2 + 2*y**2 - z**2 - 3562300)

The error remains. It would be great to have a chance solving such a diophantine equation directly using a constant which I may express as a variable.

Upvotes: 0

Views: 336

Answers (2)

JohanC
JohanC

Reputation: 80409

Maybe Z3 is an option?

from z3 import Ints, solve

x, y, z, w = Ints('x y z w')
s, t, u = (1806336, 1849600, 93636)
solve(x**2 - 2*w**2 + 2*y**2 - z**2 - s-t+u==0)

Gives [x = -368, w = -865, z = 14, y = 1569] as one possibility.

Upvotes: 3

smichr
smichr

Reputation: 19093

You already suggest a way to approach this problem in a way that SymPy can help. SymPy can solve x**2 - y**2 - c where c is a constant. Your problem can be thought of in 3 parts with that form:

x**2 - w**2 - s + y**2 + w**2 - t + y**2 - z**2 + u = 0

Here is a way it might be done, using a smaller constant, 28, which I will break up into 15 + 8 + 5. I am going to limit output to positive values since the negatives are redundantly true because of each value being squared.

>>> from sympy import diophantine
>>> from sympy.abc import x, y
>>> pos = lambda eq: [i for i in diophantine(eq) if all(j>0 for j in i)]

Since I broke 28 into 3 terms with the same sign, I will solve x**2 - y**2 - c for those 3 values of c and look for a solution that fits the pattern:

>>> pos(x**2-y**2-15)
[(8, 7), (4, 1)]
>>> pos(x**2-y**2-8)
[(3, 1)]
>>> pos(x**2-y**2-5)
[(3, 2)]

So for (x,w),(y,w),(y,z) the points (4,1),(3,1),(3,2) work. So look for a solution between sub-problems for s and t that have a matching 2nd element and then a solution for u that has a first element that match either of the first elements in the s and t candidates.

Upvotes: 2

Related Questions