Obtaining Legendre polynomial form once Legendre coefficients are determined
NickName:Palmetto_Girl86 Ask DateTime:2015-06-11T04:19:15

Obtaining Legendre polynomial form once Legendre coefficients are determined

I have obtained the coefficients for the Legendre polynomial that best fits my data. Now I am needing to determine the value of that polynomial at each time-step of my data. I need to do this so that I can subtract the fit from my data. I have looked at the documentation for the Legendre module, and I'm not sure if I just don't understand my options or if there isn't a native tool in place for what I want. If my data-points were evenly spaced, linspace would be a good option, but that's not the case here. Does anyone have a suggestion for what to try?

For those who would like to demand a minimum working example of code, just use a random array, get the coefficients, and tell me from there how you would proceed. The values themselves don't matter. It's the technique that I'm asking about here. Thanks.

Copyright Notice:Content Author:「Palmetto_Girl86」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30766512/obtaining-legendre-polynomial-form-once-legendre-coefficients-are-determined

Answers
Charles Harris 2015-06-14T04:21:21

To simplify Ahmed's example\n\nIn [1]: from numpy.polynomial import Polynomial, Legendre\n\nIn [2]: p = Polynomial([0.5, 0.3, 0.1])\n\nIn [3]: x = np.random.rand(10) * 10\n\nIn [4]: y = p(x)\n\nIn [5]: pfit = Legendre.fit(x, y, 2)\n\nIn [6]: plot(*pfit.linspace())\nOut[6]: [<matplotlib.lines.Line2D at 0x7f815364f310>]\n\nIn [7]: plot(x, y, 'o')\nOut[7]: [<matplotlib.lines.Line2D at 0x7f81535d8bd0>]\n\n\nThe Legendre functions are scaled and offset, as the data should be confined to the interval [-1, 1] to get any advantage over the usual power basis. If you want the coefficients for plain old Legendre functions\n\nIn [8]: pfit.convert()\nOut[8]: Legendre([ 0.53333333, 0.3 , 0.06666667], [-1., 1.], [-1., 1.])\n\n\nBut that isn't recommended.",


Alex Huszagh 2015-06-10T20:27:12

Once you have a function, you can just generate a numpy array for the timepoints:\n\n>>> import numpy as np\n>>> timepoints = [1,3,7,15,16,17,19]\n>>> myarray = np.array(timepoints)\n>>> def mypolynomial(bins, pfinal): #pfinal is just the estimate of the final array (i'll do quadratic)\n... a,b,c = pfinal # obviously, for a*x^2 + b*x + c\n... return (a*bins**2) + b*bins + c\n>>> mypolynomial(myarray, (1,1,0))\narray([ 2, 12, 56, 240, 272, 306, 380])\n\n\nIt automatically evaluates it for each timepoint is in the numpy array.\n\nNow all you have to do is rewrite mypolynomial to go from a simple quadratic example to a proper one for a Legendre polynomial. Treat the function as if it were evaluating a float to return the value, and when called on the numpy array it will automatically evaluate it for each value.\n\nEDIT:\nLet's say I wanted to generalize this to all standard polynomials:\n\n>>> import numpy as np\n>>> timepoints = [1,3,7,15,16,17,19]\n>>> myarray = np.array(timepoints)\n>>> def mypolynomial(bins, pfinal): #pfinal is just the estimate of the final array (i'll do quadratic)\n>>> hist = np.zeros((1, len(myarray))) # define blank return\n... for i in range(len(pfinal)):\n... # fixed a typo here, was pfinal[-i] which would give -0 rather than -1, since negative indexing starts at -1, not -0\n... const = pfinal[-i-1] # negative index to go from 0 exponent to highest exponent\n... hist += const*(bins**i)\n... return hist\n>>> mypolynomial(myarray, (1,1,0))\narray([ 2, 12, 56, 240, 272, 306, 380])\n\n\nEDIT2: Typo fix\n\nEDIT3:\n\n@Ahmed is perfect right when he states Homer's rule is good for numerical stability. The implementation here would be as follows:\n\n>>> def horner(coeffs, x):\n... acc = 0\n... for c in coeffs:\n... acc = acc * x + c\n... return acc\n>>> horner((1,1,0), myarray)\narray([ 2, 12, 56, 240, 272, 306, 380])\n\n\nSlightly modified to keep the same argument order as before, from the code here:\nhttp://rosettacode.org/wiki/Horner%27s_rule_for_polynomial_evaluation#Python",


More about “Obtaining Legendre polynomial form once Legendre coefficients are determined” related questions

Obtaining Legendre polynomial form once Legendre coefficients are determined

I have obtained the coefficients for the Legendre polynomial that best fits my data. Now I am needing to determine the value of that polynomial at each time-step of my data. I need to do this so ...

Show Detail

fitting by polynomial.legendre and got the coefficients

when I use them np.polynomial.legendre.Legendre.fit(x,y,25) I got 26 coefficients while in the Legendre formula for n=25 I should have 13 coefficients what happened here? and how can get the

Show Detail

Legendre Polynomials Python

I am trying to calculate the 720th Legendre polynomial with scipy: &gt;&gt;&gt;from scipy.special import legendre &gt;&gt;&gt;print(legendre(720)) it prints the powers correctly but returns NAN f...

Show Detail

how to access numpy legendre polynomial basis functions

I can't figure out how to access the Legendre basis functions L_i(x) with numpy.polynomial.legendre. I can only access weighted sums of them, of the sum c_i L_i where c are constant coefficients. H...

Show Detail

Legendre polynomials derivative

I use this code to create legendre polynomials, from 1st to the 7th order. N = 4000 xvals = np.linspace(-1, 1, N) def Legendre(x, n): leg = legendre(n) P_n = leg(x) return P_n for i in

Show Detail

Finding roots of Legendre polynomial in python

I'm writing a program that solves an integral by Legendre-Gauss quadrature. The algorithm for nth-order quadrature requires, at one point, finding the roots of the nth-order Legendre polynomial, P...

Show Detail

Octave Matrix of discretized Legendre polynomials

I need to get N x columns(L) matrix of legendre polynomials evaluated over L for arbitrary N. Is there a better way of computing the matrix than just explicitly evaluating the polynomial vector fo...

Show Detail

Integrate Legendre polynomials in SymPy and use these integrals as coefficients

I am trying to make a simple example in SymPy to compute some coefficients and then use them in a sum of legendre polynomials. Finally plot it. Very simple but can not make it work. I want to use i...

Show Detail

Functional Link Neural Network use Legendre Polynomial

How can I apply the Functional Link Neural Network (Legendre Polynomial) with 4 inputs, namely training data, target training, testing data, and target testing for a microarray. I'm working on my f...

Show Detail

Matlab code optimization for Legendre polynomials

I know Matlab has built-in functions for determining the associated Legendre functions. I want to compute the Legendre polynomials which are a particular case of those ones. I have written my own c...

Show Detail