This example is the one your Polynomial class must be able to run.
#include <cstdint>
#include <string>
using std::string;
#include <iostream>
using std::cout; using std::endl;
#include "mod.hpp"
#include "modp.hpp"
#include "rational.hpp"
#include "polynomial.hpp"
string bstring(bool b) {
const static string yes = "true";
const static string no = "false";
if (b) return yes;
else return no;
}
void test_basic_construction() {
Polynomial<Mod<int32_t, 6> > p = { 2, 4, 0, 2, 4 };
vector<Mod<int32_t, 6> > v = { 3, 0, 3, 0, 3 };
Polynomial<Mod<int32_t, 6> > q(v);
Polynomial<Mod<int32_t, 6> > r(p);
cout << "first and third should be the same:\n";
cout << '\t' << p << endl;
cout << '\t' << q << endl;
cout << '\t' << r << endl;
cout << "is " << p << " cancellable? (should be false)\n";
cout << '\t' << bstring(p.is_cancellable()) << endl;
Rational<int16_t> a(3,2);
Rational<int16_t> b(4,7);
Polynomial<Rational<int16_t> > f = { a, 1, 0, a };
Polynomial<Rational<int16_t> > g = { 0, 2, b };
cout << "rational polynomials:\n";
cout << '\t' << f << endl;
cout << '\t' << g << endl;
cout << "is " << f << " cancellable? (should be true)\n";
cout << '\t' << bstring(f.is_cancellable()) << endl;
}
void test_comparison() {
Polynomial<Mod<int32_t, 6> > p = { 2, 4, 0, 2, 4 };
vector<Mod<int32_t, 6> > v = { 3, 0, 3, 0, 3 };
Polynomial<Mod<int32_t, 6> > q(v);
Polynomial<Mod<int32_t, 6> > r(p);
cout << "second and last should be true, others false\n";
cout << '\t' << bstring(p == q) << endl;
cout << '\t' << bstring(p == r) << endl;
cout << '\t' << bstring(q == r) << endl;
Rational<int16_t> a(3,2);
Rational<int16_t> b(4,7);
Polynomial<Rational<int16_t> > f = { a, 1, 0, a };
Polynomial<Rational<int16_t> > g = { 0, 2, b };
cout << '\t' << bstring(f == g) << endl;
cout << '\t' << bstring(f == f) << endl;
}
void test_arithmetic() {
Polynomial<Mod<int32_t, 6> > p = { 2, 4, 0, 4, 2 };
vector<Mod<int32_t, 6> > v = { 3, 0, 3, 0, 3 };
Polynomial<Mod<int32_t, 6> > q(v);
Polynomial<Mod<int32_t, 6> > r(p);
cout << "Working modulo 6:\n";
cout << "\t( " << p << " ) + ( " << q << " ) = " << p + q << endl;
cout << "\t( " << p << " ) - ( " << q << " ) = " << p - q << endl;
cout << "\t( " << p << " ) * ( " << q << " ) = " << p *q << endl;
Rational<int16_t> a(3,2);
Rational<int16_t> b(4,7);
Polynomial<Rational<int16_t> > f = { a, 1, 0, a };
Polynomial<Rational<int16_t> > g = { 0, 2, b };
cout << "Working over the rationals:\n";
cout << "\t( " << f << " ) + ( " << f << " ) = " << f + f << endl;
cout << "\t( " << f << " ) - ( " << f << " ) = " << f - f << endl;
cout << "\t( " << f << " ) + ( " << g << " ) = " << f + g << endl;
cout << "\t( " << f << " ) * ( " << g << " ) = " << f * g << endl;
}
void test_division() {
Polynomial<Mod<int32_t, 5> > p = { 2, 4, 0, 4, 2, 0, 1 };
vector<Mod<int32_t, 5> > v = { 3, 0, 3, 0, 1 };
Polynomial<Mod<int32_t, 5> > q(v);
cout << "Working modulo 5:\n";
cout << "\t( " << p << " ) / ( " << q << " ) = " << p / q << endl;
cout << "\t( " << p << " ) % ( " << q << " ) = " << p % q << endl;
Rational<int16_t> a(3,2);
Rational<int16_t> b(4,7);
Polynomial<Rational<int16_t> > f = { a, a, 0, 1 };
Polynomial<Rational<int16_t> > g = { b, 2, 1 };
cout << "Working over the rationals:\n";
cout << "\t( " << f << " ) / ( " << f << " ) = " << f / f << endl;
cout << "\t( " << f << " ) % ( " << f << " ) = " << f % f << endl;
cout << "\t( " << f << " ) / ( " << g << " ) = " << f / g << endl;
cout << "\t( " << f << " ) % ( " << g << " ) = " << f % g << endl;
}
int main() {
test_basic_construction();
test_comparison();
test_arithmetic();
test_division();
}