Re: [sympy] Addition of 'BigInteger' to Numbers

84 views
Skip to first unread message

Tim Lahey

unread,
Mar 4, 2014, 8:06:51 PM3/4/14
to sy...@googlegroups.com
Hi,

Is there integers that Sympy can't represent? The Integer class is
independent of python int. What would this module add to Sympy?
Alternatively, should it be part of Numpy instead, if it's just
numerics?

I'm just a bit unclear on what you want to do.

Cheers,

Tim.

On 4 Mar 2014, at 18:59, Ambar Mehrotra wrote:

> Hi,
>
> I am Ambar Mehrotra and undergraduate from BITS Pilani, India pursuing
> Information Systems.
> I like to play with numbers and have a descent experience with python.
>
> I would like to suggest the addition of a 'BigInteger' module in the
> sympy.core.numbers class.
> Currently there is no convenient way of storing and working with very
> large
> numbers in python. I came across this problem while solving a question
> of
> Google Code Jam where python ran out of memory while doing a long
> calculation.
> I would like to take user input in form of a string and then use
> algorithms
> to do some basic math operations using numpy to make things faster.
>
> Please tell me if this idea is worth considering and guide me further
> if
> you like it.
>
> with regards,
> Ambar Mehrotra.
>
> --
> 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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/7ec24c8c-02d0-4e02-afb4-ca35a75207f5%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Ondřej Čertík

unread,
Mar 4, 2014, 8:08:47 PM3/4/14
to sympy
On Tue, Mar 4, 2014 at 4:59 PM, Ambar Mehrotra <ambar....@gmail.com> wrote:
> Hi,
>
> I am Ambar Mehrotra and undergraduate from BITS Pilani, India pursuing
> Information Systems.
> I like to play with numbers and have a descent experience with python.
>
> I would like to suggest the addition of a 'BigInteger' module in the
> sympy.core.numbers class.
> Currently there is no convenient way of storing and working with very large
> numbers in python. I came across this problem while solving a question of
> Google Code Jam where python ran out of memory while doing a long
> calculation.
> I would like to take user input in form of a string and then use algorithms
> to do some basic math operations using numpy to make things faster.
>
> Please tell me if this idea is worth considering and guide me further if you
> like it.

SymPy can also use GMP through gmpy through mpmath, so that should be
able to handle any large integers.

Can you post your code where you need large integers in Python that
runs out of memory?

Ondrej

Ambar Mehrotra

unread,
Mar 4, 2014, 8:37:20 PM3/4/14
to sy...@googlegroups.com
@Tim

Even in python an integer is automatically converted to long type but it cannot go further. And as sympy has a numbers class for other data types as well this can also be included. Wanted to build something similar to this http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html.

Tim Lahey

unread,
Mar 4, 2014, 8:41:49 PM3/4/14
to sy...@googlegroups.com
Hi,

Sympy's Integer class is not the same as a python int. Is there
something that the Integer class or gmpy through mpmath can't do?

Cheers,

Tim.

On 4 Mar 2014, at 20:37, Ambar Mehrotra wrote:

> @Tim
>
> Even in python an integer is automatically converted to long type but
> it
> cannot go further. And as sympy has a numbers class for other data
> types as
> well this can also be included. Wanted to build something similar to
> this
> http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html.
>
> On Wednesday, 5 March 2014 06:38:47 UTC+5:30, Ondřej Čertík wrote:
>>
>> On Tue, Mar 4, 2014 at 4:59 PM, Ambar Mehrotra
>> <ambar....@gmail.com<javascript:>>
>>> email to sympy+un...@googlegroups.com <javascript:>.
>>> To post to this group, send email to
>>> sy...@googlegroups.com<javascript:>.
>>
>>> Visit this group at http://groups.google.com/group/sympy.
>>> To view this discussion on the web visit
>>>
>> https://groups.google.com/d/msgid/sympy/7ec24c8c-02d0-4e02-afb4-ca35a75207f5%40googlegroups.com.
>>
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
> --
> 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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/7312fed9-2752-4de0-a9c1-9ddbd8b08767%40googlegroups.com.

