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