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; } -------------------------------------------------------------------------------
Hi, On Mon, Apr 12, 2010 at 09:40:17PM +0530, Akshay Srinivasan wrote:
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.
This behavior is documented in the manual (section 5.7.2, titled as 'Expanding and collecting'): "Again, since the canonical form in GiNaC is not easy to guess you should be prepared to see different orderings of terms in such sums!".
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 ?
This is kind of design decision: we trade deterministic output for speed. In general, the canonical form of GiNaC expressions is unpredictable, i.e. it changes from run to run (no matter what is the type of the expression). The following script demonstrates this: for i in `seq 1 100`; do echo "a = x; b = y; x - y;" | ginsh done | sort -n | uniq x x-y y -y+x Depending on luck, canonical form of the same expression (x-y) might be either 'x-y' or '-y+x' (of course it's the same during one run). If you need (more) predictable term order in the output please write a custom printing method. Best regards, Alexei
participants (2)
-
Akshay Srinivasan
-
Alexei Sheplyakov