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