]> www.ginac.de Git - ginac.git/blob - check/powerlaws.cpp
- added on-line help system
[ginac.git] / check / powerlaws.cpp
1 /** @file powerlaws.cpp
2  *
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. */
5
6 /*
7  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <ginac/ginac.h>
25 using namespace GiNaC;
26
27 static unsigned powerlaws1(void)
28 {
29     // (x^a)^b = x^(a*b)
30     
31     symbol x("x");
32     symbol a("a");
33     symbol b("b");
34     
35     ex e1=power(power(x,a),b);
36     if (!(is_ex_exactly_of_type(e1,power) &&
37           is_ex_exactly_of_type(e1.op(0),power) &&
38           is_ex_exactly_of_type(e1.op(0).op(0),symbol) &&
39           is_ex_exactly_of_type(e1.op(0).op(1),symbol) &&
40           is_ex_exactly_of_type(e1.op(1),symbol) &&
41           e1.is_equal(power(power(x,a),b)) )) {
42         clog << "(x^a)^b, x,a,b symbolic wrong" << endl;
43         clog << "returned: " << e1 << endl;
44         return 1;
45     }
46     
47     ex e2=e1.subs(a==1);
48     if (!(is_ex_exactly_of_type(e2,power) &&
49           is_ex_exactly_of_type(e2.op(0),symbol) &&
50           is_ex_exactly_of_type(e2.op(1),symbol) &&
51           e2.is_equal(power(x,b)) )) {
52         clog << "(x^a)^b, x,b symbolic, a==1 wrong" << endl;
53         clog << "returned: " << e2 << endl;
54         return 1;
55     }
56     
57     ex e3=e1.subs(a==-1);
58     if (!(is_ex_exactly_of_type(e3,power) &&
59           is_ex_exactly_of_type(e3.op(0),power) &&
60           is_ex_exactly_of_type(e3.op(0).op(0),symbol) &&
61           is_ex_exactly_of_type(e3.op(0).op(1),numeric) &&
62           is_ex_exactly_of_type(e3.op(1),symbol) &&
63           e3.is_equal(power(power(x,-1),b)) )) {
64         clog << "(x^a)^b, x,b symbolic, a==-1 wrong" << endl;
65         clog << "returned: " << e3 << endl;
66         return 1;
67     }
68     
69     ex e4=e1.subs(lst(a==-1,b==2.5));
70     if (!(is_ex_exactly_of_type(e4,power) &&
71           is_ex_exactly_of_type(e4.op(0),power) &&
72           is_ex_exactly_of_type(e4.op(0).op(0),symbol) &&
73           is_ex_exactly_of_type(e4.op(0).op(1),numeric) &&
74           is_ex_exactly_of_type(e4.op(1),numeric) &&
75           e4.is_equal(power(power(x,-1),2.5)) )) {
76         clog << "(x^a)^b, x symbolic, a==-1, b==2.5 wrong" << endl;
77         clog << "returned: " << e4 << endl;
78         return 1;
79     }
80     
81     ex e5=e1.subs(lst(a==-0.9,b==2.5));
82     if (!(is_ex_exactly_of_type(e5,power) &&
83           is_ex_exactly_of_type(e5.op(0),symbol) &&
84           is_ex_exactly_of_type(e5.op(1),numeric) &&
85           e5.is_equal(power(x,numeric(-0.9)*numeric(2.5))) )) {
86         clog << "(x^a)^b, x symbolic, a==-0.9, b==2.5 wrong" << endl;
87         clog << "returned: " << e5 << endl;
88         return 1;
89     }
90     
91     ex e6=e1.subs(lst(a==numeric(3)+numeric(5.3)*I,b==-5));
92     if (!(is_ex_exactly_of_type(e6,power) &&
93           is_ex_exactly_of_type(e6.op(0),symbol) &&
94           is_ex_exactly_of_type(e6.op(1),numeric) &&
95           e6.is_equal(power(x,numeric(-15)+numeric(5.3)*numeric(-5)*I)) )) {
96         clog << "(x^a)^b, x symbolic, a==3+5.3*I, b==-5 wrong" << endl;
97         clog << "returned: " << e6 << endl;
98         return 1;
99     }
100     return 0;
101 }
102
103 static unsigned powerlaws2(void)
104 {
105     // (a*x)^b = a^b * x^b
106     
107     symbol x("x");
108     symbol a("a");
109     symbol b("b");
110     
111     ex e1=power(a*x,b);
112     if (!(is_ex_exactly_of_type(e1,power) &&
113           is_ex_exactly_of_type(e1.op(0),mul) &&
114           (e1.op(0).nops()==2) &&
115           is_ex_exactly_of_type(e1.op(0).op(0),symbol) &&
116           is_ex_exactly_of_type(e1.op(0).op(1),symbol) &&
117           is_ex_exactly_of_type(e1.op(1),symbol) &&
118           e1.is_equal(power(a*x,b)) )) {
119         clog << "(a*x)^b, x,a,b symbolic wrong" << endl;
120         clog << "returned: " << e1 << endl;
121         return 1;
122     }
123     
124     ex e2=e1.subs(a==3);
125     if (!(is_ex_exactly_of_type(e2,power) &&
126           is_ex_exactly_of_type(e2.op(0),mul) &&
127           (e2.op(0).nops()==2) &&
128           is_ex_exactly_of_type(e2.op(0).op(0),symbol) &&
129           is_ex_exactly_of_type(e2.op(0).op(1),numeric) &&
130           is_ex_exactly_of_type(e2.op(1),symbol) &&
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;
134         return 1;
135     }
136     
137     ex e3=e1.subs(b==-3);
138     if (!(is_ex_exactly_of_type(e3,mul) &&
139           (e3.nops()==2) &&
140           is_ex_exactly_of_type(e3.op(0),power) &&
141           is_ex_exactly_of_type(e3.op(1),power) &&
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;
145         return 1;
146     }
147     
148     ex e4=e1.subs(b==4.5);
149     if (!(is_ex_exactly_of_type(e4,power) &&
150           is_ex_exactly_of_type(e4.op(0),mul) &&
151           (e4.op(0).nops()==2) &&
152           is_ex_exactly_of_type(e4.op(0).op(0),symbol) &&
153           is_ex_exactly_of_type(e4.op(0).op(1),symbol) &&
154           is_ex_exactly_of_type(e4.op(1),numeric) &&
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;
158         return 1;
159     }
160     
161     ex e5=e1.subs(lst(a==3.2,b==3+numeric(5)*I));
162     if (!(is_ex_exactly_of_type(e5,mul) &&
163           (e5.nops()==2) &&
164           is_ex_exactly_of_type(e5.op(0),power) &&
165           is_ex_exactly_of_type(e5.op(1),numeric) &&
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;
170         return 1;
171     }
172     
173     ex e6=e1.subs(lst(a==-3.2,b==3+numeric(5)*I));
174     if (!(is_ex_exactly_of_type(e6,mul) &&
175           (e6.nops()==2) &&
176           is_ex_exactly_of_type(e6.op(0),power) &&
177           is_ex_exactly_of_type(e6.op(1),numeric) &&
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;
182         return 1;
183     }
184     
185     ex e7=e1.subs(lst(a==3+numeric(5)*I,b==3.2));
186     if (!(is_ex_exactly_of_type(e7,power) &&
187           is_ex_exactly_of_type(e7.op(0),mul) &&
188           (e7.op(0).nops()==2) &&
189           is_ex_exactly_of_type(e7.op(0).op(0),symbol) &&
190           is_ex_exactly_of_type(e7.op(0).op(1),numeric) &&
191           is_ex_exactly_of_type(e7.op(1),numeric) &&
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;
195         return 1;
196     }
197     
198     return 0;
199 }
200
201 static unsigned powerlaws3(void)
202 {
203     // numeric evaluation
204
205     ex e1=power(numeric(4),numeric(1)/numeric(2));
206     if (e1 != 2) {
207         clog << "4^(1/2) wrongly returned " << e1 << endl;
208         return 1;
209     }
210     
211     ex e2=power(numeric(27),numeric(2)/numeric(3));
212     if (e2 != 9) {
213         clog << "27^(2/3) wrongly returned " << e2 << endl;
214         return 1;
215     }
216
217     ex e3=power(numeric(5),numeric(1)/numeric(2));
218     if (!(is_ex_exactly_of_type(e3,power) &&
219           e3.op(0).is_equal(numeric(5)) &&
220           e3.op(1).is_equal(numeric(1)/numeric(2)) )) {
221         clog << "5^(1/2) wrongly returned " << e3 << endl;
222         return 1;
223     }
224     
225     ex e4=power(numeric(5),evalf(numeric(1)/numeric(2)));
226     if (!(is_ex_exactly_of_type(e4,numeric))) {
227         clog << "5^(0.5) wrongly returned " << e4 << endl;
228         return 1;
229     }
230     
231     ex e5=power(evalf(numeric(5)),numeric(1)/numeric(2));
232     if (!(is_ex_exactly_of_type(e5,numeric))) {
233         clog << "5.0^(1/2) wrongly returned " << e5 << endl;
234         return 1;
235     }
236     
237     return 0;
238 }
239
240 static unsigned powerlaws4(void)
241 {
242     // test for mul::eval()
243
244     symbol a("a");
245     symbol b("b");
246     symbol c("c");
247     
248     ex f1=power(a*b,ex(1)/ex(2));
249     ex f2=power(a*b,ex(3)/ex(2));
250     ex f3=c;
251
252     exvector v;
253     v.push_back(f1);
254     v.push_back(f2);
255     v.push_back(f3);
256     ex e1=mul(v);
257     if (e1!=a*a*b*b*c) {
258         clog << "(a*b)^(1/2)*(a*b)^(3/2)*c wrongly returned " << e1 << endl;
259         return 1;
260     }
261     return 0;
262 }
263
264 unsigned powerlaws(void)
265 {
266     unsigned result = 0;
267     
268     cout << "checking power laws..." << flush;
269     clog << "---------power laws:" << endl;
270     
271     result += powerlaws1();
272     result += powerlaws2();
273     result += powerlaws3();
274     result += powerlaws4();
275     
276     if (!result) {
277         cout << " passed ";
278         clog << "(no output)" << endl;
279     } else {
280         cout << " failed ";
281     }
282     return result;
283 }