Assignments
Assignments
polynomial.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019, Michael Kazhdan
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
8Redistributions of source code must retain the above copyright notice, this list of
9conditions and the following disclaimer. Redistributions in binary form must reproduce
10the above copyright notice, this list of conditions and the following disclaimer
11in the documentation and/or other materials provided with the distribution.
12
13Neither the name of the Johns Hopkins University nor the names of its contributors
14may be used to endorse or promote products derived from this software without specific
15prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26DAMAGE.
27*/
28
29#ifndef POLYNOMIAL_INCLUDED
30#define POLYNOMIAL_INCLUDED
31#include <iostream>
32#include <Util/geometry.h>
33#include <Util/algebra.h>
34
35namespace 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 >
84
86 template< unsigned int _Degree >
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
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 >
123
125 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 >
127
129 template< unsigned int Dim , unsigned int Degree1 , unsigned int Degree2 >
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 >
185
187 template< unsigned int _Degree >
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
217
218 };
219
221 template< unsigned int Degree1 , unsigned int Degree2 >
223
225 template< unsigned int Degree1 , unsigned int Degree2 >
227
229 template< unsigned int Degree1 , unsigned int Degree2 >
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 >
242
244 template< unsigned int Degree >
246
248 template< unsigned int Degree >
250
252 template< unsigned int Degree >
254}
255#include "polynomial.inl"
256#endif // POLYNOMIAL_INCLUDED
Definition: geometry.h:52
Definition: polynomial.h:139
Definition: polynomial.h:45
bool _isConstant(unsigned int maxDegree) const
Definition: polynomial.inl:340
friend Polynomial< _Dim, Degree1+Degree2 > operator*(const Polynomial< _Dim, Degree1 > &, const Polynomial< _Dim, Degree2 > &)
double _evaluate(const double coordinates[], unsigned int maxDegree) const
Definition: polynomial.inl:305
const double & coefficient(UnsignedInts ... indices) const
Definition: polynomial.inl:349
Polynomial< Dim, Degree-1 > d(int dim) const
Definition: polynomial.inl:379
Polynomial< Dim-1, Degree > _polynomials[Degree+1]
Definition: polynomial.h:54
friend class Polynomial
Definition: polynomial.h:46
double operator()(Doubles ... coordinates) const
Definition: polynomial.inl:367
friend Polynomial< _Dim, Max< Degree1, Degree2 >::Value > operator-(const Polynomial< _Dim, Degree1 > &, const Polynomial< _Dim, Degree2 > &)
unsigned int roots(double *r) const
Definition: polynomial.inl:167
bool _isZero(unsigned int maxDegree) const
Definition: polynomial.inl:333
friend std::ostream & operator<<(std::ostream &, const Polynomial< _Dim, _Degree > &)
friend Polynomial< _Dim, Max< Degree1, Degree2 >::Value > operator+(const Polynomial< _Dim, Degree1 > &, const Polynomial< _Dim, Degree2 > &)
Polynomial & operator=(const Polynomial< Dim, _Degree > &p)
const double & _coefficient(const unsigned int indices[], unsigned int maxDegree) const
Definition: polynomial.inl:291
Definition: geometry.h:299
Definition: algebra.h:66
Definition: algebra.h:7
std::ostream & operator<<(std::ostream &stream, const Point< Dim > &p)
Definition: geometry.inl:127
Polynomial< Dim, Max< Degree1, Degree2 >::Value > operator-(const Polynomial< Dim, Degree1 > &p1, const Polynomial< Dim, Degree2 > &p2)
Definition: polynomial.inl:455
Ray< Dim > operator*(const Matrix< Dim+1, Dim+1 > &m, const Ray< Dim > &ray)
Definition: geometry.inl:493
Polynomial< Dim, Max< Degree1, Degree2 >::Value > operator+(const Polynomial< Dim, Degree1 > &p1, const Polynomial< Dim, Degree2 > &p2)
Definition: polynomial.inl:446
Definition: polynomial.h:40
static const unsigned int Value
Definition: polynomial.h:40
Definition: polynomial.h:38
static const unsigned int Value
Definition: polynomial.h:38