Keeping coefficients as floats when using solve

84 views
Skip to first unread message

Mike

unread,
Feb 19, 2012, 7:58:25 PM2/19/12
to sage-support
When I run:

x,y=var('x,y', domain=RR)
solve(2.0*x+3.0*y==4.0, y)

I get

[y == -2/3*x + 4/3]

but I would like to get

[y == -0.666666666666666*x + 1.3333333333333]

How can I do this?

Michael Orlitzky

unread,
Feb 20, 2012, 10:39:11 AM2/20/12
to sage-s...@googlegroups.com

Wild guess: the float coefficients are coerced to QQ, because otherwise
numerical inaccuracy would prevent us from finding a solution. For example,

(0.333333... + 0.666666...)*x

might not equal x.

For a workaround, someone recently showed me this. You would call
`symbolic_approx` on your result.

---

class NumericEvaluator(Converter):

def arithmetic(self, ex, operator):
return reduce(operator, map(self, ex.operands()))

def pyobject(self, ex, obj):
return ex.n()

def symbol(self, ex):
return SR(ex)

def symbolic_approx(expr):
ne = NumericEvaluator()
return ne(expr)

Michael Orlitzky

unread,
Feb 20, 2012, 10:41:44 AM2/20/12
to sage-s...@googlegroups.com
On 02/20/12 10:39, Michael Orlitzky wrote:
> For a workaround, someone recently showed me this. You would call
> `symbolic_approx` on your result.


Whoops, you'll need this, too:

from sage.symbolic.expression_conversions import Converter

Burcin Erocal

unread,
Feb 20, 2012, 11:35:54 AM2/20/12
to sage-s...@googlegroups.com


Or you can do this:

sage: t = -2/3*x + 4/3
sage: t._convert(RR)
-0.666666666666667*x + 1.33333333333333


Cheers,
Burcin

kcrisman

unread,
Feb 20, 2012, 1:44:43 PM2/20/12
to sage-support


On Feb 20, 10:39 am, Michael Orlitzky <mich...@orlitzky.com> wrote:
> On 02/19/12 19:58, Mike wrote:
>
> > When I run:
>
> > x,y=var('x,y', domain=RR)
> > solve(2.0*x+3.0*y==4.0, y)
>
> > I get
>
> > [y == -2/3*x + 4/3]
>
> > but I would like to get
>
> > [y == -0.666666666666666*x + 1.3333333333333]
>
> > How can I do this?
>
> Wild guess: the float coefficients are coerced to QQ, because otherwise
> numerical inaccuracy would prevent us from finding a solution. For example,

No, this is just Maxima. We try to "keep float" as much as possible,
but in their *sub*routines like solve, sometimes this distinction is
not preserved for technical reasons - probably analogous to the one
that you allude to about QQ.


(%i2) keepfloat:True;
(%o2) True
(%i3) solve(2.0*x+3.0*y=4.0,y);

rat: replaced -4.0 by -4/1 = -4.0

rat: replaced 2.0 by 2/1 = 2.0

rat: replaced 3.0 by 3/1 = 3.0
2 x - 4
(%o3) [y = - -------]
3

Michael Orlitzky

unread,
Feb 20, 2012, 2:41:45 PM2/20/12
to sage-s...@googlegroups.com
On 02/20/12 11:35, Burcin Erocal wrote:
>
>
> Or you can do this:
>
> sage: t = -2/3*x + 4/3
> sage: t._convert(RR)
> -0.666666666666667*x + 1.33333333333333
>

Where were you a few weeks ago? =)

Would anyone be opposed to making this a visible method?

Michael Orlitzky

unread,
Feb 23, 2012, 3:26:36 PM2/23/12
to sage-s...@googlegroups.com
On 02/20/2012 11:35 AM, Burcin Erocal wrote:
>
>
> Or you can do this:
>
> sage: t = -2/3*x + 4/3
> sage: t._convert(RR)
> -0.666666666666667*x + 1.33333333333333
>

I opened a ticket to make this visible:

http://trac.sagemath.org/sage_trac/ticket/12577

Thanks again for the easy solution.

Mike

unread,
Mar 14, 2012, 4:07:20 PM3/14/12
to sage-support
On Feb 20, 12:35 pm, Burcin Erocal <bur...@erocal.org> wrote:
> Or you can do this:
>
> sage: t = -2/3*x + 4/3
> sage: t._convert(RR)
> -0.666666666666667*x + 1.33333333333333
>
> Cheers,
> Burcin

This is perfect - thank you! For the record, I've used that idea to
write the following bits:

def convert_dict(d, F):
# Converts the values in the dictionary d to the field F
return dict([(k, v._convert(F)) for k, v in d.items()])

def complex_solutions(eqns, vars):
sol=solve(eqns, vars, solution_dict=True)
return [convert_dict(d, CC) for d in sol]
Reply all
Reply to author
Forward
0 new messages