# Symbolics: get the coefficients of the polynomial binomial(x, c)

**URL:** <https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [January 4, 2023, 2:43pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511 "2023-01-04T14:43:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 4, 2023, 2:43pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/1 "2023-01-04T14:43:06Z")

</div>

How to get a polynomial expression from, e.g., `binomial(x, 3)`?

```julia
julia> using Symbolics

julia> @variables x
1-element Vector{Num}:
 x

julia> binomial(x, 3)
binomial(x, 3)

julia> simplify(ans)
binomial(x, 3)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 4, 2023, 5:44pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/2 "2023-01-04T17:44:05Z")

</div>

> [@nsajko](#):
>
> How to get a polynomial expression from, e.g., `binomial(x, 3)`?

`binomial(x, 3)` is not a polynomial. It corresponds to the quantity \frac{x!}{3! (x-3)!} (which could be extended to non-integer x via the [gamma function](https://en.wikipedia.org/wiki/Gamma_function), a transcendental function of x).

What polynomial do you want?

---

<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:** [January 4, 2023, 5:54pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/3 "2023-01-04T17:54:37Z")

</div>

Trying to reverse engineer the question, maybe the meaning is for:

```julia
julia> @variables x
1-element Vector{Num}:
 x

julia> p = expand((1+x)^3)
1 + x^3 + 3x + 3(x^2)

```

That’s a polynomial, and is related to binomials (coefficients are the binomial numbers). To obtain the sequence of binomial coefficients with Symbolics, there must be a better way, but:

```julia
julia> pp = groebner_basis([p])[1]
(x^3) + 3(x^2) + 3x + 1

julia> using DynamicPolynomials

julia> map(coefficient, pp.p)
4-element Vector{Int64}:
 1
 3
 3
 1

```

An alternative is to use the Polynomials package:

```julia
julia> using Polynomials

julia> p = Polynomial([1,1])
Polynomial(1 + x)

julia> p^3
Polynomial(1 + 3*x + 3*x^2 + x^3)

julia> coeffs(p^3)
4-element Vector{Int64}:
 1
 3
 3
 1

```

All this… if the question was regarding this. For the Gamma function interpretation (from Wikipedia for Binomial Coefficient):

 ![gamma_func](https://global.discourse-cdn.com/julialang/original/3X/e/a/ead626fd869617ecaa9b24252be4c85e8e3c900f.png)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 4, 2023, 7:17pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/4 "2023-01-04T19:17:53Z")

</div>

`binomial(x, 3)` should be \binom{x}{3} = \frac{x!}{3! (x-3)!} = {\frac{1}{3!} x (x - 1) (x - 2)} = {\frac{1}{3!} (x^3 - 3x^2 + 2x)}

That’s what I meant.

Hmmm, even the following fails to do as expected:

```julia
julia> factorial(x) / (factorial(3) * factorial(x - 3))
factorial(x) / (6factorial(x - 3))

julia> factorial(x) / factorial(x - 3)
factorial(x) / factorial(x - 3)

julia> simplify(ans)
factorial(x) / factorial(x - 3)

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 4, 2023, 7:27pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/5 "2023-01-04T19:27:55Z")

</div>

These simplifications are only valid if `x` is a natural number, by default Symbolics `variables` are Real. If `x` were negative, fractional, etc, the expression would look more like the one from Wikipedia above. If you create `x` as a `Num{Integer}` instead of a `Num{Real}`, I think you can define custom dispatches to get the expression you want, but I haven’t actually tried to do this.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 4, 2023, 7:30pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/6 "2023-01-04T19:30:46Z")

</div>

> [@contradict](#):
>
> These simplifications are only valid if `x` is a natural number

I’d say they’re perfectly valid as-is, but the other direction perhaps wouldn’t be valid, because a polynomial is defined over all reals, which include the integers.

Although I admit that these technical limitations may be reasonable in this case.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 4, 2023, 7:33pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/7 "2023-01-04T19:33:04Z")

