File: exam_heur_gcd.cpp

package info (click to toggle)
ginac 1.8.10-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 8,000 kB
  • sloc: cpp: 52,440; sh: 5,402; makefile: 455; python: 193
file content (57 lines) | stat: -rw-r--r-- 1,865 bytes parent folder | download
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
/** @file heur_gcd_bug.cpp
 *
 *  exam_heur_gcd.cpp Check for a bug in heur_gcd().
 *
 *  heur_gcd() did not check if the arguments are integer polynomials
 *  (and did not convert them to integer polynomials), which lead to
 *  endless loop or (even worse) wrong result. */

/*
 *  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;

int main(int argc, char** argv)
{
	cout << "examining if heur_gcd() can cope with rational polynomials. ";
	const symbol x("x");
	const ex _ex1(1);
	ex a1 = x + numeric(5, 4);
	ex a2 = x + numeric(5, 2);
	ex b =  pow(x, 2) + numeric(15, 4)*x + numeric(25, 8);
	// note: both a1 and a2 divide b
	
	// a2 divides b, so cofactor of a2 should be a (rational) number
	ex ca2, cb2;
	ex g2 = gcd(a2, b, &ca2, &cb2);
	if (!is_a<numeric>(ca2)) {
		cerr << "gcd(" << a2 << ", " << b << ") was miscomputed" << endl;
		return 1;
	}
	ex ca1, cb1;
	// a1 divides b, so cofactor of a1 should be a (rational) number
	ex g1 = gcd(a1, b, &ca1, &cb1);
	if (!is_a<numeric>(ca1)) {
		cerr << "gcd(" << a1 << ", " << b << ") was miscomputed" << endl;
		return 1;
	}
	return 0;
}