# solveset_integers for Integer solution. #11234

Open
wants to merge 11 commits into
from

## Conversation

Projects
None yet
7 participants
Member

### Shekharrajak commented Jun 13, 2016

 Solves a given equation in domain `S.Integers`. Using `diophantine`, defined in `solvers/diophantine.py`. Examples: ``````>>> pprint(solveset_integers(x**2 + 3*x*y + 4*x), use_unicode = False) {{(0, n1), (3*t_0 - 4, -t_0)} | n1, t_0 in Integers()} >>> pprint(solveset_integers(x**2 + 3*x*y + 4*x, [x, y]), use_unicode = False) {{(0, n1), (3*t_0 - 4, -t_0)} | n1, t_0 in Integers()} >>> pprint(solveset_integers(x**2 + 3*x*y + 4*x, [y, x]), use_unicode = False) {{(n1, 0), (-t_0, 3*t_0 - 4)} | n1, t_0 in Integers()} >>> pprint(solveset_integers(x**2 + 3*x*y + 4*x, [y, x], k), use_unicode = False) {{(-k_0, 3*k_0 - 4), (n1, 0)} | k_0, n1 in Integers()} >>> pprint(solveset_integers(2*x + 1), use_unicode = False) EmptySet() >>> pprint(solveset_integers(x + 1), use_unicode = False) {(-1,)} ``````

Member Author

### Shekharrajak commented Jun 13, 2016

 Ping @aktech @hargup @kshitij10496 @smichr . Please check the API and share your views.
Member Author

### Shekharrajak commented Jun 15, 2016

 Ping @aktech @hargup
Member

### aktech commented Jun 15, 2016

 What's the motivation behind this new function?
Member Author

### Shekharrajak commented Jun 15, 2016

 `solveset_integers` is for getting solution in Integer domain. When we have no Integer solution then it returns Emptyset. If finite number of integer solution(s) is there then FiniteSet ordered tuple otherwise (when parameter(s) is used in solution) ImageSet. Examples : `````` >>> pprint(solveset_integers(2*x + 1), use_unicode = False) EmptySet() >>> pprint(solveset_integers(x + 1), use_unicode = False) {(-1,)} >>> pprint(solveset_integers(x**2 + 3*x*y + 4*x, [x, y]), use_unicode = False) {{(0, n1), (3*t_0 - 4, -t_0)} | n1, t_0 in Integers()} ``````
Member

### hargup commented Jun 22, 2016

 @Shekharrajak Does diophantine solver returns all the solutions in all cases? If not can you please identify such cases and raise an error for them.
Member Author

### Shekharrajak commented Jun 22, 2016

 @hargup , It is for polynomial eq with Rational coefficients. I will add some lines in next commit. Thanks.
Member Author

### Shekharrajak commented Jun 24, 2016

 @hargup , `diophantine` itself raise `TypeError` if eq is non polynomial or don't have rational coeff.

Merged

Member

### hargup commented Aug 4, 2016

 @Shekharrajak There are merge conflicts please fix them.
Member Author

### Shekharrajak commented Aug 5, 2016 • edited

 @hargup , I will do required changes after this PR #11334

Closed