Ambar Mehrotra

unread,
Mar 4, 2014, 8:53:43 PM3/4/14
to sy...@googlegroups.com
@certik,

I don't have the code with me now as this incident is more than an year old.
I want to implement it in various other functions for huge calculations which are native to sympy and do not require any external libraries to be installed. Since sympy has a numbers class this could make for a good addition to it.


On Wednesday, 5 March 2014 06:38:47 UTC+5:30, Ondřej Čertík wrote:

Ambar Mehrotra

unread,
Mar 4, 2014, 9:06:15 PM3/4/14
to sy...@googlegroups.com
@Tim,
This link http://docs.sympy.org/latest/tutorial/manipulation.html says that SymPy's Integer class is similar to python's built-in int type. It just plays well with other sympy classes.
Gmpy is currently not supported on google app engine so I presume it will have issues working with sympy live. Plus the point is creating something that is native to sympy rather than using other libraries, both because sympy already has a class for various data types and people use sympy a lot for calculations.

Aaron Meurer

unread,
Mar 4, 2014, 9:07:39 PM3/4/14
to sy...@googlegroups.com
Well NumPy is an external library.

SymPy numbers exist so that they play well with the rest of the SymPy
objects, and also so that division results in rational numbers, which
are not natively supported in Python.

I'm not convinced that Python's native int/long is inefficient, and
I'm *really* not convinced that you could do better in pure Python
(even with NumPy). And there's no way you can do as good as GMP,
because that's the gold standard.

But please prove me wrong. I haven't stressed Python's big integers to
the point of memory error before.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/a4532a04-24c8-4047-966e-ea250f880448%40googlegroups.com.

Tim Lahey

unread,
Mar 4, 2014, 9:10:12 PM3/4/14
to sy...@googlegroups.com
Hi,

I just tried the following in Sympy Live,

>>> a = Integer(10e300)
>>> a*a
100000000000000010500952051040884325415878661871005563386469026563183901107141845888855375804821112590900462906138078215714886901403063702688781015242737775003588508381608255081426313715035434527894909745341950247076736339289993280859129588137156894028824450360604004190215620763638302731984361410625939709810767686156430360442971171310648422643033807071123172876798870920735189695774152261693007436174667258386817039537801780474418562810236222461868084545932215472986213590436821015700837024803434436804935851157278080662231126577922910966922900650502620173747227034491861267997370685309929977282560000

It looks like it supports large integers.

Cheers,

Tim.
>>> an email to sympy+un...@googlegroups.com <javascript:>.
>>> To post to this group, send email to
>>> sy...@googlegroups.com<javascript:>.
>>
>>> Visit this group at http://groups.google.com/group/sympy.
>>> To view this discussion on the web visit
>>>
>> https://groups.google.com/d/msgid/sympy/7ec24c8c-02d0-4e02-afb4-ca35a75207f5%40googlegroups.com.
>>
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
> --
> 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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/cc383ace-4523-40be-9213-e00b347e2239%40googlegroups.com.

Ambar Mehrotra

unread,
Mar 4, 2014, 9:27:42 PM3/4/14
to sy...@googlegroups.com
@Tim,

Well seems like the idea was a bugger :p


On Wednesday, 5 March 2014 06:36:51 UTC+5:30, Tim Lahey wrote:

Ondřej Čertík

unread,
Mar 4, 2014, 11:17:45 PM3/4/14
to sympy
On Tue, Mar 4, 2014 at 6:37 PM, Ambar Mehrotra <ambar....@gmail.com> wrote:
> @Tim
>
> Even in python an integer is automatically converted to long type but it
> cannot go further. And as sympy has a numbers class for other data types as

