1 /** @file exam_differentiation.cpp
3 * Tests for symbolic differentiation, including various functions. */
6 * GiNaC Copyright (C) 1999-2000 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 static unsigned check_diff(const ex &e, const symbol &x,
26 const ex &d, unsigned nth=1)
28 ex ed = e.diff(x, nth);
29 if ((ed - d).compare(ex(0)) != 0) {
45 clog << "derivative of " << e << " by " << x << " returned "
46 << ed << " instead of " << d << endl;
47 clog << "returned:" << endl;
49 clog << endl << "instead of" << endl;
57 // Simple (expanded) polynomials
58 static unsigned exam_differentiation1(void)
61 symbol x("x"), y("y");
64 // construct bivariate polynomial e to be diff'ed:
65 e1 = pow(x, -2) * 3 + pow(x, -1) * 5 + 7 + x * 11 + pow(x, 2) * 13;
66 e2 = pow(y, -2) * 5 + pow(y, -1) * 7 + 11 + y * 13 + pow(y, 2) * 17;
67 e = (e1 * e2).expand();
70 d = 121 - 55*pow(x,-2) - 66*pow(x,-3) - 30*pow(x,-3)*pow(y,-2)
71 - 42*pow(x,-3)*pow(y,-1) - 78*pow(x,-3)*y
72 - 102*pow(x,-3)*pow(y,2) - 25*pow(x,-2) * pow(y,-2)
73 - 35*pow(x,-2)*pow(y,-1) - 65*pow(x,-2)*y
74 - 85*pow(x,-2)*pow(y,2) + 77*pow(y,-1) + 143*y + 187*pow(y,2)
75 + 130*x*pow(y,-2) + 182*pow(y,-1)*x + 338*x*y + 442*x*pow(y,2)
76 + 55*pow(y,-2) + 286*x;
77 result += check_diff(e, x, d);
80 d = 91 - 30*pow(x,-2)*pow(y,-3) - 21*pow(x,-2)*pow(y,-2)
81 + 39*pow(x,-2) + 102*pow(x,-2)*y - 50*pow(x,-1)*pow(y,-3)
82 - 35*pow(x,-1)*pow(y,-2) + 65*pow(x,-1) + 170*pow(x,-1)*y
83 - 77*pow(y,-2)*x + 143*x + 374*x*y - 130*pow(y,-3)*pow(x,2)
84 - 91*pow(y,-2)*pow(x,2) + 169*pow(x,2) + 442*pow(x,2)*y
85 - 110*pow(y,-3)*x - 70*pow(y,-3) + 238*y - 49*pow(y,-2);
86 result += check_diff(e, y, d);
89 d = 286 + 90*pow(x,-4)*pow(y,-2) + 126*pow(x,-4)*pow(y,-1)
90 + 234*pow(x,-4)*y + 306*pow(x,-4)*pow(y,2)
91 + 50*pow(x,-3)*pow(y,-2) + 70*pow(x,-3)*pow(y,-1)
92 + 130*pow(x,-3)*y + 170*pow(x,-3)*pow(y,2)
93 + 130*pow(y,-2) + 182*pow(y,-1) + 338*y + 442*pow(y,2)
94 + 198*pow(x,-4) + 110*pow(x,-3);
95 result += check_diff(e, x, d, 2);
98 d = 238 + 90*pow(x,-2)*pow(y,-4) + 42*pow(x,-2)*pow(y,-3)
99 + 102*pow(x,-2) + 150*pow(x,-1)*pow(y,-4)
100 + 70*pow(x,-1)*pow(y,-3) + 170*pow(x,-1) + 330*x*pow(y,-4)
101 + 154*x*pow(y,-3) + 374*x + 390*pow(x,2)*pow(y,-4)
102 + 182*pow(x,2)*pow(y,-3) + 442*pow(x,2) + 210*pow(y,-4)
104 result += check_diff(e, y, d, 2);
109 // Trigonometric functions
110 static unsigned exam_differentiation2(void)
113 symbol x("x"), y("y"), a("a"), b("b");
116 // construct expression e to be diff'ed:
117 e1 = y*pow(x, 2) + a*x + b;
119 e = b*pow(e2, 2) + y*e2 + a;
121 d = 2*b*e2*cos(e1)*(2*x*y + a) + y*cos(e1)*(2*x*y + a);
122 result += check_diff(e, x, d);
124 d = 2*b*pow(cos(e1),2)*pow(2*x*y + a, 2) + 4*b*y*e2*cos(e1)
125 - 2*b*pow(e2,2)*pow(2*x*y + a, 2) - y*e2*pow(2*x*y + a, 2)
126 + 2*pow(y,2)*cos(e1);
127 result += check_diff(e, x, d, 2);
129 d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
130 result += check_diff(e, y, d);
132 d = 2*b*pow(cos(e1),2)*pow(x,4) - 2*b*pow(e2,2)*pow(x,4)
133 + 2*cos(e1)*pow(x,2) - y*e2*pow(x,4);
134 result += check_diff(e, y, d, 2);
136 // construct expression e to be diff'ed:
138 e = b*pow(e2, 2) + y*e2 + a;
140 d = -2*b*e2*sin(e1)*(2*x*y + a) - y*sin(e1)*(2*x*y + a);
141 result += check_diff(e, x, d);
143 d = 2*b*pow(sin(e1),2)*pow(2*y*x + a,2) - 4*b*e2*sin(e1)*y
144 - 2*b*pow(e2,2)*pow(2*y*x + a,2) - y*e2*pow(2*y*x + a,2)
145 - 2*pow(y,2)*sin(e1);
146 result += check_diff(e, x, d, 2);
148 d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
149 result += check_diff(e, y, d);
151 d = -2*b*pow(e2,2)*pow(x,4) + 2*b*pow(sin(e1),2)*pow(x,4)
152 - 2*sin(e1)*pow(x,2) - y*e2*pow(x,4);
153 result += check_diff(e, y, d, 2);
159 static unsigned exam_differentiation3(void)
162 symbol x("x"), y("y"), a("a"), b("b");
165 // construct expression e to be diff'ed:
166 e1 = y*pow(x, 2) + a*x + b;
168 e = b*pow(e2, 2) + y*e2 + a;
170 d = 2*b*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
171 result += check_diff(e, x, d);
173 d = 4*b*pow(e2,2)*pow(2*y*x + a,2) + 4*b*pow(e2,2)*y
174 + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2);
175 result += check_diff(e, x, d, 2);
177 d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
178 result += check_diff(e, y, d);
180 d = 4*b*pow(e2,2)*pow(x,4) + 2*e2*pow(x,2) + y*e2*pow(x,4);
181 result += check_diff(e, y, d, 2);
187 static unsigned exam_differentiation4(void)
190 symbol x("x"), y("y"), a("a"), b("b");
193 // construct expression e to be diff'ed:
194 e1 = y*pow(x, 2) + a*x + b;
196 e = b*pow(e2, 2) + y*e2 + a;
198 d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
199 result += check_diff(e, x, d);
201 d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1
202 - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1
203 - y*pow(2*x*y + a,2)*pow(e1,-2);
204 result += check_diff(e, x, d, 2);
206 d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
207 result += check_diff(e, y, d);
209 d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4)
210 + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2);
211 result += check_diff(e, y, d, 2);
216 // Functions with two variables
217 static unsigned exam_differentiation5(void)
220 symbol x("x"), y("y"), a("a"), b("b");
224 e1 = y*pow(x, 2) + a*x + b;
225 e2 = x*pow(y, 2) + b*y + a;
228 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))
229 +(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)));
232 d = ((a+2*y*x)*pow(y*b+pow(y,2)*x+a,-1)-(a*x+b+y*pow(x,2))*
233 pow(y*b+pow(y,2)*x+a,-2)*pow(y,2))*
234 pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1);
237 d = pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1)
238 *pow(y*b+pow(y,2)*x+a,-1)*(a+2*y*x)
239 +pow(y,2)*(-a*x-b-y*pow(x,2))*
240 pow(pow(y*b+pow(y,2)*x+a,2)+pow(a*x+b+y*pow(x,2),2),-1);
242 d = pow(y,2)*pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)*
244 pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)*
245 (y*b+pow(y,2)*x+a)*(2*y*x+a);
246 result += check_diff(e, x, d);
252 static unsigned exam_differentiation6(void)
257 e = sin(x).series(x==0, 8);
258 d = cos(x).series(x==0, 7);
260 ed = series_to_poly(ed);
261 d = series_to_poly(d);
263 if ((ed - d).compare(ex(0)) != 0) {
264 clog << "derivative of " << e << " by " << x << " returned "
265 << ed << " instead of " << d << ")" << endl;
271 unsigned exam_differentiation(void)
275 cout << "examining symbolic differentiation" << flush;
276 clog << "----------symbolic differentiation:" << endl;
278 result += exam_differentiation1(); cout << '.' << flush;
279 result += exam_differentiation2(); cout << '.' << flush;
280 result += exam_differentiation3(); cout << '.' << flush;
281 result += exam_differentiation4(); cout << '.' << flush;
282 result += exam_differentiation5(); cout << '.' << flush;
283 result += exam_differentiation6(); cout << '.' << flush;
286 cout << " passed " << endl;
287 clog << "(no output)" << endl;
289 cout << " failed " << endl;