How to generate orthogonal polynomials in R?


We can say that orthogonal is a synonym of perpendicular. If the inner product (inner product is generalization of dot product) of two polynomials is zero then we call them orthogonal polynomials. In R, we can find the orthogonal product by using poly function as shown in the below examples.

Example1

Live Demo

> x<-rnorm(10)
> x

Output

[1]  1.53798786 -0.85463326 2.39444451 0.82559418 -2.22197322 -1.04243823
[7] -0.04693054 -0.68691236 -1.63040923 -1.42408865

Example

> orthogonal_x<-poly(x,degree=2)
> orthogonal_x

Output

          1          2
[1,] 0.41743651 -0.01687537
[2,] -0.12158589 -0.21414848
[3,] 0.61038362 0.54027924
[4,] 0.25694468 -0.28489882
[5,] -0.42962751 0.57201520
[6,] -0.16389559 -0.14490046
[7,] 0.06037767 -0.37137903
[8,] -0.08380084 -0.26556522
[9,] -0.29635683 0.15165057
[10,] -0.24987583 0.03382235
attr(,"coefs")
attr(,"coefs")$alpha
[1] -0.3149359 0.5267254

attr(,"coefs")$norm2
[1] 1.00000 10.00000 19.70309 32.71111

attr(,"degree")
[1] 1 2
attr(,"class")
[1] "poly" "matrix"

Example2

> orthogonal_x_degree3<-poly(x,degree=3)
> orthogonal_x_degree3

Output

1 2 3
[1,] 0.41743651 -0.01687537 -0.41882840
[2,] -0.12158589 -0.21414848 0.23310682
[3,] 0.61038362 0.54027924 0.42176833
[4,] 0.25694468 -0.28489882 -0.44362203
[5,] -0.42962751 0.57201520 -0.48208095
[6,] -0.16389559 -0.14490046 0.25573226
[7,] 0.06037767 -0.37137903 -0.07651475
[8,] -0.08380084 -0.26556522 0.19221655
[9,] -0.29635683 0.15165057 0.11263775
[10,] -0.24987583 0.03382235 0.20558443
attr(,"coefs")
attr(,"coefs")$alpha
[1] -0.3149359 0.5267254 -0.1157637

attr(,"coefs")$norm2
[1] 1.00000 10.00000 19.70309 32.71111 59.69013

attr(,"degree")
[1] 1 2 3
attr(,"class")
[1] "poly" "matrix"

Example3

> orthogonal_x_degree5<-poly(x,degree=5)
> orthogonal_x_degree5

Output

          1           2           3         4             5
[1,] 0.41743651 -0.01687537 -0.41882840 -0.53824181 -0.4545569
[2,] -0.12158589 -0.21414848 0.23310682 0.05839481 -0.2859929
[3,] 0.61038362 0.54027924 0.42176833 0.21609548 0.0994605
[4,] 0.25694468 -0.28489882 -0.44362203 0.06801947 0.5307426
[5,] -0.42962751 0.57201520 -0.48208095 0.33629533 -0.1868526
[6,] -0.16389559 -0.14490046 0.25573226 -0.09590512 -0.1828276
[7,] 0.06037767 -0.37137903 -0.07651475 0.48513908 0.1252141
[8,] -0.08380084 -0.26556522 0.19221655 0.19189132 -0.3055896
[9,] -0.29635683 0.15165057 0.11263775 -0.37914934 0.4409641
[10,] -0.24987583 0.03382235 0.20558443 -0.34253923 0.2194384
attr(,"coefs")
attr(,"coefs")$alpha
[1] -0.3149359 0.5267254 -0.1157637 0.1206771 -0.1404103

attr(,"coefs")$norm2
[1] 1.00000 10.00000 19.70309 32.71111 59.69013 67.13315 68.51457

attr(,"degree")
[1] 1 2 3 4 5
attr(,"class")
[1] "poly" "matrix"

Example4

> orthogonal_y_degree3<-poly(y,degree=3)
> orthogonal_y_degree3

