]> www.ginac.de Git - ginac.git/blob - check/exam_real_imag.cpp
Merge branch 'c++11' in preparation for version 1.7.0.
[ginac.git] / check / exam_real_imag.cpp
1 /** @file exam_misc.cpp
2  *
3  */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2016 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "ginac.h"
24 using namespace GiNaC;
25
26 #include <iostream>
27 using namespace std;
28
29 /* Exam real/imaginary part of polynomials. */
30 static unsigned exam_polynomials()
31 {
32         realsymbol a("a"), b("b");
33         ex e = pow(a + I*b,3).expand() + I;
34
35         if (e.real_part() != pow(a,3)-3*a*pow(b,2) ||
36             e.imag_part() != 1+3*pow(a,2)*b-pow(b,3)) {
37                 clog << "real / imaginary part miscomputed" << endl;
38                 return 1;
39         }
40         return 0;
41 }
42
43 /* Exam symbolic expansion of nested expression. */
44 static unsigned exam_monster()
45 {
46         // This little monster is inspired by Sage's Symbench R1.
47         // It is much more aggressive that the original and covers more code.
48         struct {  // C++ doesn't have nested functions...
49                 ex operator()(const ex & z) {
50                         return sqrt(ex(1)/3) * pow(z, 11) - I / pow(2*z, 3);
51                 }
52         } f;
53         ex monster = f(f(f(f(I/2))));  // grows exponentially with number of nestings..
54         ex r = real_part(monster);
55         ex i = imag_part(monster);
56
57         // Check with precomputed result
58         double r_eps = ex_to<numeric>(evalf(r)).to_double() - 0.2000570104163233;
59         double i_eps = ex_to<numeric>(evalf(i)).to_double() - 0.5284320312415462;
60         if (abs(r_eps) > 1e-9  ||  abs(i_eps) > 1e-9) {
61                 clog << "iterated function was miscomputed" << endl;
62                 return 1;
63         }
64         return 0;
65 }
66
67 unsigned exam_real_imag()
68 {
69         unsigned result = 0;
70
71         cout << "examining real/imaginary part separation" << flush;
72
73         result += exam_polynomials(); cout << '.' << flush;
74         result += exam_monster(); cout << '.' << flush;
75
76         return result;
77 }
78
79 int main(int argc, char** argv)
80 {
81         return exam_real_imag();
82 }