On Fri, Apr 15, 2022 at 2:23 PM David Bailey <d...@dbailey.co.uk> wrote:
>
> Dear Group,
>
> I would like to understand the workings of SymPy a little better, and to
> that end, I thought it would be instructive to create a trivial 'special
> function' that had some or all of the properties of the SymPy in-built
> functions. The following link was helpful:
>
> https://github.com/Shekharrajak/sympy/wiki/About-implementing-special-functions

This is a link to someone's fork of the wiki. The actual page is here
https://github.com/sympy/sympy/wiki/Development-Tips-:-About-implementing-special-functions

>
> However, I could not tell from that whether I could have the new
> function stored separately from the directory tree holding the SymPy
> software. I'd much prefer it if I could experiment without disturbing
> the SymPy software at all.

Yes, you can have it separate. The location of a function in the code
has no bearing on its behavior.

>
> I decided to call the function bailey, so following the recipe in the
> above link, I created a file bailey.py in a directory outside the SymPy
> tree, and to test it my calling Python from that directory, as follows:
>
> from sympy.core.function import Function
> class bailey(Function):
>     """
>     The experimental function
>     """
>     nargs=1
>     @classmethod
>     def eval(cls, arg):
>             # This body does nothing useful at all
>          z=arg[0]
>          if z is S.Zero:
>              return S.Zero
>          else: return 1
>
> Now I tried to use this module:
>
>  >>> import sympy
>  >>> import bailey
>
>  >>> bailey(a+3)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: 'module' object is not callable

When you run 'import bailey' then 'bailey' will be the module (because
you called your file bailey.py). To get the function do

from bailey import bailey

>
> Alternatively I tried:
>
>  >>> (a+3).bailey(1)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> AttributeError: 'Add' object has no attribute 'bailey'

You can't add new methods to existing SymPy objects. The first way you
tried is the correct way to access your bailey function.

>
> I admit to being fairly confused at this level! What I would like to
> know is:
>
> 1)       Can I experiment in this way without actually adding files to
> SymPy itself or modifying anything there already?
>
> 2)       I would expect to get back a function call: bailey(a+3) or
> perhaps the answer 1.

The function as you've written it will just return 1. If you want a
symbolic answer, eval() needs to return None (which is also the same
as not returning anything).

Since your eval() always returns something, the function will never be
symbolic. It is effectively no different from a normal Python function

def bailey(x):
    if x == 0:
        return 0
    else:
        return 1

>
> 3)       Is there a place where this is described more completely?

I think the best docs right now are at
https://docs.sympy.org/latest/modules/core.html#id55. I will probably
end up writing something better at some point as this an important
thing that is somewhat underdocumented right now.

Aaron Meurer

>
> Thanks,
>
> David
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sympy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/04eaf831-b1a1-6f1c-68b7-2ae9a7d60c2e%40dbailey.co.uk.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6LU80JDR7XJBFK%2BVg0eJKeX1BxCLs5LzEfNk2jafhQxVQ%40mail.gmail.com.

Reply via email to