Hi Noud,

On Sun, 9 Sep 2012 04:17:24 -0700 (PDT)
Noud <[email protected]> wrote:

> Let's first take a look at this example
> 
> sage: f = 2*cos(1/2)
> sage: f
> 2*cos(1/2)
> sage: f.n()
> 1.75516512378075
> 
> I would like to program something like cos(x) that behaves the same
> as what I showed above. So I want to program a function F which can
> interact with RingElements and stays symbolic until I want to
> evaluate it. Then this function F (always) becomes an element of the
> Ring so you can multiply, add, etc. So I want to create a class F
> such that you can so something like this:
> 
> sage: g = 2*pi*F(1,2,3,4)/3
> sage: g
> 2/3*pi*F(1,2,3,4)
> sage: g.n()
> 1.23456789        # or any other element from some arbitrary Ring in
> which you work
> 
> I looked at the source code of /sage/functions/trig.py, but cos(x) is
> using the GinacFunction class to calculate symbolically. Could
> someone tell me if this thing is possible? If yes, how can you do
> this. Are there examples in the source code of Sage which I should
> look up?

Yes, this is possible. You can look at the implementation of
exponential integrals deriving from BuiltinFunction in this patch for
an example:

http://trac.sagemath.org/sage_trac/attachment/ticket/11143/trac_11143-v2.5-rebased.4.patch

cos() is not a good example, because custom methods for evaluation,
differentiation, etc. are implemented in c++ by ginac already. We don't
need to implement them again in Sage.

> ps. I also want to let it interact with itself! Something like
> sage: F(1,2,3,4) * F(5,6,7,8)
> F(9,10,11,12)

You cannot get this to work automatically. When you evaluate your
function, as in

sage: t = F(1, 2, 3, 4)

that object, t, is an element of the symbolic ring. Arithmetic
between these objects call the same method as any other symbolic
expression. Checking for such rules would slow down the multiplication
of symbolic expressions.

You can use the pattern matching .match() and substitution .subs()
functions to implement this and call it manually when you want to
normalize your expressions.


Cheers,
Burcin

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/sage-devel?hl=en.


Reply via email to