Skip to content

Instantly share code, notes, and snippets.

@jamesthompson
Created July 25, 2013 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesthompson/6084479 to your computer and use it in GitHub Desktop.
Save jamesthompson/6084479 to your computer and use it in GitHub Desktop.
Bonnet's Recursion Formula using Spire for Polynomials... Legendre Polynomials
def legendres(i: Int) : List[Polynomial[Rational]] = {
val one = Polynomial(0 -> r"1/1")
val x = Polynomial(1 -> r"1/1")
lazy val leg : Stream[Polynomial[Rational]] = {
def loop(pnm1: Polynomial[Rational], pn: Polynomial[Rational], n: Int = 1) : Stream[Polynomial[Rational]] = {
pn #:: loop(pn, Polynomial(0 -> Numeric[Rational].fromInt(n + 1).reciprocal) * (
(Polynomial(1 -> Numeric[Rational].fromInt(2 * n + 1)) * pn) +
(Polynomial(0 -> Numeric[Rational].fromInt(n).unary_-) * pnm1)), n + 1)
}
one #:: loop(one, x)
}
leg.take(i).toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment