Komheim
Komheim

Reputation: 1

How to solve a Diophantine equation with 3 variables and 3 equations using SymPy?

How does one add 3 variables and 3 equations using Python's SymPy? I tried using the documentation to SymPy, but it doesn't have any examples? One of the equations is to set a specific gcd.

Upvotes: 0

Views: 396

Answers (1)

Alex Sveshnikov
Alex Sveshnikov

Reputation: 4329

SymPy cannot solve inhomogeneous ternary quadratic equations, so your equation must be transformed. So, your initial equation is

x^2+y^2+z-xy-y-zx

Let's introduce two new variables u=y+z, and v=y-z. You can easily express the old variables as y=(u+v)/2, and z=(u-v)/2. Substitution of these formulas into the original equation gives after simplification

x^2+(u+v)^2/4-ux-v=0

Multiply this equation by 4:

4x^2+(u+v)^2-4ux-4v=0

Now, because (2x-u)^2 we can write the equation as

enter image description here

Let's introduce new variables

enter image description here

enter image description here

Then

enter image description here

and our equation becomes

enter image description here

Finally, this can be written as

enter image description here

That means, that a, b-2, and u-2 form a Pythagorian triple. The general solution for these is well known:

enter image description here

enter image description here

enter image description here

Now we need to return to our initial variables:

enter image description here

enter image description here

enter image description here

From the last equation we express u, substitute it to the first equation and use enter image description here to get the full solution in terms of initial variables:

enter image description here

enter image description here

enter image description here

It must be noted, that due to the nature of the substitution which we made in order to solve the equation, not every possible combination of p and q will give a solution of the initial equation in integer numbers. We got some spurious non-integer solutions here. However, every integer solution of the initial equation can be obtained from these formulas with some values of k, p, and q.

We can easily check that these formulas indeed give a solution, by substitution these formulas into the original equation and simplification, for example on Wolfram Alpha:

enter image description here

Now, let's take into account the last set of inequalities: x>=y>=z. It's obvious, that in order for y to be larger or equal to z, the parameter q must be equal to zero. If we substitute q=0 into expressions for x, y, and z we obtain the same result

enter image description here

It's obvious, that from this expression we can can get all integer numbers. Consequently, the only solution to your equation which satisfies the inequalities is x = y = z for all integer values.

Upvotes: 1

Related Questions