I'm trying to convert a symbolic expression to an array in python.
For example, I have this symbolic expression:
import sympy as sp
import numpy as np
A,B = sp.symbols("A B")
G = 3*A**2*B - 2*A*B + A**2
What I want is to convert this to an array looking as follows:
[3*A**2*B,-2*A*B,A**2]
I know how to do this in Matlab, but I need to do it in Python as well.
What I've tried:
I've converted the symbolic expression to a polynomial, which allowed me to collect the coefficients.
C = G.as_poly().coeffs()
This gets me halfway there:
C = [3,-2,1]
But I want to have at least a similar list for all the variables.
Does anyone have an idea?