If you want a Sage number X of n (binary) bits precision to be converted to 
a Maxima bigfloat of n bits,
then you can do this.
First in Sage compute Xrat  which is an exact rational that is equal to X.  
It could be computed
by something like  (some integer) times 2^(some power):

then in maxima, you utter the program fragment:  

  block([?fpprec:n], bfloat(Xrat))

note ?fpprec is the BINARY version of fpprec.




On Wednesday, June 13, 2012 4:44:28 PM UTC-7, Eviatar wrote:
>
> Here's a possible way to solve the precision problems with Maxima. This 
> replaces RealNumbers and RealLiterals with variables before simplifying an 
> Expression.
>
> from sage.symbolic.expression_conversions import Converter
>
> class DoNothing(Converter):
>     def arithmetic(self, ex, operator):
>         return reduce(operator, map(self, ex.operands()))
>     def pyobject(self, ex, obj):
>         return ex
>     def symbol(self, ex):
>         return ex
>     def relation(self, ex, operator):
>         return operator(*map(self, ex.operands()))
>     def derivative(self, ex, operator):
>         #We'll just ignore this for now
>         return ex
>     def composition(self, ex, operator):
>         return operator(*map(self, ex.operands()))
>
> class MaintainPrec(DoNothing):
>     def __init__(self):
>         self.repl_dict = {}
>         self.counter = 1
>     def pyobject(self, ex, obj):
>         if isinstance(obj, sage.rings.real_mpfr.RealLiteral) or 
> isinstance(obj, sage.rings.real_mpfr.RealNumber):
>             newvar = var('repl%s' % self.counter)
>             self.repl_dict[newvar] = obj
>             self.counter += 1
>             return newvar
>         else:
>             return obj
>
> def safe_simplify(expr):
>     replacer = MaintainPrec()
>     return replacer(expr).simplify_full().subs(replacer.repl_dict)
>
> Now:
>
> sage: a = RealField(200)(8.987551787368175506591796875e9)                 
> sage: var('y')
> y
> sage: b = (a * x).mul(y, hold=True)
> sage: (b / (x * y)).simplify()
> 8987551787.37
> sage: safe_simplify(b / (x * y))
> 8.9875517873681755065917968750000000000000000000000000000000e9
>
> Does this look like a good solution? It should be done before sending any 
> Expression to Maxima, because Maxima itself does not try to preserve 
> precision at all with its 
> bigfloats<http://trac.sagemath.org/sage_trac/ticket/11643#comment:3>
> .
>

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to