diff --git a/src/DataTree.cc b/src/DataTree.cc index 0eea7962ad25f3f380de7d2e40a3184ffb7756ad..69e7d938bf21d5c75eadcbf889f5822af1dfb363 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 ef91555faaa3ec1807e5ad1a47b0dae1dc9e68ce..ba84c443d56d16dba93043ba9a524cdad37ff230 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 efcfad762cf4f0bb263dd69766c7c03a86836c00..f6c19af001181a841393d91eb792fec43bea2c1c 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 32b17bff04d88115f4d23eb8ab3212749272d61e..7150ba6d432b105b12e8a1268f7d2d6f1779f1c1 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"); }