]> www.ginac.de Git - ginac.git/blob - check/exam_differentiation.cpp
49eddd66b5dc57040e86810ffbf8a7551892dd60
[ginac.git] / check / exam_differentiation.cpp
1 /** @file exam_differentiation.cpp
2  *
3  *  Tests for symbolic differentiation, including various functions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "exams.h"
24
25 static unsigned check_diff(const ex &e, const symbol &x,
26                            const ex &d, unsigned nth=1)
27 {
28     ex ed = e.diff(x, nth);
29     if ((ed - d).compare(ex(0)) != 0) {
30         switch (nth) {
31         case 0:
32             clog << "zeroth ";
33             break;
34         case 1:
35             break;
36         case 2:
37             clog << "second ";
38             break;
39         case 3:
40             clog << "third ";
41             break;
42         default:
43             clog << nth << "th ";
44         }
45         clog << "derivative of " << e << " by " << x << " returned "
46              << ed << " instead of " << d << endl;
47         clog << "returned:" << endl;
48         ed.printtree(clog);
49         clog << endl << "instead of" << endl;
50         d.printtree(clog);
51
52         return 1;
53     }
54     return 0;
55 }
56
57 // Simple (expanded) polynomials
58 static unsigned exam_differentiation1(void)
59 {
60     unsigned result = 0;
61     symbol x("x"), y("y");
62     ex e1, e2, e, d;
63     
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();
68     
69     // d e / dx:
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);
78     
79     // d e / dy:
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);
87     
88     // d^2 e / dx^2:
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);
96     
97     // d^2 e / dy^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)
103         + 98*pow(y,-3);
104     result += check_diff(e, y, d, 2);
105     
106     return result;
107 }
108
109 // Trigonometric functions
110 static unsigned exam_differentiation2(void)
111 {
112     unsigned result = 0;
113     symbol x("x"), y("y"), a("a"), b("b");
114     ex e1, e2, e, d;
115     
116     // construct expression e to be diff'ed:
117     e1 = y*pow(x, 2) + a*x + b;
118     e2 = sin(e1);
119     e = b*pow(e2, 2) + y*e2 + a;
120     
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);
123     
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);
128     
129     d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
130     result += check_diff(e, y, d);
131
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);
135     
136     // construct expression e to be diff'ed:
137     e2 = cos(e1);
138     e = b*pow(e2, 2) + y*e2 + a;
139     
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);
142     
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);
147     
148     d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
149     result += check_diff(e, y, d);
150     
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);
154
155         return result;
156 }
157     
158 // exp function
159 static unsigned exam_differentiation3(void)
160 {
161     unsigned result = 0;
162     symbol x("x"), y("y"), a("a"), b("b");
163     ex e1, e2, e, d;
164
165     // construct expression e to be diff'ed:
166     e1 = y*pow(x, 2) + a*x + b;
167     e2 = exp(e1);
168     e = b*pow(e2, 2) + y*e2 + a;
169     
170     d = 2*b*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
171     result += check_diff(e, x, d);
172     
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);
176     
177     d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
178     result += check_diff(e, y, d);
179     
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);
182
183         return result;
184 }
185
186 // log functions
187 static unsigned exam_differentiation4(void)
188 {
189     unsigned result = 0;
190     symbol x("x"), y("y"), a("a"), b("b");
191     ex e1, e2, e, d;
192     
193     // construct expression e to be diff'ed:
194     e1 = y*pow(x, 2) + a*x + b;
195     e2 = log(e1);
196     e = b*pow(e2, 2) + y*e2 + a;
197     
198     d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
199     result += check_diff(e, x, d);
200     
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);
205     
206     d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
207     result += check_diff(e, y, d);
208     
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);
212
213         return result;
214 }
215
216 // Functions with two variables
217 static unsigned exam_differentiation5(void)
218 {
219     unsigned result = 0;
220     symbol x("x"), y("y"), a("a"), b("b");
221     ex e1, e2, e, d;
222     
223     // test atan2
224     e1 = y*pow(x, 2) + a*x + b;
225     e2 = x*pow(y, 2) + b*y + a;
226     e = atan2(e1,e2);
227     /*
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)));
230     */
231     /*
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);
235     */
236     /*
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);
241     */
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)*
243         (-b-y*pow(x,2)-x*a)+
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);
247     
248     return result;
249 }
250
251 // Series
252 static unsigned exam_differentiation6(void)
253 {
254     symbol x("x");
255     ex e, d, ed;
256     
257     e = sin(x).series(x==0, 8);
258     d = cos(x).series(x==0, 7);
259     ed = e.diff(x);
260     ed = series_to_poly(ed);
261     d = series_to_poly(d);
262     
263     if ((ed - d).compare(ex(0)) != 0) {
264         clog << "derivative of " << e << " by " << x << " returned "
265              << ed << " instead of " << d << ")" << endl;
266         return 1;
267     }
268     return 0;
269 }
270
271 unsigned exam_differentiation(void)
272 {
273     unsigned result = 0;
274     
275     cout << "examining symbolic differentiation" << flush;
276     clog << "----------symbolic differentiation:" << endl;
277     
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;
284     
285     if (!result) {
286         cout << " passed " << endl;
287         clog << "(no output)" << endl;
288     } else {
289         cout << " failed " << endl;
290     }
291     return result;
292 }