Question

I know how to declare integer symbols:

from sympy import symbols


a, b, c, m, n = symbols('a b c m n', Integer=True)

But I want that symbols are natural numbers without zero (strictly positive integers). How can I do it? Thanks

Edit:

If I calculate diophantine equation, it gives me negative solutions too, and i want only positive solutions:

from sympy.solvers.diophantine import diophantine
from sympy import symbols


a, b, c, m, n = symbols('a b c m n', integer=True, positive=True)

a = n ** 2 - m ** 2
b = 2 * n * m
c = n ** 2 + m ** 2

diof = diophantine(n ** 2 + m * n - 500)
print(diof)

Look: {(121, 4), (499, -500), (95, -100), (499, 1), (-40, -10), (40, -50), (-499, 500), (-95, -5), (-40, 50), (-95, 100), (5, 20), (-5, 25), (-248, 250), (-499, -1), (121, -125), (5, -25), (-121, 125), (-5, -20), (-121, -4), (248, -250), (95, 5), (-248, -2), (40, 10), (248, 2)}

Was it helpful?

Solution

I've opened https://github.com/sympy/sympy/issues/7444 about this. In the mean time, you can just filter the solutions the old fashioned way.

>>> {(i, j) for i, j in diof if i > 0 and j > 0}
set([(5, 20), (40, 10), (95, 5), (121, 4), (248, 2), (499, 1)])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top