33 #include "autodiff/forward/dual.hpp"
34 #include "autodiff/forward/real.hpp"
36 #include <autodiff/forward/dual/dual.hpp>
43 template <
typename T >
55 double exp(
double x );
63 constexpr
double radToDeg(
const double alpha )
70 constexpr
double degToRad(
const double alpha )
79 return scalar >= 0 ? scalar : 0.0;
86 return scalar >= 0 ? 1 : 0;
91 template <
typename T >
92 constexpr T
sgn( T val )
94 return val / std::abs( val );
97 double makeReal(
const double& value );
98 double makeReal(
const std::complex< double >& value );
99 double makeReal(
const autodiff::real& value );
101 template <
typename T,
typename G >
102 double makeReal(
const autodiff::detail::Dual< T, G >& number )
104 return double( number );
110 template <
int nRows,
int nCols >
111 Eigen::Matrix< double, nRows, nCols >
macaulyMatrix(
const Eigen::Matrix< double, nRows, nCols >& mat )
113 Eigen::Matrix< double, nRows, nCols > positivePart = Eigen::Matrix< double, nRows, nCols >::Zero();
114 for (
int i = 0; i < nRows; i++ ) {
115 for (
int j = 0; j < nCols; j++ ) {
116 positivePart( i, j ) =
macauly( mat( i, j ) );
124 template <
typename functionType,
typename yType,
typename... Args >
125 yType
explicitEuler( yType yN,
const double dt, functionType fRate, Args&&... fRateArgs )
127 return yN + fRate( yN, fRateArgs... ) * dt;
135 template <
int ySize,
typename functionType,
typename... Args >
139 Args&&... fRateArgs )
141 Eigen::Matrix< double, ySize, ySize > fS = Eigen::Matrix< double, ySize, ySize >::Zero();
142 Eigen::Matrix< double, ySize, ySize > Iy = Eigen::Matrix< double, ySize, ySize >::Identity();
144 Eigen::VectorXd leftX( ySize );
145 Eigen::VectorXd rightX( ySize );
147 for (
size_t i = 0; i < ySize; i++ ) {
153 fS.col( i ) = 1. / ( 2. * h ) * ( fRate( rightX, fRateArgs... ) - fRate( leftX, fRateArgs... ) );
156 return yN + ( Iy - dt * fS ).inverse() * dt * fRate( yN, fRateArgs... );
162 template <
int nRows,
int nCols,
typename functionType >
163 Eigen::Matrix< double, nRows, nCols >
centralDiff( functionType f,
const Eigen::Matrix< double, nCols, 1 >& X )
165 Eigen::Matrix< double, nRows, nCols > dXdY = Eigen::Matrix< double, nRows, nCols >::Zero();
167 Eigen::VectorXd leftX( nCols );
168 Eigen::VectorXd rightX( nCols );
170 for (
size_t i = 0; i < nCols; i++ ) {
176 dXdY.col( i ) = 1. / ( 2. * h ) * ( f( rightX ) - f( leftX ) );
186 template <
typename functionType,
typename yType,
typename... Args >
189 yType fN = fRate( yN, fRateArgs... );
190 yType u = yN + fN * dt;
191 yType v = yN + fN * dt / 2.;
192 yType w = v + fRate( v, fRateArgs... ) * dt / 2.;
201 template <
int ySize,
typename functionType,
typename... Args >
203 Eigen::Matrix< double, ySize, 1 > yN,
207 Args&&... fRateArgs )
210 typedef Eigen::Matrix< double, ySize, 1 > ySized;
211 ySized fN = fRate( yN, fRateArgs... );
212 ySized u = yN + fN * dt;
213 ySized v = yN + fN * dt / 2.;
214 ySized w = v + fRate( v, fRateArgs... ) * dt / 2.;
215 ySized yNew = 2. * w - u;
218 const double AERR = 1.0;
219 const double aI = AERR / TOL;
220 const double rI = 1.0;
222 ySized ESTVec = ySized::Zero();
224 for (
int i = 0; i < ySize; i++ ) {
225 scaling = aI + rI * std::max( abs( yNew( i ) ), abs( yN( i ) ) );
226 ESTVec( i ) = abs( w( i ) - u( i ) ) / abs( scaling );
229 const double EST = ESTVec.maxCoeff();
230 const double tauNew = dt * std::min( 2., std::max( 0.2, 0.9 * std::sqrt( TOL / EST ) ) );
232 return std::make_tuple( yNew, tauNew );