Simplify number

40 views
Skip to first unread message

SiL588 .

unread,
May 29, 2014, 10:43:26 AM5/29/14
to sage-s...@googlegroups.com
Hi, i tried to simplify a number doing this:
 m1.simplify()
but the output is 
AttributeError: 'sage.rings.real_mpfr.RealNumber' object has no
attribute 'simplify'

What does it mean?
What did I do wrong? I declared m1 like this:
m1 = var('m1')

thank you very much!

Simon King

unread,
May 29, 2014, 10:56:22 AM5/29/14
to sage-s...@googlegroups.com
Hi!
In order to be able to help, we need code that actually results in the
error you mention. If one just defines m1 as a symbolic variable, then
it works (in a trivial way, of course):
sage: m1 = var('m1')
sage: m1.simplify()
m1

But let me guess, based on your using the word "declare": Do you expect
that m1 will always be a symbolic variable after you did "m1=var('m1')"?
That's not how Python works. In contrast to C, there are no static types
associated with a variable.

So, is it perhaps the case that at some point you did this:
sage: m1 = 1324.67
sage: m1.simplify()
?
It is then no surprise to get an attribute error, because after re-defining
m1 as 1324.67 it is a real number, and real numbers have no
simplify method.

Best regards,
Simon


SiL588 .

unread,
May 29, 2014, 11:32:58 AM5/29/14
to sage-s...@googlegroups.com
Unfortunately I don't know the rules of Phyton language, i just started using Sage notebook to do linear algebra computation.
I think I did what you said, I assinged m a value that was the result of multiplication between real numbers.
So, if a real number have no simplify methods, how can I simplify it?

Robert Bradshaw

unread,
May 29, 2014, 11:46:51 AM5/29/14
to sage-s...@googlegroups.com
What exactly do you mean by "simplify a real number?"
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.

SiL588 .

unread,
May 29, 2014, 11:59:37 AM5/29/14
to sage-s...@googlegroups.com
The output i have is this: 
12.0000000000000
and I didn't want all those zeroes after the point.

William Stein

unread,
May 29, 2014, 12:04:06 PM5/29/14
to sage-support
On Thu, May 29, 2014 at 8:59 AM, SiL588 . <ch4...@hotmail.com> wrote:
> The output i have is this:
>
> 12.0000000000000
>
> and I didn't want all those zeroes after the point.

Try doing

int(m)

or floor(m)

William
--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Simon King

unread,
May 29, 2014, 12:06:19 PM5/29/14
to sage-s...@googlegroups.com
Hi,

On 2014-05-29, SiL588 . <ch4...@hotmail.com> wrote:
> Unfortunately I don't know the rules of Phyton language,

Sage's main language for programming is Python, and also the language for
user interaction is close to Python. We believe it is a big plus of Sage
that it uses a mainstream language!

I don't think it is needed to have in-depth knowledge of Python for using
Sage. However, in *any* computer algebra system, one has to learn the
basics of whatever language the system is using; in most other systems
it is a language that is less nice than Python...

> I think I did what you said, I assinged m a value that was the result of=20
> multiplication between real numbers.

I was reading the worksheet that you have attached. In the first cell,
you define a lot of symbolic variables (a,b,c,d,e,f,R,Rt). In the second
cell, you override most of them (a=10, b=3, ...).

Hence, at least for those variables that you override in the second
cell, the first cell is of no use.

> So, if a real number have no simplify methods, how can I simplify it?

I don't know what you mean by "simplify a real number".

Anyway, try what happens if you comment out m1.simplify() (i.e., simply
put the character "#" in front of it; that's Python syntax again).

The cell that is currently giving you the error then becomes
m1=a*b*c*rho1
m2=-b*e*f*rho1
m3=(pi*R^2*b)/2*rho1
m4=-pi*Rt^2*b*rho1
m5=pi*Rt^2*3*b*rho2
#m1=m1.simplify()
show(m1)
show(m2)
show(m3)
show(m4)
show(m5)

As you will see, m1=12.0, m2=-1.8: How do you expect this to be
simplified?

On the other hand, m3 is not a real number, but a symbolic expression,
as it contains the symbolic constant pi: m3=0.6*pi
Here, you could ask for simplification, for example
(0.6*pi).simplify()
However, the result will of course still be the same symbolic
expression, as 0.6*pi can not be further simplified.

If you want to express 0.6*pi as a real number, you can do this:
RR(0.6*pi)
which returns 1.88495559215388. Explanation: RR denotes the field of
real numbers (with some digital precision). RR(...) converts the given
input into an element of RR, if possible.

Best regards,
Simon


SiL588 .

unread,
May 29, 2014, 12:20:39 PM5/29/14
to sage-s...@googlegroups.com
Il giorno giovedì 29 maggio 2014 18:06:19 UTC+2, Simon King ha scritto:

> Hence, at least for those variables that you override in the second
cell, the first cell is of no use.

Oh ok, I didn't understand that's the way it works
 
> I don't know what you mean by "simplify a real number". 

Like I said to Robert, by simplify I mean to remove the zeroes after the point, for example if i have 0.8000000 I'd like to have 0.8. 

> If you want to express 0.6*pi as a real number, you can do this:
>  RR(0.6*pi)
>  which returns 1.88495559215388. Explanation: RR denotes the field of
>  real numbers (with some digital precision). RR(...) converts the given
>  input into an element of RR, if possible. 

Thank you, that was also something I was going to ask you! :) 

SiL588 .

unread,
May 29, 2014, 12:23:19 PM5/29/14
to sage-s...@googlegroups.com
Yes, but it works if i work with int numbers, if i have something like 0.800000 I want it to be 0.8 and not 1

Dominique Laurain

unread,
May 29, 2014, 12:53:23 PM5/29/14
to sage-s...@googlegroups.com
We get in trouble with your question, because you used "simplify" verb...which should have been refering to other SAGE simplifying functions (floor, simplify symbolic expression and so on).... when you wanted to "display only few significant digits of that real number".

Function for you is :  n(digits=...)

Try this in cell to understand better :

m = 12.0000000 ; print m
print type(m)
mr = m.n(digits=3) ; print mr
print type(mr)
x = m - mr + 1; print x
print type(x)
y = x.n(digits=5) ; print y
print type(y)
z = y ; print z
print type(z)
u = 1.000
ur = u.n(digits=8) ; print ur
print "n() is attribute of object RealNumber :"
print z
print ur

William Stein

unread,
May 29, 2014, 12:57:45 PM5/29/14
to sage-support
If you just want to safely print numbers with the trailing zeros
removed, used strip:

a = 12.0000000000
b = 0.80000

str(a).rstrip('0')
str(b).rstrip('0')

SiL588 .

unread,
May 29, 2014, 1:03:38 PM5/29/14
to sage-s...@googlegroups.com
I'm sorry I wasn't clear, as I said I just started using Sage and I thought that was what the simplify method was for.
Thank you very much for your explanation, now I got it :)

SiL588 .

unread,
May 29, 2014, 1:05:04 PM5/29/14
to sage-s...@googlegroups.com
Okay, thank you very much! :)
Reply all
Reply to author
Forward
0 new messages