How to collect sin(t) or (sin(t)) ^2 coefficients in a expression


using LinearAlgebra
using SymPy
@vars t x y 
m=[cos(t) -sin(t) 0;
      sin(t)  cos(t)  0; 
0 0 1];
a = inv(m)*[x;y;t]
b=a'*a

So, I want the coefficient of sin(t) or (sin(t))^2 in final expression b.

If you use b.expand().coef(sin(t)) or b.expand().coeff(sin(t)^2), you should get corresponding coefficients.

@vars t
@vars x y 
m=[cos(t) -sin(t) sin(t);
      sin(t)  cos(t)*cos(t)  0; 
0 0 1];
a = inv(m)*[x;y;t]
b=simplify.(a.T*a)
b.expand().coeff(sin(t))



I got an error,
KeyError: key :coeff not found

one f in coef

1 Like


I tried this one also. and got the same error

Your b is a vector, not a scalar. Try b[1] and the above should flow through. (In the first post, you have b=a'*a which dispatches to dot which should return a scalar in this usage.)

1 Like