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