1 /** @file differentiation.cpp
3 * Tests for symbolic differentiation, including various functions. */
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #include <ginac/ginac.h>
24 using namespace GiNaC;
26 static unsigned check_diff(const ex &e, const symbol &x,
27 const ex &d, unsigned nth=1)
29 ex ed = e.diff(x, nth);
30 if ((ed - d).compare(exZERO()) != 0) {
46 clog << "derivative of " << e << " by " << x << " returned "
47 << ed << " instead of " << d << endl;
48 clog << "returned:" << endl;
50 clog << endl << "instead of" << endl;
58 // Simple (expanded) polynomials
59 static unsigned differentiation1(void)
62 symbol x("x"), y("y");
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();
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);
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);
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);
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)
105 result += check_diff(e, y, d, 2);
110 // Trigonometric and transcendental functions
111 static unsigned differentiation2(void)
114 symbol x("x"), y("y"), a("a"), b("b");
117 // construct expression e to be diff'ed:
118 e1 = y*pow(x, 2) + a*x + b;
120 e = b*pow(e2, 2) + y*e2 + a;
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);
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);
130 d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
131 result += check_diff(e, y, d);
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);
137 // construct expression e to be diff'ed:
139 e = b*pow(e2, 2) + y*e2 + a;
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);
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);
149 d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
150 result += check_diff(e, y, d);
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);
156 // construct expression e to be diff'ed:
158 e = b*pow(e2, 2) + y*e2 + a;
160 d = 2*b*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
161 result += check_diff(e, x, d);
163 d = 4*b*pow(e2,2)*pow(2*y*x + a,2) + 4*b*pow(e2,2)*y
164 + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2);
165 result += check_diff(e, x, d, 2);
167 d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
168 result += check_diff(e, y, d);
170 d = 4*b*pow(e2,2)*pow(x,4) + 2*e2*pow(x,2) + y*e2*pow(x,4);
171 result += check_diff(e, y, d, 2);
173 // construct expression e to be diff'ed:
175 e = b*pow(e2, 2) + y*e2 + a;
177 d = 2*b*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
178 result += check_diff(e, x, d);
180 d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1
181 - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1
182 - y*pow(2*x*y + a,2)*pow(e1,-2);
183 result += check_diff(e, x, d, 2);
185 d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
186 result += check_diff(e, y, d);
188 d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4)
189 + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2);
190 result += check_diff(e, y, d, 2);
192 // test for functions with two variables: atan2
193 e1 = y*pow(x, 2) + a*x + b;
194 e2 = x*pow(y, 2) + b*y + a;
197 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))
198 +(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)));
201 d = ((a+2*y*x)*pow(y*b+pow(y,2)*x+a,-1)-(a*x+b+y*pow(x,2))*
202 pow(y*b+pow(y,2)*x+a,-2)*pow(y,2))*
203 pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1);
205 d = pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1)
206 *pow(y*b+pow(y,2)*x+a,-1)*(a+2*y*x)
207 +pow(y,2)*(-a*x-b-y*pow(x,2))*
208 pow(pow(y*b+pow(y,2)*x+a,2)+pow(a*x+b+y*pow(x,2),2),-1);
209 result += check_diff(e, x, d);
215 static unsigned differentiation3(void)
220 e = sin(x).series(x, exZERO(), 8);
221 d = cos(x).series(x, exZERO(), 7);
223 ed = static_cast<series *>(ed.bp)->convert_to_poly();
224 d = static_cast<series *>(d.bp)->convert_to_poly();
226 if ((ed - d).compare(exZERO()) != 0) {
227 clog << "derivative of " << e << " by " << x << " returned "
228 << ed << " instead of " << d << ")" << endl;
234 unsigned differentiation(void)
238 cout << "checking symbolic differentiation..." << flush;
239 clog << "---------symbolic differentiation:" << endl;
241 result += differentiation1();
242 result += differentiation2();
243 result += differentiation3();
247 clog << "(no output)" << endl;