# [Maxima] Implementation of 2F1(a,b; a+b+1/2; z)

Dieter Kaiser drdieterkaiser at web.de
Wed Jan 6 17:54:06 CST 2010

```I think the implementation of 2F1(a,b; a+b+1/2; z) has a problem. This
affects the implementation of 2F1(a,b; a+b-1/2) too. These
hypergeometric functions use the algorithm legf20.

We can see it the following way:

Maxima knows the special case 2F1(n,-1/2; n-1/2+1/2; x):

(%i1) hgfred([a,-1/2],[a-1/2+1/2],x);
(%o1) sqrt(1-x)

This result does not depend on the parameter a. For a positive argument
2 the result is:

(%i3) hgfred([n,-1/2],[n-1/2+1/2],2);
(%o3) %i

For a negative argument -2 we get:

(%i4) hgfred([n,-1/2],[n-1/2+1/2],-2);
(%o4) sqrt(3)

Now we calculate the more general result for an argument z=2. This uses
the algorithm legf20 for the assoicated legendre function:

(%i6) res:hgfred([a,b],[a+b+1/2],2);
(%o6) assoc_legendre_p(-b+a-1/2,-b-a+1/2,%i)
*(-2)^((-b-a+1/2)/2)*2^(b+a-1/2)*gamma(b+a+1/2)

If we insert the special case b=-1/2 and choose different integer values
for the parameter a. We get the following different results:

(%i7) res,a=1,b=-1/2; /* This is the expected result */
(%o7) %i

(%i8) res,a=2,b=-1/2; /* The following results differ by a phase */
(%o8) 1

(%i9) res,a=3,b=-1/2;
(%o9) -%i

(%i10) res,a=4,b=-1/2;
(%o10) -1

The same with a negative argument -2:

(%i11) res:hgfred([a,b],[a+b+1/2],-2);
(%o11) assoc_legendre_p(-b+a-1/2,-b-a+1/2,sqrt(3))
*2^(b+(-b-a+1/2)/2+a-1/2)*gamma(b+a+1/2)

Again we get different results when we choose different integer values
for a:

(%i12) res,a=1,b=-1/2; /* This is the expected result */
(%o12) sqrt(3)

(%i13) res,a=2,b=-1/2; /* The following results differ by a phase */
(%o13) sqrt(3)*%i

(%i14) res,a=3,b=-1/2;
(%o14) -sqrt(3)

(%i15) res,a=4,b=-1/2;
(%o15) -sqrt(3)*%i

This is the most simple example. We can repeat this for other values of
b e.g. b=-3/2 and z=-2. Again the special case which uses an algorithm
different from the implemented legendre function:

(%i16) hgfred([n,-3/2],[n-3/2+1/2],-2);
(%o16) 3^(3/2)/(n-1)+3^(3/2)

Now we get for different integer values different results, all results
are real:

(%i17) ratsimp(hgfred([n,-3/2],[n-3/2+1/2],-2)),n=2;
(%o17) 2*3^(3/2)

(%i18) ratsimp(hgfred([n,-3/2],[n-3/2+1/2],-2)),n=3;
(%o18) 3^(5/2)/2

(%i19) ratsimp(hgfred([n,-3/2],[n-3/2+1/2],-2)),n=4;
(%o19) 4*sqrt(3)

(%i20) ratsimp(hgfred([n,-3/2],[n-3/2+1/2],-2)),n=5;
(%o20) 5*3^(3/2)/4

Again we first calculate the more general result:

(%i21) res:hgfred([a,b],[a+b+1/2],-2);

(%o21) assoc_legendre_p(-b+a-1/2,-b-a+1/2,sqrt(3))
*2^(b+(-b-a+1/2)/2+a-1/2)*gamma(b+a+1/2)

Now we simplify the more general result and get again wrong results
which differ from above:

(%i22) ratsimp(res),a=2,b=-3/2;
(%o22) 2*3^(3/2)

(%i23) ratsimp(res),a=3,b=-3/2; /* Again an extra phase factor */
(%o23) 3^(5/2)*%i/2

(%i24) ratsimp(res),a=4,b=-3/2;
(%o24) -4*sqrt(3)

(%i25) ratsimp(res),a=5,b=-3/2;
(%o25) -5*3^(3/2)*%i/4

We get an extra phase factor exp(+/- %i*%pi*a/2). For values 0<x<1 all
is correct.

The implementation for x>1 and x<0 uses A&S 15.4.12. This formula has a
factor

(-z)^(1/2*(1/2-a-b))

The negative sign of z is responsible for the extra phase factor of the
result. functions.wolfram.com has this formula too, but claims that it
is correct for all values z without an extra negative sign. Furthermore,
functions.wolfram.com has an equivalent formula which includes the
negative sign too. But this formula uses an associated legendre function
of type 3. This type of legendre function has a different phase factor.
For a positive value x>1 the phase factor is exp(+%i*%pi*m/2) and for
x<-1 exp(-%i*%pi*m/2), where m is the order of the associated legendre
function.

A&S 8.1.2 defines the associated legendre function with a factor

(z+1)^(m/2)/(z-1)^(m/2)

but this is the factor function.wolfram.com uses to define the
associated legendre function of type 3. The associated legendre
functions implemented in Maxima are of type 2. These are defined with a
factor

(z+1)^(m/2)/(1-z)^(m/2)

Example:
type 2:	P(1,1,z) = -sqrt(1-z^2)
type 3: P(1,1,z) = sqrt(z-1)*sqrt(z+1)

Summary:

I think the implementation of legf20 should not introduce the extra
negative sign and the examples from above will be correct. The
difference occur because of different definitions of the associated
legendre function in A&S and Maxima. This is the formula we need to get
the expected results:

2F1(a,b,a+b+1/2) = 2^(a+b+-1/2) * gamma(a+b+1/2) * z^(1/2*(1/2-a-b))
* assoc_legendre_p(a-b-1/2,1/2-a-b,sqrt(1-z))

Remark: Examples 137 and 138 are tests for the implemented algorithm
legf20 and will fail. But these tests do not really check the algorithm,
but reproduce it.

By the way: The formula of functions.wolfram.com seems to have an error
too. There the degree of the associated legendre function is written as
b-a-1/2. We have a-b-1/2 like A&S 15.4.12 and A&S 15.4.13.

Dieter Kaiser

```