On Sat, Jun 7, 2008 at 11:34 AM, Ondrej Certik <[EMAIL PROTECTED]> wrote: > On Fri, Jun 6, 2008 at 7:50 PM, Rickard Armiento <[EMAIL PROTECTED]> wrote: >> >> Hi, >> >> I recently discovered sympy and really like the idea of a symbolic >> math engine in python. I'm trying to learn how to use it, but I am >> confusing myself with how to work with generic functions. Any help is >> much appreciated. >> >> Sorry for being so verbose, but I thought that it might be helpful to >> see the process that got me into my current state of confusion... >> >> Just as a first test, this of course worked nicely: >> >> In [1]: sin(5*x).diff(x) >> Out[1]: 5*cos(5*x) >> >> So I was surprised that I couldn't do the same with a generic >> function: >> >> In [2]: f = Function("f") >> In [3]: f(5*x).diff(x) >> ** backtrace ending in core/function.py in __new__(cls, expr, >> *symbols, **assumptions) >> <type 'exceptions.TypeError'>: __new__() argument after * must be a >> sequence > > This is a serious bug, thanks a lot for discovering it. This should > just work. I created a new issue for it: > > http://code.google.com/p/sympy/issues/detail?id=877 > >> >> After digging into the framework I think I understood that the >> 'problem' is that the generic function I get out of Function("f") is >> not 'prepared' to be derived. Or, if you will, not assumed to be >> analytical. > > Well, it's just a bug that needs to be fixed. > >> >> So, based on the tutorial I instead created my own function like this: >> ======= >> class myfunction(Function): >> nargs = 1 >> def fdiff(self, argindex=1): >> return Function("myfunctionPrime")(self.args[0]) >> ======= >> >> This works fine: >> In [6]: myfunction(5*x).diff(x) >> Out[6]: 5*myfunction_prime(5*x) >> >> But, of course I want my function to be indefinitely derivable so that >> I can, for example, work with series expansions. So I fiddled around >> trying to create a clever fdiff implementation that adds a 'Prime' to >> the previous name. But, when trying to solve my problems with that >> approach I realized that it seems stupid to go for a specific function >> definition like this, and what I *really* would want is to subclass >> Function to an AnalyticalFunction and use as a generic function >> factory for functions with the relevant property. That is, I would >> like it to work like this: >> >> In [1]: f = AnalyticalFunction("f") >> In [2]: f(5*x).diff(x) >> Out[2]: 5*fPrime(5*x) >> >> And: >> >> In [3]: f(x).series(x,0,3) >> Out[3]: f(0) + x*fPrime(0) + x**2/2*fPrimePrime(0)+O(x**3) >> >> A first simple attempt was something along the lines of: >> ======= >> class AnalyticFunction(Function): >> >> def fdiff(self, argindex=1): >> return AnalyticFunction(self.__class__.__name__+"Prime") >> (self.args[0]) >> ======= >> >> However, I just cannot get any subclass of Function to work as a >> general function factory! I always get something like this: >> >> In [1]: f = AnalyticalFunction("f") >> In [2]: f(1) >> ** backtrace ending in core/basic.py in __call__(self, *args, >> **removeme) >> <type 'exceptions.NameError'>: global name 'Function' is not defined > > SymPy can what you need and I think you are doing things right -- I > need to go now, but I'll look into it in the afternoon (couple hours > from now) why it isn't working and how to fix it. > >> >> So, given the complexity I have run into, I have clearly barked off in >> the wrong direction, right? All I wanted to do at this point was to >> work with general, derivable, functions. Where did I go wrong? Or is >> this part of sympy still very much work in progress? > > Well, some things work already, e.g.: > > In [1]: f(x).diff(x) > Out[1]: > d > ──(f(x)) > dx > > In [2]: (f(x)**2).diff(x) > Out[2]: > d > 2*f(x)*──(f(x)) > dx > > In [3]: f(f(x)).diff(x) > Out[3]: > d d > ─────(f(f(x)))*──(f(x)) > df(x) dx > > >> >> Thank you for your great work with sympy, > > Thanks a lot for your feedback and I hope you weren't disappointed too > much -- SymPy is a young project, but we try to fix all such problems > that people find quickly and we always write tests for them, so that > they never happen again. I'll look into this in the afternoon and I > would appreciate very much if you could provide us feedback how *you* > would like things to work, so that things are easy to use and easy to > understand for new people coming to SymPy. > > See you for now and I'll write soon after I figure out what's wrong, > now I need to go,
Just a note: any comments how we could improve our tutorial would be welcome. Also any comments how the Function interface and classes should look like so that it's easy to use. Ondrej --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---