1 /** @file exam_differentiation.cpp
3 * Tests for symbolic differentiation, including various functions. */
6 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 using namespace GiNaC;
28 static unsigned check_diff(const ex &e, const symbol &x,
29 const ex &d, unsigned nth=1)
31 ex ed = e.diff(x, nth);
32 if (!(ed - d).is_zero()) {
48 clog << "derivative of " << e << " by " << x << " returned "
49 << ed << " instead of " << d << endl;
50 clog << "returned:" << endl;
51 clog << tree << ed << "instead of\n" << d << dflt;
58 // Simple (expanded) polynomials
59 static unsigned exam_differentiation1()
62 symbol x("x"), y("y");
65 // construct bivariate polynomial e to be diff'ed:
66 e1 = pow(x, -2) * 3 + pow(x, -1) * 5 + 7 + x * 11 + pow(x, 2) * 13;
67 e2 = pow(y, -2) * 5 + pow(y, -1) * 7 + 11 + y * 13 + pow(y, 2) * 17;
68 e = (e1 * e2).expand();
71 d = ex("121-55/x^2-66/x^3-30/x^3/y^2-42/x^3/y-78/x^3*y-102/x^3*y^2-25/x^2/y^2-35/x^2/y-65/x^2*y-85/x^2*y^2+77/y+143*y+187*y^2+130*x/y^2+182/y*x+338*x*y+442*x*y^2+55/y^2+286*x",lst(x,y));
72 result += check_diff(e, x, d);
75 d = ex("91-30/x^2/y^3-21/x^2/y^2+39/x^2+102/x^2*y-50/x/y^3-35/x/y^2+65/x+170/x*y-77*x/y^2+143*x+374*x*y-130/y^3*x^2-91/y^2*x^2+169*x^2+442*x^2*y-110/y^3*x-70/y^3+238*y-49/y^2",lst(x,y));
76 result += check_diff(e, y, d);
79 d = ex("286+90/x^4/y^2+126/x^4/y+234/x^4*y+306/x^4*y^2+50/x^3/y^2+70/x^3/y+130/x^3*y+170/x^3*y^2+130/y^2+182/y+338*y+442*y^2+198/x^4+110/x^3",lst(x,y));
80 result += check_diff(e, x, d, 2);
83 d = ex("238+90/x^2/y^4+42/x^2/y^3+102/x^2+150/x/y^4+70/x/y^3+170/x+330*x/y^4+154*x/y^3+374*x+390*x^2/y^4+182*x^2/y^3+442*x^2+210/y^4+98/y^3",lst(x,y));
84 result += check_diff(e, y, d, 2);
89 // Trigonometric functions
90 static unsigned exam_differentiation2()
93 symbol x("x"), y("y"), a("a"), b("b");
96 // construct expression e to be diff'ed:
97 e1 = y*pow(x, 2) + a*x + b;
99 e = b*pow(e2, 2) + y*e2 + a;
101 d = 2*b*e2*cos(e1)*(2*x*y + a) + y*cos(e1)*(2*x*y + a);
102 result += check_diff(e, x, d);
104 d = 2*b*pow(cos(e1),2)*pow(2*x*y + a, 2) + 4*b*y*e2*cos(e1)
105 - 2*b*pow(e2,2)*pow(2*x*y + a, 2) - y*e2*pow(2*x*y + a, 2)
106 + 2*pow(y,2)*cos(e1);
107 result += check_diff(e, x, d, 2);
109 d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
110 result += check_diff(e, y, d);
112 d = 2*b*pow(cos(e1),2)*pow(x,4) - 2*b*pow(e2,2)*pow(x,4)
113 + 2*cos(e1)*pow(x,2) - y*e2*pow(x,4);
114 result += check_diff(e, y, d, 2);
116 // construct expression e to be diff'ed:
118 e = b*pow(e2, 2) + y*e2 + a;
120 d = -2*b*e2*sin(e1)*(2*x*y + a) - y*sin(e1)*(2*x*y + a);
121 result += check_diff(e, x, d);
123 d = 2*b*pow(sin(e1),2)*pow(2*y*x + a,2) - 4*b*e2*sin(e1)*y
124 - 2*b*pow(e2,2)*pow(2*y*x + a,2) - y*e2*pow(2*y*x + a,2)
125 - 2*pow(y,2)*sin(e1);
126 result += check_diff(e, x, d, 2);
128 d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
129 result += check_diff(e, y, d);
131 d = -2*b*pow(e2,2)*pow(x,4) + 2*b*pow(sin(e1),2)*pow(x,4)
132 - 2*sin(e1)*pow(x,2) - y*e2*pow(x,4);
133 result += check_diff(e, y, d, 2);
139 static unsigned exam_differentiation3()
142 symbol x("x"), y("y"), a("a"), b("b");
145 // construct expression e to be diff'ed:
146 e1 = y*pow(x, 2) + a*x + b;
148 e = b*pow(e2, 2) + y*e2 + a;
150 d = 2*b*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
151 result += check_diff(e, x, d);
153 d = 4*b*pow(e2,2)*pow(2*y*x + a,2) + 4*b*pow(e2,2)*y
154 + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2);
155 result += check_diff(e, x, d, 2);
157 d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
158 result += check_diff(e, y, d);
160 d = 4*b*pow(e2,2)*pow(x,4) + 2*e2*pow(x,2) + y*e2*pow(x,4);
161 result += check_diff(e, y, d, 2);
167 static unsigned exam_differentiation4()
170 symbol x("x"), y("y"), a("a"), b("b");
173 // construct expression e to be diff'ed:
174 e1 = y*pow(x, 2) + a*x + b;
176 e = b*pow(e2, 2) + y*e2 + a;
178 d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
179 result += check_diff(e, x, d);
181 d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1
182 - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1
183 - y*pow(2*x*y + a,2)*pow(e1,-2);
184 result += check_diff(e, x, d, 2);
186 d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
187 result += check_diff(e, y, d);
189 d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4)
190 + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2);
191 result += check_diff(e, y, d, 2);
196 // Functions with two variables
197 static unsigned exam_differentiation5()
200 symbol x("x"), y("y"), a("a"), b("b");
204 e1 = y*pow(x, 2) + a*x + b;
205 e2 = x*pow(y, 2) + b*y + a;
208 d = pow(y,2)*pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)*
210 +pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)*
211 (y*b+pow(y,2)*x+a)*(2*y*x+a);
212 result += check_diff(e, x, d);
218 static unsigned exam_differentiation6()
223 e = sin(x).series(x==0, 8);
224 d = cos(x).series(x==0, 7);
226 ed = series_to_poly(ed);
227 d = series_to_poly(d);
229 if (!(ed - d).is_zero()) {
230 clog << "derivative of " << e << " by " << x << " returned "
231 << ed << " instead of " << d << ")" << endl;
237 // Hashing can help a lot, if differentiation is done cleverly
238 static unsigned exam_differentiation7()
242 ex e = (P.diff(x) / P).diff(x, 2);
243 ex d = 6/P - 18*x/pow(P,2) - 54*pow(x,3)/pow(P,2) + 2/pow(P,3)
244 +18*pow(x,2)/pow(P,3) + 54*pow(x,4)/pow(P,3) + 54*pow(x,6)/pow(P,3);
246 if (!(e-d).expand().is_zero()) {
247 clog << "expanded second derivative of " << (P.diff(x) / P) << " by " << x
248 << " returned " << e.expand() << " instead of " << d << endl;
252 clog << "second derivative of " << (P.diff(x) / P) << " by " << x
253 << " has " << e.nops() << " operands. "
254 << "The result is still correct but not optimal: 3 are enough! "
255 << "(Hint: maybe the product rule for objects of class mul should be more careful about assembling the result?)" << endl;
261 unsigned exam_differentiation()
265 cout << "examining symbolic differentiation" << flush;
267 result += exam_differentiation1(); cout << '.' << flush;
268 result += exam_differentiation2(); cout << '.' << flush;
269 result += exam_differentiation3(); cout << '.' << flush;
270 result += exam_differentiation4(); cout << '.' << flush;
271 result += exam_differentiation5(); cout << '.' << flush;
272 result += exam_differentiation6(); cout << '.' << flush;
273 result += exam_differentiation7(); cout << '.' << flush;
278 int main(int argc, char** argv)
280 return exam_differentiation();