From 9773bafc5b18b4852b98ca83b7cec8f37cc06ba0 Mon Sep 17 00:00:00 2001 From: MichelJuillard <michel.juillard@mjui.fr> Date: Mon, 26 Apr 2021 10:52:18 +0000 Subject: [PATCH] Julia: write definition of get_power_deriv inside generated static/dynamic files --- src/DataTree.cc | 20 ++++++++++++++++++++ src/DataTree.hh | 2 ++ src/DynamicModel.cc | 8 ++++++-- src/StaticModel.cc | 10 +++++++--- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/DataTree.cc b/src/DataTree.cc index 0eea7962..69e7d938 100644 --- a/src/DataTree.cc +++ b/src/DataTree.cc @@ -932,6 +932,26 @@ DataTree::writePowerDeriv(ostream &output) const << "}" << endl; } +void +DataTree::writePowerDerivJulia(ostream &output) const +{ + if (isBinaryOpUsed(BinaryOpcode::powerDeriv)) + output << "nearbyint(x::Float64) = (abs((x)-floor(x)) < abs((x)-ceil(x)) ? floor(x) : ceil(x))" << endl + << endl + << "function get_power_deriv(x::Float64, p::Float64, k::Int64)" << endl + << " if (abs(x) < 1e-12 && p > 0 && k > p && abs(p-nearbyint(p)) < 1e-12 )" << endl + << " return 0.0" << endl + << " else" << endl + << " dxp = x^(p-k)" << endl + << " for i = 1:k" << endl + << " dxp *= p" << endl + << " p -= 1" << endl + << " end" << endl + << " return dxp" << endl + << " end" << endl + << "end" << endl; +} + void DataTree::writePowerDerivHeader(ostream &output) const { diff --git a/src/DataTree.hh b/src/DataTree.hh index ef91555f..ba84c443 100644 --- a/src/DataTree.hh +++ b/src/DataTree.hh @@ -287,6 +287,8 @@ public: void writePowerDeriv(ostream &output) const; //! Write getPowerDeriv in C (prototype) void writePowerDerivHeader(ostream &output) const; + //! Write getPowerDeriv in Julia + void writePowerDerivJulia(ostream &output) const; //! Thrown when trying to access an unknown variable by deriv_id class UnknownDerivIDException { diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index efcfad76..f6c19af0 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -2133,7 +2133,6 @@ DynamicModel::writeDynamicModel(const string &basename, ostream &DynamicOutput, << "# NB: this file was automatically generated by Dynare" << endl << "# from " << basename << ".mod" << endl << "#" << endl - << "using Dynare: get_power_deriv" << endl << "using StatsFuns" << endl << endl << "export tmp_nbr, dynamic!, dynamicResid!, dynamicG1!, dynamicG2!, dynamicG3!" << endl << endl << "#=" << endl @@ -2323,7 +2322,12 @@ DynamicModel::writeDynamicModel(const string &basename, ostream &DynamicOutput, << " dynamicResid!(T, residual, y, x, params, steady_state, it_, false)" << endl << " return nothing" << endl << "end" << endl - << "end" << endl; + << endl; + + // Write function definition if BinaryOpcode::powerDeriv is used + writePowerDerivJulia(output); + + output << "end" << endl; writeToFileIfModified(output, basename + "Dynamic.jl"); } diff --git a/src/StaticModel.cc b/src/StaticModel.cc index 32b17bff..7150ba6d 100644 --- a/src/StaticModel.cc +++ b/src/StaticModel.cc @@ -1476,7 +1476,6 @@ StaticModel::writeStaticModel(const string &basename, << "# NB: this file was automatically generated by Dynare" << endl << "# from " << basename << ".mod" << endl << "#" << endl - << "using Dynare: get_power_deriv" << endl << "using StatsFuns" << endl << endl << "export tmp_nbr, static!, staticResid!, staticG1!, staticG2!, staticG3!" << endl << endl << "#=" << endl @@ -1666,8 +1665,13 @@ StaticModel::writeStaticModel(const string &basename, << " staticG1!(T, g1, y, x, params, false, false)" << endl << " staticResid!(T, residual, y, x, params, false)" << endl << " return nothing" << endl - << "end" << endl << endl - << "end" << endl; + << "end" << endl + << endl; + + // Write function definition if BinaryOpcode::powerDeriv is used + writePowerDerivJulia(output); + + output << "end" << endl; writeToFileIfModified(output, basename + "Static.jl"); } -- GitLab