1 /** @file exam_misc.cpp
6 * GiNaC Copyright (C) 1999-2021 Johannes Gutenberg University Mainz, Germany
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.
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.
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
24 using namespace GiNaC;
29 /* Exam real/imaginary part of polynomials. */
30 static unsigned exam_polynomials()
32 realsymbol a("a"), b("b");
33 ex e = pow(a + I*b,3).expand() + I;
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;
43 /* Exam symbolic expansion of nested expression. */
44 static unsigned exam_monster()
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);
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);
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;
67 unsigned exam_real_imag()
71 cout << "examining real/imaginary part separation" << flush;
73 result += exam_polynomials(); cout << '.' << flush;
74 result += exam_monster(); cout << '.' << flush;
79 int main(int argc, char** argv)
81 return exam_real_imag();