adevs_poly.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright (c) 2013, James Nutaro
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * The views and conclusions contained in the software and documentation are those
  26. * of the authors and should not be interpreted as representing official policies,
  27. * either expressed or implied, of the FreeBSD Project.
  28. *
  29. * Bugs, comments, and questions can be sent to nutaro@gmail.com
  30. */
  31. #ifndef _adevs_poly_h_
  32. #define _adevs_poly_h_
  33. #include <cstdlib>
  34. namespace adevs
  35. {
  36. /**
  37. * This class implements Lagrange interpolating
  38. * polynomials of arbitrary order for functions of
  39. * a single variable. This can be
  40. * useful in many discrete event simulations where
  41. * tabular values, possible produced by discrete time
  42. * calculations, need to be interpolated for use
  43. * in a discrete event system. GDEVS is one particular
  44. * example of this.
  45. */
  46. class InterPoly
  47. {
  48. public:
  49. /**
  50. * Construct a polynomial to interpolate
  51. * a function u(t). The u- values
  52. * are the dependent variable and t- the independent
  53. * variables.
  54. */
  55. InterPoly(const double* u, const double* t, unsigned int n);
  56. /**
  57. * Construct a polynomial to interpolate u(t) with data points
  58. * that are regularly spaced in time from an offset t0
  59. */
  60. InterPoly(const double* u, double dt, unsigned int n, double t0 = 0.0);
  61. /**
  62. * Assign new values to the data set. If t is NULL, then
  63. * only new u values will be assigned and the old t data is
  64. * kept.
  65. */
  66. void setData(const double* u, const double* t = NULL);
  67. /**
  68. * Get the interpolated value at t
  69. */
  70. double interpolate(double t) const;
  71. /**
  72. * Overloaded operator for the interpolate method
  73. */
  74. double operator()(double t) const;
  75. /**
  76. * Approximate the function derivative at t
  77. */
  78. double derivative(double t) const;
  79. /**
  80. * Destructor
  81. */
  82. ~InterPoly();
  83. private:
  84. InterPoly(){}
  85. InterPoly(const InterPoly&){}
  86. void operator=(const InterPoly&){}
  87. double* tdat;
  88. double* udat;
  89. unsigned int n;
  90. };
  91. }
  92. #endif