Output

          1            2        3
[1,] 0.58967559 0.71092333 0.3049628
[2,] -0.07288125 -0.11174051 0.2422385
[3,] -0.07288125 -0.11174051 0.2422385
[4,] 0.05963012 -0.15968259 0.0224123
[5,] 0.19214148 -0.10138725 -0.2628119
[6,] 0.19214148 -0.10138725 -0.2628119
[7,] 0.19214148 -0.10138725 -0.2628119
[8,] -0.07288125 -0.11174051 0.2422385
[9,] -0.20539262 0.04243901 0.2150832
[10,] -0.07288125 -0.11174051 0.2422385
[11,] 0.05963012 -0.15968259 0.0224123
[12,] -0.33790399 0.30285595 -0.2406372
[13,] -0.07288125 -0.11174051 0.2422385
[14,] -0.33790399 0.30285595 -0.2406372
[15,] 0.19214148 -0.10138725 -0.2628119
[16,] 0.05963012 -0.15968259 0.0224123
[17,] -0.33790399 0.30285595 -0.2406372
[18,] -0.20539262 0.04243901 0.2150832
[19,] 0.05963012 -0.15968259 0.0224123
[20,] 0.19214148 -0.10138725 -0.2628119
attr(,"coefs")
attr(,"coefs")$alpha
[1] 4.550000 5.352546 6.177915

attr(,"coefs")$norm2
[1] 1.0000 20.0000 56.9500 354.4091 1091.8162

attr(,"degree")
[1] 1 2 3
attr(,"class")
[1] "poly" "matrix"

Example5

> orthogonal_y_degree5<-poly(y,degree=5)
> orthogonal_y_degree5

Output

           1         2         3          4            5
[1,] 0.58967559 0.71092333 0.3049628 0.06126607 0.01071962
[2,] -0.07288125 -0.11174051 0.2422385 0.02345881 -0.27013433
[3,] -0.07288125 -0.11174051 0.2422385 0.02345881 -0.27013433
[4,] 0.05963012 -0.15968259 0.0224123 0.30209786 0.28138992
[5,] 0.19214148 -0.10138725 -0.2628119 -0.16763520 -0.07503731
[6,] 0.19214148 -0.10138725 -0.2628119 -0.16763520 -0.07503731
[7,] 0.19214148 -0.10138725 -0.2628119 -0.16763520 -0.07503731
[8,] -0.07288125 -0.11174051 0.2422385 0.02345881 -0.27013433
[9,] -0.20539262 0.04243901 0.2150832 -0.46796052 0.37518656
[10,] -0.07288125 -0.11174051 0.2422385 0.02345881 -0.27013433
[11,] 0.05963012 -0.15968259 0.0224123 0.30209786 0.28138992
[12,] -0.33790399 0.30285595 -0.2406372 0.12904848 -0.05359808
[13,] -0.07288125 -0.11174051 0.2422385 0.02345881 -0.27013433
[14,] -0.33790399 0.30285595 -0.2406372 0.12904848 -0.05359808
[15,] 0.19214148 -0.10138725 -0.2628119 -0.16763520 -0.07503731
[16,] 0.05963012 -0.15968259 0.0224123 0.30209786 0.28138992
[17,] -0.33790399 0.30285595 -0.2406372 0.12904848 -0.05359808
[18,] -0.20539262 0.04243901 0.2150832 -0.46796052 0.37518656
[19,] 0.05963012 -0.15968259 0.0224123 0.30209786 0.28138992
[20,] 0.19214148 -0.10138725 -0.2628119 -0.16763520 -0.07503731
attr(,"coefs")
attr(,"coefs")$alpha
[1] 4.550000 5.352546 6.177915 4.717763 4.126941

attr(,"coefs")$norm2
[1] 1.0000 20.0000 56.9500 354.4091 1091.8162 982.6517 729.7255

attr(,"degree")
[1] 1 2 3 4 5
attr(,"class")
[1] "poly" "matrix"

Updated on: 19-Nov-2020

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements