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

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.

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

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?

Thank you for your great work with sympy,
Best regards,
Rickard

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to