]> www.ginac.de Git - ginac.git/blob - check/differentiation.cpp
- split into smaller functions to make it compile faster
[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     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 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     e2 = log(e1);
195     e = b*pow(e2, 2) + y*e2 + a;
196     
197     d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
198     result += check_diff(e, x, d);
199     
200     d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1
201         - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1
202         - y*pow(2*x*y + a,2)*pow(e1,-2);
203     result += check_diff(e, x, d, 2);
204     
205     d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
206     result += check_diff(e, y, d);
207     
208     d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4)
209         + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2);
210     result += check_diff(e, y, d, 2);
211
212         return result;
213 }
214
215 // Functions with two variables
216 static unsigned differentiation5(void)
217 {
218     unsigned result = 0;
219     symbol x("x"), y("y"), a("a"), b("b");
220     ex e1, e2, e, d;
221     
222     // test atan2
223     e1 = y*pow(x, 2) + a*x + b;
224     e2 = x*pow(y, 2) + b*y + a;
225     e = atan2(e1,e2);
226     /*
227     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))
228         +(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)));
229     */
230     /*
231     d = ((a+2*y*x)*pow(y*b+pow(y,2)*x+a,-1)-(a*x+b+y*pow(x,2))*
232          pow(y*b+pow(y,2)*x+a,-2)*pow(y,2))*
233         pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1);
234     */
235     d = pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1)
236         *pow(y*b+pow(y,2)*x+a,-1)*(a+2*y*x)
237         +pow(y,2)*(-a*x-b-y*pow(x,2))*
238         pow(pow(y*b+pow(y,2)*x+a,2)+pow(a*x+b+y*pow(x,2),2),-1);
239     result += check_diff(e, x, d);
240     
241     return result;
242 }
243
244 // Series
245 static unsigned differentiation6(void)
246 {
247     symbol x("x");
248     ex e, d, ed;
249     
250     e = sin(x).series(x, exZERO(), 8);
251     d = cos(x).series(x, exZERO(), 7);
252     ed = e.diff(x);
253     ed = static_cast<series *>(ed.bp)->convert_to_poly();
254     d = static_cast<series *>(d.bp)->convert_to_poly();
255     
256     if ((ed - d).compare(exZERO()) != 0) {
257         clog << "derivative of " << e << " by " << x << " returned "
258              << ed << " instead of " << d << ")" << endl;
259         return 1;
260     }
261     return 0;
262 }
263
264 unsigned differentiation(void)
265 {
266     unsigned result = 0;
267     
268     cout << "checking symbolic differentiation..." << flush;
269     clog << "---------symbolic differentiation:" << endl;
270     
271     result += differentiation1();
272     result += differentiation2();
273     result += differentiation3();
274     result += differentiation4();
275     result += differentiation5();
276     result += differentiation6();
277     
278     if (!result) {
279         cout << " passed ";
280         clog << "(no output)" << endl;
281     } else {
282         cout << " failed ";
283     }
284     return result;
285 }