From c19df582e032abd5ea43faa3017c3d2912b46700 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan.bastani@ens.fr>
Date: Thu, 7 Oct 2010 14:15:56 +0200
Subject: [PATCH] external_function: modified to pass functions as string names
 as opposed to function handles to reduce number of calls to mexCallMatlab
 when use_dll option is invoked

---
 matlab/hess_element.m    | 4 +++-
 matlab/jacob_element.m   | 4 +++-
 preprocessor/ExprNode.cc | 6 +++---
 3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/matlab/hess_element.m b/matlab/hess_element.m
index 8bf24a5b96..176919e717 100644
--- a/matlab/hess_element.m
+++ b/matlab/hess_element.m
@@ -3,7 +3,7 @@ function d=hess_element(func,element1,element2,args)
 % returns an entry of the finite differences approximation to the hessian of func
 %
 % INPUTS
-%    func       [function handle]  associated with the function
+%    func       [function name]    string with name of the function
 %    element1   [int]              the indices showing the element within the hessian that should be returned
 %    element2   [int]
 %    args       [cell array]       arguments provided to func
@@ -31,6 +31,8 @@ function d=hess_element(func,element1,element2,args)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
+func = str2func(func);
+
 h=10e-6;
 p10 = args;
 p01 = args;
diff --git a/matlab/jacob_element.m b/matlab/jacob_element.m
index 5876a2b895..83bbbf1d0f 100644
--- a/matlab/jacob_element.m
+++ b/matlab/jacob_element.m
@@ -3,7 +3,7 @@ function d=jacob_element(func,element,args)
 % returns an entry of the finite differences approximation to the jacobian of func
 %
 % INPUTS
-%    func       [function handle]  associated with the function
+%    func       [function name]    string with name of the function
 %    element    [int]              the index showing the element within the jacobian that should be returned
 %    args       [cell array]       arguments provided to func
 %
@@ -30,6 +30,8 @@ function d=jacob_element(func,element,args)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
+func = str2func(func);
+
 h=10e-6;
 pargs=args;
 margs=args;
diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc
index e927e1fb36..861f479ce2 100644
--- a/preprocessor/ExprNode.cc
+++ b/preprocessor/ExprNode.cc
@@ -3963,8 +3963,8 @@ FirstDerivExternalFunctionNode::writeExternalFunctionOutput(ostream &output, Exp
       if (first_deriv_symb_id == symb_id)
         return;
       else if (first_deriv_symb_id == eExtFunNotSet)
-        output << "TEFD_fdd_" << getIndxInTefTerms(symb_id, tef_terms) << "_" << inputIndex << " = jacob_element(@"
-               << datatree.symbol_table.getName(symb_id) << "," << inputIndex << ",{";
+        output << "TEFD_fdd_" << getIndxInTefTerms(symb_id, tef_terms) << "_" << inputIndex << " = jacob_element('"
+               << datatree.symbol_table.getName(symb_id) << "'," << inputIndex << ",{";
       else
         {
           tef_terms[make_pair(first_deriv_symb_id, arguments)] = (int) tef_terms.size();
@@ -4065,7 +4065,7 @@ SecondDerivExternalFunctionNode::writeExternalFunctionOutput(ostream &output, Ex
         return;
       else if (second_deriv_symb_id == eExtFunNotSet)
         output << "TEFDD_fdd_" << getIndxInTefTerms(symb_id, tef_terms) << "_" << inputIndex1 << "_" << inputIndex2
-               << " = hess_element(@" << datatree.symbol_table.getName(symb_id) << ","
+               << " = hess_element('" << datatree.symbol_table.getName(symb_id) << "',"
                << inputIndex1 << "," << inputIndex2 << ",{";
       else
         {
-- 
GitLab