Zulip Chat Archive
Stream: Is there code for X?
Topic: Legendre polynomials
Junqi Liu (Apr 28 2024 at 13:53):
How to define a class of polynomials like P_n(x) = n * x? For any n : N, P_n(x) will be an certain polynomial.
Adam Topaz (Apr 28 2024 at 13:56):
you can do something like this:
import Mathlib.Data.Polynomial.Basic
noncomputable
example (n : ℕ) (R : Type*) [Semiring R] : Polynomial R :=
n * .X
Junqi Liu (Apr 28 2024 at 14:03):
Thank you so much! But I want to ask you that what is the mean of Semiring R here? And if I want to define P_n(x) = (1 / x)^(n) which means the n-th derivative of 1 / x, what should I do?
Yury G. Kudryashov (Apr 28 2024 at 14:09):
deriv^[n] (fun x => 1 / x)
. Not tested. Clearly, this is not a polynomial.
Junqi Liu (Apr 28 2024 at 14:12):
Yury G. Kudryashov said:
deriv^[n] (fun x => 1 / x)
. Not tested. Clearly, this is not a polynomial.
yeah! I konw. Actually I want to define Legendre Polynomial which is a polynomial. haha! Thanks so much!
Junqi Liu (Apr 28 2024 at 14:23):
Yury G. Kudryashov said:
deriv^[n] (fun x => 1 / x)
. Not tested. Clearly, this is not a polynomial.
I find it doesn't work very well, can you help me define a set of legendre polynomials?
Notification Bot (Apr 28 2024 at 15:33):
This topic was moved here from #lean4 > define polynomial by Yury G. Kudryashov.
Yury G. Kudryashov (Apr 28 2024 at 15:38):
You can use
import Mathlib
open scoped Nat
namespace Polynomial
noncomputable def legendre (n : ℕ) : ℝ[X] :=
C (1 / (2^n * n !) : ℝ) * derivative^[n] ((X ^ 2 - 1) ^ n)
noncomputable def legendre' (n : ℕ) : ℝ[X] :=
(1 / (2^n) : ℝ) •
∑ k in Finset.range (n + 1), (Nat.choose n k) ^ 2 • (X - 1) ^ (n - k) * (X + 1) ^ k
end Polynomial
Yury G. Kudryashov (Apr 28 2024 at 15:43):
If you're going to contribute it to Mathlib, then there is a design choice:
- define it as real polynomials, use
aeval
to evaluate at complex numbers; - take
{K : Type*} [ROrCLike K]
as an argument; - define it over any field;
- define a non-normed version (the numerator, or the numerator divided by , it's still a polynomial with integer coefficients, see primed definition) over any ring, then define the normed version over any field.
I don't know if Legendre polynomials over fields other than or are useful.
Mitchell Lee (Apr 28 2024 at 15:59):
They are useful
Mitchell Lee (Apr 28 2024 at 16:04):
I like the fourth choice the most, and you can do it for any -algebra (or more generally, any ring in which 2 is invertible)
Junqi Liu (Apr 29 2024 at 09:02):
nice! Thanks, it's very helpful to me!
Antoine Chambert-Loir (May 02 2024 at 05:31):
Replacing the interval by the interval , and the polynomial by , one gets the shifted Legendre polynomials which have better integrality properties.
Last updated: May 02 2025 at 03:31 UTC