Ive defined a class and need to pass instances of it to any standard
real function in Sage such as exp and log (i.e. functions that accept
numbers (ints, reals etc) and symbolic vars but obviously they wont
accept this new class thats been created). I cant modify the functions
to accept this new type (because I dont "own" them, they are part of
the Sage library) but there is a well-defined value that can be
returned for ANY Sage/Python real function and any instance of the
class.

(If youre into Probability and Statistics: Ive defined a Random
Variable class and for any instance X, the expressions exp(X) or
log(X) (or F(X) for any real function F) are well-defined random
variables and should be returned as new instances defined in terms of
X - but ignore this if youre not into Prob&Stats).

When I try

X = NormalRV(mu,sigma)
Y = log(X)

I (quite expectedly) get
TypeError: cannot coerce arguments: no canonical coercion from <class
'__main__.NormalRV'> to Symbolic Ring

By implementing the python __call__() method I get the answer needed
i.e. the expression
Y = X(log) works as desired but looks really weird:
Y = log(X) looks natural but not Y = X(log).

Is there any way I can get Sage to execute
Y = X(log)
to invoke the __call__ method and get the right answer, every time the
user enters the more natural
Y = log(X)
?
Ive had limited success with the preparser and now thinking of hooking
into the exception handling or using coercion  to somehow make exp,
log, sin etc understand this new class (without modifying them) in the
same way they understand symbolic variables as well as numbers.

Is coercion possible? Is it the way to go or is there a better
approach?



-- 
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