1 /** @file differentiation.cpp
3 * Tests for symbolic differentiation, including various functions.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
22 #include <ginac/ginac.h>
24 static unsigned check_diff(const ex &e, const symbol &x,
25 const ex &d, unsigned nth=1)
27 ex ed = e.diff(x, nth);
28 if ((ed - d).compare(exZERO()) != 0) {
44 clog << "derivative of " << e << " by " << x << " returned "
45 << ed << " instead of " << d << endl;
46 clog << "returned:" << endl;
48 clog << endl << "instead of" << endl;
56 // Simple (expanded) polynomials
57 static unsigned differentiation1(void)
60 symbol x("x"), y("y");
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();
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);
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);
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);
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)
103 result += check_diff(e, y, d, 2);
108 // Trigonometric and transcendental functions
109 static unsigned differentiation2(void)
112 symbol x("x"), y("y"), a("a"), b("b");
115 // construct expression e to be diff'ed:
116 e1 = y*pow(x, 2) + a*x + b;
118 e = b*pow(e2, 2) + y*e2 + a;
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);
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);
128 d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
129 result += check_diff(e, y, d);
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);
135 // construct expression e to be diff'ed:
137 e = b*pow(e2, 2) + y*e2 + a;
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);
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);
147 d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
148 result += check_diff(e, y, d);
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);
154 // construct expression e to be diff'ed:
156 e = b*pow(e2, 2) + y*e2 + a;
158 d = 2*b*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
159 result += check_diff(e, x, d);
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);
165 d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
166 result += check_diff(e, y, d);
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);
171 // construct expression e to be diff'ed:
173 e = b*pow(e2, 2) + y*e2 + a;
175 d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
176 result += check_diff(e, x, d);
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);
183 d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
184 result += check_diff(e, y, d);
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);
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;
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)));
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);
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);
213 static unsigned differentiation3(void)
218 e = sin(x).series(x, exZERO(), 8);
219 d = cos(x).series(x, exZERO(), 7);
221 ed = static_cast<series *>(ed.bp)->convert_to_poly();
222 d = static_cast<series *>(d.bp)->convert_to_poly();
224 if ((ed - d).compare(exZERO()) != 0) {
225 clog << "derivative of " << e << " by " << x << " returned "
226 << ed << " instead of " << d << ")" << endl;
232 unsigned differentiation(void)
236 cout << "checking symbolic differentiation..." << flush;
237 clog << "---------symbolic differentiation:" << endl;
239 result += differentiation1();
240 result += differentiation2();
241 result += differentiation3();
245 clog << "(no output)" << endl;