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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.