12 #ifndef ECL_GEOMETRY_POLYNOMIALS_HPP_ 13 #define ECL_GEOMETRY_POLYNOMIALS_HPP_ 23 #include <ecl/config/macros.hpp> 24 #include <ecl/concepts/macros.hpp> 25 #include <ecl/concepts/streams.hpp> 26 #include <ecl/containers/array.hpp> 27 #include <ecl/exceptions/standard_exception.hpp> 28 #include <ecl/errors/compile_time_assert.hpp> 29 #include <ecl/utilities/blueprints.hpp> 30 #include <ecl/formatters/floats.hpp> 44 template <
unsigned int N>
class Polynomial;
61 template<
unsigned int N>
62 class BluePrintFactory< Polynomial<N> > {
109 template <
unsigned int N>
115 typedef Array<double,N+1> Coefficients;
127 Polynomial() : coeff(Coefficients::Constant(0.0)) {};
153 template<
typename Derived>
154 Polynomial(
const BluePrint< Derived > &blueprint) {
155 blueprint.implementApply(*
this);
157 virtual ~Polynomial() {}
176 void shift_horizontal(
const double &shift);
189 Polynomial<N-1> derivative()
const {
190 Polynomial<N-1> derivative_polynomial;
191 typename Polynomial<N-1>::Coefficients &derivative_coefficients = derivative_polynomial.coefficients();
192 for (
unsigned int i = 0; i < N; ++i ) {
193 derivative_coefficients[i] = (i+1)*coeff[i+1];
195 return derivative_polynomial;
205 double derivative(
const double &x)
const;
214 double dderivative(
const double &x)
const;
233 Coefficients& coefficients() {
return coeff; };
239 const Coefficients& coefficients()
const {
return coeff; };
249 double operator ()(
const double &x)
const;
263 template <
typename OutputStream,
unsigned int Degree>
264 friend OutputStream& operator << (OutputStream &ostream, const Polynomial<Degree> &polynomial);
284 typedef Array<double,1> Coefficients;
295 Polynomial() : coeff(Coefficients::Constant(0.0)) {};
296 virtual ~Polynomial() {};
307 void shift_horizontal(
const double& ) {};
319 Polynomial<0> derivative()
const {
320 return Polynomial<0>();
329 double derivative(
const double& )
const {
339 double dderivative(
const double& )
const {
360 Coefficients& coefficients() {
return coeff; };
366 const Coefficients& coefficients()
const {
return coeff; };
375 double operator ()(
const double& )
const {
395 template <
unsigned int N>
402 for (
unsigned int i = 0; i < N; ++i ) {
405 for ( iter = (pascals_triangle.
begin(i)+1); iter != pascals_triangle.
end(i); ++iter ) {
406 coeff[i] += (*iter)*tmp*coeff[j];
415 template <
unsigned int N>
419 double value = coeff[0];
420 for (
unsigned int i = 1; i <= N; ++i ) {
421 value += coeff[i]*tmp;
426 template <
unsigned int N>
430 return derivative()(x);
436 template <
unsigned int N>
440 return derivative().derivative()(x);
450 template <
typename OutputStream,
unsigned int Degree>
451 OutputStream& operator << (OutputStream &ostream, const Polynomial<Degree> &polynomial)
458 ostream << format(polynomial.coeff[0]);
459 for (
unsigned int i = 1; i <= Degree; ++i) {
460 ostream <<
" + " << format(polynomial.coeff[i]) <<
"x^" << i;
471 namespace blueprints {
507 LinearInterpolation(
const double x_i,
const double y_i,
const double x_f,
const double y_f) :
514 virtual ~LinearInterpolation() {}
534 double x_initial, y_initial;
535 double x_final, y_final;
569 LinearPointSlopeForm(
const double x_f,
const double y_f,
const double s) :
575 virtual ~LinearPointSlopeForm() {}
596 double x_final, y_final;
638 CubicDerivativeInterpolation(
const double x_i,
const double y_i,
const double ydot_i,
639 const double x_f,
const double y_f,
const double ydot_f) :
642 ydot_initial(ydot_i),
648 virtual ~CubicDerivativeInterpolation() {}
667 double x_initial, y_initial, ydot_initial;
668 double x_final, y_final, ydot_final;
708 CubicSecondDerivativeInterpolation(
const double x_i,
const double y_i,
const double yddot_i,
709 const double x_f,
const double y_f,
const double yddot_f) :
712 yddot_initial(yddot_i),
718 virtual ~CubicSecondDerivativeInterpolation() {}
727 base_type instantiate();
735 void apply(base_type& polynomial)
const;
738 double x_initial, y_initial, yddot_initial;
739 double x_final, y_final, yddot_final;
784 QuinticInterpolation(
const double x_i,
const double y_i,
const double ydot_i,
const double yddot_i,
785 const double x_f,
const double y_f,
const double ydot_f,
const double yddot_f) :
788 ydot_initial(ydot_i),
789 yddot_initial(yddot_i),
796 virtual ~QuinticInterpolation() {}
816 double x_initial, y_initial, ydot_initial, yddot_initial;
817 double x_final, y_final, ydot_final, yddot_final;
861 static LinearInterpolation Interpolation(
const double x_i,
const double y_i,
const double x_f,
const double y_f);
870 static LinearPointSlopeForm PointSlopeForm(
const double x_f,
const double y_f,
const double slope);
909 static CubicDerivativeInterpolation DerivativeInterpolation(
const double x_i,
const double y_i,
const double ydot_i,
910 const double x_f,
const double y_f,
const double ydot_f);
929 static CubicSecondDerivativeInterpolation SecondDerivativeInterpolation(
const double x_i,
const double y_i,
const double yddot_i,
930 const double x_f,
const double y_f,
const double yddot_f);
971 static QuinticInterpolation Interpolation(
const double x_i,
const double y_i,
const double ydot_i,
const double yddot_i,
972 const double x_f,
const double y_f,
const double ydot_f,
const double yddot_f);
1002 ECL_PUBLIC double operator()(
const double& x_begin,
const double& x_end,
const LinearFunction &
function);
1003 virtual ~Maximum() {}
1026 ECL_PUBLIC double operator()(
const double& x_begin,
const double& x_end,
const CubicPolynomial& cubic);
1027 virtual ~Maximum() {}
1051 ECL_PUBLIC double operator()(
const double& x_begin,
const double& x_end,
const LinearFunction &
function);
1052 virtual ~Minimum() {}
1075 ECL_PUBLIC double operator()(
const double& x_begin,
const double& x_end,
const CubicPolynomial& cubic);
1076 virtual ~Minimum() {}
1090 Intersection() : last_operation_failed(
false) {}
1091 virtual ~Intersection() {}
1109 bool fail()
const {
return last_operation_failed; }
1112 bool last_operation_failed;
1127 virtual ~Division() {};
1135 ECL_PUBLIC LinearFunction operator()(
const QuadraticPolynomial &p,
const double &factor,
double &remainder);
1147 virtual ~Division() {};
1155 ECL_PUBLIC QuadraticPolynomial operator()(
const CubicPolynomial &p,
const double &factor,
double &remainder);
1232 class ECL_PUBLIC FunctionMath<LinearFunction> {
1235 virtual ~FunctionMath() {};
1245 point = intersection(f,g);
1256 static Array<double> Roots(
const LinearFunction &
function) {
1264 static double Minimum(
const double& x_begin,
const double& x_end,
const LinearFunction &
function) {
1272 static double Maximum(
const double& x_begin,
const double& x_end,
const LinearFunction &
function) {
1284 class ECL_PUBLIC FunctionMath<QuadraticPolynomial> {
1287 virtual ~FunctionMath() {};
1299 static LinearFunction Division(
const QuadraticPolynomial &p,
const double &factor,
double &remainder) {
1313 class ECL_PUBLIC FunctionMath<CubicPolynomial> {
1316 virtual ~FunctionMath() {};
1328 static QuadraticPolynomial Division(
const CubicPolynomial &p,
const double &factor,
double &remainder) {
1337 static double Minimum(
const double& x_begin,
const double& x_end,
const CubicPolynomial &
function) {
1345 static double Maximum(
const double& x_begin,
const double& x_end,
const CubicPolynomial &
function) {
Blueprint for interpolating a cubic polynomial between end point conditions.
#define ecl_throw(exception)
Standard ecl throw exception throw.
const_iterator end(unsigned int index=0) const
Iterator generator for diagonals of pascals triangle [end].
Blueprint for interpolating a cubic polynomial between end point conditions.
#define ecl_throw_decl(exception)
Standard ecl throw exception declaration.
Mathematical minimum on a compact interval for cubic polynomials.
Defines validating functionality for the streams concept.
#define LOC
Stringify the line of code you are at.
#define ecl_geometry_PUBLIC
Blueprint for interpolating a linear function connecting end point conditions.
X axis intercepts for linear functions.
virtual ~BluePrintFactory()
Synthetic division between cubic and a factor.
double operator()(const double &x) const
Primary template for blueprint factories.
Standard exception type, provides code location and error string.
Polynomial< 1 > LinearFunction
X axis intercepts for quadratics.
Mathematical maximum on a compact interval for linear functions.
X axis intercepts for cubic polynomials.
Polynomial< 2 > QuadraticPolynomial
Intersection of two linear functions.
Mathematical minimum on a compact interval for linear functions.
void shift_horizontal(const double &shift)
const_iterator begin(unsigned int index=0) const
Iterator generator for diagonals of pascals triangle [begin].
Synthetic division between quadratic and a factor.
Blueprint for interpolating a quintic polynomial between end point conditions.
Mathematical maximum on a compact interval for cubic polynomials.
#define ecl_compile_time_concept_check(Model)
Compile time concept checking assertion.
This is a parent template for blueprints using crtp.
Array< int,(N+2)*(N+1)/2 >::const_iterator const_iterator
Holds the coefficients for pascal's triangle up to row N.
Generic container storing a cartesian point of dimension N.
void f(int i) ecl_debug_throw_decl(StandardException)
#define ecl_catch(exception)
The catch part of a try-catch macro matching ecl_throw calls.
double dderivative(const double &x) const
Polynomial< 3 > CubicPolynomial
Representation of a polynomial function of n-th degree.
Polynomial< 5 > QuinticPolynomial
#define ecl_try
The try part of a try-catch macro matching ecl_throw calls.
Polynomial< N-1 > derivative() const