Polinomial template class
authoralpar
Tue, 16 May 2006 16:59:57 +0000
changeset 20863fc072264f77
parent 2085 1970a93dfaa8
child 2087 67258b5a057b
Polinomial template class
lemon/Makefile.am
lemon/polynomial.h
test/Makefile.am
test/polynomial_test.cc
     1.1 --- a/lemon/Makefile.am	Mon May 15 16:21:50 2006 +0000
     1.2 +++ b/lemon/Makefile.am	Tue May 16 16:59:57 2006 +0000
     1.3 @@ -28,49 +28,78 @@
     1.4  	bellman_ford.h \
     1.5  	bezier.h \
     1.6  	bfs.h \
     1.7 -	dfs.h \
     1.8  	bin_heap.h \
     1.9 +	bipartite_matching.h \
    1.10 +	bits/alteration_notifier.h \
    1.11 +	bits/array_map.h \
    1.12 +	bits/base_extender.h \
    1.13 +	bits/default_map.h \
    1.14 +	bits/edge_set_extender.h \
    1.15 +	bits/graph_adaptor_extender.h \
    1.16 +	bits/graph_extender.h \
    1.17 +	bits/invalid.h \
    1.18 +	bits/item_reader.h \
    1.19 +	bits/item_writer.h \
    1.20 +	bits/map_extender.h \
    1.21 +	bits/mingw32_rand.h \
    1.22 +	bits/mingw32_time.h \
    1.23 +	bits/traits.h \
    1.24 +	bits/utility.h \
    1.25 +	bits/vector_map.h \
    1.26  	bpugraph_adaptor.h \
    1.27 -	bipartite_matching.h \
    1.28  	bucket_heap.h \
    1.29  	color.h \
    1.30 +	concept_check.h \
    1.31 +	concept/bpugraph.h \
    1.32 +	concept/graph.h \
    1.33 +	concept/graph_component.h \
    1.34 +	concept/heap.h \
    1.35 +	concept/maps.h \
    1.36 +	concept/matrix_maps.h \
    1.37 +	concept/path.h
    1.38 +	concept/ugraph.h \
    1.39  	config.h \
    1.40  	counter.h \
    1.41 +	dag_shortest_path.h \
    1.42 +	dfs.h \
    1.43  	dijkstra.h \
    1.44  	dimacs.h \
    1.45 -	dag_shortest_path.h \
    1.46  	edge_set.h \
    1.47  	edmonds_karp.h \
    1.48 +	eps.h \
    1.49  	error.h \
    1.50 -	eps.h \
    1.51  	fib_heap.h \
    1.52  	floyd_warshall.h \
    1.53  	fredman_tarjan.h \
    1.54  	full_graph.h \
    1.55 +	graph_adaptor.h \
    1.56 +	graph_reader.h \
    1.57 +	graph_to_eps.h \
    1.58 +	graph_utils.h \
    1.59 +	graph_writer.h \
    1.60  	grid_ugraph.h \
    1.61 -	graph_adaptor.h \
    1.62 -	graph_utils.h \
    1.63 -	graph_to_eps.h \
    1.64  	hypercube_graph.h \
    1.65  	iterable_maps.h \
    1.66  	johnson.h \
    1.67  	kruskal.h \
    1.68 +	lemon_reader.h \
    1.69 +	lemon_writer.h \
    1.70  	list_graph.h \
    1.71  	lp.h \
    1.72  	lp_base.h \
    1.73  	lp_cplex.h \
    1.74  	lp_glpk.h \
    1.75  	lp_skeleton.h \
    1.76 +	map_iterator.h \
    1.77  	maps.h \
    1.78  	matrix_maps.h \
    1.79 -	map_iterator.h \
    1.80  	max_matching.h \
    1.81  	min_cost_arborescence.h \
    1.82  	min_cost_flow.h \
    1.83  	min_cut.h \
    1.84 -	suurballe.h \
    1.85 +	path.h \
    1.86 +	polynomial.h \
    1.87  	preflow.h \
    1.88 -	path.h \
    1.89  	prim.h \
    1.90  	radix_heap.h \
    1.91  	radix_sort.h \
    1.92 @@ -78,39 +107,11 @@
    1.93  	simann.h \
    1.94  	smart_graph.h \
    1.95  	sub_graph.h \
    1.96 +	suurballe.h \
    1.97  	tabu_search.h \
    1.98  	time_measure.h \
    1.99 +	tolerance.h \
   1.100  	topology.h \
   1.101  	ugraph_adaptor.h \
   1.102  	unionfind.h \
   1.103 -	xy.h \
   1.104 -	concept_check.h \
   1.105 -	lemon_reader.h \
   1.106 -	lemon_writer.h \
   1.107 -	graph_reader.h \
   1.108 -	graph_writer.h \
   1.109 -	tolerance.h \
   1.110 -	bits/alteration_notifier.h \
   1.111 -	bits/array_map.h \
   1.112 -	bits/base_extender.h \
   1.113 -	bits/default_map.h \
   1.114 -	bits/vector_map.h \
   1.115 -	bits/map_extender.h \
   1.116 -	bits/graph_extender.h \
   1.117 -	bits/graph_adaptor_extender.h \
   1.118 -	bits/edge_set_extender.h \
   1.119 -	bits/invalid.h \
   1.120 -	bits/item_reader.h \
   1.121 -	bits/item_writer.h \
   1.122 -	bits/traits.h \
   1.123 -	bits/utility.h \
   1.124 -	bits/mingw32_rand.h \
   1.125 -	bits/mingw32_time.h \
   1.126 -	concept/bpugraph.h \
   1.127 -	concept/graph.h \
   1.128 -	concept/graph_component.h \
   1.129 -	concept/ugraph.h \
   1.130 -	concept/matrix_maps.h \
   1.131 -	concept/maps.h \
   1.132 -	concept/heap.h \
   1.133 -	concept/path.h
   1.134 +	xy.h
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/lemon/polynomial.h	Tue May 16 16:59:57 2006 +0000
     2.3 @@ -0,0 +1,349 @@
     2.4 +/* -*- C++ -*-
     2.5 + *
     2.6 + * This file is a part of LEMON, a generic C++ optimization library
     2.7 + *
     2.8 + * Copyright (C) 2003-2006
     2.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    2.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    2.11 + *
    2.12 + * Permission to use, modify and distribute this software is granted
    2.13 + * provided that this copyright notice appears in all copies. For
    2.14 + * precise terms see the accompanying LICENSE file.
    2.15 + *
    2.16 + * This software is provided "AS IS" with no warranty of any kind,
    2.17 + * express or implied, and with no claim as to its suitability for any
    2.18 + * purpose.
    2.19 + *
    2.20 + */
    2.21 +
    2.22 +#ifndef LEMON_BEZIER_H
    2.23 +#define LEMON_BEZIER_H
    2.24 +
    2.25 +///\ingroup misc
    2.26 +///\file
    2.27 +///\brief A simple class implementing polynomials.
    2.28 +///
    2.29 +///\author Alpar Juttner
    2.30 +
    2.31 +#include<vector>
    2.32 +
    2.33 +namespace lemon {
    2.34 +
    2.35 +  /// \addtogroup misc
    2.36 +  /// @{
    2.37 +
    2.38 +  ///Simple polinomial class
    2.39 +
    2.40 +  ///This class implements a polynomial where the coefficients are of
    2.41 +  ///type \c T.
    2.42 +  ///
    2.43 +  ///The coefficients are stored in an std::vector.
    2.44 +  template<class T>
    2.45 +  class Polynomial
    2.46 +  {
    2.47 +    std::vector<T> _coeff;
    2.48 +  public:
    2.49 +    ///Construct a polynomial of degree \c d.
    2.50 +    explicit Polynomial(int d=0) : _coeff(d+1) {}
    2.51 +    ///\e
    2.52 +    template<class U> Polynomial(const U &u) : _coeff(1,u) {}
    2.53 +    ///\e
    2.54 +    template<class U> Polynomial(const Polynomial<U> &u) : _coeff(u.deg()+1)
    2.55 +    {
    2.56 +      for(int i=0;i<(int)_coeff.size();i++) _coeff[i]=u[i];
    2.57 +    }
    2.58 +    ///Query the degree of the polynomial.
    2.59 +    
    2.60 +    ///Query the degree of the polynomial.
    2.61 +    ///\warning This number differs from real degree of the polinomial if
    2.62 +    ///the coefficient of highest degree is 0.
    2.63 +    int deg() const { return _coeff.size()-1; }
    2.64 +    ///Set the degree of the polynomial.
    2.65 +
    2.66 +    ///Set the degree of the polynomial. In fact it resizes the
    2.67 +    ///coefficient vector.
    2.68 +    void deg(int d) { _coeff.resize(d+1);}
    2.69 +
    2.70 +    ///Returns (as a reference) the coefficient of degree \c d.
    2.71 +    typename std::vector<T>::reference operator[](int d) { return _coeff[d]; }
    2.72 +    ///Returns (as a const reference) the coefficient of degree \c d.
    2.73 +    typename std::vector<T>::const_reference
    2.74 +    operator[](int d) const {return _coeff[d];}
    2.75 +    
    2.76 +    ///Substitute the value u into the polinomial.
    2.77 +
    2.78 +    ///Substitute the value u into the polinomial.
    2.79 +    ///The calculation will be done using type \c R.
    2.80 +    ///The following examples shows the usage of the template parameter \c R.
    2.81 +    ///\code
    2.82 +    ///  Polynomial<xy<double> > line(1);
    2.83 +    ///  line[0]=xy<double>(12,25);
    2.84 +    ///  line[1]=xy<double>(2,7);
    2.85 +    ///  ...
    2.86 +    ///  xy<double> d = line.subst<xy<double> >(23.2);
    2.87 +    ///\endcode
    2.88 +    ///
    2.89 +    ///\code
    2.90 +    ///  Polynomial<double> p;
    2.91 +    ///  Polynomial<double> q;
    2.92 +    ///  ...
    2.93 +    ///  Polynomial<double> s = p.subst<Polynomial<double> >(q);
    2.94 +    ///\endcode
    2.95 +    template<class R,class U>
    2.96 +    R subst(const U &u) const
    2.97 +    {
    2.98 +      typename std::vector<T>::const_reverse_iterator i=_coeff.rbegin();
    2.99 +      R v=*i;
   2.100 +      for(++i;i!=_coeff.rend();++i) v=v*u+*i;
   2.101 +      return v;
   2.102 +    }
   2.103 +    ///Substitute the value u into the polinomial.
   2.104 +
   2.105 +    ///Substitute the value u into the polinomial.
   2.106 +    ///The calculation will be done using type \c T
   2.107 +    ///(i.e. using the type of the coefficients.)
   2.108 +    template<class U>
   2.109 +    T operator()(const U &u) const 
   2.110 +    {
   2.111 +      return subst<T>(u);
   2.112 +    }
   2.113 +    
   2.114 +    ///Derivate the polynomial (in place)
   2.115 +    Polynomial &derivateMyself()
   2.116 +    {
   2.117 +      for(int i=1;i<(int)_coeff.size();i++) _coeff[i-1]=i*_coeff[i];
   2.118 +      _coeff.pop_back();
   2.119 +      return *this;
   2.120 +    }
   2.121 +    
   2.122 +    ///Return the derivate of the polynomial
   2.123 +    Polynomial derivate() const
   2.124 +    {
   2.125 +      Polynomial tmp(deg()-1);
   2.126 +      for(int i=1;i<(int)_coeff.size();i++) tmp[i-1]=i*_coeff[i];
   2.127 +      return tmp;
   2.128 +    }
   2.129 +
   2.130 +    ///Integrate the polynomial (in place)
   2.131 +    Polynomial &integrateMyself()
   2.132 +    {
   2.133 +      _coeff.push_back(T());
   2.134 +      for(int i=_coeff.size()-1;i>=0;i--) _coeff[i]=_coeff[i-1]/i;
   2.135 +      _coeff[0]=0;
   2.136 +      return *this;
   2.137 +    }
   2.138 +    
   2.139 +    ///Return the integrate of the polynomial
   2.140 +    Polynomial integrate() const
   2.141 +    {
   2.142 +      Polynomial tmp(deg()+1);
   2.143 +      tmp[0]=0;
   2.144 +      for(int i=0;i<(int)_coeff.size();i++) tmp[i+1]=_coeff[i]/(i+1);
   2.145 +      return tmp;
   2.146 +    }
   2.147 +
   2.148 +    ///\e
   2.149 +    template<class U>
   2.150 +    Polynomial &operator+=(const Polynomial<U> &p)
   2.151 +    {
   2.152 +      if(p.deg()>deg()) _coeff.resize(p.deg()+1);
   2.153 +      for(int i=0;i<=(int)std::min(deg(),p.deg());i++)
   2.154 +	_coeff[i]+=p[i];
   2.155 +      return *this;
   2.156 +    }
   2.157 +    ///\e
   2.158 +    template<class U>
   2.159 +    Polynomial &operator-=(const Polynomial<U> &p)
   2.160 +    {
   2.161 +      if(p.deg()>deg()) _coeff.resize(p.deg()+1);
   2.162 +      for(int i=0;i<=std::min(deg(),p.deg());i++) _coeff[i]-=p[i];
   2.163 +      return *this;
   2.164 +    }
   2.165 +    ///\e
   2.166 +    template<class U>
   2.167 +    Polynomial &operator+=(const U &u)
   2.168 +    {
   2.169 +      _coeff[0]+=u;
   2.170 +      return *this;
   2.171 +    }
   2.172 +    ///\e
   2.173 +    template<class U>
   2.174 +    Polynomial &operator-=(const U &u)
   2.175 +    {
   2.176 +      _coeff[0]+=u;
   2.177 +      return *this;
   2.178 +    }
   2.179 +    ///\e
   2.180 +    template<class U>
   2.181 +    Polynomial &operator*=(const U &u)
   2.182 +    {
   2.183 +      for(typename std::vector<T>::iterator i=_coeff.begin();i!=_coeff.end();++i)
   2.184 +	*i*=u;
   2.185 +      return *this;
   2.186 +    }
   2.187 +    ///\e
   2.188 +    template<class U>
   2.189 +    Polynomial &operator/=(const U &u)
   2.190 +    {
   2.191 +      for(typename std::vector<T>::iterator i=_coeff.begin();i!=_coeff.end();++i)
   2.192 +	*i/=u;
   2.193 +      return *this;
   2.194 +    }
   2.195 +
   2.196 +  };
   2.197 +  
   2.198 +  ///Equality comparison
   2.199 +
   2.200 +  ///\relates Polynomial
   2.201 +  ///\warning Two polynomials are defined to be unequal if their degrees differ,
   2.202 +  ///even if the non-zero coefficients are the same.
   2.203 +  template<class U,class V>
   2.204 +  bool operator==(const Polynomial<U> &u,const Polynomial<V> &v)
   2.205 +  {
   2.206 +    if(u.deg()!=v.deg()) return false;
   2.207 +    for(int i=0;i<=u.deg();i++) if(u[i]!=v[i]) return false;
   2.208 +    return true;
   2.209 +  }
   2.210 +
   2.211 +  ///Non-equality comparison
   2.212 +
   2.213 +  ///\relates Polynomial
   2.214 +  ///\warning Two polynomials are defined to be unequal if their degrees differ,
   2.215 +  ///even if the non-zero coefficients are the same.
   2.216 +  template<class U,class V>
   2.217 +  bool operator!=(const Polynomial<U> &u,const Polynomial<V> &v)
   2.218 +  {
   2.219 +    return !(u==v);
   2.220 +  }
   2.221 +
   2.222 +  ///\e
   2.223 +
   2.224 +  ///\relates Polynomial
   2.225 +  ///
   2.226 +  template<class U,class V>
   2.227 +  Polynomial<U> operator+(const Polynomial<U> &u,const Polynomial<V> &v)
   2.228 +  {
   2.229 +    Polynomial<U> tmp=u;
   2.230 +    tmp+=v;
   2.231 +    return tmp;
   2.232 +  }
   2.233 +
   2.234 +  ///\e
   2.235 +
   2.236 +  ///\relates Polynomial
   2.237 +  ///
   2.238 +  template<class U,class V>
   2.239 +  Polynomial<U> operator-(const Polynomial<U> &u,const Polynomial<V> &v)
   2.240 +  {
   2.241 +    Polynomial<U> tmp=u;
   2.242 +    tmp-=v;
   2.243 +    return tmp;
   2.244 +  }
   2.245 +
   2.246 +  ///\e
   2.247 +
   2.248 +  ///\relates Polynomial
   2.249 +  ///
   2.250 +  template<class U,class V>
   2.251 +  Polynomial<U> operator*(const Polynomial<U> &u,const Polynomial<V> &v)
   2.252 +  {
   2.253 +    Polynomial<U> tmp(u.deg()+v.deg());
   2.254 +    for(int i=0;i<=v.deg();i++)
   2.255 +      for(int j=0;j<=u.deg();j++)
   2.256 +	tmp[i+j]+=v[i]*u[j];
   2.257 +    return tmp;
   2.258 +  }
   2.259 +  ///\e
   2.260 +
   2.261 +  ///\relates Polynomial
   2.262 +  ///
   2.263 +  template<class U,class V>
   2.264 +  Polynomial<U> operator+(const Polynomial<U> &u,const V &v)
   2.265 +  {
   2.266 +    Polynomial<U> tmp=u;
   2.267 +    tmp+=v;
   2.268 +    return tmp;
   2.269 +  }
   2.270 +  ///\e
   2.271 +
   2.272 +  ///\relates Polynomial
   2.273 +  ///
   2.274 +  template<class U,class V>
   2.275 +  Polynomial<U> operator+(const V &v,const Polynomial<U> &u)
   2.276 +  {
   2.277 +    Polynomial<U> tmp=u;
   2.278 +    tmp+=v;
   2.279 +    return tmp;
   2.280 +  }
   2.281 +  ///\e
   2.282 +
   2.283 +  ///\relates Polynomial
   2.284 +  ///
   2.285 +  template<class U,class V>
   2.286 +  Polynomial<U> operator-(const Polynomial<U> &u,const V &v)
   2.287 +  {
   2.288 +    Polynomial<U> tmp=u;
   2.289 +    tmp-=v;
   2.290 +    return tmp;
   2.291 +  }
   2.292 +  ///\e
   2.293 +
   2.294 +  ///\relates Polynomial
   2.295 +  ///
   2.296 +  template<class U>
   2.297 +  Polynomial<U> operator-(const Polynomial<U> &u)
   2.298 +  {
   2.299 +    Polynomial<U> tmp(u.deg());
   2.300 +    for(int i=0;i<=u.deg();i++) tmp[i]=-u[i];
   2.301 +    return tmp;
   2.302 +  }
   2.303 +  ///\e
   2.304 +
   2.305 +  ///\relates Polynomial
   2.306 +  ///
   2.307 +  template<class U,class V>
   2.308 +  Polynomial<U> operator-(const V &v,const Polynomial<U> &u)
   2.309 +  {
   2.310 +    Polynomial<U> tmp=-u;
   2.311 +    tmp+=v;
   2.312 +    return tmp;
   2.313 +  }
   2.314 +  ///\e
   2.315 +
   2.316 +  ///\relates Polynomial
   2.317 +  ///
   2.318 +  template<class U,class V>
   2.319 +  Polynomial<U> operator*(const Polynomial<U> &u,const V &v)
   2.320 +  {
   2.321 +    Polynomial<U> tmp=u;
   2.322 +    tmp*=v;
   2.323 +    return tmp;
   2.324 +  }
   2.325 +  ///\e
   2.326 +
   2.327 +  ///\relates Polynomial
   2.328 +  ///
   2.329 +  template<class U,class V>
   2.330 +  Polynomial<U> operator*(const V &v,const Polynomial<U> &u)
   2.331 +  {
   2.332 +    Polynomial<U> tmp=u;
   2.333 +    tmp*=v;
   2.334 +    return tmp;
   2.335 +  }
   2.336 +  ///\e
   2.337 +
   2.338 +  ///\relates Polynomial
   2.339 +  ///
   2.340 +  template<class U,class V>
   2.341 +  Polynomial<U> operator/(const Polynomial<U> &u,const V &v)
   2.342 +  {
   2.343 +    Polynomial<U> tmp=u;
   2.344 +    tmp/=v;
   2.345 +    return tmp;
   2.346 +  }
   2.347 +    
   2.348 +  /// @}
   2.349 +
   2.350 +} //END OF NAMESPACE LEMON
   2.351 +
   2.352 +#endif // LEMON_POLYNOMIAL_H
     3.1 --- a/test/Makefile.am	Mon May 15 16:21:50 2006 +0000
     3.2 +++ b/test/Makefile.am	Tue May 16 16:59:57 2006 +0000
     3.3 @@ -20,27 +20,28 @@
     3.4  	dfs_test \
     3.5  	dijkstra_test \
     3.6  	edge_set_test \
     3.7 +	graph_adaptor_test \
     3.8  	graph_test \
     3.9 -	graph_adaptor_test \
    3.10  	graph_utils_test \
    3.11 +	heap_test \
    3.12  	kruskal_test \
    3.13  	maps_test \
    3.14  	matrix_maps_test \
    3.15  	max_matching_test \
    3.16  	min_cost_flow_test \
    3.17 -	suurballe_test \
    3.18  	path_test \
    3.19 +	polynomial_test \
    3.20  	preflow_test \
    3.21  	radix_sort_test \
    3.22  	refptr_test \
    3.23 +	simann_test \
    3.24 +	suurballe_test \
    3.25  	test_tools_fail \
    3.26  	test_tools_pass \
    3.27  	time_measure_test \
    3.28 +	ugraph_test \
    3.29  	unionfind_test \
    3.30 -	ugraph_test \
    3.31 -	xy_test \
    3.32 -	heap_test \
    3.33 -	simann_test
    3.34 +	xy_test
    3.35  
    3.36  if HAVE_GLPK
    3.37  check_PROGRAMS += lp_test
    3.38 @@ -61,27 +62,28 @@
    3.39  dfs_test_SOURCES = dfs_test.cc
    3.40  dijkstra_test_SOURCES = dijkstra_test.cc
    3.41  edge_set_test_SOURCES = edge_set_test.cc
    3.42 +graph_adaptor_test_SOURCES = graph_adaptor_test.cc
    3.43  graph_test_SOURCES = graph_test.cc
    3.44  graph_utils_test_SOURCES = graph_utils_test.cc
    3.45 -graph_adaptor_test_SOURCES = graph_adaptor_test.cc
    3.46 +heap_test_SOURCES = heap_test.cc
    3.47  kruskal_test_SOURCES = kruskal_test.cc
    3.48  maps_test_SOURCES = maps_test.cc
    3.49  matrix_maps_test_SOURCES = matrix_maps_test.cc
    3.50 +max_matching_test_SOURCES = max_matching_test.cc
    3.51  min_cost_flow_test_SOURCES = min_cost_flow_test.cc
    3.52 -max_matching_test_SOURCES = max_matching_test.cc
    3.53 -suurballe_test_SOURCES = suurballe_test.cc
    3.54  path_test_SOURCES = path_test.cc
    3.55 +polynomial_test_SOURCES = polynomial_test.cc
    3.56 +preflow_test_SOURCES = preflow_test.cc
    3.57  radix_sort_test_SOURCES = radix_sort_test.cc
    3.58  refptr_test_SOURCES = refptr_test.cc
    3.59 -preflow_test_SOURCES = preflow_test.cc
    3.60 -time_measure_test_SOURCES = time_measure_test.cc
    3.61 +simann_test_SOURCES = simann_test.cc
    3.62 +suurballe_test_SOURCES = suurballe_test.cc
    3.63  test_tools_fail_SOURCES = test_tools_fail.cc
    3.64  test_tools_pass_SOURCES = test_tools_pass.cc
    3.65 +time_measure_test_SOURCES = time_measure_test.cc
    3.66 +ugraph_test_SOURCES = ugraph_test.cc
    3.67  unionfind_test_SOURCES = unionfind_test.cc
    3.68  xy_test_SOURCES = xy_test.cc
    3.69 -ugraph_test_SOURCES = ugraph_test.cc
    3.70 -heap_test_SOURCES = heap_test.cc
    3.71 -simann_test_SOURCES = simann_test.cc
    3.72  
    3.73  lp_test_SOURCES = lp_test.cc
    3.74  lp_test_CXXFLAGS = $(GLPK_CFLAGS) $(CPLEX_CFLAGS)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/polynomial_test.cc	Tue May 16 16:59:57 2006 +0000
     4.3 @@ -0,0 +1,81 @@
     4.4 +/* -*- C++ -*-
     4.5 + *
     4.6 + * This file is a part of LEMON, a generic C++ optimization library
     4.7 + *
     4.8 + * Copyright (C) 2003-2006
     4.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    4.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    4.11 + *
    4.12 + * Permission to use, modify and distribute this software is granted
    4.13 + * provided that this copyright notice appears in all copies. For
    4.14 + * precise terms see the accompanying LICENSE file.
    4.15 + *
    4.16 + * This software is provided "AS IS" with no warranty of any kind,
    4.17 + * express or implied, and with no claim as to its suitability for any
    4.18 + * purpose.
    4.19 + *
    4.20 + */
    4.21 +
    4.22 +#include <lemon/polynomial.h>
    4.23 +#include <lemon/xy.h>
    4.24 +#include <iostream>
    4.25 +#include "test_tools.h"
    4.26 +
    4.27 +using namespace std;
    4.28 +using namespace lemon;
    4.29 +int main()
    4.30 +{
    4.31 +  Polynomial<int> pi(5);
    4.32 +  check(pi.deg()==5,"Something is wrong here.");
    4.33 +  pi[0]=12;
    4.34 +  pi[5]=3;
    4.35 +  pi[2]=7;
    4.36 +  check(pi[1]==0,"Uninitialized elements should be(?) zero.");
    4.37 +  {
    4.38 +    Polynomial<double> pd=pi;
    4.39 +    check(pd.deg()==5,"Something is wrong here.");
    4.40 +    check(pd[0]==12,"Something is wrong here.");
    4.41 +    check(pd[5]==3,"Something is wrong here.");
    4.42 +
    4.43 +  }
    4.44 +
    4.45 +  Polynomial<double> pd;
    4.46 +  pd=pi;
    4.47 +  check(pd.deg()==5,"Something is wrong here.");
    4.48 +  check(pd[0]==12,"Something is wrong here.");
    4.49 +  check(pd[5]==3,"Something is wrong here.");
    4.50 +
    4.51 +  check(pd(0)==12,"Something is wrong here.");
    4.52 +  check(pd(1)==22,"Something is wrong here.");
    4.53 +  check(pd(2)==136,"Something is wrong here.");
    4.54 +
    4.55 +  check((pd*pi).deg()==10,"Something is wrong here.");
    4.56 +  check((pd*pi)[10]==9,"Something is wrong here.");
    4.57 +  check((pd*pi)[7]==42,"Something is wrong here.");
    4.58 +
    4.59 +  Polynomial<double> pd2=pd+pi;
    4.60 +  check(pd2[5]==6,"Something is wrong here.");
    4.61 +  pd2+=pd;
    4.62 +  pd2+=pi;
    4.63 +  check(pd2[5]==12,"Something is wrong here.");
    4.64 +  pd2-=pd;
    4.65 +  pd2-=pi;
    4.66 +  check(pd2[5]==6,"Something is wrong here.");
    4.67 +  check((pd-pi)[5]==0,"Something is wrong here.");
    4.68 +
    4.69 +  Polynomial<double> pdd=pd.derivate();
    4.70 +  pd.derivateMyself();
    4.71 +  check(pdd==pd,"Something is wrong here.");
    4.72 +  check(pd.deg()==4,"Something is wrong here.");
    4.73 +  check(pd[4]==15,"Something is wrong here.");
    4.74 +
    4.75 +  
    4.76 +  Polynomial<double> pdi=pd.integrate();
    4.77 +  pd.integrateMyself();
    4.78 +  check(pdi==pd,"Something is wrong here.");
    4.79 +  check(pd.deg()==5,"Something is wrong here.");
    4.80 +  check(pd[5]==3,"Something is wrong here.");
    4.81 +  check(pd[0]==0,"Something is wrong here.");
    4.82 +  
    4.83 +  
    4.84 +}