Search code examples
c++c++11automatic-differentiation

Automatic Differentiation with CoDiPack


The following code:

#include <codi.hpp>
...
codi::RealForward Gcodi[l];
for (int p = 0; p < l; p++) 
{
     ...
     double a = Gcodi[p];
}

gives me the compilation error:

nnBFAD.cpp: In function ‘void OptBF()’:

nnBFAD.cpp:156:25: error: cannot convert ‘codi::RealForward {aka codi::ActiveReal >}’ to ‘double’ in initialization double

a = Gcodi[p];

Hints?


Solution

  • According to the official doc here, RealForward is a type with an assignment operator overloaded, so you can assign that with a double..

    like doing

    codi::RealForward a = 3.0;
    

    the opposite direction is of course not defined,

    that is the raeson why you can not convert directly a codi::RealForward into a double just by doing:

    double a = Gcodi[p];
    

    but you can call the functions on that, i.e.

    double a = Gcodi[p].getGradient();
    

    UPDATE:

    then you can assign a RealForward object with a double like doing

    double myDouble{3.3};
    RealForward a = myDouble;
    

    but is not legal to assign a double directly from the REalForwad:

    RealForward a = ...;
    double myDouble = a; //not valid!
    

    other examples

    RealForward b = a * a; //this is ok because a * a is a double