Hi,
I am trying to replicate a simulation that requires using Legendre polynomial function. I have a vector of values between -1 and 1 and need to calculate the Legendre function of different orders evaluated at each element in this vector. I noticed that Matlab has a function (legendre(n,x)) that calculates associated legendre polynomials of degree n with order m (I only need it for m=0) and this produces the values that I expect to have for this vector. However, I'm doing this simulation work in SAS and am wondering if there is an equivalent built-in function in SAS/IML.
I noticed that there is the ORPOL function but I am not quite sure if this is the same function as it generates different values.
Any help is much appreciated.
I think you didn't read all the way to the end of the doc for ORPOL. It includes an example of how to generate Legendre polynomials by using the usual three-term recurrence. The doc example computes the Legendre polynomials up through degree 6. The only thing I added was writing to a SAS data set and plotting the polynomials on [-1, 1]:
proc iml;
maxDegree = 6;
/* evaluate polynomials at these points */
x = T( do(-1,1,0.05) );
/* define the standard Legendre Polynomials
Using the 3-term recurrence with
A[j]=0, B[j]=(2j-1)/j, and C[j]=(j-1)/j
and the standardization P_j(1)=1
which implies P_0(x)=1, P_1(x)=x. */
legendre = j(nrow(x), maxDegree+1);
legendre[,1] = 1; /* P_0 */
legendre[,2] = x; /* P_1 */
do j = 2 to maxDegree;
legendre[,j+1] = (2*j-1)/j # x # legendre[,j] -
(j-1)/j # legendre[,j-1];
end;
*print legendre;
L = x || Legendre;
create Legendre from L[c=('x' || ('L0':'L6'))];
append from L;
close;
QUIT;
proc sgplot data=Legendre;
series x=x y=L0;
series x=x y=L1;
series x=x y=L2;
series x=x y=L3;
series x=x y=L4;
series x=x y=L5;
series x=x y=L6;
run;
I think you didn't read all the way to the end of the doc for ORPOL. It includes an example of how to generate Legendre polynomials by using the usual three-term recurrence. The doc example computes the Legendre polynomials up through degree 6. The only thing I added was writing to a SAS data set and plotting the polynomials on [-1, 1]:
proc iml;
maxDegree = 6;
/* evaluate polynomials at these points */
x = T( do(-1,1,0.05) );
/* define the standard Legendre Polynomials
Using the 3-term recurrence with
A[j]=0, B[j]=(2j-1)/j, and C[j]=(j-1)/j
and the standardization P_j(1)=1
which implies P_0(x)=1, P_1(x)=x. */
legendre = j(nrow(x), maxDegree+1);
legendre[,1] = 1; /* P_0 */
legendre[,2] = x; /* P_1 */
do j = 2 to maxDegree;
legendre[,j+1] = (2*j-1)/j # x # legendre[,j] -
(j-1)/j # legendre[,j-1];
end;
*print legendre;
L = x || Legendre;
create Legendre from L[c=('x' || ('L0':'L6'))];
append from L;
close;
QUIT;
proc sgplot data=Legendre;
series x=x y=L0;
series x=x y=L1;
series x=x y=L2;
series x=x y=L3;
series x=x y=L4;
series x=x y=L5;
series x=x y=L6;
run;
Did my response answer your question? If so, please mark as "Answered." If not, let us know your other questions.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.