</div>

Oh, I mean the simplification `x! == prod(1:x)` is only valid for `x∈𝒩`

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 4, 2023, 7:36pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/8 "2023-01-04T19:36:51Z")

</div>

> [@contradict](#):
>
> If you create `x` as a `Num{Integer}` instead of a `Num{Real}`

How to do this? I can’t figure it out.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 4, 2023, 7:38pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/9 "2023-01-04T19:38:37Z")

</div>

Me neither, I thought It was in the docs but I can’t find it. I think I have a fundamental misunderstanding of how Symbolics does this.

---

<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:** [January 4, 2023, 7:57pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/10 "2023-01-04T19:57:41Z")

</div>

```julia
Bincoeffs(n)=coeffs(mapreduce( x->1//(x+1)*Polynomial([-x,1]), *, 0:n-1 ))

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 4, 2023, 8:14pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/11 "2023-01-04T20:14:27Z")

</div>

> [@nsajko](#):
>
> `binomial(x, 3)` should be \binom{x}{3} = \frac{x!}{3! (x-3)!} = {\frac{1}{3!} x (x - 1) (x - 2)} = {\frac{1}{3!} (x^3 - 3x^2 + 2x)} (x3)=x!3!(x−3)!=13!x(x−1)(x−2)=13!(x3−3x2+2x)
> 
> That’s what I meant.

Oh right, [this generalization](https://en.wikipedia.org/wiki/Binomial_coefficient#Generalization_and_connection_to_the_binomial_series). Yes, this is a indeed a polynomial for integer `k`. Right now, however, Julia only defines the `binomial` function for integers:

```jl
julia> methods(binomial)
# 3 methods for generic function "binomial":
[1] binomial(n::BigInt, k::UInt64) in Base.GMP at gmp.jl:684
[2] binomial(n::BigInt, k::Integer) in Base.GMP at gmp.jl:685
[3] binomial(n::T, k::T) where T<:Integer in Base at intfuncs.jl:1050

```

Arguably, it should have a method `binomial(x::Number, k::Integer)` ([binomial(x,k) for non-integer x by stevengj · Pull Request #48124 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/48124)). If we add such a method:

```julia-auto
function binomial(x::Number, k::Integer)
    k < 0 && throw(DomainError("k=$k must be non-negative"))
    k == 0 && return oneunit(x)/one(k)
    # we don't use prod(i -> (x-i+1), 1:k) / factorial(k),
    # and instead divide each term by i, to avoid spurious overflow.
    return prod(i -> (x-i+1)/i, OneTo(k))
end

```

along with a method `Base.binomial(x::Num, k::Integer) = invoke(binomial, Tuple{Number, Integer}, x, k)` to eliminate a method ambiguity in Symbolics.jl, then it works:

```julia-auto
julia> binomial(x, 3)
x*((1//3)*x - (2//3))*((1//2)*x - (1//2))

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 4, 2023, 10:01pm UTC](https://discourse.julialang.org/t/symbolics-get-the-coefficients-of-the-polynomial-binomial-x-c/92511/12 "2023-01-04T22:01:28Z")

</div>

> [@nsajko](#):
>
> Hmmm, even the following fails to do as expected:
> 
> ```julia-auto
> julia> factorial(x) / (factorial(3) * factorial(x - 3))
> 
> ```

The Julia `factorial` function is not also not defined for non-integer `x`. This definition definitely _would_ require the Gamma function, which is not provided in the stdlib. At one point `factorial(::Number)` was provided by the SpecialFunctions.jl package (which was originally in Base but was split off for Julia 1.0), but was [removed](https://github.com/JuliaMath/SpecialFunctions.jl/pull/297) because it is type piracy.

So, if you want to use the `factorial(x)` for non-integer `x`, you need to explicitly call the `gamma(x+1)` function from SpecialFunctions.jl. However, looks like Symbolics.jl does not know how to simplify the ratio of two gamma functions into a polynomial.
