lexer: when switching to another output stream, clean last read character.
[ginac.git] / check / exam_differentiation.cpp
index 13e54711f089945d2a68000fab95811d26791a7b..2b5a595650388afddcad9f274bd9f1c8a8f07871 100644 (file)
@@ -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-2008 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
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include "exams.h"
+#include <iostream>
+#include "ginac.h"
+using namespace std;
+using namespace GiNaC;
 
 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 +46,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 +56,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 +87,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 +102,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 +121,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 +151,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 +164,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 +179,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 +204,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,44 +226,43 @@ 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;
        
        cout << "examining symbolic differentiation" << flush;
-       clog << "----------symbolic differentiation:" << endl;
        
        result += exam_differentiation1();  cout << '.' << flush;
        result += exam_differentiation2();  cout << '.' << flush;
@@ -286,11 +272,10 @@ unsigned exam_differentiation(void)
        result += exam_differentiation6();  cout << '.' << flush;
        result += exam_differentiation7();  cout << '.' << flush;
        
-       if (!result) {
-               cout << " passed " << endl;
-               clog << "(no output)" << endl;
-       } else {
-               cout << " failed " << endl;
-       }
        return result;
 }
+
+int main(int argc, char** argv)
+{
+       return exam_differentiation();
+}