Ask Your Question

Revision history [back]

Add a personnal coercion rule

Hi all !

For some reasons I have to use a class MyAngle which basically describes an angle, but with some personal methods.

Obviously, sage's cos function cannot take an instance of my class as argument.

SAGE

#! /usr/bin/sage -python
# -*- coding: utf8 -*-

from sage.all import *

class MyAngle(object):
    def __init__(self,value):
        self.value=value

x=MyAngle(pi/2)
cos(x)

ERROR

Traceback (most recent call last):
File "./sagess.py", line 11, in <module>
    cos(x)
  File "function.pyx", line 733, in sage.symbolic.function.GinacFunction.__call__   (sage/symbolic/function.cpp:6572)
  File "function.pyx", line 417, in sage.symbolic.function.Function.__call__ (sage/symbolic    /function.cpp:4336)
TypeError: cannot coerce arguments: no canonical coercion from <class '__main__.MyAngle'> to Symbolic Ring

What do I have to add to my class to make Sage's cosine understand that when x=MyAngle(pi/2),

cos(x)

means

cos(x.value)

I found that, but I didn't helped me so much. http://www.sagemath.org/doc/developer/coding_in_python.html

Thanks for help ! have a good night Laurent