Programming Guide



I looked at the tutorial, and found it interesting.
However, in the section on Legendre Polynomials, it says..
 
The Legendre polynomials are defined by:

  p0(x) = 1

  p1(x) = x

  n*pn(x) = (2*n -1)*x*pn-1(x) - (n - 1)*pn-2(x)

The objective of this section is to develop programs that compute the
Legendre polynomial for a given value of n.

For a first attempt we try to follow the definition as close as possible:

Legendre1(n, x) :=

block ( [],

    if n = 0 

       then 1

       else

        if n = 1

           then x

           else  ((2*n - 1)*x*Legendre1 (n - 1, x)

                  - (n - 1)  *Legendre1 (n - 2, x)) / n

)
 
...............
In fact, the Legendre Polynomials can be defined by following the definition
this way:
p[0](x):=1;
p[1](x):=x;
p[n](x):=(1/n)*((2*n-1)*x*p[n-1](x)-(n-1)*p[n-2](x));
 
almost exactly as defined.  It can be made to run faster than the LegendreNN
version below by setting p[0](x):=rat(1). 
and is about 6 times faster than the version below.  Though for reasons of 
limited stack space it may be better in some maximas not to try computing
p[200](x)
without, say, computing p[100](x) first.  All the polynomials are stored in
memory 
which may also be unnecessary.  Using mapping functions may be faster than
the while and for loops, too.
 

LegendreNN(n, x) :=

block ( [cnt, pn, coeffsP0, coeffsP1, coeffsPN, oldList ],

   if n = 0

      then return (1)

      else if n = 1

              then return (x),

   coeffsP0: makelist (0, x, 0, n),

   coeffsP1: makelist (0, x, 0, n),

   coeffsPN: makelist (0, x, 0, n),

   coeffsP0[1]: 1,

   coeffsP1[2]: 1,

   cnt: 2,

   while cnt <= n do

    (coeffsPN[1]: -coeffsP0[1]*(cnt - 1)/cnt,

       for idx : 2 thru cnt + 1 do

          coeffsPN[idx] :  ((2*cnt - 1)*coeffsP1[idx - 1]

                             -(cnt - 1)*coeffsP0[idx])/ cnt,

        

       oldList: coeffsP0,

       coeffsP0: coeffsP1,

       coeffsP1: coeffsPN,

       coeffsPN: oldList,

       cnt: cnt + 1 

    ),

   pn: 0,

   for idx:1 thru n + 1 do

     pn: pn + coeffsP1[idx]*x^(idx - 1),

   pn      

);
 
 


  _____  

From: maxima-bounces at math.utexas.edu [mailto:maxima-bounces at math.utexas.edu]
On Behalf Of Alasdair McAndrew
Sent: Friday, March 30, 2007 10:25 PM
To: Zoho Vignochi; maxima list
Subject: Re: [Maxima] Programming Guide


For starting with Maxima programming, there is Boris Gaertner's tutorial at

http://maxima.sourceforge.net/docs/tutorial/en/gaertner-tutorial-revision/Pa
ges/Programming0001.htm
<http://maxima.sourceforge.net/docs/tutorial/en/gaertner-tutorial-revision/P
ages/Programming0001.htm> 

or you can just browse through *.mac and *.lisp in the Maxima directory
tree.

-Alasdair