### hargup reviewed Aug 7, 2016

 @@ -882,6 +882,101 @@ def solveset_complex(f, symbol): return solveset(f, symbol, S.Complexes) def solveset_integers(f, symbols = None, param = Symbol("t", integer = True)): r"""Solves a given equation in domain S.Integers. `diophantine`, defined in solvers/diophantine.py; is used to get the integer solution..

#### hargup Aug 7, 2016

Member

Extra `.`.

### hargup reviewed Aug 7, 2016

 ======= Set A FiniteSet, if finite number of solution is present otherwise

#### hargup Aug 7, 2016

Member

Can you paraphrase the description for clarity. Using shorter sentences might help.

#### Shekharrajak Aug 9, 2016

Author Member

@hargup , I have edited this in new commit

Member Author

### Shekharrajak added some commits Jun 13, 2016

``` solveset_integers function added ```
``` 8a31fc8 ```
``` some more testcases ```
``` d1f4c79 ```
``` doctest edit ```
``` 6229d06 ```
``` return conditionset when required ```
``` 21335a1 ```
``` docstring minor change ```
``` 4f60948 ```

### Shekharrajak added some commits Aug 23, 2016

``` conflict fixed ```
``` a27bf70 ```
``` minor change ```
``` 21ab430 ```
Member Author

Member

### smichr commented Dec 16, 2016

 @hargup , I will do required changes after this PR #11334 #11334 has been committed.

### Shekharrajakforce-pushed the Shekharrajak:gsoc_diophantine branch 3 times, most recently from `21ab430` to `bddd29a`Dec 18, 2016

``` using _is_int() ```
``` b4fb99a ```

### Shekharrajakforce-pushed the Shekharrajak:gsoc_diophantine branch from `bddd29a` to `b4fb99a`Dec 30, 2016

``` few more doctest ```
``` a62e92c ```
Member Author

### Shekharrajak commented Jan 1, 2017

 @smichr , please review. One problem I see is, we can't iterate ImageSet having more than one lambda variables. `````` In [ ]: soln1 = solveset_integers(x**2 + 3*x*y + 4*x) In [ ]: soln2 = solveset_integers(x**2 + 3*x*y + 4*x, [x, y], param=None) In [ ]: soln1 Out[ ]: {{(0, n₁), (3⋅t₀ - 4, -t₀)} | n₁, t₀ ∊ ℤ} In [ ]: soln2 Out[ ]: {{(-4, 0), (0, n₁)} | n₁ ∊ ℤ} In [ ]: it = iter(soln2) In [ ]: next(it) Out[ ]: {(-4, 0), (0, 0)} # this will not work with soln1. I don't know whether there is way to do this (for multiple lambda variables). ``````

Contributor

### rathmann commented Jan 20, 2017

 More a question than a comment -- I have been using the diop_DN function currently checked into master (and so far, it is working well). That function " Returns one solution tuple, (`x, y)` for each class of the solutions. Other solutions of the class can be constructed according to the values of `D` and `N`" It is indeed straightforward to construct recurrences for further solutions, but how to do so is exactly the kind of knowledge that would be nice to incorporate into Sympy. Will this PR (or 12011) provide packaging for this case? Thanks.
Member Author

### Shekharrajak commented Jan 20, 2017

 @rathmann , we must use upper level functions, so we should use `diophantine()` to get all the solution(not `diop_DN()`) . See this example : `````` In [23]: eq Out[23]: 2 2 x - 13⋅y + 4 In [24]: diop_DN(13, -4) Out[24]: [(3, 1), (393, 109), (36, 10)] In [25]: diophantine(eq) Out[25]: ⎛⎡⎛ t t t t t t ⎜⎢⎜ 109⋅√13⋅(-180⋅√13 + 649) 393⋅(-180⋅√13 + 649) 393⋅(180⋅√13 + 649) 109⋅√13⋅(180⋅√13 + 649) 393⋅√13⋅(-180⋅√13 + 649) 109⋅(-180⋅√13 + 649) 393⋅√13⋅(180⋅√13 set⎜⎢⎜- ───────────────────────── + ───────────────────── + ──────────────────── + ────────────────────────, - ───────────────────────── + ───────────────────── + ──────────────── ⎝⎣⎝ 2 2 2 2 26 2 26 t t⎞ ⎛ t + 649) 109⋅(180⋅√13 + 649) ⎟ ⎜ t t t t 18⋅√13⋅(-180⋅√13 + 649) ──────── + ────────────────────⎟, ⎜- 5⋅√13⋅(-180⋅√13 + 649) + 18⋅(-180⋅√13 + 649) + 18⋅(180⋅√13 + 649) + 5⋅√13⋅(180⋅√13 + 649) , - ──────────────────────── + 5⋅(-180⋅√13 + 649) 2 ⎠ ⎝ 13 t ⎞ ⎛ t t t t t t 18⋅√13⋅(180⋅√13 + 649) t⎟ ⎜ √13⋅(-180⋅√13 + 649) 3⋅(-180⋅√13 + 649) 3⋅(180⋅√13 + 649) √13⋅(180⋅√13 + 649) 3⋅√13⋅(-180⋅√13 + 649) (-180⋅√1 + ─────────────────────── + 5⋅(180⋅√13 + 649) ⎟, ⎜- ───────────────────── + ─────────────────── + ────────────────── + ────────────────────, - ─────────────────────── + ──────── 13 ⎠ ⎝ 2 2 2 2 26 t t t⎞ ⎛ t t t t 3 + 649) 3⋅√13⋅(180⋅√13 + 649) (180⋅√13 + 649) ⎟ ⎜ 393⋅(-180⋅√13 + 649) 109⋅√13⋅(-180⋅√13 + 649) 109⋅√13⋅(180⋅√13 + 649) 393⋅(180⋅√13 + 649) 109⋅(-180⋅√13 + 6 ───────── + ────────────────────── + ────────────────⎟, ⎜- ───────────────────── + ───────────────────────── - ──────────────────────── - ────────────────────, - ───────────────── 2 26 2 ⎠ ⎝ 2 2 2 2 2 t t t t⎞ ⎛ 49) 393⋅√13⋅(-180⋅√13 + 649) 109⋅(180⋅√13 + 649) 393⋅√13⋅(180⋅√13 + 649) ⎟ ⎜ t t t ──── + ───────────────────────── - ──────────────────── - ────────────────────────⎟, ⎜- 18⋅(-180⋅√13 + 649) + 5⋅√13⋅(-180⋅√13 + 649) - 5⋅√13⋅(180⋅√13 + 649) - 18⋅(180⋅√13 + 649 26 2 26 ⎠ ⎝ t t⎞ ⎛ t t t t t 18⋅√13⋅(-180⋅√13 + 649) t 18⋅√13⋅(180⋅√13 + 649) ⎟ ⎜ 3⋅(-180⋅√13 + 649) √13⋅(-180⋅√13 + 649) √13⋅(180⋅√13 + 649) 3⋅(18 ) , - 5⋅(-180⋅√13 + 649) + ──────────────────────── - 5⋅(180⋅√13 + 649) - ───────────────────────⎟, ⎜- ─────────────────── + ───────────────────── - ──────────────────── - ───── 13 13 ⎠ ⎝ 2 2 2 t t t t t⎞⎤⎞ 0⋅√13 + 649) (-180⋅√13 + 649) 3⋅√13⋅(-180⋅√13 + 649) (180⋅√13 + 649) 3⋅√13⋅(180⋅√13 + 649) ⎟⎥⎟ ─────────────, - ───────────────── + ─────────────────────── - ──────────────── - ──────────────────────⎟⎥⎟ 2 2 26 2 26 ⎠⎦⎠ `````` The PR #12011 is only for combining the ImageSets (if possible). I hope I understood your query correctly.
Contributor

### rathmann commented Jan 20, 2017

 Got it, thanks. It makes sense that diop_DN would not have this as part of its interface.
Member

### asmeurer commented Jun 27, 2017

 I don't know why I had this on the 1.1 milestone, but it doesn't seem to be blocking. If anyone knows why this should block the release, and is able to finish it within the next couple of days, let me know.

### asmeurer removed this from the SymPy 1.1 milestone Jun 27, 2017

``` Merge branch 'master' into gsoc_diophantine ```
``` 06594c9 ```
Member Author

### Shekharrajak commented Jun 28, 2017

 If the output format is fine and thinking this is not that big issue, then I think, this PR is good to go. Let me know if I need to do something more in this PR.

### Shekharrajak reopened this Jun 28, 2017

``` Symbol imported ```
``` f5cca44 ```