From 509cc15191311c96e65dec89a9c5619d63865975 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 18 Jul 2011 14:28:47 +0200 Subject: [PATCH] bug fix: getPowerDeriv not accessible from modfile.m --- matlab/getPowerDeriv.m | 42 ++++++++++++++++++++++++++++++++++++ preprocessor/DataTree.cc | 22 +------------------ preprocessor/DynamicModel.cc | 6 ------ preprocessor/StaticModel.cc | 3 --- 4 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 matlab/getPowerDeriv.m diff --git a/matlab/getPowerDeriv.m b/matlab/getPowerDeriv.m new file mode 100644 index 000000000..e87eabe1e --- /dev/null +++ b/matlab/getPowerDeriv.m @@ -0,0 +1,42 @@ +function dxp=getPowerDeriv(x,p,k) +%function dxp=getPowerDeriv(x,p,k) +% The k-th derivative of x^p +% +% INPUTS +% x: base +% p: power +% k: derivative order +% +% OUTPUTS +% dxp: k-th derivative of x^p +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2011 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +if (abs(x) < 1e-12) && (p > 0) && (k >= p) && (abs(p - round(p)) < 1e-12) + dxp = 0; +else + dxp = x^(p-k); + for i=0:k-1 + dxp = dxp*p; + p = p-1; + end +end +end diff --git a/preprocessor/DataTree.cc b/preprocessor/DataTree.cc index 87052a6e1..267a97e60 100644 --- a/preprocessor/DataTree.cc +++ b/preprocessor/DataTree.cc @@ -660,10 +660,7 @@ DataTree::writePowerDerivCHeader(ostream &output) const void DataTree::writePowerDeriv(ostream &output, bool use_dll) const { - if (!isBinaryOpUsed(oPowerDeriv)) - return; - - if (use_dll) + if (use_dll && isBinaryOpUsed(oPowerDeriv)) output << "/*" << endl << " * The k-th derivative of x^p" << endl << " */" << endl @@ -683,21 +680,4 @@ DataTree::writePowerDeriv(ostream &output, bool use_dll) const << " return dxp;" << endl << " }" << endl << "}" << endl; - else - output << endl - << "%" << endl - << "% The k-th derivative of x^p" << endl - << "%" << endl - << "function dxp=getPowerDeriv(x,p,k)" << endl - << " if (abs(x) < " << NEAR_ZERO << ") && (p > 0) && (k >= p) && (abs(p - round(p)) < " << NEAR_ZERO << ")" << endl - << " dxp = 0;" << endl - << " else" << endl - << " dxp = x^(p-k);" << endl - << " for i=0:k-1" << endl - << " dxp = dxp*p;" << endl - << " p = p-1;" << endl - << " end" << endl - << " end" << endl - << "end" << endl; - } diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 38f57595d..f37bf240b 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -780,7 +780,6 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const break; } output << "end" << endl; - writePowerDeriv(output, false); output.close(); } } @@ -1513,7 +1512,6 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const writeDynamicModel(mDynamicModelFile, false); mDynamicModelFile << "end" << endl; // Close *_dynamic function - writePowerDeriv(mDynamicModelFile, false); mDynamicModelFile.close(); } @@ -2022,7 +2020,6 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri mDynamicModelFile << " oo_.endo_simul = y';\n"; mDynamicModelFile << "return;\n"; mDynamicModelFile << "end" << endl; - writePowerDeriv(mDynamicModelFile, false); mDynamicModelFile.close(); @@ -3674,9 +3671,6 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const paramsDerivsFile << "end" << endl << "end" << endl; - - writePowerDeriv(paramsDerivsFile, false); - paramsDerivsFile.close(); } diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 0fe9a3790..c81f21918 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -396,7 +396,6 @@ StaticModel::writeModelEquationsOrdered_M(const string &static_basename) const break; } output << "end" << endl; - writePowerDeriv(output, false); output.close(); } } @@ -1238,7 +1237,6 @@ StaticModel::writeStaticMFile(const string &func_name) const output << "end" << endl; // Close the if nargout >= 3 statement output << "end" << endl; // Close the *_static function - writePowerDeriv(output, false); output.close(); } @@ -1322,7 +1320,6 @@ StaticModel::writeStaticBlockMFSFile(const string &basename) const } output << " end" << endl << "end" << endl; - writePowerDeriv(output, false); output.close(); }