Particular solution of Diophantine equation

17 views
Skip to first unread message

Faisal Riyaz

unread,
Jul 17, 2020, 8:42:32 AM7/17/20
to sy...@googlegroups.com
How can I get a single solution of a diophantine equation when the solution is returned
in terms of independent parameters?

>>> from sympy.abc import x, y, z, t
>>> from sympy import diophantine, Symbols
>>> diophantine(x + y + z, t)
{(t_0, t_1, -t_0 - t_1)}
>>> x, y, z = _.pop()

Substitution does not work:
>>> t_0, t_1 = Symbols('t_0 t_1')
>>> x.subs(t_0, 1)
t_0
>>> y.subs(t_1, 1)
t_1
>>> z.subs({t_0: 1, t_1: 1})
-t_0 - t_1

Thanks
Faisal Riyaz

Oscar Benjamin

unread,
Jul 17, 2020, 9:23:38 AM7/17/20
to sympy
This is because symbols in sympy only match if they have the same assumptions:

In [23]: (x, y, z), = diophantine(x + y + z, t)

In [24]: x
Out[24]: t₀

In [25]: type(x)
Out[25]: sympy.core.symbol.Symbol

In [26]: x.name
Out[26]: 't_0'

In [28]: x.assumptions0
Out[28]:
{'integer': True,
'complex': True,
'commutative': True,
'rational': True,
'algebraic': True,
'extended_real': True,
'imaginary': False,
'real': True,
'irrational': False,
'hermitian': True,
'finite': True,
'infinite': False,
'transcendental': False,
'noninteger': False}

In [29]: x == Symbol('t_0')
Out[29]: False

In [30]: x == Symbol('t_0', integer=True)
Out[30]: True


If we use integer=True then the substitution will work:

In [31]: t_0, t_1 = symbols('t_0, t_1', integer=True)

In [32]: tuple(s.subs({t_0:0, t_1:0}) for s in (x, y, z))
Out[32]: (0, 0, 0)


Note that this kind of situation was discussed in a recent thread and
you can use free symbols:

In [1]: sol, = diophantine(x + y + z, t)

In [2]: sol
Out[2]: (t₀, t₁, -t₀ - t₁)

In [3]: syms = Tuple(*sol).free_symbols

In [4]: rep = {s: 0 for s in syms}

In [5]: tuple(s.subs(rep) for s in sol)
Out[5]: (0, 0, 0)


Personally I think it's a poor API that inserts new symbols without
also returning the list of those symbols and it would be better if it
was like:

(sol, params), = diophantine(x + y + z, t)

Here params would directly return (t_0, t_1) for further use.

Alternatively it should be possible to provide the symbols directly:

sol, = diophantine(x + y + z, [t_0, t_1])


Oscar

Faisal Riyaz

unread,
Jul 17, 2020, 10:02:45 AM7/17/20
to sy...@googlegroups.com
I think we should put this on the docs page of the diophantine.

Alternatively it should be possible to provide the symbols directly:

sol, = diophantine(x + y + z, [t_0, t_1])

If the user does not have any idea about the number of parameters in the solution,
this won't work.

Thanks
Faisal

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxRgbgxzfP8AU-Ak9rdy53fgPQQx2kYKA3L4yqyeMb2pjQ%40mail.gmail.com.

Oscar Benjamin

unread,
Jul 17, 2020, 11:57:19 AM7/17/20
to sympy
On Fri, 17 Jul 2020 at 15:02, Faisal Riyaz <faisalr...@gmail.com> wrote:
>
> I think we should put this on the docs page of the diophantine.

I agree

>> Alternatively it should be possible to provide the symbols directly:
>>
>> sol, = diophantine(x + y + z, [t_0, t_1])
>
> If the user does not have any idea about the number of parameters in the solution,
> this won't work.

If you have three unknowns is it possible to end up with more than
three parameters in the solution?

Another possibility is an iterator that gives symbols.

--
Oscar

Faisal Riyaz

unread,
Jul 17, 2020, 12:12:29 PM7/17/20
to sy...@googlegroups.com
 If you have three unknowns is it possible to end up with more than
three parameters in the solution?

Yes, the number of parameters cannot be more than 3.
 
Another possibility is an iterator that gives symbols.

Yes, returning an iterator seems like a good solution.

Faisal

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages