Ask Your Question

Numerical approximation of symbolic equation

asked 2010-12-30 18:51:37 +0200

lxman gravatar image

updated 2011-01-03 21:14:17 +0200

kcrisman gravatar image

What I would like to do is the following:

sage: var('theta')
sage: theta=25*
sage: print theta
sage: a=sin(theta)
sage: print a

At this point, I type 'a.' and tab and in the popup list of functions I find numerical_approx. So I type in:

sage: a.numerical_approx()
Traceback (click to the left of this block for traceback)
TypeError: cannot evaluate symbolic expression numerically

Okay, it would appear that adding the units to theta turns it into a symbolic variable which turns my expression a into a symbolic expression. But what I would like to do is enter my angle theta in either degrees or radians and get a numerical approximation of the answer based on the units of the input. Is there a clean way to do this?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2010-12-30 23:37:57 +0200

DSM gravatar image

updated 2010-12-30 23:42:17 +0200

In the show-your-work spirit, here was my line of thought. First, I thought that maybe all that was missing was adding a .n method to degree, to get degree to pretend as if it were pi/180, but that didn't work like I was hoping. Then I realized that this was silly: pi already behaves the way I want it to, I just want to define a new pi-like constant with name "degree" and value "pi/180". So looking at how pi is implemented, it subclasses Constant and then takes the expression from that. So in a similar spirit:

from sage.symbolic.constants import Constant

class ConstantFromExpression(Constant):
    def __init__(self, name, expr):
        conversions = dict(maxima=repr(maxima.coerce(expr)))
        Constant.__init__(self, name,conversions=conversions)
        self._expr = expr
    def __float__(self): return float(self._expr)
    def __complex__(self): return complex(self._expr)
    def _mpfr_(self, R): return R(self._expr)
    def _real_double_(self, R): return R(self._expr)

def NamedExpression(name, expr):
    return ConstantFromExpression(name,expr).expression()

which should allow you to do what you want:

sage: degree = NamedExpression('degree', pi/180)
sage: degree
sage: float(degree)
sage: sin(25*degree)
sage: a = sin(25*degree)
sage: a.n()
sage: RealField(100)(a)
sage: sin(90*degree)
sage: simplify(sin(90*degree))
sage: sin(45*degree)
sage: simplify(sin(45*degree))

There are lots of conversions and features I didn't implement, of course, but I had to do at least the maxima one to get simplify to work correctly. I don't know enough about the maxima coercion repr to trust it entirely -- should probably put some tests in, to make sure the result tests equal to the input. But for this trivial case, it seems to work okay.

Maybe a NamedExpression wrapper would be useful?

edit flag offensive delete link more


Thank you for all the hard work. This definitely looks promising. I will experiment with it to see how it goes. It seems to me that if this sort of thing was implemented generally it would make sage a *much* more attractive package.

lxman gravatar imagelxman ( 2010-12-31 19:03:50 +0200 )edit

Let me know if there's something you need it to do that it doesn't. And if it works, come back here and accept my answer. :^)

DSM gravatar imageDSM ( 2011-01-02 06:33:27 +0200 )edit

If this works out, presumably all the dimensionless angle units in sage.symbolic.units should be treated this way - hopefully it wouldn't be too hard to do so. DSM, if so, this would be worth raising on sage-devel, since there was a lot of discussion of the best way to implement units there when it happened.

kcrisman gravatar imagekcrisman ( 2011-01-03 21:21:56 +0200 )edit

@kcrisman: ISTM modifying the above to make it a real unit so that sin(5*degree) would work but sin(5*degree**2) didn't would be a bit tough though.

DSM gravatar imageDSM ( 2011-01-04 22:21:34 +0200 )edit

Good point. What do unit packages in general programming languages do in these cases?

kcrisman gravatar imagekcrisman ( 2011-01-05 00:09:55 +0200 )edit

answered 2011-10-14 12:05:36 +0200

ADuC812 gravatar image

updated 2011-10-14 13:13:26 +0200

Is there any way to split up a symbolic expression like '25*degree' into units and value? some function like:

sage: t = 25*;
sage: get_numeric(t)
sage: t/get_numeric(t)

This will make possible the usage of all functions, designed for numeric arguments, with units of measurement,after some simple redefinition. As the units work like symbolic variables, we can set them to unity to get a numerical value:

sage: float(t(

For that, one has to know the units involved. it can be achieved by arguments() function:

sage: t.arguments() 

Putting all together:

def get_numeric(t)
    for arg in t.arguments():
    return (float(t(dic)));

Don't know about the performance of such method.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2010-12-30 18:51:37 +0200

Seen: 4,107 times

Last updated: Oct 14 '11