added example of 2nd derivative with vector version that does not work... Also, added operator+-*/ for scalar op Dual and scalar op dualvector

This commit is contained in:
Jérôme 2019-03-25 21:36:23 +01:00
parent 066abc4659
commit bd46adc942
3 changed files with 114 additions and 1 deletions

View file

@ -119,6 +119,23 @@ class Dual
Scalar b; /// Infinitesimal part Scalar b; /// Infinitesimal part
}; };
template<typename A, typename B, int N>
Dual<B, N> operator+(A const& v, Dual<B, N> const& x) {
return (Dual<B, N>(v) + x);
}
template<typename A, typename B, int N>
Dual<B, N> operator-(A const& v, Dual<B, N> const& x) {
return (Dual<B, N>(v) - x);
}
template<typename A, typename B, int N>
Dual<B, N> operator*(A const& v, Dual<B, N> const& x) {
return (Dual<B, N>(v) * x);
}
template<typename A, typename B, int N>
Dual<B, N> operator/(A const& v, Dual<B, N> const& x) {
return (Dual<B, N>(v) / x);
}
// Basic mathematical functions for Scalar numbers // Basic mathematical functions for Scalar numbers
// Trigonometric functions // Trigonometric functions

View file

@ -42,8 +42,13 @@ class DualVector
b = VectorT(_b, N); b = VectorT(_b, N);
} }
DualVector const& operator=(Scalar const& _a)
{
*this = DualVector(_a);
}
/// Use this function to set what variable is to be derived : x + DualVector::d(i) /// Use this function to set what variable is to be derived : x + DualVector::d(i)
static DualVector d(int i = 0) static DualVector D(int i = 0)
{ {
assert(i >= 0); assert(i >= 0);
assert(i < N); assert(i < N);
@ -62,6 +67,14 @@ class DualVector
return *this; return *this;
} }
/// Returns the derivative value at index i
Scalar const& d(int i) const
{
assert(i >= 0);
assert(i < N);
return b[i];
}
DualVector & operator+=(const DualVector & x) DualVector & operator+=(const DualVector & x)
{ {
a += x.a; a += x.a;
@ -127,6 +140,23 @@ class DualVector
VectorT b; /// Infinitesimal parts VectorT b; /// Infinitesimal parts
}; };
template<typename A, typename B, int N>
DualVector<B, N> operator+(A const& v, DualVector<B, N> const& x) {
return (DualVector<B, N>(v) + x);
}
template<typename A, typename B, int N>
DualVector<B, N> operator-(A const& v, DualVector<B, N> const& x) {
return (DualVector<B, N>(v) - x);
}
template<typename A, typename B, int N>
DualVector<B, N> operator*(A const& v, DualVector<B, N> const& x) {
return (DualVector<B, N>(v) * x);
}
template<typename A, typename B, int N>
DualVector<B, N> operator/(A const& v, DualVector<B, N> const& x) {
return (DualVector<B, N>(v) / x);
}
// Basic mathematical functions for Scalar numbers // Basic mathematical functions for Scalar numbers
// Trigonometric functions // Trigonometric functions

View file

@ -0,0 +1,66 @@
#include "../AutomaticDifferentiationVector.hpp"
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
#define PRINT_VAR(x) std::cout << #x << "\t= " << std::setprecision(16) << (x) << std::endl
#define PRINT_DUAL(x) std::cout << #x << "\t= " << std::fixed << std::setprecision(4) << std::setw(10) << (x).a << ", " << std::setw(10) << (x).b << std::endl
template<typename T>
T f(const T & x)
{
return 1 + x + x*x + 1/x + log(x);
}
template<typename T>
T df(const T & x)
{
return 2*x + 1 + 1.0/x - 1/pow(x, 2);
}
template<typename T>
T ddf(const T & x)
{
return 2 - 1/pow(x, 2) + 2/pow(x, 3);
}
int main()
{
double xdbl = 1.5;
{
cout << "Analytical\n";
cout << "f(x) = " << f(xdbl) << endl;
cout << "df(x)/dt = " << df(xdbl) << endl;
cout << "d²f(x)/dt = " << ddf(xdbl) << endl;
}
// 1st derivative forward
{
using Fd = DualVector<double,1>;
Fd x = xdbl;
x.diff(0);
Fd y = f(x);
cout << "\nForward\n";
cout << "f(x) = " << y.a << endl;
cout << "df(x)/dt = " << y.d(0) << endl;
}
// 2nd derivative forward
/*
{
using Fdd = DualVector<DualVector<double,2>,2>;
Fdd x(xdbl);
x.diff(0);
x.a.diff(1);
Fdd y = f(x);
cout << "\nForward 2nd der\n";
cout << "f(x) = " << y.a.a << endl;
cout << "df(x)/dt = " << y.d(0).a << endl;
cout << "d²f(x)/dt = " << y.d(0).d(1) << endl;
}//*/
return 0;
}