Sympy change of base?

10 views
Skip to first unread message

John Connor

unread,
Jun 22, 2010, 12:54:01 AM6/22/10
to sy...@googlegroups.com
Hello all,
I was wondering how sympy handles numbers with different bases
(positional notation)? I would like to be able to convert a number
from any base to any base, but because 'base' means so many different
things in different contexts searching the docs / source has been
fruitless. I am currently abusing pythons int(x[, base]) function,
but it only works with bases up to 32, and I need to be able to use
arbitrarily large bases. Can sympy do this? If someone could just
point me in the right direction with the source or docs I would
appreciate it very much.

Thanks,
--Connor

Ondrej Certik

unread,
Jun 22, 2010, 1:13:23 AM6/22/10
to sy...@googlegroups.com

Is this just a printing issue? And a way to create new integers?

Ondrej

John Connor

unread,
Jun 22, 2010, 2:15:18 AM6/22/10
to sy...@googlegroups.com
It's not so much a printing issue as an ease of manipulation issue.
In base 16 I can go:
In [30]: hex = "ee36"

In [31]: int(hex, 16)
Out[31]: 60982

In [32]: int(hex[0], 16)
Out[32]: 14

In [34]: int(hex[2], 16)
Out[34]: 3

I would like to be able to do that with any base, so I could say:
base1000 = base(1000, 34987563294875623489756324576)

if int(base1000[0], 1000) > 900: Do_Stuff()
...

I was doing a bunch of iterations using divmod to pull apart the
integers, but then I realized that conceptually what I was doing was
just change of base operations with really large bases, and so I think
I can really clean up my code a *lot* if I can figure out a way to do
this. As far as printing goes, it isn't that important to me, because
once the base goes over 52 or so, things would start to get
incomprehensible (i.e.
e'k"rw12""34yf'4vw'e1234rkhfv'b124214"".124'5gew""rgsfv X 1000^-31)

Thanks,
--Connor

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
>

Aaron S. Meurer

unread,
Jun 22, 2010, 2:22:58 AM6/22/10
to sy...@googlegroups.com
Hi.

I do not think this is currently implemented in SymPy. If you would like to implement it, that would be great! Basically, you just have to extend the current Integer() to handle the base argument of int() (SymPy's Integer() is already a wrapper around int()), and maybe add some methods to do whatever things you need (change of base, etc.), and probably some kind of printer so you don't get confused.

By the way, for what it's worth, gmpy's mpz type supports multiple bases too:

In [7]: from gmpy import mpz

In [8]: mpz("ee36", 16)
Out[8]: 60982

Aaron Meurer

Bastian Weber

unread,
Jun 24, 2010, 10:56:49 AM6/24/10
to sy...@googlegroups.com
Hello all,


is there a way to simplify

(x**2)**(3*numbers.One()/2)

to

x**3

?

It is not that important for me but the expressions would look cleaner.

Thanks,
Bastian.


Stepan Roucka

unread,
Jun 24, 2010, 11:18:54 AM6/24/10
to sy...@googlegroups.com
Hello Bastian,
this simplification is valid only for positive x in general (try x = -1).

In [1]: x = Symbol("x", positive=True)

In [2]: (x**2)**(3*numbers.One()/2)
Out[2]:
3
x

for real x, it simplifies to absolute value

In [3]: x = Symbol("x", real=True)

In [4]: (x**2)**(3*numbers.One()/2)
Out[4]:
3
│x│


Best regards
Stepan

Bastian Weber

unread,
Jun 24, 2010, 11:36:15 AM6/24/10
to sy...@googlegroups.com
Hello Stepan,

thanks for the quick and good answer.

Greetings,
Bastian.

Kevin Hunter

unread,
Jun 24, 2010, 11:39:43 AM6/24/10
to sympy
Bastian,

Please start new threads, rather than hitting "Reply" in your mail
client and just changing the subject line. The message ids and
history information are still in the reply which messes up threading
for folks who read online, or for surfers after the fact. Reference
this very thread as a direct example.

http://groups.google.com/group/sympy/browse_thread/thread/4a9dda871d37e249?hl=en

Kevin Hunter

unread,
Jun 24, 2010, 11:40:03 AM6/24/10
to sympy
Mathematically speaking, another way to think about it is this:

sqrt(x**2) != x (same reasoning, try x < 0)

A little more generically:

sqrt(x**2) == norm(x) ("2 norm of x")

You may (not) be interested in this wikipedia article:
http://en.wikipedia.org/wiki/Norm_(mathematics)

Bastian Weber

unread,
Jun 24, 2010, 12:24:43 PM6/24/10
to sy...@googlegroups.com


Uff. I did not knew about that. Thank you very much for the advice.

Also thanks for the explanation with the 2-norm. In principle I know
that (x**2)**(3/2) == x**3 is only valid for non-negative x. But I
forgot to inform sympy about the implicit assumptions I had in mind.

Regards,
Bastian.

Aaron S. Meurer

unread,
Jun 25, 2010, 11:34:15 PM6/25/10
to sy...@googlegroups.com
Even so, I think we should have a function (powsimp() probably) that is able to convert (x**a)**b to x**(a*b), because it is a common simplification that I find myself unable to make in SymPy.

Aaron Meurer

Reply all
Reply to author
Forward
0 new messages