1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
28 ////////////////////////////////////////////////////////////////////
29 // Class Polynomial<X> represents a polynomial of type X. Type X
30 // must be a field(in the mathematical sense, not C).
31 ////////////////////////////////////////////////////////////////////
33 #include <AD/generic/generic.h>
38 ////////////////////////////////////////////////////////////////////
39 // We use a simple array representation storing the coeficients
40 // of the polynomial. More storage is needed may be preallocated
41 // to allow for future expansion.
42 ////////////////////////////////////////////////////////////////////
43 int max_degree
; // maximum degree currently allowed
44 int degree
; // degree of polynomial
45 X
* coef
; // array of coefficients
47 void norm(); // normalize result
49 friend int max(int i
,int j
) { return i
> j
? i
: j
; }
50 friend int min(int i
,int j
) { return i
< j
? i
: j
; }
51 Polynomial(int deg
) : degree(deg
), coef(new X
[deg
+ 1]) {}
55 ////////////////////////////////////////////////////////////////
56 // constructor and destructor
57 ////////////////////////////////////////////////////////////////
59 Polynomial() : degree(-1), coef(0) {}
60 Polynomial(const X
& x
) : degree(0), coef(new X
[1]) { coef
[0] = x
; }
61 Polynomial(const Polynomial
& p
) : coef(0) { *this = p
; }
62 ~Polynomial() { delete [] coef
; }
64 ////////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////////
67 void operator = (const Polynomial
&);
69 ////////////////////////////////////////////////////////////////
70 // operator () -- evaluate the polynomial at a point
71 // operator [] -- returns the coefficient of a certain degree
72 // degree() -- returns the maximum degree
73 ////////////////////////////////////////////////////////////////
74 X
operator () (const X
&) const;
75 X
& operator [] (int i
) const { return coef
[i
]; }
76 int deg() const { return degree
; }
79 void operator += (const X
&);
80 void operator -= (const X
&);
81 void operator *= (const X
&);
82 void operator /= (const X
&);
83 void operator += (const Polynomial
&);
84 void operator -= (const Polynomial
&);
85 void operator *= (const Polynomial
&);
86 void operator /= (const Polynomial
&);
88 Polynomial
operator - () const;
89 friend Polynomial
operator + (const Polynomial
&, const Polynomial
&);
90 friend Polynomial
operator - (const Polynomial
&, const Polynomial
&);
91 friend Polynomial
operator * (const Polynomial
&, const Polynomial
&);
92 friend Polynomial
operator / (const Polynomial
&, const Polynomial
&);
93 friend Polynomial
operator % (const Polynomial
&, const Polynomial
&);
94 friend Polynomial
operator + (const Polynomial
&, const X
&);
95 friend Polynomial
operator - (const Polynomial
&, const X
&);
96 friend Polynomial
operator * (const Polynomial
&, const X
&);
97 friend Polynomial
operator / (const Polynomial
&, const X
&);
98 friend Polynomial
operator + (const X
&, const Polynomial
&);
99 friend Polynomial
operator - (const X
&, const Polynomial
&);
100 friend Polynomial
operator * (const X
&, const Polynomial
&);
102 friend Bool
operator == (const Polynomial
&, const Polynomial
&);
103 friend Bool
operator != (const Polynomial
&, const Polynomial
&);
104 friend Bool
operator == (const Polynomial
&, const X
&);
105 friend Bool
operator != (const Polynomial
&, const X
&);
106 friend Bool
operator == (const X
&, const Polynomial
&);
107 friend Bool
operator != (const X
&, const Polynomial
&);
110 //////////////////////////////////////////////////////////////////////////
111 // Implementation of the template methods
112 //////////////////////////////////////////////////////////////////////////
115 void Polynomial
<X
>::norm()
116 { while (degree
> 0 && coef
[degree
] == 0) --degree
; }
119 void Polynomial
<X
>::negate()
120 { for (int i
= degree
; i
>= 0; i
--) coef
[i
] = -coef
[i
]; }
122 void Polynomial
<X
>::operator += (const X
& x
)
123 { for (int i
= degree
; i
>= 0; i
--) coef
[i
] += x
; norm(); }
125 void Polynomial
<X
>::operator -= (const X
& x
)
126 { for (int i
= degree
; i
>= 0; i
--) coef
[i
] -= x
; norm(); }
128 void Polynomial
<X
>::operator *= (const X
& x
)
129 { for (int i
= degree
; i
>= 0; i
--) coef
[i
] *= x
; norm(); }
131 void Polynomial
<X
>::operator /= (const X
& x
)
132 { for (int i
= degree
; i
>= 0; i
--) coef
[i
] /= x
; norm(); }
135 void Polynomial
<X
>::operator += (const Polynomial
<X
>& p
)
139 void Polynomial
<X
>::operator -= (const Polynomial
<X
>& p
)
140 { for (int i
= degree
; i
>= 0; i
--) coef
[i
] -= p
[i
]; norm(); }
143 void Polynomial
<X
>::operator *= (const Polynomial
<X
>& p
)
144 { Polynomial
<X
> r(this->deg() + p
.deg());
145 { for (int i
= r
.deg(); i
>= 0; i
--) r
[i
] = 0; }
146 { for (int i
= degree
; i
>= 0; i
--)
147 for (int j
= p
.deg(); j
>= 0; j
--)
148 coef
[i
+j
] += (*this)[i
] * p
[j
];
155 Polynomial
<X
> Polynomial
<X
>::operator - () const
156 { Polynomial
<X
> r(degree
);
157 for (int i
= degree
; i
>= 0; i
--) r
[i
] = -coef
[i
];
161 Polynomial
<X
> operator + (const Polynomial
<X
>& p
, const Polynomial
<X
>& q
)
162 { Polynomial
<X
> r(max(p
.deg(),q
.deg()));
165 Polynomial
<X
> operator - (const Polynomial
<X
>& p
, const Polynomial
<X
>& q
)
168 Polynomial
<X
> operator * (const Polynomial
<X
>& p
, const Polynomial
<X
>& q
)
171 Polynomial
<X
> operator + (const Polynomial
<X
>& p
, const X
& x
)
174 Polynomial
<X
> operator - (const Polynomial
<X
>& p
, const X
& x
)
177 Polynomial
<X
> operator * (const Polynomial
<X
>& p
, const X
& x
)
180 Polynomial
<X
> operator / (const Polynomial
<X
>& p
, const X
& x
)
183 Polynomial
<X
> operator + (const X
& x
, const Polynomial
<X
>& p
)
186 Polynomial
<X
> operator - (const X
& x
, const Polynomial
<X
>& p
)