Is it possible to subclass Function that will create an UndefinedFunction?

22 views
Skip to first unread message

Duane Nykamp

unread,
Nov 23, 2015, 3:32:26 PM11/23/15
to sympy
I'm trying to create a subclass of Symbol that returns a subclass of Function when called.   The only reason is to customize the is_commutative property so that an expression like P(A)/P(Eq(x,1)) will output with the P(x=1) in the denominator rather than as P^-1(x=1), which might confuse my students.

However, if I create a unmodified Function subclass, the resulting function is not callable.

In [7]: class Function2(Function):
   ...:     pass
   ...:

In [8]: P=Function2('P')

In [9]: P(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-49600f79dd99> in <module>()
----> 1 P(x)

TypeError: 'Function2' object is not callable


The problem is due to __new__ of Function

    def __new__(cls, *args, **options):
        # Handle calls like Function('f')
        if cls is Function:
            return UndefinedFunction(*args, **options)

but, I can't change the condition to issubclass(cls,Function), as that makes it always true and messes up the function of Function.

Or, the specific question I'm actually struggling with is:
Can I create a
P=Function('P')
so that

In [4]: P(A)/P(Eq(x,1))
Out[4]:
      -1      
P(A)⋅P  (x = 1)

outputs with latex with the P(x=1) in the denomiator.

Thanks,
Duane

Aaron Meurer

unread,
Nov 23, 2015, 4:29:41 PM11/23/15
to sy...@googlegroups.com
Function("P") is just syntactic sugar. As you can see from the
constructor, it's the same as UndefinedFunction("P"). If you want to
customize the behavior, you should subclass UndefinedFunction.

However, in this case, it looks like UndefinedFunction("P",
is_commutative=True) works.

Aaron Meurer
> --
> 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+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/aa0c8473-f7f3-4e3e-bd87-9cc3757ad6d3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Duane Nykamp

unread,
Nov 23, 2015, 5:37:06 PM11/23/15
to sympy
Yeah, I was stuck in a rut so that it took me until I stepped away for a bit to realize the same conclusion!

I didn't realize I could just add the is_commutative flag to UndefinedFunction, so I like your solution better than mine, where I had ended up subclassing AppliedUndef (as that is the subclass of Function that has the is_commutative property).  Since I'm unlikely to deal with non-commutative symbols, your solution should serve me fine.

Thanks!
Duane
Reply all
Reply to author
Forward
0 new messages