1 /** @file exam_powerlaws.cpp
3 * Tests for power laws. You shouldn't try to draw much inspiration from
4 * this code, it is a sanity check rather deeply rooted in GiNaC's classes. */
7 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 static unsigned exam_powerlaws1()
34 ex e1 = power(power(x,a), b);
35 if (!(is_exactly_a<power>(e1) &&
36 is_exactly_a<power>(e1.op(0)) &&
37 is_exactly_a<symbol>(e1.op(0).op(0)) &&
38 is_exactly_a<symbol>(e1.op(0).op(1)) &&
39 is_exactly_a<symbol>(e1.op(1)) &&
40 e1.is_equal(power(power(x,a),b)) )) {
41 clog << "(x^a)^b, x,a,b symbolic wrong" << endl;
42 clog << "returned: " << e1 << endl;
46 ex e2 = e1.subs(a==1);
47 if (!(is_exactly_a<power>(e2) &&
48 is_exactly_a<symbol>(e2.op(0)) &&
49 is_exactly_a<symbol>(e2.op(1)) &&
50 e2.is_equal(power(x,b)) )) {
51 clog << "(x^a)^b, x,b symbolic, a==1 wrong" << endl;
52 clog << "returned: " << e2 << endl;
56 ex e3 = e1.subs(a==-1);
57 if (!(is_exactly_a<power>(e3) &&
58 is_exactly_a<power>(e3.op(0)) &&
59 is_exactly_a<symbol>(e3.op(0).op(0)) &&
60 is_exactly_a<numeric>(e3.op(0).op(1)) &&
61 is_exactly_a<symbol>(e3.op(1)) &&
62 e3.is_equal(power(power(x,-1),b)) )) {
63 clog << "(x^a)^b, x,b symbolic, a==-1 wrong" << endl;
64 clog << "returned: " << e3 << endl;
68 ex e4 = e1.subs(lst(a==-1, b==2.5));
69 if (!(is_exactly_a<power>(e4) &&
70 is_exactly_a<power>(e4.op(0)) &&
71 is_exactly_a<symbol>(e4.op(0).op(0)) &&
72 is_exactly_a<numeric>(e4.op(0).op(1)) &&
73 is_exactly_a<numeric>(e4.op(1)) &&
74 e4.is_equal(power(power(x,-1),2.5)) )) {
75 clog << "(x^a)^b, x symbolic, a==-1, b==2.5 wrong" << endl;
76 clog << "returned: " << e4 << endl;
80 ex e5 = e1.subs(lst(a==-0.9, b==2.5));
81 if (!(is_exactly_a<power>(e5) &&
82 is_exactly_a<symbol>(e5.op(0)) &&
83 is_exactly_a<numeric>(e5.op(1)) &&
84 e5.is_equal(power(x,numeric(-0.9)*numeric(2.5))) )) {
85 clog << "(x^a)^b, x symbolic, a==-0.9, b==2.5 wrong" << endl;
86 clog << "returned: " << e5 << endl;
90 ex e6 = e1.subs(lst(a==numeric(3)+numeric(5.3)*I, b==-5));
91 if (!(is_exactly_a<power>(e6) &&
92 is_exactly_a<symbol>(e6.op(0)) &&
93 is_exactly_a<numeric>(e6.op(1)) &&
94 e6.is_equal(power(x,numeric(-15)+numeric(5.3)*numeric(-5)*I)) )) {
95 clog << "(x^a)^b, x symbolic, a==3+5.3*I, b==-5 wrong" << endl;
96 clog << "returned: " << e6 << endl;
103 static unsigned exam_powerlaws2()
105 // (a*x)^b = a^b * x^b
111 ex e1 = power(a*x,b);
112 if (!(is_exactly_a<power>(e1) &&
113 is_exactly_a<mul>(e1.op(0)) &&
114 (e1.op(0).nops()==2) &&
115 is_exactly_a<symbol>(e1.op(0).op(0)) &&
116 is_exactly_a<symbol>(e1.op(0).op(1)) &&
117 is_exactly_a<symbol>(e1.op(1)) &&
118 e1.is_equal(power(a*x,b)) )) {
119 clog << "(a*x)^b, x,a,b symbolic wrong" << endl;
120 clog << "returned: " << e1 << endl;
124 ex e2 = e1.subs(a==3);
125 if (!(is_exactly_a<power>(e2) &&
126 is_exactly_a<mul>(e2.op(0)) &&
127 (e2.op(0).nops()==2) &&
128 is_exactly_a<symbol>(e2.op(0).op(0)) &&
129 is_exactly_a<numeric>(e2.op(0).op(1)) &&
130 is_exactly_a<symbol>(e2.op(1)) &&
131 e2.is_equal(power(3*x,b)) )) {
132 clog << "(a*x)^b, x,b symbolic, a==3 wrong" << endl;
133 clog << "returned: " << e2 << endl;
137 ex e3 = e1.subs(b==-3);
138 if (!(is_exactly_a<mul>(e3) &&
140 is_exactly_a<power>(e3.op(0)) &&
141 is_exactly_a<power>(e3.op(1)) &&
142 e3.is_equal(power(a,-3)*power(x,-3)) )) {
143 clog << "(a*x)^b, x,a symbolic, b==-3 wrong" << endl;
144 clog << "returned: " << e3 << endl;
148 ex e4 = e1.subs(b==4.5);
149 if (!(is_exactly_a<power>(e4) &&
150 is_exactly_a<mul>(e4.op(0)) &&
151 (e4.op(0).nops()==2) &&
152 is_exactly_a<symbol>(e4.op(0).op(0)) &&
153 is_exactly_a<symbol>(e4.op(0).op(1)) &&
154 is_exactly_a<numeric>(e4.op(1)) &&
155 e4.is_equal(power(a*x,4.5)) )) {
156 clog << "(a*x)^b, x,a symbolic, b==4.5 wrong" << endl;
157 clog << "returned: " << e4 << endl;
161 ex e5 = e1.subs(lst(a==3.2, b==3+numeric(5)*I));
162 if (!(is_exactly_a<mul>(e5) &&
164 is_exactly_a<power>(e5.op(0)) &&
165 is_exactly_a<numeric>(e5.op(1)) &&
166 e5.is_equal(power(x,3+numeric(5)*I)*
167 power(numeric(3.2),3+numeric(5)*I)) )) {
168 clog << "(a*x)^b, x symbolic, a==3.2, b==3+5*I wrong" << endl;
169 clog << "returned: " << e5 << endl;
173 ex e6 = e1.subs(lst(a==-3.2, b==3+numeric(5)*I));
174 if (!(is_exactly_a<mul>(e6) &&
176 is_exactly_a<power>(e6.op(0)) &&
177 is_exactly_a<numeric>(e6.op(1)) &&
178 e6.is_equal(power(-x,3+numeric(5)*I)*
179 power(numeric(3.2),3+numeric(5)*I)) )) {
180 clog << "(a*x)^b, x symbolic, a==-3.2, b==3+5*I wrong" << endl;
181 clog << "returned: " << e6 << endl;
185 ex e7 = e1.subs(lst(a==3+numeric(5)*I, b==3.2));
186 if (!(is_exactly_a<power>(e7) &&
187 is_exactly_a<mul>(e7.op(0)) &&
188 (e7.op(0).nops()==2) &&
189 is_exactly_a<symbol>(e7.op(0).op(0)) &&
190 is_exactly_a<numeric>(e7.op(0).op(1)) &&
191 is_exactly_a<numeric>(e7.op(1)) &&
192 e7.is_equal(power((3+numeric(5)*I)*x,3.2)) )) {
193 clog << "(a*x)^b, x symbolic, a==3+5*I, b==3.2 wrong" << endl;
194 clog << "returned: " << e7 << endl;
201 static unsigned exam_powerlaws3()
203 // numeric evaluation
205 ex e1 = power(numeric(4),numeric(1,2));
207 clog << "4^(1/2) wrongly returned " << e1 << endl;
211 ex e2 = power(numeric(27),numeric(2,3));
213 clog << "27^(2/3) wrongly returned " << e2 << endl;
217 ex e3 = power(numeric(5),numeric(1,2));
218 if (!(is_exactly_a<power>(e3) &&
219 e3.op(0).is_equal(numeric(5)) &&
220 e3.op(1).is_equal(numeric(1,2)))) {
221 clog << "5^(1/2) wrongly returned " << e3 << endl;
225 ex e4 = power(numeric(5),evalf(numeric(1,2)));
226 if (!(is_exactly_a<numeric>(e4))) {
227 clog << "5^(0.5) wrongly returned " << e4 << endl;
231 ex e5 = power(evalf(numeric(5)),numeric(1,2));
232 if (!(is_exactly_a<numeric>(e5))) {
233 clog << "5.0^(1/2) wrongly returned " << e5 << endl;
240 static unsigned exam_powerlaws4()
242 // test for mul::eval()
248 ex f1 = power(a*b,ex(1)/ex(2));
249 ex f2 = power(a*b,ex(3)/ex(2));
258 clog << "(a*b)^(1/2)*(a*b)^(3/2)*c wrongly returned " << e1 << endl;
265 static unsigned exam_powerlaws5()
267 // cabinet of slightly pathological cases
273 clog << "1^a wrongly returned " << e1 << endl;
278 if (!(is_exactly_a<power>(e2))) {
279 clog << "0^a was evaluated to " << e2
280 << " though nothing is known about a." << endl;
287 unsigned exam_powerlaws()
291 cout << "examining power laws" << flush;
292 clog << "----------power laws:" << endl;
294 result += exam_powerlaws1(); cout << '.' << flush;
295 result += exam_powerlaws2(); cout << '.' << flush;
296 result += exam_powerlaws3(); cout << '.' << flush;
297 result += exam_powerlaws4(); cout << '.' << flush;
298 result += exam_powerlaws5(); cout << '.' << flush;
301 cout << " passed " << endl;
302 clog << "(no output)" << endl;
304 cout << " failed " << endl;