# How to determine the coefficients of a polynom

**URL:** https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648
**Category:** General Usage
**Tags:** question, symbolics
**Created:** [November 1, 2023, 10:29am UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648 "2023-11-01T10:29:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [November 1, 2023, 10:29am UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/1 "2023-11-01T10:29:21Z")

</div>

How can I determine the coefficients of a polynom?

```julia
using Symbolics

@variables R a b c d U J Q s

input = 2s + 4U*a*s^2 + Q*(J+s)

function split_s(input::Num)
end

res = split_s(input)

# expected output:
# the factors of s², of s and without s
# [4U*a, 2+Q, Q*J]

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 1, 2023, 3:11pm UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/2 "2023-11-01T15:11:26Z")

</div>

[here](https://laustep.github.io/stlahblog/posts/juliaPolynomialExpansion.html) maybe you’ll find some ideas for your problem

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 1, 2023, 3:38pm UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/3 "2023-11-01T15:38:15Z")

</div>

```julia
split_s(input::Num, v::Num) = 
  vcat([Symbolics.coeff(input, v^i) for i in Symbolics.degree(input, v):-1:1],
  substitute(input, v => 0))

```

gives:

```julia
julia> split_s(input,s)
  4U*a
 2 + Q
   J*Q

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [November 1, 2023, 3:52pm UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/4 "2023-11-01T15:52:49Z")

</div>

Perfect! Thank you very much.

My code now:

```julia
function split_s(input)
    inp = simplify(input, expand=true)
    vcat([Num(Symbolics.coeff(inp, s^i)) for i in Symbolics.degree(inp, s):-1:1],
    substitute(inp, s => 0))
end

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 1, 2023, 6:40pm UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/5 "2023-11-01T18:40:58Z")

</div>

two questions:  
what is the need to have `Num()` wrapped on `Symbolics.coeff(inp, s^i)`?  
why doesn’t `Symbolics.coeff(inp, s^0)` give what you would expect?

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 1, 2023, 7:00pm UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/6 "2023-11-01T19:00:38Z")

</div>

You are right, this was done quite hastily. I’ll remove it from the answer. TBH not really an expert on Symbolics.jl, just fiddled around. Oops… my code doesn’t actually have this. Was confused by reply to me. Tagging @ufechner7

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [November 1, 2023, 7:17pm UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/7 "2023-11-01T19:17:36Z")

</div>

If I do not wrap this I get an vector of any, and if I then try to create a transfer function with tf(num, den) it fails.

```julia
julia> include("mwes/mwe20.jl")
4-element Vector{Any}:
  4U*a
 0
  2 + Q + J*Q
 0

julia> input
2s + (s + J*s)*Q + 4U*a*(s^3)

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 1, 2023, 9:09pm UTC](https://discourse.julialang.org/t/how-to-determine-the-coefficients-of-a-polynom/105648/8 "2023-11-01T21:09:34Z")

</div>

It seems like someone has already thought of this feature.  
The biggest difficulty is understanding that the second argument must be a tuple.

```julia
julia> polynomial_coeffs(input, s)
ERROR: MethodError: no method matching iterate(::SymbolicUtils.BasicSymbolic{Real})

julia> polynomial_coeffs(input, (s,))
(Dict{Any, Any}(s^2 => 4U*a, s => 2 + Q, 1 => J*Q), 0)

help?> polynomial_coeffs
search: polynomial_coeffs

  polynomial_coeffs(expr, vars)

  Find coefficients of a polynomial in vars.

  Returns a tuple of two elements:

    1. A dictionary of coefficients keyed by
       monomials in vars

    2. A residual expression which is the
       constant term

```
