]> www.ginac.de Git - ginac.git/blob - check/exam_factor.cpp
Fixed compile errors introduced in previous commit
[ginac.git] / check / exam_factor.cpp
1 /** @file exam_factor.cpp
2  *
3  *  Factorization test suite. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 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 <iostream>
24 #include "ginac.h"
25 using namespace std;
26 using namespace GiNaC;
27
28 static symbol w("w"), x("x"), y("y"), z("z");
29
30 static unsigned check_factor(const ex& e)
31 {
32         ex ee = e.expand();
33         ex answer = factor(ee);
34         if ( answer.expand() != ee || answer != e ) {
35                 clog << "factorization of " << e << " == " << ee << " gave wrong result: " << answer << endl;
36                 return 1;
37         }
38         return 0;
39 }
40
41 static unsigned exam_factor1()
42 {
43         unsigned result = 0;
44         ex e;
45         symbol x("x");
46         lst syms;
47         syms.append(x);
48         
49         e = ex("1+x-x^3", syms);
50         result += check_factor(e);
51
52         e = ex("1+x^6+x", syms);
53         result += check_factor(e);
54
55         e = ex("1-x^6+x", syms);
56         result += check_factor(e);
57
58         e = ex("(1+x)^3", syms);
59         result += check_factor(e);
60
61         e = ex("(x+1)*(x+4)", syms);
62         result += check_factor(e);
63
64         e = ex("x^6-3*x^5+x^4-3*x^3-x^2-3*x+1", syms);
65         result += check_factor(e);
66
67         e = ex("(-1+x)^3*(1+x)^3*(1+x^2)", syms);
68         result += check_factor(e);
69
70         e = ex("-(-168+20*x-x^2)*(30+x)", syms);
71         result += check_factor(e);
72
73         e = ex("x^2*(x-3)^2*(x^3-5*x+7)", syms);
74         result += check_factor(e);
75
76         e = ex("-6*x^2*(x-3)", syms);
77         result += check_factor(e);
78
79         e = ex("x^16+11*x^4+121", syms);
80         result += check_factor(e);
81
82         e = ex("x^8-40*x^6+352*x^4-960*x^2+576", syms);
83         result += check_factor(e);
84
85         e = ex("x*(2+x^2)*(1+x+x^3+x^2+x^6+x^5+x^4)*(1+x)^2*(1-x+x^2)^2*(-1+x)", syms);
86         result += check_factor(e);
87
88         e = ex("(x+4+x^2-x^3+43*x^4)*(x+1-x^2-3*x^3+4*x^4)", syms);
89         result += check_factor(e);
90
91         e = ex("-x^2*(x-1)*(1+x^2)", syms);
92         result += check_factor(e);
93
94         e = x;
95         result += check_factor(e);
96
97         // x^37 + 1
98         e = ex("(1+x)*(1+x^2-x^29-x^11-x^25-x^9-x^35+x^20-x^3+x^16-x^15-x-x^13+x^28+x^24-x^33+x^8-x^19+x^36+x^12-x^27+x^10-x^23+x^18+x^14+x^34-x^31+x^32+x^30-x^5+x^26+x^4+x^22-x^21-x^7-x^17+x^6)", syms);
99         result += check_factor(e);
100
101         e = ex("(1+4*x)*x^2*(1-4*x+16*x^2)*(3+5*x+92*x^3)", syms);
102         result += check_factor(e);
103
104         e = ex("(77+11*x^3+25*x^2+27*x+102*x^4)*(85+57*x^3+92*x^2+29*x+66*x^4)", syms);
105         result += check_factor(e);
106
107         return result;
108 }
109
110 static unsigned exam_factor2()
111 {
112         unsigned result = 0;
113         ex e;
114         symbol x("x"), y("y"), z("z");
115         lst syms;
116         syms = x, y, z;
117         
118         e = ex("x+y", syms);
119         result += check_factor(e);
120
121         e = ex("(x^2-y+1)*(x+y)", syms);
122         result += check_factor(e);
123
124         e = ex("-2*(x+y)*(x-y)", syms);
125         result += check_factor(e);
126
127         e = ex("(16+x^2*z^3)*(-17+3*x-5*z)*(2*x+3*z)*(x-y^2-z^3)", syms);
128         result += check_factor(e);
129
130         e = ex("(x-y*z)*(x-y^2-z^3)*(x+y+z)", syms);
131         result += check_factor(e);
132         
133         e = ex("-(y^2-x+z^3)*x*(x+y+z)", syms);
134         result += check_factor(e);
135         
136         e = ex("-316*(3*x-4*z)*(2*x+3*z)*(x+y)*(-1+x)", syms);
137         result += check_factor(e);
138         
139         e = ex("(x+x^3+z^2)*(3*x-4*z)", syms);
140         result += check_factor(e);
141         
142         e = ex("250*(-3+x)*(4*z-3*x)*(x^3+z^2+x)*x", syms);
143         result += check_factor(e);
144         
145         e = ex("327*(x+z^2+x^3)*(3*x-4*z)*(-7+5*x-x^3)*(1+x+x^2)", syms);
146         result += check_factor(e);
147         
148         e = ex("x-y^2-z^3", syms);
149         result += check_factor(e);
150         
151         e = ex("-390*(7+3*x^4)*(2+x^2)*(x-z^3-y^2)", syms);
152         result += check_factor(e);
153         
154         e = ex("55*(1+x)^2*(3*x-4*z)*(1+x+x^2)*(x+x^3+z^2)", syms);
155         result += check_factor(e);
156         
157         e = ex("x+y*x-1", syms);
158         result += check_factor(e);
159         
160         e = ex("390*(-1+x^6-x)*(7+3*x^4)*(2+x^2)*(y+x)*(-1+y-x^2)*(1+x^2+x)^2", syms);
161         result += check_factor(e);
162         
163         e = ex("310*(y+x)*(-1+y-x^2)", syms);
164         result += check_factor(e);
165
166         return result;
167 }
168
169 static unsigned exam_factor3()
170 {
171         unsigned result = 0;
172         ex e;
173         symbol k("k"), n("n");
174         lst syms;
175         syms = k, n;
176         
177         e = ex("1/2*(-3+3*k-n)*(-2+3*k-n)*(-1+3*k-n)", syms);
178         result += check_factor(e);
179
180         e = ex("1/4*(2*k-n)*(-1+2*k-n)", syms);
181         result += check_factor(e);
182
183         return result;
184 }
185
186 unsigned exam_factor()
187 {
188         unsigned result = 0;
189
190         cout << "examining polynomial factorization" << flush;
191
192         result += exam_factor1(); cout << '.' << flush;
193         result += exam_factor2(); cout << '.' << flush;
194         result += exam_factor3(); cout << '.' << flush;
195
196         return result;
197 }
198
199 int main(int argc, char** argv)
200 {
201         return exam_factor();
202 }