diff libinterp/corefcn/lsode.cc @ 26864:041caa61ed34

use get_function_handle instead of extract_function * cellfun.cc (Fcellfun, Farrayfun): Use get_function_handle instead of extract_function. * daspk.cc (daspk_fcn, daspk_jac): Now octave_value objects instead of a pointers to octave_function objects. Change all uses. (Fdaspk): Use get_function_handle instead of extract_function. * dasrt.cc (dasrt_fcn, dasrt_jac, dasrt_cf): Now octave_value objects instead of a pointers to octave_function objects. Change all uses. (Fdasrt): Use get_function_handle instead of extract_function. * dassl.cc (dassl_fcn, dassl_jac): Now octave_value objects instead of a pointers to octave_function objects. Change all uses. (Fdassl): Use get_function_handle instead of extract_function. * lsode.cc (lsode_fcn, lsode_jac): Now octave_value objects instead of a pointers to octave_function objects. Change all uses. (Flsode): Use get_function_handle instead of extract_function. * quad.cc (quad_fcn): Now an octave_value object instead of a pointer to an octave_function object. Change all uses. (Fquad): Use get_function_handle instead of extract_function. New tests. * quadcc.cc (fcn): Now an octave_value object instead of a pointer to an octave_function object. Change all uses. (Fquadcc): Use get_function_handle instead of extract_function. * __eigs__.cc (eigs_fcn): Now an octave_value object instead of a pointer to an octave_function object. Change all uses. (F__eigs__): Use get_function_handle instead of extract_function. New tests. * interpreter-private.cc (get_function_handle): Don't scold users for poor choices. * variables.h (extract_function): Tag as deprecated.
author John W. Eaton <jwe@octave.org>
date Fri, 08 Mar 2019 06:24:34 +0000
parents 00f796120a6d
children b442ec6dda5c
line wrap: on
line diff
--- a/libinterp/corefcn/lsode.cc	Fri Mar 08 02:49:52 2019 +0000
+++ b/libinterp/corefcn/lsode.cc	Fri Mar 08 06:24:34 2019 +0000
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <list>
 #include <string>
 
 #include "LSODE.h"
@@ -32,6 +33,7 @@
 #include "defun.h"
 #include "error.h"
 #include "errwarn.h"
+#include "interpreter-private.h"
 #include "ovl.h"
 #include "ov-fcn.h"
 #include "ov-cell.h"
@@ -45,10 +47,10 @@
 #include "LSODE-opts.cc"
 
 // Global pointer for user defined function required by lsode.
-static octave_function *lsode_fcn;
+static octave_value lsode_fcn;
 
 // Global pointer for optional user defined jacobian function used by lsode.
-static octave_function *lsode_jac;
+static octave_value lsode_jac;
 
 // Have we warned about imaginary values returned from user function?
 static bool warned_fcn_imaginary = false;
@@ -66,7 +68,7 @@
   args(1) = t;
   args(0) = x;
 
