1 /** @file exam_collect.cpp
6 * GiNaC Copyright (C) 1999-2022 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 // Correctness of .collect(Z[x], x).
30 static unsigned exam_collect_1()
33 symbol x("x"), y("y");
34 ex a = (pow(x, 3) - 2*pow(x, 2) + 4*x) * pow(y, 2)
35 + (pow(x, 2) - x - 1) * y
37 ex b = pow(y, 2) * pow(x, 3)
38 + (y - 2*pow(y, 2)) * pow(x, 2)
39 + (4*pow(y, 2) - y + 1) * x
42 ex a_x = collect(a, x);
44 clog << "collect(" << a << ", " << x << ") erroneously returned "
45 << a_x << " instead of " << b << endl;
49 ex b_y = collect(b, y);
51 clog << "collect(" << b << ", " << y << ") erroneously returned "
52 << b_y << " instead of " << a << endl;
56 ex amb_x = collect(a - b, x);
58 clog << "collect(" << a - b << ", " << x << ") erroneously returned "
59 << amb_x << " instead of 0" << endl;
63 ex amb_y = collect(a - b, y);
65 clog << "collect(" << a - b << ", " << y << ") erroneously returned "
66 << amb_y << " instead of 0" << endl;
73 // Consistency of .collect(Z[x,y], {x,y}) with .coeff(x).
74 static unsigned exam_collect_2()
77 symbol x("x"), y("y"), p("p"), q("q");
81 ex a = expand(e1 * e2);
83 ex a_x = a.collect(x).coeff(x, 1);
85 clog << "collect(" << a << ", " << x << ") erroneously returned "
86 << a_x << " as coefficient of " << x << endl;
90 ex a_p = a.collect(p).coeff(p, 1);
92 clog << "collect(" << a << ", " << p << ") erroneously returned "
93 << a_p << " as coefficient of " << p << endl;
97 ex a_xy = a.collect(lst{x,y});
100 clog << "collect(" << a << ", {" << x << ", " << y << "}) erroneously returned "
101 << a_xy << " instead of " << ref << endl;
108 // Consistency of .collect(Z[f(x)], f(x)) with .coeff(f(x)).
109 static unsigned exam_collect_3()
112 symbol x("x"), p("p"), q("q");
114 for (unsigned deg = 2; deg < 7; ++deg) {
116 ex a1 = expand(pow(p + q + x, deg));
119 ex a2 = expand(pow(p + q + sin(x), deg));
120 a2 = a2.collect(sin(x));
122 for (unsigned i = 0; i < deg; ++i) {
123 ex a1_i = a1.coeff(x, i);
124 ex a2_i = a2.coeff(sin(x), i);
125 if (!expand(a1_i - a2_i).is_zero()) {
126 clog << "collect(" << a1 << ",sin(x)) inconsistent with "
127 "collect(" << a2 << ",x)" << endl;
136 unsigned exam_collect()
140 cout << "examining collect coefficients" << flush;
142 result += exam_collect_1(); cout << '.' << flush;
143 result += exam_collect_2(); cout << '.' << flush;
144 result += exam_collect_3(); cout << '.' << flush;
149 int main(int argc, char** argv)
151 return exam_collect();