75 #ifndef SLIP_POLYNOMIAL_HPP
76 #define SLIP_POLYNOMIAL_HPP
94 #include <boost/serialization/access.hpp>
95 #include <boost/serialization/split_member.hpp>
96 #include <boost/serialization/string.hpp>
97 #include <boost/serialization/complex.hpp>
98 #include <boost/serialization/version.hpp>
103 template <
typename T>
106 template <
typename T>
119 bool operator<(const Polynomial<T>& x,
127 bool operator<=(const Polynomial<T>& x,
148 template <
typename T>
216 template<
typename InputIterator>
220 assert(last > first);
221 array_->resize(last - first);
318 friend std::ostream& operator<< <>(std::ostream & out,
353 std::fill_n(this->
begin(),this->
size(),value);
358 (*array_)[0] = value;
382 template<
typename InputIterator>
583 const std::size_t
order);
620 template<
typename InputIterator,
typename OutputIterator>
622 OutputIterator result)
const
624 for(;first != last; first++, result++)
641 std::string
name()
const;
696 std::size_t count_zeros(
const std::size_t
size);
699 template<
class Archive>
700 void save(Archive & ar,
const unsigned int version)
const
704 template<
class Archive>
705 void load(Archive & ar,
const unsigned int version)
709 BOOST_SERIALIZATION_SPLIT_MEMBER();
723 Polynomial<T>
operator+(
const Polynomial<T>& V1,
724 const Polynomial<T>& V2);
734 Polynomial<T>
operator+(
const Polynomial<T>& V,
746 const Polynomial<T>& V);
758 Polynomial<T>
operator-(
const Polynomial<T>& V1,
759 const Polynomial<T>& V2);
769 Polynomial<T>
operator-(
const Polynomial<T>& V,
781 const Polynomial<T>& V);
792 Polynomial<T>
operator*(
const Polynomial<T>& V1,
793 const Polynomial<T>& V2);
803 Polynomial<T>
operator*(
const Polynomial<T>& V,
815 const Polynomial<T>& V);
827 Polynomial<T>
operator/(
const Polynomial<T>& V,
837 array_(new slip::
Array<T>())
843 array_(new slip::
Array<T>(n+1))
850 array_(new slip::
Array<T>(n+1,val))
857 array_(new slip::
Array<T>(n+1,val))
865 array_(new slip::
Array<T>((*rhs.array_)))
874 *array_ = *(rhs.array_);
891 array_->operator=(val);
900 return array_->begin();
907 return array_->end();
914 return array_->begin();
921 return array_->end();
929 return array_->rbegin();
936 return array_->rend();
943 return array_->rbegin();
950 return array_->rend();
954 template <
typename T>
964 out<<
" + "<<v[i]<<
" x^"<<i;
1000 return (*array_)(i);
1003 template<
typename T>
1009 if(this->degree()!=0)
1012 array_->resize(this->degree());
1015 (*this)[i-1] = T(i)*Ptmp[i];
1027 template<
typename T>
1032 array_->resize(this->size()+1);
1036 (*this)[i] = Ptmp[i-1] / i;
1042 template<
typename T>
1047 const std::size_t order)
1052 std::size_t coeff_size = order + 1;
1055 double chi_square = 0.0;
1056 array_->resize(coeff_size);
1058 chi_square = slip::svd_least_square<iterator,iterator,iterator>(xi_first,xi_last,yi_first,this->begin(),this->end(),power_basis);
1063 template<
typename T>
1068 std::size_t degree = this->degree();
1069 assert(degree >= 2);
1072 T norm = (*array_)[degree];
1073 for(std::size_t k = 0, j = (tmp.
cols() - 1); k < degree; j--,++k)
1075 tmp[0][j] = -(*array_)[k] / norm;
1081 for(std::size_t k = 1; k < degree; ++k)
1091 template<
typename T>
1102 template<
typename T>
1110 template<
typename T>
1115 template<
typename T>
1120 template<
typename T>
1123 if(this->size() != 0)
1124 return (this->size() - 1);
1129 template<
typename T>
1133 return this->degree();
1136 template<
typename T>
1143 template<
typename T>
1147 template<
typename T>
1151 array_->swap(*(V.array_));
1155 template<
typename T>
1161 *array_->
begin()+=val;
1165 template<
typename T>
1171 *array_->
begin()-=val;
1175 template<
typename T>
1179 std::transform(array_->begin(),array_->end(),array_->begin(),std::bind2nd(std::multiplies<T>(),val));
1184 template<
typename T>
1188 std::transform(array_->begin(),array_->end(),array_->begin(),std::bind2nd(std::multiplies<T>(),T(1)/val));
1194 template<
typename T>
1199 std::transform(array_->begin(),array_->end(),tmp.
begin(),std::negate<T>());
1203 template<
typename T>
1207 std::size_t max_size =
std::max(this->size(),rhs.
size());
1209 array_->resize(max_size);
1211 if(max_size == tmp.size())
1213 std::transform((*(rhs.array_)).begin(),(*(rhs.array_)).end(),tmp.begin(),this->begin(),std::plus<T>());
1218 std::transform(tmp.begin(),tmp.end(),(*(rhs.array_)).begin(),this->begin(),std::plus<T>());
1226 template<
typename T>
1230 std::size_t max_size =
std::max(this->size(),rhs.
size());
1232 array_->resize(max_size);
1233 if(max_size == tmp.size())
1235 std::transform(tmp.begin(),tmp.begin()+rhs.
size(),rhs.
begin(),this->begin(),std::minus<T>());
1240 std::transform(tmp.begin(),tmp.end(),(*(rhs.array_)).begin(),this->begin(),std::minus<T>());
1242 std::transform(this->begin()+tmp.size(),this->end(),this->begin()+tmp.size(),std::negate<T>());
1250 template<
typename T>
1254 assert(rhs.
size() > 0);
1255 assert(this->size() > 0);
1272 template<
typename T>
1278 std::size_t new_size = size;
1279 if((*
this)[new_size-1] == T(0))
1281 return count_zeros(new_size-1);
1294 template<
typename T>
1296 void Polynomial<T>::kill_zeros()
1298 std::size_t new_size = this->count_zeros(this->size());
1299 if(new_size != this->size())
1302 (this->array_)->resize(this->count_zeros(new_size));
1303 std::copy(tmp.begin(),tmp.begin()+new_size,this->begin());
1310 template<
typename T>
1320 template<
typename T>
1330 template<
typename T>
1338 template<
typename T>
1348 template<
typename T>
1358 template<
typename T>
1366 template<
typename T>
1388 template<
typename T>
1398 template<
typename T>
1408 template<
typename T>
1420 template<
typename T>
1425 return *(x.array_) == *(y.array_);
1428 template<
typename T>
1438 template<
typename T>
1440 bool operator<(const Polynomial<T>& x,
1443 return *(x.array_) < *(y.array_);
1446 template<
typename T>
1454 template<
typename T>
1456 bool operator<=(const Polynomial<T>& x,
1462 template<
typename T>
1472 #endif //SLIP_POLYNOMIAL_HPP
std::string name() const
Returns the name of the class.
void fill(const T *value)
Fills the container range [begin(),begin()+size()) with a copy of the value array.
bool operator!=(const Array< T > &x, const Array< T > &y)
self & operator+=(const T &val)
Add val to each element of the Polynomial.
friend class boost::serialization::access
self & derivative()
Returns the derivative the polynomial.
const Polynomial< T > const_self
size_type max_size() const
Returns the maximal size (number of elements) in the Polynomial.
double fit(iterator xi_first, iterator xi_last, iterator yi_first, const std::size_t order)
Return a polynomial P(X) of degree order that minimizes sum_i (P(x(i)) - y(i))^2 to best fit the data...
Color< T > operator+(const Color< T > &V1, const Color< T > &V2)
iterator begin()
Returns a read/write iterator that points to the first element in the Polynomial. Iteration is done i...
size_type cols() const
Returns the number of columns (second dimension size) in the Matrix.
void fill(InputIterator first, InputIterator last)
Fills the container range [begin(),begin()+size()) with a copy of the range [first,last)
reverse_iterator rend()
Returns a read/write reverse iterator that points to one before the first element in the Polynomial...
slip::Vector< std::complex< T > > roots() const
Computes the roots of the polynomial.
const value_type * const_pointer
bool operator>(const Array< T > &x, const Array< T > &y)
self & operator=(const self &rhs)
Assign a Polynomial.
Provides a class to manipulate 1d dynamic and generic arrays.
reference operator[](const size_type i)
Subscript access to the data contained in the Polynomial.
void eigen(const Matrix1 &A, Vector1 &E, bool sort=false)
Eigen Values Computation for non symmetric matrix.
Provides some polynomial algorithms.
self & operator-=(const T &val)
bool empty() const
Returns true if the Polynomial is empty. (Thus size() == 0)
const_pointer const_iterator
ptrdiff_t difference_type
Provides some statistics algorithms.
Provides some algorithms to computes eigenvalues and eigenvectors.
size_type degree() const
Returns the degree of the Polynomial.
void copy(_II first, _II last, _OI output_first)
Copy algorithm optimized for slip iterators.
Polynomial()
Constructs a Polynomial.
size_type order() const
Returns the order of the Polynomial.
Numerical Polynomial class. This container statisfies the RandomAccessContainer concepts of the STL T...
Numerical Vector class. This container statisfies the RandomAccessContainer concepts of the Standard ...
self & operator*=(const T &val)
reverse_iterator rbegin()
Returns a read/write reverse iterator that points to the last element in the Polynomial. Iteration is done in reverse element order.
Polynomial(InputIterator first, InputIterator last)
Contructs a Polynomial from a range.
T eval_horner(RandomAccessIterator first, RandomAccessIterator last, const T &x)
Returns the evaluation of the polynomial given throw the range [first,last) at x using the horner sch...
std::random_access_iterator_tag iterator_category
std::ostream & operator<<(std::ostream &out, const Array< T > &a)
void fill(const T &value)
Fills the container range [begin(),begin()+size()) with copies of value.
std::reverse_iterator< iterator > reverse_iterator
size_type rows() const
Returns the number of rows (first dimension size) in the Matrix.
std::reverse_iterator< const_iterator > const_reverse_iterator
const value_type & const_reference
void multi_evaluate(InputIterator first, InputIterator last, OutputIterator result) const
Returns the evaluation of the polynomial at each element of the range [first,last).
Provides some convolution algorithms.
reference operator()(const size_type i)
Subscript access to the data contained in the Polynomial.
size_type size() const
Returns the number of elements in the Polynomial.
T evaluate(const T &x) const
Returns the evaluation of the polynomial at x.
slip::Matrix< T > companion() const
Computes the companion matrix corresponding to polynomial coefficients.
void swap(self &M)
Swaps data with another Polynomial.
Provides a class to manipulate Numerical Matrix.
~Polynomial()
Destructor of the Polynomial.
void full_convolution(SrcIter first, SrcIter last, KernelIter kernel_first, KernelIter kernel_last, ResIter result)
Computes the full convolution of signal by a 1d-kernel.
bool operator==(const Array< T > &x, const Array< T > &y)
This is a linear (one-dimensional) dynamic template container. This container statisfies the RandomAc...
Provides some linear least square algorithms.
Color< T > operator*(const Color< T > &V1, const Color< T > &V2)
iterator end()
Returns a read/write iterator that points one past the last element in the Polynomial. Iteration is done in ordinary element order.
Provides a class to manipulate numerical vectors.
T & max(const GrayscaleImage< T > &M1)
Returns the max element of a GrayscaleImage.
Color< T > operator/(const Color< T > &V1, const Color< T > &V2)
self & integral()
Returns the integral of the polynomial. The constant of integrationis set to zero.
bool operator>=(const Array< T > &x, const Array< T > &y)
Color< T > operator-(const Color< T > &V1, const Color< T > &V2)
self & operator/=(const T &val)