Ask Your Question
0

How to "recover" real number from expression?

asked 2012-06-13 00:55:45 +0100

Eviatar Bach gravatar image

How do I separate a RealNumber from an Expression, once it is constructed?

sage: b = RealField(200)(5)
sage: type(b)
<type 'sage.rings.real_mpfr.RealNumber'>
sage: type((b^x).operands()[0])
<type 'sage.symbolic.expression.Expression'>
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2012-06-13 23:11:32 +0100

Mike Hansen gravatar image

You can use the pyobject method:

sage: b = RealField(200)(5)
sage: bb = (b^x).operands()[0]
sage: bb.pyobject(), type(bb.pyobject()) 
(5.0000000000000000000000000000000000000000000000000000000000, <type 'sage.rings.real_mpfr.RealNumber'>)
edit flag offensive delete link more

Comments

That's much easier, thank you! I don't think `pyobject` is a good name for this method though.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-13 23:29:38 +0100 )edit

Yeah, the name make s more sense when you're familiar with the underlying pynac library. Can you think of a better name that we could use as an alias?

Mike Hansen gravatar imageMike Hansen ( 2012-06-14 03:04:13 +0100 )edit
1

answered 2012-06-13 07:12:50 +0100

Volker Braun gravatar image

By explicit conversion:

sage: expr = (0.5^x).operands()[0]
sage: expr
0.500000000000000
sage: type(expr)
<type 'sage.symbolic.expression.Expression'>

As a machine double precision floating point number:

sage: RDF(expr)
0.5

As a 200 bit software floating point number:

sage: RealField(200)(expr)
0.50000000000000000000000000000000000000000000000000000000000

As a interval arithmetic 200 bit floating point number:

sage: RealIntervalField(200)(expr)
0.50000000000000000000000000000000000000000000000000000000000000?

And so on...

edit flag offensive delete link more

Comments

Thanks, but I was looking for a way to maintain the original precision. It seems that `RealNumber(expr)` works, but for whatever reason there is a minor loss in precision; `RealNumber(RealField(200)(5)).prec()` = 196. Any idea why?

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-13 13:47:13 +0100 )edit

As well, is there a way to detect what type `expr` used to be? To detect that `expr` was a `RealNumber`, for example, or that `(4^x).operands()[0]` was an `Integer`.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-13 13:56:35 +0100 )edit

I found http://ask.sagemath.org/question/411/substituting-expressions-for-numbers. That looks like it could work for what I want to do. I'll post another answer if it does.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-13 13:59:52 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-06-13 00:55:45 +0100

Seen: 373 times

Last updated: Jun 13 '12