Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solveset_integers for Integer solution. #11234

Open
wants to merge 11 commits into
base: master
from

Conversation

Projects
None yet
7 participants
@Shekharrajak
Copy link
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,)}

@Shekharrajak Shekharrajak force-pushed the Shekharrajak:gsoc_diophantine branch from dc6091b to 152f413 Jun 13, 2016

@Shekharrajak

This comment has been minimized.

Copy link
Member Author

Shekharrajak commented Jun 13, 2016

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

@Shekharrajak

This comment has been minimized.

Copy link
Member Author

Shekharrajak commented Jun 15, 2016

@aktech

This comment has been minimized.

Copy link
Member

aktech commented Jun 15, 2016

What's the motivation behind this new function?

@Shekharrajak

This comment has been minimized.

Copy link
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()}

@hargup

This comment has been minimized.

Copy link
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.

@Shekharrajak

This comment has been minimized.

Copy link
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.

@Shekharrajak

This comment has been minimized.

Copy link
Member Author

Shekharrajak commented Jun 24, 2016

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

@Shekharrajak Shekharrajak force-pushed the Shekharrajak:gsoc_diophantine branch from 60288d1 to 94a7b33 Jun 25, 2016

@hargup

This comment has been minimized.

Copy link
Member

hargup commented Aug 4, 2016

@Shekharrajak There are merge conflicts please fix them.

@Shekharrajak

This comment has been minimized.

Copy link
Member Author

Shekharrajak commented Aug 5, 2016

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

@Shekharrajak Shekharrajak force-pushed the Shekharrajak:gsoc_diophantine branch from 74f286b to 6bd9689 Aug 5, 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..

This comment has been minimized.

@hargup

hargup Aug 7, 2016

Member

Extra ..

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

This comment has been minimized.

@hargup

hargup Aug 7, 2016

Member

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

This comment has been minimized.

@Shekharrajak

Shekharrajak Aug 9, 2016

Author Member

@hargup , I have edited this in new commit

@Shekharrajak Shekharrajak force-pushed the Shekharrajak:gsoc_diophantine branch from ad2392e to 899e69b Aug 8, 2016

@Shekharrajak Shekharrajak force-pushed the Shekharrajak:gsoc_diophantine branch 2 times, most recently from f664b8d to 8b36570 Aug 18, 2016

@Shekharrajak

This comment has been minimized.

Copy link
Member Author

Shekharrajak commented Aug 19, 2016

@Shekharrajak Shekharrajak force-pushed the Shekharrajak:gsoc_diophantine branch from 8b36570 to 4f60948 Aug 23, 2016

Shekharrajak added some commits Aug 23, 2016

@Shekharrajak

This comment has been minimized.

Copy link
Member Author

Shekharrajak commented Aug 23, 2016

@smichr

This comment has been minimized.

Copy link
Member

smichr commented Dec 16, 2016

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

#11334 has been committed.

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

@Shekharrajak Shekharrajak force-pushed the Shekharrajak:gsoc_diophantine branch from bddd29a to b4fb99a Dec 30, 2016

@Shekharrajak

This comment has been minimized.

Copy link
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).

@asmeurer asmeurer added this to the SymPy 1.1 milestone Jan 5, 2017

@rathmann

This comment has been minimized.

Copy link
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.

@Shekharrajak

This comment has been minimized.

Copy link
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.

@rathmann

This comment has been minimized.

Copy link
Contributor

rathmann commented Jan 20, 2017

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

@asmeurer

This comment has been minimized.

Copy link
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 asmeurer removed this from the SymPy 1.1 milestone Jun 27, 2017

@Shekharrajak

This comment has been minimized.

Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.