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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/** @file exam_collect.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;
// Correctness of .collect(Z[x], x).
static unsigned exam_collect_1()
{
unsigned result = 0;
symbol x("x"), y("y");
ex a = (pow(x, 3) - 2*pow(x, 2) + 4*x) * pow(y, 2)
+ (pow(x, 2) - x - 1) * y
+ (x + 1);
ex b = pow(y, 2) * pow(x, 3)
+ (y - 2*pow(y, 2)) * pow(x, 2)
+ (4*pow(y, 2) - y + 1) * x
+ (1 - y);
ex a_x = collect(a, x);
if (a_x != b) {
clog << "collect(" << a << ", " << x << ") erroneously returned "
<< a_x << " instead of " << b << endl;
++result;
}
ex b_y = collect(b, y);
if (b_y != a) {
clog << "collect(" << b << ", " << y << ") erroneously returned "
<< b_y << " instead of " << a << endl;
++result;
}
ex amb_x = collect(a - b, x);
if (amb_x != 0) {
clog << "collect(" << a - b << ", " << x << ") erroneously returned "
<< amb_x << " instead of 0" << endl;
++result;
}
ex amb_y = collect(a - b, y);
if (amb_y != 0) {
clog << "collect(" << a - b << ", " << y << ") erroneously returned "
<< amb_y << " instead of 0" << endl;
++result;
}
return result;
}
// Consistency of .collect(Z[x,y], {x,y}) with .coeff(x).
static unsigned exam_collect_2()
{
unsigned result = 0;
symbol x("x"), y("y"), p("p"), q("q");
ex e1 = x + y;
ex e2 = 1 + p + q;
ex a = expand(e1 * e2);
ex a_x = a.collect(x).coeff(x, 1);
if (a_x != e2) {
clog << "collect(" << a << ", " << x << ") erroneously returned "
<< a_x << " as coefficient of " << x << endl;
++result;
}
ex a_p = a.collect(p).coeff(p, 1);
if (a_p != e1) {
clog << "collect(" << a << ", " << p << ") erroneously returned "
<< a_p << " as coefficient of " << p << endl;
++result;
}
ex a_xy = a.collect(lst{x,y});
ex ref = e2*x + e2*y;
if (a_xy != ref) {
clog << "collect(" << a << ", {" << x << ", " << y << "}) erroneously returned "
<< a_xy << " instead of " << ref << endl;
++result;
}
return result;
}
// Consistency of .collect(Z[f(x)], f(x)) with .coeff(f(x)).
static unsigned exam_collect_3()
{
unsigned result = 0;
symbol x("x"), p("p"), q("q");
for (unsigned deg = 2; deg < 7; ++deg) {
ex a1 = expand(pow(p + q + x, deg));
a1 = a1.collect(x);
ex a2 = expand(pow(p + q + sin(x), deg));
a2 = a2.collect(sin(x));
for (unsigned i = 0; i < deg; ++i) {
ex a1_i = a1.coeff(x, i);
ex a2_i = a2.coeff(sin(x), i);
if (!expand(a1_i - a2_i).is_zero()) {
clog << "collect(" << a1 << ",sin(x)) inconsistent with "
"collect(" << a2 << ",x)" << endl;
++result;
}
}
}
return result;
}
unsigned exam_collect()
{
unsigned result = 0;
cout << "examining collect coefficients" << flush;
result += exam_collect_1(); cout << '.' << flush;
result += exam_collect_2(); cout << '.' << flush;
result += exam_collect_3(); cout << '.' << flush;
return result;
}
int main(int argc, char** argv)
{
return exam_collect();
}
|