joaoleal / CppADCodeGen

Source Code Generation for Automatic Differentiation using Operator Overloading
Other
162 stars 36 forks source link

How to get the value of a CppADCodeGen scalar type #79

Open FenglongSong opened 1 year ago

FenglongSong commented 1 year ago

Hi,

First of all, thanks for developing this great tool! I'm new to CppADCodeGen, I'd like to ask is there any way to get the value of a scalar type CppAD::AD<CppAD::cg::CG<double>> (or convert this type to the double type). I know in CppAD this is possible by using CppAD::Value() of Var2Par() function, but I don't know how to do that in CppADCodeGen. Would you please give me any advice? Thanks a lot!

joaoleal commented 1 year ago

Hello,

It is possible to CppAD::Value() and Var2Par() to retrieve the CG<double> and then CG<double>::getValue() to retrive the value (which can throw an exception if the value is not defined). For instance:

        CppAD::vector<ADCG> x(2);
        x[0] = 2.; // values must be defined!
        x[1] = 3.; // values must be defined!
        Independent(x);

        ADCG a = x[0] / 1. + x[1] * x[1];
        ADCG b = a / 2e-6;

        std::cout << CppAD::Value(CppAD::Var2Par(b)).getValue() << std::endl;

Kind regards