X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=check%2Fexam_differentiation.cpp;h=ccb97dab9a8926c55598585ffd683ec5ffb36028;hp=13e54711f089945d2a68000fab95811d26791a7b;hb=96af2609db413ddf70371e0423e6c07ecb5ee813;hpb=af922d5eb36ed70e4a9e3ffaf4c24492cf89a1a6 diff --git a/check/exam_differentiation.cpp b/check/exam_differentiation.cpp index 13e54711..ccb97dab 100644 --- a/check/exam_differentiation.cpp +++ b/check/exam_differentiation.cpp @@ -3,7 +3,7 @@ * Tests for symbolic differentiation, including various functions. */ /* - * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,7 +26,7 @@ static unsigned check_diff(const ex &e, const symbol &x, const ex &d, unsigned nth=1) { ex ed = e.diff(x, nth); - if ((ed - d).compare(ex(0)) != 0) { + if (!(ed - d).is_zero()) { switch (nth) { case 0: clog << "zeroth "; @@ -43,11 +43,9 @@ static unsigned check_diff(const ex &e, const symbol &x, clog << nth << "th "; } clog << "derivative of " << e << " by " << x << " returned " - << ed << " instead of " << d << endl; + << ed << " instead of " << d << endl; clog << "returned:" << endl; - ed.printtree(clog); - clog << endl << "instead of" << endl; - d.printtree(clog); + clog << tree << ed << "instead of\n" << d << dflt; return 1; } @@ -55,7 +53,7 @@ static unsigned check_diff(const ex &e, const symbol &x, } // Simple (expanded) polynomials -static unsigned exam_differentiation1(void) +static unsigned exam_differentiation1() { unsigned result = 0; symbol x("x"), y("y"); @@ -86,7 +84,7 @@ static unsigned exam_differentiation1(void) } // Trigonometric functions -static unsigned exam_differentiation2(void) +static unsigned exam_differentiation2() { unsigned result = 0; symbol x("x"), y("y"), a("a"), b("b"); @@ -101,15 +99,15 @@ static unsigned exam_differentiation2(void) result += check_diff(e, x, d); d = 2*b*pow(cos(e1),2)*pow(2*x*y + a, 2) + 4*b*y*e2*cos(e1) - - 2*b*pow(e2,2)*pow(2*x*y + a, 2) - y*e2*pow(2*x*y + a, 2) - + 2*pow(y,2)*cos(e1); + - 2*b*pow(e2,2)*pow(2*x*y + a, 2) - y*e2*pow(2*x*y + a, 2) + + 2*pow(y,2)*cos(e1); result += check_diff(e, x, d, 2); d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2); result += check_diff(e, y, d); d = 2*b*pow(cos(e1),2)*pow(x,4) - 2*b*pow(e2,2)*pow(x,4) - + 2*cos(e1)*pow(x,2) - y*e2*pow(x,4); + + 2*cos(e1)*pow(x,2) - y*e2*pow(x,4); result += check_diff(e, y, d, 2); // construct expression e to be diff'ed: @@ -120,22 +118,22 @@ static unsigned exam_differentiation2(void) result += check_diff(e, x, d); d = 2*b*pow(sin(e1),2)*pow(2*y*x + a,2) - 4*b*e2*sin(e1)*y - - 2*b*pow(e2,2)*pow(2*y*x + a,2) - y*e2*pow(2*y*x + a,2) - - 2*pow(y,2)*sin(e1); + - 2*b*pow(e2,2)*pow(2*y*x + a,2) - y*e2*pow(2*y*x + a,2) + - 2*pow(y,2)*sin(e1); result += check_diff(e, x, d, 2); d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2); result += check_diff(e, y, d); d = -2*b*pow(e2,2)*pow(x,4) + 2*b*pow(sin(e1),2)*pow(x,4) - - 2*sin(e1)*pow(x,2) - y*e2*pow(x,4); + - 2*sin(e1)*pow(x,2) - y*e2*pow(x,4); result += check_diff(e, y, d, 2); return result; } // exp function -static unsigned exam_differentiation3(void) +static unsigned exam_differentiation3() { unsigned result = 0; symbol x("x"), y("y"), a("a"), b("b"); @@ -150,7 +148,7 @@ static unsigned exam_differentiation3(void) result += check_diff(e, x, d); d = 4*b*pow(e2,2)*pow(2*y*x + a,2) + 4*b*pow(e2,2)*y - + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2); + + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2); result += check_diff(e, x, d, 2); d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2); @@ -163,7 +161,7 @@ static unsigned exam_differentiation3(void) } // log functions -static unsigned exam_differentiation4(void) +static unsigned exam_differentiation4() { unsigned result = 0; symbol x("x"), y("y"), a("a"), b("b"); @@ -178,22 +176,22 @@ static unsigned exam_differentiation4(void) result += check_diff(e, x, d); d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1 - - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1 - - y*pow(2*x*y + a,2)*pow(e1,-2); + - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1 + - y*pow(2*x*y + a,2)*pow(e1,-2); result += check_diff(e, x, d, 2); d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1; result += check_diff(e, y, d); d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4) - + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2); + + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2); result += check_diff(e, y, d, 2); return result; } // Functions with two variables -static unsigned exam_differentiation5(void) +static unsigned exam_differentiation5() { unsigned result = 0; symbol x("x"), y("y"), a("a"), b("b"); @@ -203,32 +201,18 @@ static unsigned exam_differentiation5(void) e1 = y*pow(x, 2) + a*x + b; e2 = x*pow(y, 2) + b*y + a; e = atan2(e1,e2); - /* - d = pow(y,2)*(-b-y*pow(x,2)-a*x)/(pow(b+y*pow(x,2)+a*x,2)+pow(x*pow(y,2)+b*y+a,2)) - +(2*y*x+a)/((x*pow(y,2)+b*y+a)*(1+pow(b*y*pow(x,2)+a*x,2)/pow(x*pow(y,2)+b*y+a,2))); - */ - /* - d = ((a+2*y*x)*pow(y*b+pow(y,2)*x+a,-1)-(a*x+b+y*pow(x,2))* - pow(y*b+pow(y,2)*x+a,-2)*pow(y,2))* - pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1); - */ - /* - d = pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1) - *pow(y*b+pow(y,2)*x+a,-1)*(a+2*y*x) - +pow(y,2)*(-a*x-b-y*pow(x,2))* - pow(pow(y*b+pow(y,2)*x+a,2)+pow(a*x+b+y*pow(x,2),2),-1); - */ + d = pow(y,2)*pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)* - (-b-y*pow(x,2)-x*a)+ - pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)* - (y*b+pow(y,2)*x+a)*(2*y*x+a); + (-b-y*pow(x,2)-x*a) + +pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)* + (y*b+pow(y,2)*x+a)*(2*y*x+a); result += check_diff(e, x, d); return result; } // Series -static unsigned exam_differentiation6(void) +static unsigned exam_differentiation6() { symbol x("x"); ex e, d, ed; @@ -239,39 +223,39 @@ static unsigned exam_differentiation6(void) ed = series_to_poly(ed); d = series_to_poly(d); - if ((ed - d).compare(ex(0)) != 0) { + if (!(ed - d).is_zero()) { clog << "derivative of " << e << " by " << x << " returned " - << ed << " instead of " << d << ")" << endl; + << ed << " instead of " << d << ")" << endl; return 1; } return 0; } // Hashing can help a lot, if differentiation is done cleverly -static unsigned exam_differentiation7(void) +static unsigned exam_differentiation7() { symbol x("x"); ex P = x + pow(x,3); ex e = (P.diff(x) / P).diff(x, 2); ex d = 6/P - 18*x/pow(P,2) - 54*pow(x,3)/pow(P,2) + 2/pow(P,3) - +18*pow(x,2)/pow(P,3) + 54*pow(x,4)/pow(P,3) + 54*pow(x,6)/pow(P,3); + +18*pow(x,2)/pow(P,3) + 54*pow(x,4)/pow(P,3) + 54*pow(x,6)/pow(P,3); if (!(e-d).expand().is_zero()) { clog << "expanded second derivative of " << (P.diff(x) / P) << " by " << x - << " returned " << e.expand() << " instead of " << d << endl; + << " returned " << e.expand() << " instead of " << d << endl; return 1; } if (e.nops() > 3) { clog << "second derivative of " << (P.diff(x) / P) << " by " << x - << " has " << e.nops() << " operands. " - << "The result is still correct but not optimal: 3 are enough! " - << "(Hint: maybe the product rule for objects of class mul should be more careful about assembling the result?)" << endl; + << " has " << e.nops() << " operands. " + << "The result is still correct but not optimal: 3 are enough! " + << "(Hint: maybe the product rule for objects of class mul should be more careful about assembling the result?)" << endl; return 1; } return 0; } -unsigned exam_differentiation(void) +unsigned exam_differentiation() { unsigned result = 0;