You might be misunderstanding how Python integers work. Your
BigInteger class is just Python int. E.g., in pure Python:

>>> 2**100
1267650600228229401496703205376L
>>> 2**1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376L
>>> 2**10000
19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376L

etc.

Yes, Python ints are not as optimized as GMP for large integers, but
it shouldn't run out of memory or have other issues. It is just
slower, though usually fast enough for most applications.

If this speed really matters to you, then use gmpy with sympy and you
are all set.

Ondrej
> https://groups.google.com/d/msgid/sympy/7312fed9-2752-4de0-a9c1-9ddbd8b08767%40googlegroups.com.

Sergey Kirpichev

unread,
Mar 5, 2014, 8:30:27 AM3/5/14
to sy...@googlegroups.com

On Wednesday, March 5, 2014 5:08:47 AM UTC+4, Ondřej Čertík wrote:
Can you post your code where you need large integers in Python that
runs out of memory?

A good example is this one:
http://code.google.com/p/sympy/issues/detail?id=3736
:)

But I think it's a very different (and very difficult!) problem (mostly
related with rules of evaluation for sympy's objects.

Tim Lahey

unread,
Mar 5, 2014, 9:45:55 AM3/5/14
to sy...@googlegroups.com
That one appears to be an mpmath problem, rather than a Sympy problem (specifically). Even if Sympy had a BigInteger support, that wouldn't fix this problem as it fails in mpmath code (which doesn't use any Sympy classes).

I'd like to know of any examples of large integers that Sympy doesn't support.

Cheers,

Tim.
> --
> 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 post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/26bc3365-0553-4653-a432-6dc3a7c6f887%40googlegroups.com.

Sergey B Kirpichev

unread,
Mar 5, 2014, 10:12:55 AM3/5/14
to sy...@googlegroups.com
On Wed, Mar 05, 2014 at 09:45:55AM -0500, Tim Lahey wrote:
> That one appears to be an mpmath problem, rather than a Sympy problem (specifically). Even if Sympy had a BigInteger support, that wouldn't fix this problem as it fails in mpmath code (which doesn't use any Sympy classes).
> I'd like to know of any examples of large integers that Sympy doesn't support.

Just change in the above example E and floats to some rounded values.

In [1]: 2**2**2**2**7
---------------------------------------------------------------------------
MemoryError Traceback (most recent call
last)
<ipython-input-1-22f2c4609245> in <module>()
----> 1 2**2**2**2**7

MemoryError:

(isympy was under ulimit -v 1048576)

Aaron Meurer

unread,
Mar 5, 2014, 10:13:39 AM3/5/14
to sy...@googlegroups.com
That issue is about dealing with numbers like googolplex, which are
too big to be represented by any computer (even a computer the size of
the universe). The solution is to keep things unevaluated. A
googolplex is representable, as 10**10**100. It just needs to be kept
from evaluating.

This is a problem in SymPy that *hasn't* been solved. There are
suggestions on the issue on how to do so.

Once you implements unevaluated big numbers, it might be fun to
implement Knuth up arrow notation on them.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2FCB035D-F50C-4308-AD9B-9B314F548A56%40gmail.com.

Ondřej Čertík

unread,
Mar 5, 2014, 11:34:56 AM3/5/14
to sympy
On Wed, Mar 5, 2014 at 8:13 AM, Aaron Meurer <asme...@gmail.com> wrote:
> That issue is about dealing with numbers like googolplex, which are
> too big to be represented by any computer (even a computer the size of
> the universe). The solution is to keep things unevaluated. A
> googolplex is representable, as 10**10**100. It just needs to be kept
> from evaluating.
>
> This is a problem in SymPy that *hasn't* been solved. There are
> suggestions on the issue on how to do so.

Right, that's a good point.

Ondrej
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/-5371055096513839258%40unknownmsgid.
Reply all
Reply to author
Forward
0 new messages