[GiNaC-list] Inconsistent output, with integration.

Akshay Srinivasan akshaysrinivasan at gmail.com
Mon Apr 12 18:10:17 CEST 2010

```Hello,
I recently wrote a simple program to generate legendre
polynomials of a given degree n. Thing though is that although the
program is working perfectly in principle, the output thrown out by
the program is not the same everytime I run it. The signs of the
coefficient change between executions; it doesn't seem to follow any
pattern either. I couldn't really figure out why this happens. Can
someone throw more light on this ?

Akshay
--------------------------------------------------------------------
Bash Session:

[neptune ~/devel/cpp] ./legendre 3
L(3) == 35/8*sqrt(8/175)*(5*x^3-3*x)
[neptune ~/devel/cpp] ./legendre 3
L(3) == -35/8*(3*x-5*x^3)*sqrt(8/175)
[neptune ~/devel/cpp] ./legendre 3
L(3) == 35/8*(5*x^3-3*x)*sqrt(8/175)
[neptune ~/devel/cpp] ./legendre 3
L(3) == 35/8*sqrt(8/175)*(5*x^3-3*x)
[neptune ~/devel/cpp] ./legendre 3
L(3) == -35/8*sqrt(8/175)*(3*x-5*x^3)
[neptune ~/devel/cpp]
------------------------------------------------------------------

My program:

#include<iostream>
#include<ginac/ginac.h>

using namespace std;
using namespace GiNaC;

ex inner(ex a, ex b, symbol &x){
return integral(x, -1, 1, a*b).eval_integ();
}

ex legendre(unsigned int n, symbol &x){
ex lp, t;
lp = pow(x, n);
if(n != 0){
for(int i = 0; i < n; i++){
t = legendre(i, x);
lp = lp - inner(lp, t, x) * t;
}
}
lp = lp / sqrt(inner(lp, lp, x));
return lp;
}

int main(int argc, char *argv[]){
symbol x("x");
int n;
ex lp, ln;
n = atoi(argv[1]);
lp = legendre(n, x);
cout << "L(" << n << ") == " << lp << endl;

return 0;
}
-------------------------------------------------------------------------------
```