]> www.ginac.de Git - ginac.git/blob - check/differentiation.cpp
- changed function::diff() to be more tolerant by checking first if the
[ginac.git] / check / differentiation.cpp
1 /** @file differentiation.cpp
2  *
3  *  Tests for symbolic differentiation, including various functions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999 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 <ginac/ginac.h>
24 using namespace GiNaC;
25
26 static unsigned check_diff(const ex &e, const symbol &x,
27                            const ex &d, unsigned nth=1)
28 {
29     ex ed = e.diff(x, nth);
30     if ((ed - d).compare(exZERO()) != 0) {
31         switch (nth) {
32         case 0:
33             clog << "zeroth ";
34             break;
35         case 1:
36             break;
37         case 2:
38             clog << "second ";
39             break;
40         case 3:
41             clog << "third ";
42             break;
43         default:
44             clog << nth << "th ";
45         }
46         clog << "derivative of " << e << " by " << x << " returned "
47              << ed << " instead of " << d << endl;
48         clog << "returned:" << endl;
49         ed.printtree(clog);
50         clog << endl << "instead of" << endl;
51         d.printtree(clog);
52
53         return 1;
54     }
55     return 0;
56 }
57
58 // Simple (expanded) polynomials
59 static unsigned differentiation1(void)
60 {
61     unsigned result = 0;
62     symbol x("x"), y("y");
63     ex e1, e2, e, d;
64     
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();
69     
70     // d e / dx:
71     d = 121 - 55*pow(x,-2) - 66*pow(x,-3) - 30*pow(x,-3)*pow(y,-2)
72         - 42*pow(x,-3)*pow(y,-1) - 78*pow(x,-3)*y
73         - 102*pow(x,-3)*pow(y,2) - 25*pow(x,-2) * pow(y,-2)
74         - 35*pow(x,-2)*pow(y,-1) - 65*pow(x,-2)*y
75         - 85*pow(x,-2)*pow(y,2) + 77*pow(y,-1) + 143*y + 187*pow(y,2)
76         + 130*x*pow(y,-2) + 182*pow(y,-1)*x + 338*x*y + 442*x*pow(y,2)
77         + 55*pow(y,-2) + 286*x;
78     result += check_diff(e, x, d);
79     
80     // d e / dy:
81     d = 91 - 30*pow(x,-2)*pow(y,-3) - 21*pow(x,-2)*pow(y,-2)
82         + 39*pow(x,-2) + 102*pow(x,-2)*y - 50*pow(x,-1)*pow(y,-3)
83         - 35*pow(x,-1)*pow(y,-2) + 65*pow(x,-1) + 170*pow(x,-1)*y
84         - 77*pow(y,-2)*x + 143*x + 374*x*y - 130*pow(y,-3)*pow(x,2)
85         - 91*pow(y,-2)*pow(x,2) + 169*pow(x,2) + 442*pow(x,2)*y
86         - 110*pow(y,-3)*x - 70*pow(y,-3) + 238*y - 49*pow(y,-2);
87     result += check_diff(e, y, d);
88     
89     // d^2 e / dx^2:
90     d = 286 + 90*pow(x,-4)*pow(y,-2) + 126*pow(x,-4)*pow(y,-1)
91         + 234*pow(x,-4)*y + 306*pow(x,-4)*pow(y,2)
92         + 50*pow(x,-3)*pow(y,-2) + 70*pow(x,-3)*pow(y,-1)
93         + 130*pow(x,-3)*y + 170*pow(x,-3)*pow(y,2)
94         + 130*pow(y,-2) + 182*pow(y,-1) + 338*y + 442*pow(y,2)
95         + 198*pow(x,-4) + 110*pow(x,-3);
96     result += check_diff(e, x, d, 2);
97     
98     // d^2 e / dy^2:
99     d = 238 + 90*pow(x,-2)*pow(y,-4) + 42*pow(x,-2)*pow(y,-3)
100         + 102*pow(x,-2) + 150*pow(x,-1)*pow(y,-4)
101         + 70*pow(x,-1)*pow(y,-3) + 170*pow(x,-1) + 330*x*pow(y,-4)
102         + 154*x*pow(y,-3) + 374*x + 390*pow(x,2)*pow(y,-4)
103         + 182*pow(x,2)*pow(y,-3) + 442*pow(x,2) + 210*pow(y,-4)
104         + 98*pow(y,-3);
105     result += check_diff(e, y, d, 2);
106     
107     return result;
108 }
109
110 // Trigonometric functions
111 static unsigned differentiation2(void)
112 {
113     unsigned result = 0;
114     symbol x("x"), y("y"), a("a"), b("b");
115     ex e1, e2, e, d;
116     
117     // construct expression e to be diff'ed:
118     e1 = y*pow(x, 2) + a*x + b;
119     e2 = sin(e1);
120     e = b*pow(e2, 2) + y*e2 + a;
121     
122     d = 2*b*e2*cos(e1)*(2*x*y + a) + y*cos(e1)*(2*x*y + a);
123     result += check_diff(e, x, d);
124     
125     d = 2*b*pow(cos(e1),2)*pow(2*x*y + a, 2) + 4*b*y*e2*cos(e1)
126         - 2*b*pow(e2,2)*pow(2*x*y + a, 2) - y*e2*pow(2*x*y + a, 2)
127         + 2*pow(y,2)*cos(e1);
128     result += check_diff(e, x, d, 2);
129     
130     d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
131     result += check_diff(e, y, d);
132
133     d = 2*b*pow(cos(e1),2)*pow(x,4) - 2*b*pow(e2,2)*pow(x,4)
134         + 2*cos(e1)*pow(x,2) - y*e2*pow(x,4);
135     result += check_diff(e, y, d, 2);
136     
137     // construct expression e to be diff'ed:
138     e2 = cos(e1);
139     e = b*pow(e2, 2) + y*e2 + a;
140     
141     d = -2*b*e2*sin(e1)*(2*x*y + a) - y*sin(e1)*(2*x*y + a);
142     result += check_diff(e, x, d);
143     
144     d = 2*b*pow(sin(e1),2)*pow(2*y*x + a,2) - 4*b*e2*sin(e1)*y 
145         - 2*b*pow(e2,2)*pow(2*y*x + a,2) - y*e2*pow(2*y*x + a,2)
146         - 2*pow(y,2)*sin(e1);
147     result += check_diff(e, x, d, 2);
148     
149     d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
150     result += check_diff(e, y, d);
151     
152     d = -2*b*pow(e2,2)*pow(x,4) + 2*b*pow(sin(e1),2)*pow(x,4)
153         - 2*sin(e1)*pow(x,2) - y*e2*pow(x,4);
154     result += check_diff(e, y, d, 2);
155
156         return result;
157 }
158     
159 // exp function
160 static unsigned differentiation3(void)
161 {
162     unsigned result = 0;
163     symbol x("x"), y("y"), a("a"), b("b");
164     ex e1, e2, e, d;
165
166     // construct expression e to be diff'ed:
167     e1 = y*pow(x, 2) + a*x + b;
168     e2 = exp(e1);
169     e = b*pow(e2, 2) + y*e2 + a;
170     
171     d = 2*b*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
172     result += check_diff(e, x, d);
173     
174     d = 4*b*pow(e2,2)*pow(2*y*x + a,2) + 4*b*pow(e2,2)*y
175         + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2);
176     result += check_diff(e, x, d, 2);
177     
178     d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
179     result += check_diff(e, y, d);
180     
181     d = 4*b*pow(e2,2)*pow(x,4) + 2*e2*pow(x,2) + y*e2*pow(x,4);
182     result += check_diff(e, y, d, 2);
183
184         return result;
185 }
186
187 // log functions
188 static unsigned differentiation4(void)
189 {
190     unsigned result = 0;
191     symbol x("x"), y("y"), a("a"), b("b");
192     ex e1, e2, e, d;
193     
194     // construct expression e to be diff'ed:
195     e1 = y*pow(x, 2) + a*x + b;
196     e2 = log(e1);
197     e = b*pow(e2, 2) + y*e2 + a;
198     
199     d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
200     result += check_diff(e, x, d);
201     
202     d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1
203         - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1
204         - y*pow(2*x*y + a,2)*pow(e1,-2);
205     result += check_diff(e, x, d, 2);
206     
207     d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
208     result += check_diff(e, y, d);
209     
210     d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4)
211         + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2);
212     result += check_diff(e, y, d, 2);
213
214         return result;
215 }
216
217 // Functions with two variables
218 static unsigned differentiation5(void)
219 {
220     unsigned result = 0;
221     symbol x("x"), y("y"), a("a"), b("b");
222     ex e1, e2, e, d;
223     
224     // test atan2
225     e1 = y*pow(x, 2) + a*x + b;
226     e2 = x*pow(y, 2) + b*y + a;
227     e = atan2(e1,e2);
228     /*
229     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))
230         +(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)));
231     */
232     /*
233     d = ((a+2*y*x)*pow(y*b+pow(y,2)*x+a,-1)-(a*x+b+y*pow(x,2))*
234          pow(y*b+pow(y,2)*x+a,-2)*pow(y,2))*
235         pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1);
236     */
237     /*
238     d = pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1)
239         *pow(y*b+pow(y,2)*x+a,-1)*(a+2*y*x)
240         +pow(y,2)*(-a*x-b-y*pow(x,2))*
241         pow(pow(y*b+pow(y,2)*x+a,2)+pow(a*x+b+y*pow(x,2),2),-1);
242     */
243     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         (-b-y*pow(x,2)-x*a)+
245         pow(pow(b+y*pow(x,2)+x*a,2)+pow(y*b+pow(y,2)*x+a,2),-1)*
246         (y*b+pow(y,2)*x+a)*(2*y*x+a);
247     result += check_diff(e, x, d);
248     
249     return result;
250 }
251
252 // Series
253 static unsigned differentiation6(void)
254 {
255     symbol x("x");
256     ex e, d, ed;
257     
258     e = sin(x).series(x, exZERO(), 8);
259     d = cos(x).series(x, exZERO(), 7);
260     ed = e.diff(x);
261     ed = static_cast<series *>(ed.bp)->convert_to_poly();
262     d = static_cast<series *>(d.bp)->convert_to_poly();
263     
264     if ((ed - d).compare(exZERO()) != 0) {
265         clog << "derivative of " << e << " by " << x << " returned "
266              << ed << " instead of " << d << ")" << endl;
267         return 1;
268     }
269     return 0;
270 }
271
272 unsigned differentiation(void)
273 {
274     unsigned result = 0;
275     
276     cout << "checking symbolic differentiation..." << flush;
277     clog << "---------symbolic differentiation:" << endl;
278     
279     result += differentiation1();
280     result += differentiation2();
281     result += differentiation3();
282     result += differentiation4();
283     result += differentiation5();
284     result += differentiation6();
285     
286     if (!result) {
287         cout << " passed ";
288         clog << "(no output)" << endl;
289     } else {
290         cout << " failed ";
291     }
292     return result;
293 }