1 // check/differentiation.cpp
3 /* Tests for symbolic differentiation, including various functions. */
5 #include <ginac/ginac.h>
7 static unsigned check_diff(const ex &e, const symbol &x,
8 const ex &d, unsigned nth=1)
10 ex ed = e.diff(x, nth);
11 if ((ed - d).compare(exZERO()) != 0) {
27 clog << "derivative of " << e << " by " << x << " returned "
28 << ed << " instead of " << d << endl;
29 clog << "returned:" << endl;
31 clog << endl << "instead of" << endl;
39 // Simple (expanded) polynomials
40 static unsigned differentiation1(void)
43 symbol x("x"), y("y");
46 // construct bivariate polynomial e to be diff'ed:
47 e1 = pow(x, -2) * 3 + pow(x, -1) * 5 + 7 + x * 11 + pow(x, 2) * 13;
48 e2 = pow(y, -2) * 5 + pow(y, -1) * 7 + 11 + y * 13 + pow(y, 2) * 17;
49 e = (e1 * e2).expand();
52 d = 121 - 55*pow(x,-2) - 66*pow(x,-3) - 30*pow(x,-3)*pow(y,-2)
53 - 42*pow(x,-3)*pow(y,-1) - 78*pow(x,-3)*y
54 - 102*pow(x,-3)*pow(y,2) - 25*pow(x,-2) * pow(y,-2)
55 - 35*pow(x,-2)*pow(y,-1) - 65*pow(x,-2)*y
56 - 85*pow(x,-2)*pow(y,2) + 77*pow(y,-1) + 143*y + 187*pow(y,2)
57 + 130*x*pow(y,-2) + 182*pow(y,-1)*x + 338*x*y + 442*x*pow(y,2)
58 + 55*pow(y,-2) + 286*x;
59 result += check_diff(e, x, d);
62 d = 91 - 30*pow(x,-2)*pow(y,-3) - 21*pow(x,-2)*pow(y,-2)
63 + 39*pow(x,-2) + 102*pow(x,-2)*y - 50*pow(x,-1)*pow(y,-3)
64 - 35*pow(x,-1)*pow(y,-2) + 65*pow(x,-1) + 170*pow(x,-1)*y
65 - 77*pow(y,-2)*x + 143*x + 374*x*y - 130*pow(y,-3)*pow(x,2)
66 - 91*pow(y,-2)*pow(x,2) + 169*pow(x,2) + 442*pow(x,2)*y
67 - 110*pow(y,-3)*x - 70*pow(y,-3) + 238*y - 49*pow(y,-2);
68 result += check_diff(e, y, d);
71 d = 286 + 90*pow(x,-4)*pow(y,-2) + 126*pow(x,-4)*pow(y,-1)
72 + 234*pow(x,-4)*y + 306*pow(x,-4)*pow(y,2)
73 + 50*pow(x,-3)*pow(y,-2) + 70*pow(x,-3)*pow(y,-1)
74 + 130*pow(x,-3)*y + 170*pow(x,-3)*pow(y,2)
75 + 130*pow(y,-2) + 182*pow(y,-1) + 338*y + 442*pow(y,2)
76 + 198*pow(x,-4) + 110*pow(x,-3);
77 result += check_diff(e, x, d, 2);
80 d = 238 + 90*pow(x,-2)*pow(y,-4) + 42*pow(x,-2)*pow(y,-3)
81 + 102*pow(x,-2) + 150*pow(x,-1)*pow(y,-4)
82 + 70*pow(x,-1)*pow(y,-3) + 170*pow(x,-1) + 330*x*pow(y,-4)
83 + 154*x*pow(y,-3) + 374*x + 390*pow(x,2)*pow(y,-4)
84 + 182*pow(x,2)*pow(y,-3) + 442*pow(x,2) + 210*pow(y,-4)
86 result += check_diff(e, y, d, 2);
91 // Trigonometric and transcendental functions
92 static unsigned differentiation2(void)
95 symbol x("x"), y("y"), a("a"), b("b");
98 // construct expression e to be diff'ed:
99 e1 = y*pow(x, 2) + a*x + b;
101 e = b*pow(e2, 2) + y*e2 + a;
103 d = 2*b*e2*cos(e1)*(2*x*y + a) + y*cos(e1)*(2*x*y + a);
104 result += check_diff(e, x, d);
106 d = 2*b*pow(cos(e1),2)*pow(2*x*y + a, 2) + 4*b*y*e2*cos(e1)
107 - 2*b*pow(e2,2)*pow(2*x*y + a, 2) - y*e2*pow(2*x*y + a, 2)
108 + 2*pow(y,2)*cos(e1);
109 result += check_diff(e, x, d, 2);
111 d = 2*b*e2*cos(e1)*pow(x, 2) + e2 + y*cos(e1)*pow(x, 2);
112 result += check_diff(e, y, d);
114 d = 2*b*pow(cos(e1),2)*pow(x,4) - 2*b*pow(e2,2)*pow(x,4)
115 + 2*cos(e1)*pow(x,2) - y*e2*pow(x,4);
116 result += check_diff(e, y, d, 2);
118 // construct expression e to be diff'ed:
120 e = b*pow(e2, 2) + y*e2 + a;
122 d = -2*b*e2*sin(e1)*(2*x*y + a) - y*sin(e1)*(2*x*y + a);
123 result += check_diff(e, x, d);
125 d = 2*b*pow(sin(e1),2)*pow(2*y*x + a,2) - 4*b*e2*sin(e1)*y
126 - 2*b*pow(e2,2)*pow(2*y*x + a,2) - y*e2*pow(2*y*x + a,2)
127 - 2*pow(y,2)*sin(e1);
128 result += check_diff(e, x, d, 2);
130 d = -2*b*e2*sin(e1)*pow(x,2) + e2 - y*sin(e1)*pow(x, 2);
131 result += check_diff(e, y, d);
133 d = -2*b*pow(e2,2)*pow(x,4) + 2*b*pow(sin(e1),2)*pow(x,4)
134 - 2*sin(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*pow(e2, 2)*(2*x*y + a) + y*e2*(2*x*y + a);
142 result += check_diff(e, x, d);
144 d = 4*b*pow(e2,2)*pow(2*y*x + a,2) + 4*b*pow(e2,2)*y
145 + 2*pow(y,2)*e2 + y*e2*pow(2*y*x + a,2);
146 result += check_diff(e, x, d, 2);
148 d = 2*b*pow(e2,2)*pow(x,2) + e2 + y*e2*pow(x,2);
149 result += check_diff(e, y, d);
151 d = 4*b*pow(e2,2)*pow(x,4) + 2*e2*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*e2*(2*x*y + a)/e1 + y*(2*x*y + a)/e1;
159 result += check_diff(e, x, d);
161 d = 2*b*pow((2*x*y + a),2)*pow(e1,-2) + 4*b*y*e2/e1
162 - 2*b*e2*pow(2*x*y + a,2)*pow(e1,-2) + 2*pow(y,2)/e1
163 - y*pow(2*x*y + a,2)*pow(e1,-2);
164 result += check_diff(e, x, d, 2);
166 d = 2*b*e2*pow(x,2)/e1 + e2 + y*pow(x,2)/e1;
167 result += check_diff(e, y, d);
169 d = 2*b*pow(x,4)*pow(e1,-2) - 2*b*e2*pow(e1,-2)*pow(x,4)
170 + 2*pow(x,2)/e1 - y*pow(x,4)*pow(e1,-2);
171 result += check_diff(e, y, d, 2);
173 // test for functions with two variables: atan2
174 e1 = y*pow(x, 2) + a*x + b;
175 e2 = x*pow(y, 2) + b*y + a;
178 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))
179 +(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)));
182 d = ((a+2*y*x)*pow(y*b+pow(y,2)*x+a,-1)-(a*x+b+y*pow(x,2))*
183 pow(y*b+pow(y,2)*x+a,-2)*pow(y,2))*
184 pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1);
186 d = pow(1+pow(a*x+b+y*pow(x,2),2)*pow(y*b+pow(y,2)*x+a,-2),-1)
187 *pow(y*b+pow(y,2)*x+a,-1)*(a+2*y*x)
188 +pow(y,2)*(-a*x-b-y*pow(x,2))*
189 pow(pow(y*b+pow(y,2)*x+a,2)+pow(a*x+b+y*pow(x,2),2),-1);
190 result += check_diff(e, x, d);
196 static unsigned differentiation3(void)
201 e = sin(x).series(x, exZERO(), 8);
202 d = cos(x).series(x, exZERO(), 7);
204 ed = static_cast<series *>(ed.bp)->convert_to_poly();
205 d = static_cast<series *>(d.bp)->convert_to_poly();
207 if ((ed - d).compare(exZERO()) != 0) {
208 clog << "derivative of " << e << " by " << x << " returned "
209 << ed << " instead of " << d << ")" << endl;
215 unsigned differentiation(void)
219 cout << "checking symbolic differentiation..." << flush;
220 clog << "---------symbolic differentiation:" << endl;
222 result += differentiation1();
223 result += differentiation2();
224 result += differentiation3();
228 clog << "(no output)" << endl;