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

**URL:** <https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109>\
**Category:** General Usage\
**Tags:** symbolics, sympy\
**Created:** [September 12, 2022, 5:58am UTC](https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109 "2022-09-12T05:58:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [September 12, 2022, 5:58am UTC](https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109/1 "2022-09-12T05:58:35Z")

</div>

```julia

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.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [September 12, 2022, 6:35am UTC](https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109/2 "2022-09-12T06:35:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [September 13, 2022, 8:04pm UTC](https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109/3 "2022-09-13T20:04:32Z")

</div>

```julia
@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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/2/a2a1b0c41dd3774824413eab5fb3dbda952197dc.png)

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 13, 2022, 8:25pm UTC](https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109/4 "2022-09-13T20:25:19Z")

</div>

> [@Starpower\_Power](#):
>
> KeyError: key :coeff not found

> [@tomaklutfu](#):
>
> b.expand().coef(

one f in coef

---

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [September 15, 2022, 11:58pm UTC](https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109/5 "2022-09-15T23:58:44Z")

</div>

> [@tomaklutfu](#):
>
> b.expand().coef(sin(t))

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/8/783ed3ac08256c5b54e6adb680bd2da45f086eb8.png)  
I tried this one also. and got the same error

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [September 16, 2022, 12:48am UTC](https://discourse.julialang.org/t/how-to-collect-sin-t-or-sin-t-2-coefficients-in-a-expression/87109/6 "2022-09-16T00:48:16Z")

</div>

> [@Starpower\_Power](#):
>
> ```julia
> 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
> 
> ```

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.)
