This is an example on how to use the Differentiation file.
#include "../../Test.h"
class DifferentiationTestCase : public Test
{
int m = 10;
double a = 0;
double b = 2 * M_PI;
return in.
Apply([](
const double& val) {
return cos(val); });
}
return in.
Apply([](
const double& val) {
return -sin(val); });
}
bool TestDiffs() {
auto expected = df(xk);
auto f_xk = f(xk);
plot();
return true;
}
bool TestBackwardDiffs() {
auto expected = df(xk);
auto f_xk = f(xk);
{ 0 },
{ -0.33511665007288566 },
{ -0.84854514524284763 },
{ -0.9649299364248537 },
{ -0.6298132863519682 },
{ 0 },
{ 0.62981328635196787 },
{ 0.9649299364248537 },
{ 0.84854514524284785 },
{ 0.33511665007288594 },
});
return true;
}
bool TestForwardDiffs() {
auto expected = df(xk);
auto f_xk = f(xk);
{ -0.33511665007288566 },
{ -0.84854514524284763 },
{ -0.9649299364248537 },
{ -0.6298132863519682 },
{ 0 },
{ 0.62981328635196787 },
{ 0.9649299364248537 },
{ 0.84854514524284785 },
{ 0.33511665007288594 },
{ 0 },
});
return true;
}
bool TestCentralDiffs() {
auto expected = df(xk);
auto f_xk = f(xk);
{ 0 },
{ -0.59183089765786667 },
{ -0.90673754083385083 },
{ -0.79737161138841095 },
{ -0.31490664317598421 },
{ 0.31490664317598377 },
{ 0.79737161138841095 },
{ 0.90673754083385083 },
{ 0.59183089765786689 },
{ 0 },
});
return true;
}
bool TestBackwardDiffs2() {
auto expected = df(xk);
auto f_xk = f(xk);
{ 0 },
{ 0 },
{ -1.1052593928278287 },
{ -1.0231223320158569 },
{ -0.46225496131552568 },
{ 0.31490664317598388 },
{ 0.94471992952795147 },
{ 1.132488261461297 },
{ 0.79035274965184488 },
{ 0.078402402487904935 },
});
return true;
}
bool TestCentralDiffs4() {
auto expected = df(xk);
auto f_xk = f(xk);
{ 0 },
{ -0.97744963627075465 },
{ -0.85955478451624223 },
{ -0.33946469619924124 },
{ 0.33946469619924052 },
{ 0.8595547845162419 },
{ 0.9774496362707551 },
{ 0 },
{ 0 } });
return true;
}
public:
virtual void run() {
#if USE_VIS
TestDiffs();
#endif
TestBackwardDiffs();
TestBackwardDiffs2();
TestForwardDiffs();
TestCentralDiffs();
TestCentralDiffs4();
}
};
int main() {
DifferentiationTestCase().run();
return 0;
}
Matrix< double > forwardDiff(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:53
Matrix< double > backwardDiff(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:73
Matrix< double > centralDiff4(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:129
Matrix< double > centralDiff(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:92
Matrix< double > backwardDiff2(const Matrix< double > &x, const Matrix< double > &y)
Definition: Differentiation.h:110
constexpr Matrix< T > Transpose() const
Definition: Matrix.h:256
Matrix< T > Apply(const std::function< T(T)> &fun) const
Definition: Matrix.h:375
void AddData(const Matrix< double > &x, const Matrix< double > &y, const std::string &name, DataTypes dataType=DataTypes::NONE, const char *character=nullptr)
Definition: Plot.h:182
Matrix< T > HorizontalConcat(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:81
Matrix< double > linspace(double start, double end, unsigned long num_elements)