I have a huge symbolic sympy expression on the form
expression = factor * (f1*a + f2*b + f3*c + f4*d + f5*e)
where all of the factors a through e all consists of several terms. I.e:
a = exp(2x) + exp(3x) + sin(Ix).
I want to create en array on the form
array = factor * [a,b,c,d,e]
But dont see a cleaver way to do this. I´ve tried to use the factor function, but it only gives me the expression on the form of "expression" above.
Until now i have used
print(expression)
and then done some brute force copy paste of the factors a through e. Since I am going to get expressions with way more terms than in this example, I want to do this without the copy paste routine. Any ideas?
Here's a simple example you can extrapolate for more terms
import sympy as sp
x = sp.var('x')
f1, f2 = sp.symbols('f1:3')
factor = sp.symbols('factor')
a = x**2 + sp.sin(x) + sp.exp(sp.I * x)
b = sp.log(x)/(x+1)**2
# example expression:
expression = (factor * (f1 * a + f2 * b)).expand()
print(expression)
# collect coefficients of f1 and f2
coeffs = sp.collect(expression.expand(),[f1,f2], evaluate=False)
print(coeffs)
# show the coefficients w/o the factor factor
[(coeffs[f]/factor).simplify() for f in (f1,f2)]
f1*factor*x**2 + f1*factor*exp(I*x) + f1*factor*sin(x) + f2*factor*log(x)/(x**2 + 2*x + 1) {f2: factor*log(x)/(x**2 + 2*x + 1), f1: factor*x**2 + factor*exp(I*x) + factor*sin(x)} [x**2 + exp(I*x) + sin(x), log(x)/(x**2 + 2*x + 1)]