Thanks guys exp, log, sqrt etc covers well over 50% of the requirement so Im really ahead - Once again - THANKS!
It seems functional.py is definitely the place to be looking at. Whats needed is a "catch-all/anonymous" function in functional.py so that X.myfunction() is "try'ed" whenever myfunction(X) is called (much like exp(X) invokes X.exp() in functional.py) (can lambda functions or __call__ help here?) Anyone have any ideas what mechanism might do that? On Sat, Sep 4, 2010 at 1:37 PM, Jason Grout <jason-s...@creativetrax.com>wrote: > On 9/3/10 10:53 PM, Ross Kyprianou wrote: > >> 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) >> ? >> > > > The log function (and many other top-level functions defined in > sage/misc/functional.py) first try to call the .log() method of the object. > So in the case of logs, you should be able to define a .log() method that > does the right thing, and log(X) will then first try to call X.log(). > > See the code in sage/misc/functional.py for details. > > Thanks, > > Jason > > > > > > 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<sage-devel%2bunsubscr...@googlegroups.com> > For more options, visit this group at > http://groups.google.com/group/sage-devel > URL: http://www.sagemath.org > -- 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