substitute sympy function with arbitrary arguments

38 views
Skip to first unread message

Craig Stringham

unread,
Jan 14, 2015, 5:50:21 PM1/14/15
to sy...@googlegroups.com

This should be an easy task, but I'm having a hard time getting it to work in Sympy. I want to substitute an undefined function with arbitrary arguments with a specific formula for example:

from sympy import *
var('a b c')
f= Function('f')
test= f(a+b)
lin= test.subs({f(c):2*(c)})
print(lin)

I want this to print out

2*(a+b)

However, for that I have to use

lin= test.subs({f(a+b):2*(a+b)})

Do I have to define f as a class in order to do this substitution?

Sergey Kirpichev

unread,
Jan 14, 2015, 6:57:08 PM1/14/15
to sy...@googlegroups.com
You could use pattern matching in the replace method:

In [12]: var('a b')
Out[12]: (a, b)

In [13]: c = Wild('c')

In [14]: f = Function('f')

In [15]: f(a + b).replace(f(c), 2*c)
Out[15]: 2⋅a + 2⋅b
Reply all
Reply to author
Forward
0 new messages