-  if (lsode_fcn)
+  if (lsode_fcn.is_defined ())
     {
       octave_value_list tmp;
 
@@ -106,7 +108,7 @@
   args(1) = t;
   args(0) = x;
 
-  if (lsode_jac)
+  if (lsode_jac.is_defined ())
     {
       octave_value_list tmp;
 
@@ -275,11 +277,14 @@
   octave::symbol_table& symtab = interp.get_symbol_table ();
 
   std::string fcn_name, fname, jac_name, jname;
-  lsode_fcn = nullptr;
-  lsode_jac = nullptr;
+
+  lsode_fcn = octave_value ();
+  lsode_jac = octave_value ();
 
   octave_value f_arg = args(0);
 
+  std::list<std::string> parameter_names ({"x", "t"});
+
   if (f_arg.iscell ())
     {
       Cell c = f_arg.cell_value ();
@@ -287,92 +292,49 @@
         f_arg = c(0);
       else if (c.numel () == 2)
         {
-          if (c(0).is_function_handle () || c(0).is_inline_function ())
-            lsode_fcn = c(0).function_value ();
-          else
-            {
-              fcn_name = unique_symbol_name ("__lsode_fcn__");
-              fname = "function y = ";
-              fname.append (fcn_name);
-              fname.append (" (x, t) y = ");
-              lsode_fcn = extract_function (c(0), "lsode", fcn_name, fname,
-                                            "; endfunction");
-            }
+          lsode_fcn = octave::get_function_handle (interp, c(0),
+                                                   parameter_names);
 
-          if (lsode_fcn)
+          if (lsode_fcn.is_defined ())
             {
-              if (c(1).is_function_handle () || c(1).is_inline_function ())
-                lsode_jac = c(1).function_value ();
-              else
-                {
-                  jac_name = unique_symbol_name ("__lsode_jac__");
-                  jname = "function jac = ";
-                  jname.append (jac_name);
-                  jname.append (" (x, t) jac = ");
-                  lsode_jac = extract_function (c(1), "lsode", jac_name,
-                                                jname, "; endfunction");
+              lsode_jac = octave::get_function_handle (interp, c(1),
+                                                       parameter_names);
 
-                  if (! lsode_jac)
-                    {
-                      if (fcn_name.length ())
-                        symtab.clear_function (fcn_name);
-                      lsode_fcn = nullptr;
-                    }
-                }
+              if (lsode_jac.is_undefined ())
+                lsode_fcn = octave_value ();
             }
         }
       else
         error ("lsode: incorrect number of elements in cell array");
     }
 
-  if (! lsode_fcn && ! f_arg.iscell ())
+  if (lsode_fcn.is_undefined () && ! f_arg.iscell ())
     {
       if (f_arg.is_function_handle () || f_arg.is_inline_function ())
-        lsode_fcn = f_arg.function_value ();
+        lsode_fcn = f_arg;
       else
         {
           switch (f_arg.rows ())
             {
             case 1:
-              do
-                {
-                  fcn_name = unique_symbol_name ("__lsode_fcn__");
-                  fname = "function y = ";
-                  fname.append (fcn_name);
-                  fname.append (" (x, t) y = ");
-                  lsode_fcn = extract_function (f_arg, "lsode", fcn_name,
-                                                fname, "; endfunction");
-                }
-              while (0);
+              lsode_fcn = octave::get_function_handle (interp, f_arg,
+                                                       parameter_names);
               break;
 
             case 2:
               {
                 string_vector tmp = f_arg.string_vector_value ();
 
-                fcn_name = unique_symbol_name ("__lsode_fcn__");
-                fname = "function y = ";
-                fname.append (fcn_name);
-                fname.append (" (x, t) y = ");
-                lsode_fcn = extract_function (tmp(0), "lsode", fcn_name,
-                                              fname, "; endfunction");
+                lsode_fcn = octave::get_function_handle (interp, tmp(0),
+                                                         parameter_names);
 
-                if (lsode_fcn)
+                if (lsode_fcn.is_defined ())
                   {
-                    jac_name = unique_symbol_name ("__lsode_jac__");
-                    jname = "function jac = ";
-                    jname.append (jac_name);
-                    jname.append (" (x, t) jac = ");
-                    lsode_jac = extract_function (tmp(1), "lsode",
-                                                  jac_name, jname,
-                                                  "; endfunction");
+                    lsode_jac = octave::get_function_handle (interp, tmp(1),
+                                                             parameter_names);
 
-                    if (! lsode_jac)
-                      {
-                        if (fcn_name.length ())
-                          symtab.clear_function (fcn_name);
-                        lsode_fcn = nullptr;
-                      }
+                    if (lsode_jac.is_undefined ())
+                      lsode_fcn = octave_value ();
                   }
               }
               break;
@@ -383,7 +345,7 @@
         }
     }
 
-  if (! lsode_fcn)
+  if (lsode_fcn.is_undefined ())
     error ("lsode: FCN argument is not a valid function name or handle");
 
   ColumnVector state = args(1).xvector_value ("lsode: initial state X_0 must be a vector");
@@ -402,7 +364,8 @@
   double tzero = out_times (0);
 
   ODEFunc func (lsode_user_function);
-  if (lsode_jac)
+
+  if (lsode_jac.is_defined ())
     func.set_jacobian_function (lsode_user_jacobian);
 
   LSODE ode (state, tzero, func);