]> www.ginac.de Git - ginac.git/blob - check/differentiation.cpp
- account for new GNU-automake built-scheme
[ginac.git] / check / differentiation.cpp
1 /** @file differentiation.cpp
2  *
3  *  Tests for symbolic differentiation, including various functions.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <ginac/ginac.h>
23
24 static unsigned check_diff(const ex &e, const symbol &x,
25                            const ex &d, unsigned nth=1)
26 {
27     ex ed = e.diff(x, nth);
28     if ((ed - d).compare(exZERO()) != 0) {
29         switch (nth) {
30         case 0:
31             clog << "zeroth ";
32             break;
33         case 1:
34             break;
35         case 2:
36             clog << "second ";
37             break;
38         case 3:
39             clog << "third ";
40             break;
41         default:
42             clog << nth << "th ";
43         }
44         clog << "derivative of " << e << " by " << x << " returned "
45              << ed << " instead of " << d << endl;
46         clog << "returned:" << endl;
47         ed.printtree(clog);
48         clog << endl << "instead of" << endl;
49         d.printtree(clog);
50
51         return 1;
52     }
53     return 0;
54 }
55
56 // Simple (expanded) polynomials
57 static unsigned differentiation1(void)
58 {
59     unsigned result = 0;
60     symbol x("x"), y("y");
61     ex e1, e2, e, d;
62     
63     // construct bivariate polynomial e to be diff'ed:
64     e1 = pow(x, -2) * 3 + pow(x, -1) * 5 + 7 + x * 11 + pow(x, 2) * 13;
65     e2 = pow(y, -2) * 5 + pow(y, -1) * 7 + 11 + y * 13 + pow(y, 2) * 17;
66     e = (e1 * e2).expand();
67     
68     // d e / dx:
69     d = 121 - 55*pow(x,-2) - 66*pow(x,-3) - 30*pow(x,-3)*pow(y,-2)
70         - 42*pow(x,-3)*pow(y,-1) - 78*pow(x,-3)*y
71         - 102*pow(x,-3)*pow(y,2) - 25*pow(x,-2) * pow(y,-2)
72         - 35*pow(x,-2)*pow(y,-1) - 65*pow(x,-2)*y
73         - 85*pow(x,-2)*pow(y,2) + 77*pow(y,-1) + 143*y + 187*pow(y,2)
74         + 130*x*pow(y,-2) + 182*pow(y,-1)*x + 338*x*y + 442*x*pow(y,2)
75         + 55*pow(y,-2) + 286*x;
76     result += check_diff(e, x, d);
77     
78     // d e / dy:
79     d = 91 - 30*pow(x,-2)*pow(y,-3) - 21*pow(x,-2)*pow(y,-2)
80         + 39*pow(x,-2) + 102*pow(x,-2)*y - 50*pow(x,-1)*pow(y,-3)
81         - 35*pow(x,-1)*pow(y,-2) + 65*pow(x,-1) + 170*pow(x,-1)*y
82         - 77*pow(y,-2)*x + 143*x + 374*x*y - 130*pow(y,-3)*pow(x,2)
83         - 91*pow(y,-2)*pow(x,2) + 169*pow(x,2) + 442*pow(x,2)*y
84         - 110*pow(y,-3)*x - 70*pow(y,-3) + 238*y - 49*pow(y,-2);
85     result += check_diff(e, y, d);
86     
87     // d^2 e / dx^2:
88     d = 286 + 90*pow(x,-4)*pow(y,-2) + 126*pow(x,-4)*pow(y,-1)
89         + 234*pow(x,-4)*y + 306*pow(x,-4)*pow(y,2)
90         + 50*pow(x,-3)*pow(y,-2) + 70*pow(x,-3)*pow(y,-1)
91         + 130*pow(x,-3)*y + 170*pow(x,-3)*pow(y,2)
92         + 130*pow(y,-2) + 182*pow(y,-1) + 338*y + 442*pow(y,2)
93         + 198*pow(x,-4) + 110*pow(x,-3);
94     result += check_diff(e, x, d, 2);
95     
96     // d^2 e / dy^2:
97     d = 238 + 90*pow(x,-2)*pow(y,-4) + 42*pow(x,-2)*pow(y,-3)
98         + 102*pow(x,-2) + 150*pow(x,-1)*pow(y,-4)
99         + 70*pow(x,-1)*pow(y,-3) + 170*pow(x,-1) + 330*x*pow(y,-4)
100         + 154*x*pow(y,-3) + 374*x + 390*pow(x,2)*pow(y,-4)
101         + 182*pow(x,2)*pow(y,-3) + 442*pow(x,2) + 210*pow(y,-4)
102         + 98*pow(y,-3);
103     result += check_diff(e, y, d, 2);
104     
105     return result;
106 }
107
108 // Trigonometric and transcendental functions
109 static unsigned differentiation2(void)
110 {
111     unsigned result = 0;
112     symbol x("x"), y("y"), a("a"), b("b");
113     ex e1, e2, e, d;
114     
115     // construct expression e to be diff'ed:
116     e1 = y*pow(x, 2) + a*x + b;
117     e2 = sin(e1);
118     e = b*pow(e2, 2) + y*e2 + a;
119     
120     d = 2*b*e2*cos(e1)*(2*x*y + a) + y*cos(e1)*(2*x*y + a);
121     result += check_diff(e, x, d);
122     
123     d = 2*b*pow(cos(e1),2)*pow(2*x*y + a, 2) + 4*b*y*e2*cos(e1)
124         - 2*b*pow(e2,2)*pow(2*x*y + a, 2) - y*e2*pow(2*x*y + a, 2)
125         + 2*pow(y,2)*cos(e1);
126     result += check_diff(e, x, d, 2);
127     
128     d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
129     result += check_diff(e, y, d);
130
131     d = 2*b*pow(cos(e1),2)*pow(x,4) - 2*b*pow(e2,2)*pow(x,4)
132         + 2*cos(e1)*pow(x,2) - y*e2*pow(x,4);
133     result += check_diff(e, y, d, 2);
134     
135     // construct expression e to be diff'ed:
136     e2 = cos(e1);
137     e = b*pow(e2, 2) + y*e2 + a;
138     
139     d = -2*b*e2*sin(e1)*(2*x*y + a) - y*sin(e1)*(2*x*y + a);
140     result += check_diff(e, x, d);
141     
142     d = 2*b*pow(sin(e1),2)*pow(2*y*x + a,2) - 4*b*e2*sin(e1)*y 
143         - 2*b*pow(e2,2)*pow(2*y*x + a,2) - y*e2*pow(2*y*x + a,2)
144         - 2*pow(y,2)*sin(e1);
145     result += check_diff(e, x, d, 2);
146     
147     d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
148     result += check_diff(e, y, d);
149     
150     d = -2*b*pow(e2,2)*pow(x,4) + 2*b*pow(sin(e1),2)*pow(x,4)
151         - 2*sin(e1)*pow(x,2) - y*e2*pow(x,4);
152     result += check_diff(e, y, d, 2);
153     
154     // construct expression e to be diff'ed:
155     e2 = exp(e1);
156     e = b*pow(e2, 2) + y*e2 + a;
157     
158     d = 2*b*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
159     result += check_diff(e, x, d);
160     
161     d = 4*b*pow(e2,2)*pow(2*y*x + a,2) + 4*b*pow(e2,2)*y
162         + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2);
163     result += check_diff(e, x, d, 2);
164     
165     d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
166     result += check_diff(e, y, d);
167     
168     d = 4*b*pow(e2,2)*pow(x,4) + 2*e2*pow(x,2) + y*e2*pow(x,4);
169     result += check_diff(e, y, d, 2);
170     
171     // construct expression e to be diff'ed:
172     e2 = log(e1);
173     e = b*pow(e2, 2) + y*e2 + a;
174     
175     d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
176     result += check_diff(e, x, d);
177     
178     d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1
179         - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1
180         - y*pow(2*x*y + a,2)*pow(e1,-2);
181     result += check_diff(e, x, d, 2);
182     
183     d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
184     result += check_diff(e, y, d);
185     
186     d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4)
187         + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2);
188     result += check_diff(e, y, d, 2);
189     
190     // test for functions with two variables: atan2
191     e1 = y*pow(x, 2) + a*x + b;
192     e2 = x*pow(y, 2) + b*y + a;
193     e = atan2(e1,e2);
194     /*
195     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))
196         +(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)));
197     */
198     /*
199     d = ((a+2*y*x)*pow(y*b+pow(y,2)*x+a,-1)-(a*x+b+y*pow(x,2))*
200          pow(y*b+pow(y,2)*x+a,-2)*pow(y,2))*
201         pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1);
202     */
203     d = pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1)
204         *pow(y*b+pow(y,2)*x+a,-1)*(a+2*y*x)
205         +pow(y,2)*(-a*x-b-y*pow(x,2))*
206         pow(pow(y*b+pow(y,2)*x+a,2)+pow(a*x+b+y*pow(x,2),2),-1);
207     result += check_diff(e, x, d);
208     
209     return result;
210 }
211
212 // Series
213 static unsigned differentiation3(void)
214 {
215     symbol x("x");
216     ex e, d, ed;
217     
218     e = sin(x).series(x, exZERO(), 8);
219     d = cos(x).series(x, exZERO(), 7);
220     ed = e.diff(x);
221     ed = static_cast<series *>(ed.bp)->convert_to_poly();
222     d = static_cast<series *>(d.bp)->convert_to_poly();
223     
224     if ((ed - d).compare(exZERO()) != 0) {
225         clog << "derivative of " << e << " by " << x << " returned "
226              << ed << " instead of " << d << ")" << endl;
227         return 1;
228     }
229     return 0;
230 }
231
232 unsigned differentiation(void)
233 {
234     unsigned result = 0;
235     
236     cout << "checking symbolic differentiation..." << flush;
237     clog << "---------symbolic differentiation:" << endl;
238     
239     result += differentiation1();
240     result += differentiation2();
241     result += differentiation3();
242     
243     if (!result) {
244         cout << " passed ";
245         clog << "(no output)" << endl;
246     } else {
247         cout << " failed ";
248     }
249     return result;
250 }