poly.cpp

(plain text)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

template <typename T>
class Polynomial {
public:
  typedef T value_type, &reference;
  typedef const T &const_reference, &param_type;
  typedef std::vector<T> coef_type;
  typedef typename coef_type::size_type size_type;
  
  Polynomial (param_type x = T()) : myCoef(1, x) { }
  Polynomial (size_type n, param_type x = T()) : myCoef(n, x) { }

  template <typename It>
  Polynomial (It first, It last) : myCoef(first, last) { }

  size_type degree () const { return myCoef.size() - 1; }
  void degree (size_type n, param_type x = T()) { myCoef.resize(n + 1, x); }

  T &operator[] (size_type i) { return myCoef[i]; }
  T operator[] (size_type i) const { return myCoef[i]; }

  Polynomial &operator+= (const Polynomial &p) {
    size_type size = p.myCoef.size();
    if (myCoef.size() < size) myCoef.resize(size);
    std::transform(myCoef.begin(),
		   myCoef.begin() + std::min(degree() + 1, p.degree() + 1),
		   p.myCoef.begin(), myCoef.begin(),
		   std::plus<T>());
    normalize();
    return *this;
  }

  Polynomial &operator-= (const Polynomial &p) {
    size_type size = p.myCoef.size();
    if (myCoef.size() < size) myCoef.resize(size);
    std::transform(myCoef.begin(),
		   myCoef.begin() + std::min(degree() + 1, p.degree() + 1),
		   p.myCoef.begin(), myCoef.begin(),
		   std::minus<T>());
    normalize();
    return *this;
  }

  Polynomial &operator*= (const Polynomial &p) {
    coef_type coef(myCoef.size() + p.myCoef.size());

    for (size_type i = 0, j = myCoef.size(); i < j; ++i) {
      for (size_type m = 0, n = p.myCoef.size(); m < n; ++m) {
	coef[i + m] += myCoef[i] * p.myCoef[m];
      }
    }

    coef.swap(myCoef);
    normalize();
    return *this;
  }

  void swap (Polynomial &p) {
    myCoef.swap(p.myCoef);
  }

  T operator() (const T &x) const {
    T result = 0;
    for (size_type i = myCoef.size(); i > 0; --i) {
      result = result * x + myCoef[i - 1];
    }
    return result;
  }

private:
  std::vector<T> myCoef;

  void normalize () {
    myCoef.erase(std::find_if(myCoef.rbegin(),
			      myCoef.rend(),
			      std::bind2nd(std::not_equal_to<T>(), T())).base(),
		 myCoef.end());
  }
};

template <typename T> Polynomial<T> operator+ (const Polynomial<T> &p, const Polynomial<T> &q) {
  return Polynomial<T>(p) += q;
}

template <typename T> Polynomial<T> operator+ (const Polynomial<T> &p, T q) {
  return Polynomial<T>(p) += q;
}

template <typename T> Polynomial<T> operator+ (T p, const Polynomial<T> &q) {
  return Polynomial<T>(p) += q;
}

template <typename T> Polynomial<T> operator- (const Polynomial<T> &p, const Polynomial<T> &q) {
  return Polynomial<T>(p) -= q;
}

template <typename T> Polynomial<T> operator- (const Polynomial<T> &p, T q) {
  return Polynomial<T>(p) -= q;
}

template <typename T> Polynomial<T> operator- (T p, const Polynomial<T> &q) {
  return Polynomial<T>(p) -= q;
}

template <typename T> Polynomial<T> operator* (const Polynomial<T> &p, const Polynomial<T> &q) {
  return Polynomial<T>(p) *= q;
}

template <typename T> Polynomial<T> operator* (const Polynomial<T> &p, T q) {
  return Polynomial<T>(p) *= q;
}

template <typename T> Polynomial<T> operator* (T p, const Polynomial<T> &q) {
  return Polynomial<T>(p) *= q;
}

template <typename T>
std::ostream &
operator<< (std::ostream &os, const Polynomial<T> &p) {
  os << '(';
  bool printed = false;
  
  for (typename Polynomial<T>::size_type ip1 = p.degree() + 1; ip1 > 0; --ip1) {
    typename Polynomial<T>::size_type i = ip1 - 1;

    if (T coeff = p[i]) {
      if (coeff < 0) {
	os << " - ";
	coeff = -coeff;
      }

      else if (printed) {
	os << " + ";
      }

      if (coeff != 1) os << coeff;
      if (coeff != 0 && i > 0) os << 'x';
      if (i > 1) os << '^' << i;
      printed = true;
    }
  }

  if (!printed) os << '0';
  return os << ')';
}

template <typename T, int N> int n_elements (T (&) [N]) { return N; }

int main () {
  {
    int parray[] = { 0, 1, 2, 3 };
    Polynomial<int> p(parray, parray + n_elements(parray));

    cout << (p) << endl;
    cout << (5 + p) << endl;
    cout << (p + 5) << endl;
    cout << (5 + p + 5) << endl;
    cout << (p - p) << endl;
    cout << (p(6)) << endl;
  }

  {
    int parray[] = { 2, 2 };
    Polynomial<int> p(parray, parray + n_elements(parray));
    cout << (p) << endl;
    cout << (p * p) << endl;
  }
}