Pattern matching possibly empty function arguments

15 views
Skip to first unread message

James Stankowicz

unread,
May 1, 2018, 6:26:39 PM5/1/18
to sympy
Is there a way to write a replacement rule for a function f with an arbitrary number of arguments that makes it linear in all its arguments?
An example for when f has three arguments:

1) f( x1+x4 , x2 , x3 ) = f(x4,x2,x3) + f(x1,x2,x3)
2) f( x1 , x2+x4 , x3 ) = f(x1,x2,x3) + f(x1,x4,x3)
3) f( x1 , x2 , x3+x4 ) = f(x1,x2,x3) + f(x1,x2,x4)

Using "Wild" works partially:

from sympy import *
f
=Function('f')
var("x1:5")
a
=Wild("a")
b
=Wild("b")
A
=Wild('A', exclude=[0])
B
=Wild('B', exclude=[0])
expr
=f(x1,x2+x4,x3);
print("This one works")
print expr , '->' , expr.replace(f(a,Add(A,B),b),f(a,A,b)+f(a,B,b))
print("This one doesn't on the last entry")
expr
=f(x1,x2,x3+x4);
print f(x1,x2,x3+x4) , '->' , expr.replace(f(a,Add(A,B),b),f(a,A,b)+f(a,B,b))

I know I could iterate in a variety of ways over the arguments of the function while altering the replacement, but I was hoping the functionality was built into "Wild" or "replace" already. Mathematica, for example, has "wildcards" like "a___,b___,A___,B___" which mean that "a___" could be an empty sequence, or  a single argument, or a sequence of multiple arguments. Is there something similar, or is this is close as sympy gets?
Reply all
Reply to author
Forward
0 new messages