Legendre polynomials as computed by orthopolynom.
legendre(degree, indeterminate = "x", normalized = FALSE)
degree | degree of polynomial |
---|---|
indeterminate | indeterminate |
normalized | provide normalized coefficients |
a mpoly object or mpolyList object
legendre(0)#> 1legendre(1)#> xlegendre(2)#> -0.5 + 1.5 x^2legendre(3)#> -1.5 x + 2.5 x^3legendre(4)#> 0.375 - 3.75 x^2 + 4.375 x^4legendre(5)#> 1.875 x - 8.75 x^3 + 7.875 x^5legendre(6)#> -0.3125 + 6.5625 x^2 - 19.6875 x^4 + 14.4375 x^6legendre(2)#> -0.5 + 1.5 x^2legendre(2, normalized = TRUE)#> -0.7905694 + 2.371708 x^2legendre(0:5)#> 1 #> x #> 1.5 x^2 - 0.5 #> 2.5 x^3 - 1.5 x #> 4.375 x^4 - 3.75 x^2 + 0.375 #> 7.875 x^5 - 8.75 x^3 + 1.875 xlegendre(0:5, normalized = TRUE)#> 0.707106781186547 #> 1.224745 x #> 2.371708 x^2 - 0.7905694 #> 4.677072 x^3 - 2.806243 x #> 9.280777 x^4 - 7.954951 x^2 + 0.7954951 #> 18.46851 x^5 - 20.52057 x^3 + 4.397265 xlegendre(0:5, indeterminate = "t")#> 1 #> t #> 1.5 t^2 - 0.5 #> 2.5 t^3 - 1.5 t #> 4.375 t^4 - 3.75 t^2 + 0.375 #> 7.875 t^5 - 8.75 t^3 + 1.875 t# visualize the legendre polynomials library(ggplot2); theme_set(theme_classic()) library(tidyr) s <- seq(-1, 1, length.out = 201) N <- 5 # number of legendre polynomials to plot (legPolys <- legendre(0:N))#> 1 #> x #> 1.5 x^2 - 0.5 #> 2.5 x^3 - 1.5 x #> 4.375 x^4 - 3.75 x^2 + 0.375 #> 7.875 x^5 - 8.75 x^3 + 1.875 x# see ?bernstein for a better understanding of # how the code below works df <- data.frame(s, as.function(legPolys)(s)) names(df) <- c("x", paste0("P_", 0:N)) mdf <- gather(df, degree, value, -x) qplot(x, value, data = mdf, geom = "line", color = degree)# legendre polynomials and the QR decomposition n <- 201 x <- seq(-1, 1, length.out = n) mat <- cbind(1, x, x^2, x^3, x^4, x^5) Q <- qr.Q(qr(mat)) df <- as.data.frame(cbind(x, Q)) names(df) <- c("x", 0:5) mdf <- gather(df, degree, value, -x) qplot(x, value, data = mdf, geom = "line", color = degree)Q <- apply(Q, 2, function(x) x / x[n]) df <- as.data.frame(cbind(x, Q)) names(df) <- c("x", paste0("P_", 0:5)) mdf <- gather(df, degree, value, -x) qplot(x, value, data = mdf, geom = "line", color = degree)#>#>#>#> 0 with absolute error < 3e-15#> -3.469447e-18 with absolute error < 2.6e-15#> 0 with absolute error < 2.2e-15#> [1] -0.007063278#> [1] 0.004985284#> [1] -0.004569647#> [1] -0.007063278#> [1] 0.004985284#> [1] -0.004569647