Assignments Assignments

polynomial.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, Michael Kazhdan
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27*/
28
29#ifndef POLYNOMIAL_INCLUDED
30#define POLYNOMIAL_INCLUDED
31#include <iostream>
32#include <Util/geometry.h>
33#include <Util/algebra.h>
34
35 namespace Util
36{
38 template< unsigned int D1 , unsigned int D2 > struct Min{ static const unsigned int Value = D1<D2 ? D1 : D2; };
40 template< unsigned int D1 , unsigned int D2 > struct Max{ static const unsigned int Value = D1>D2 ? D1 : D2; };
41
43 template< unsigned int Dim , unsigned int Degree >
44 class Polynomial : public VectorSpace< Polynomial< Dim , Degree > >
45 {
46 template< unsigned int _Dim , unsigned int _Degree > friend class Polynomial;
47 template< unsigned int _Dim , unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< _Dim , Degree1 + Degree2 > operator * ( const Polynomial< _Dim , Degree1 > & , const Polynomial< _Dim , Degree2 > & );
48 template< unsigned int _Dim , unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< _Dim , Max< Degree1 , Degree2 >::Value > operator + ( const Polynomial< _Dim , Degree1 > & , const Polynomial< _Dim , Degree2 > & );
49 template< unsigned int _Dim , unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< _Dim , Max< Degree1 , Degree2 >::Value > operator - ( const Polynomial< _Dim , Degree1 > & , const Polynomial< _Dim , Degree2 > & );
50 template< unsigned int _Dim , unsigned int _Degree > friend std::ostream &operator << ( std::ostream & , const Polynomial< _Dim , _Degree > & );
51
54 Polynomial< Dim-1 , Degree > _polynomials[Degree+1];
55
57 const double &_coefficient( const unsigned int indices[] , unsigned int maxDegree ) const;
58
60 double &_coefficient( const unsigned int indices[] , unsigned int maxDegree );
61
63 double _evaluate( const double coordinates[] , unsigned int maxDegree ) const;
64
66 Polynomial< 1, Degree > _evaluate( const Ray< Dim > &ray , unsigned int maxDegree ) const;
67
69 bool _isZero( unsigned int maxDegree ) const;
70
72 bool _isConstant( unsigned int maxDegree ) const;
73
74 public:
76 Polynomial( void );
77
79 Polynomial( double c );
80
82 template< unsigned int _Degree >
83 Polynomial( const Polynomial< Dim , _Degree > &p );
84
86 template< unsigned int _Degree >
87 Polynomial &operator= ( const Polynomial< Dim , _Degree > &p );
88
90 template< typename ... UnsignedInts >
91 const double &coefficient( UnsignedInts ... indices ) const;
92
94 template< typename ... UnsignedInts >
95 double &coefficient( UnsignedInts ... indices );
96
98 template< typename ... Doubles >
99 double operator()( Doubles ... coordinates ) const;
100
102 double operator()( Point< Dim > p ) const;
103
105 Polynomial< Dim , Degree-1 > d( int dim ) const;
106
108 Polynomial< 1 , Degree > operator()( const Ray< Dim > &ray ) const;
109
111 // VectorSpace methods //
113
114 Polynomial operator * ( double s ) const;
115
117 Polynomial operator + ( const Polynomial & p ) const;
118 };
119
121 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 >
122 Polynomial< Dim , Degree1 + Degree2 > operator * ( const Polynomial< Dim , Degree1 > &p1 , const Polynomial< Dim , Degree2 > &p2 );
123
125 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 >
126 Polynomial< Dim , Max< Degree1 , Degree2 >::Value > operator + ( const Polynomial< Dim , Degree1 > &p1 , const Polynomial< Dim , Degree2 > &p2 );
127
129 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 >
130 Polynomial< Dim , Max< Degree1 , Degree2 >::Value > operator - ( const Polynomial< Dim , Degree1 > &p1 , const Polynomial< Dim , Degree2 > &p2 );
131
133 template< unsigned int Dim , unsigned int Degree >
134 std::ostream &operator << ( std::ostream &stream , const Polynomial< Dim , Degree > &poly );
135
137 template< unsigned int Degree >
138 class Polynomial< 1 , Degree > : public VectorSpace< Polynomial< 1 , Degree > >
139 {
140 template< unsigned int _Dim , unsigned int _Degree > friend class Polynomial;
141 template< unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< 1 , Degree1 + Degree2 > operator * ( const Polynomial< 1 , Degree1 > & , const Polynomial< 1 , Degree2 > & );
142 template< unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< 1 , Max< Degree1 , Degree2 >::Value > operator + ( const Polynomial< 1 , Degree1 > & , const Polynomial< 1 , Degree2 > & );
143 template< unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< 1 , Max< Degree1 , Degree2 >::Value > operator - ( const Polynomial< 1 , Degree1 > & , const Polynomial< 1 , Degree2 > & );
144 template< unsigned int _Degree > friend std::ostream &operator << ( std::ostream & , const Polynomial< 1 , _Degree > & );
145 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< Dim , Degree1 + Degree2 > operator * ( const Polynomial< Dim , Degree1 > & , const Polynomial< Dim , Degree2 > & );
146 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< Dim , Max< Degree1 , Degree2 >::Value > operator + ( const Polynomial< Dim , Degree1 > & , const Polynomial< Dim , Degree2 > & );
147 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 > friend Polynomial< Dim , Max< Degree1 , Degree2 >::Value > operator - ( const Polynomial< Dim , Degree1 > & , const Polynomial< Dim , Degree2 > & );
148 template< unsigned int Dim , unsigned int _Degree > friend std::ostream &operator << ( std::ostream & , const Polynomial< Dim , _Degree > & );
149
151 double _coefficients[Degree+1];
152
154 const double &_coefficient( const unsigned int indices[] , unsigned int maxDegree ) const;
155
157 double &_coefficient( const unsigned int indices[] , unsigned int maxDegree );
158
160 double _evaluate( const double coordinates[] , unsigned int maxDegree ) const;
161
163 Polynomial _evaluate( const Ray< 1 > &ray , unsigned int maxDegree ) const;
164
166 bool _isZero( unsigned int maxDegree ) const;
167
169 bool _isConstant( unsigned int maxDegree ) const;
170 public:
172 Polynomial( void );
173
175 Polynomial( double c );
176
179 template< typename ... Doubles >
180 Polynomial( Doubles ... coefficients );
181
183 template< unsigned int _Degree >
184 Polynomial( const Polynomial< 1 , _Degree > &p );
185
187 template< unsigned int _Degree >
188 Polynomial &operator= ( const Polynomial< 1 , _Degree > &p );
189
191 const double &coefficient( unsigned int d ) const;
192
194 double &coefficient( unsigned int d );
195
197 double operator()( double x ) const;
198
200 Polynomial< 1 , Degree-1 > d( unsigned int d=0 ) const;
201
203 Polynomial operator()( const Ray< 1 > &ray ) const;
204
207 unsigned int roots( double *r ) const;
208
210 // VectorSpace methods //
212
213 Polynomial operator * ( double s ) const;
214
216 Polynomial operator + ( const Polynomial & p ) const;
217
218 };
219
221 template< unsigned int Degree1 , unsigned int Degree2 >
222 Polynomial< 1 , Degree1 + Degree2 > operator * ( const Polynomial< 1 , Degree1 > &p1 , const Polynomial< 1 , Degree2 > &p2 );
223
225 template< unsigned int Degree1 , unsigned int Degree2 >
226 Polynomial< 1 , Max< Degree1 , Degree2 >::Value > operator + ( const Polynomial< 1 , Degree1 > &p1 , const Polynomial< 1 , Degree2 > &p2 );
227
229 template< unsigned int Degree1 , unsigned int Degree2 >
230 Polynomial< 1 , Max< Degree1 , Degree2 >::Value > operator - ( const Polynomial< 1 , Degree1 > &p1 , const Polynomial< 1 , Degree2 > &p2 );
231
233 template< unsigned int Degree >
234 std::ostream &operator << ( std::ostream &stream , const Polynomial< 1 , Degree > &poly );
235
237 // Classes specialized for 1D, 2D, 3D, and 4D //
239
240 template< unsigned int Degree >
241 using Polynomial1D = Polynomial< 1 , Degree >;
242
244 template< unsigned int Degree >
245 using Polynomial2D = Polynomial< 2 , Degree >;
246
248 template< unsigned int Degree >
249 using Polynomial3D = Polynomial< 3 , Degree >;
250
252 template< unsigned int Degree >
253 using Polynomial4D = Polynomial< 4 , Degree >;
254}
255#include "polynomial.inl"
256#endif // POLYNOMIAL_INCLUDED
algebra.h
Util::Point
Definition geometry.h:52
Util::Polynomial< 1, Degree >
Definition polynomial.h:139
Util::Polynomial
Definition polynomial.h:45
Util::Polynomial::_isConstant
bool _isConstant(unsigned int maxDegree) const
Definition polynomial.inl:340
Util::Polynomial::operator*
friend Polynomial< _Dim, Degree1+Degree2 > operator*(const Polynomial< _Dim, Degree1 > &, const Polynomial< _Dim, Degree2 > &)
Util::Polynomial::_evaluate
double _evaluate(const double coordinates[], unsigned int maxDegree) const
Definition polynomial.inl:305
Util::Polynomial::coefficient
const double & coefficient(UnsignedInts ... indices) const
Definition polynomial.inl:349
Util::Polynomial::d
Polynomial< Dim, Degree-1 > d(int dim) const
Definition polynomial.inl:379
Util::Polynomial::_polynomials
Polynomial< Dim-1, Degree > _polynomials[Degree+1]
Definition polynomial.h:54
Util::Polynomial::Polynomial
friend class Polynomial
Definition polynomial.h:46
Util::Polynomial::operator()
double operator()(Doubles ... coordinates) const
Definition polynomial.inl:367
Util::Polynomial::operator-
friend Polynomial< _Dim, Max< Degree1, Degree2 >::Value > operator-(const Polynomial< _Dim, Degree1 > &, const Polynomial< _Dim, Degree2 > &)
Util::Polynomial::roots
unsigned int roots(double *r) const
Definition polynomial.inl:167
Util::Polynomial::_isZero
bool _isZero(unsigned int maxDegree) const
Definition polynomial.inl:333
Util::Polynomial::operator<<
friend std::ostream & operator<<(std::ostream &, const Polynomial< _Dim, _Degree > &)
Util::Polynomial::operator+
friend Polynomial< _Dim, Max< Degree1, Degree2 >::Value > operator+(const Polynomial< _Dim, Degree1 > &, const Polynomial< _Dim, Degree2 > &)
Util::Polynomial::operator=
Polynomial & operator=(const Polynomial< Dim, _Degree > &p)
Util::Polynomial::_coefficient
const double & _coefficient(const unsigned int indices[], unsigned int maxDegree) const
Definition polynomial.inl:291
Util::Ray
Definition geometry.h:299
Util::VectorSpace
Definition algebra.h:66
geometry.h
Util
Definition algebra.h:7
Util::operator<<
std::ostream & operator<<(std::ostream &stream, const Point< Dim > &p)
Definition geometry.inl:127
Util::operator-
Polynomial< Dim, Max< Degree1, Degree2 >::Value > operator-(const Polynomial< Dim, Degree1 > &p1, const Polynomial< Dim, Degree2 > &p2)
Definition polynomial.inl:455
Util::operator*
Ray< Dim > operator*(const Matrix< Dim+1, Dim+1 > &m, const Ray< Dim > &ray)
Definition geometry.inl:493
Util::operator+
Polynomial< Dim, Max< Degree1, Degree2 >::Value > operator+(const Polynomial< Dim, Degree1 > &p1, const Polynomial< Dim, Degree2 > &p2)
Definition polynomial.inl:446
polynomial.inl
Util::Max
Definition polynomial.h:40
Util::Max::Value
static const unsigned int Value
Definition polynomial.h:40
Util::Min
Definition polynomial.h:38
Util::Min::Value
static const unsigned int Value
Definition polynomial.h:38