# Symbolic Legendre Polynomials

**URL:** <https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [January 5, 2023, 10:50pm UTC](https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579 "2023-01-05T22:50:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)\
**Post date:** [January 5, 2023, 10:50pm UTC](https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579/1 "2023-01-05T22:50:14Z")

</div>

Is there an implemented version of the legendre Polynomials with symbolic variables?

I have checked

- [Symbolics.jl](https://juliasymbolics.org/roadmap/#the_features_of_symbolicsjl) which points to [SpecialFunctions.jl](https://specialfunctions.juliamath.org/stable/functions_overview/)

- The [Polynomials.jl](https://juliamath.github.io/Polynomials.jl/stable/)

- The [ClassicalOrthogonalPolyomials.jl](https://juliaapproximation.github.io/ClassicalOrthogonalPolynomials.jl/stable/)

But they either don’t implement the Legendre Polynomials or a symbolic equivalent.

What would be the most convenient way to implement them?

---

<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 5, 2023, 11:11pm UTC](https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579/2 "2023-01-05T23:11:35Z")

</div>

> [@elvispy](#):
>
> But they either don’t implement the Legendre Polynomials or a symbolic equivalent.

Their combination does:

```julia
julia> using ClassicalOrthogonalPolynomials, Symbolics

julia> @variables x;

julia> p4 = legendrep(4, x)
0.375 + 1.75x*(1.6666666666666667x*(1.5(x^2) - 0.5) - 0.6666666666666666x) - 1.125(x^2)

julia> expand(p4)
0.375 + 4.375(x^4) - 3.75(x^2)

```

Gotta love composability!

---

<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:** [January 5, 2023, 11:42pm UTC](https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579/3 "2023-01-05T23:42:09Z")

</div>

Following up on @stevengj’s enthusiasm for composability, if you needed exact coefficients, you might mix in `SpecialPolynomials`:

```julia
julia> using Symbolics, SpecialPolynomials

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

julia> basis(Legendre{Rational{Int}}, 4)(x) |> expand
(3//8) + (35//8)*(x^4) - (15//4)*(x^2)

```

---

<div class="post-metadata">

**Author:** ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)\
**Post date:** [January 6, 2023, 1:45am UTC](https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579/4 "2023-01-06T01:45:36Z")

</div>

Excelent! As a rule of thumb, which functions should I expect to work with Symbolics.jl?

---

<div class="post-metadata">

**Author:** ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)\
**Post date:** [January 6, 2023, 2:17am UTC](https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579/5 "2023-01-06T02:17:24Z")

</div>

Really nice! But this option seems to overflow for degrees ` n > 25`

```julia
basis(Legendre{Rational{Int}}, 25)(x) |> expand
ERROR: OverflowError: -148257227381443 * 74290 overflowed for type Int64
[...]

```

Replacing `Int` for `Int128` does not seem to work, because I’ll need to work with polynomials of degree 50 or more

---

<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 6, 2023, 2:25am UTC](https://discourse.julialang.org/t/symbolic-legendre-polynomials/92579/6 "2023-01-06T02:25:08Z")

</div>

> [@elvispy](#):
>
> Replacing `Int` for `Int128` does not seem to work, because I’ll need to work with polynomials of degree 50 or more

Use `BigInt` if you need exact rational coefficients for high degrees?
