view scripts/legacy/@inline/vectorize.m @ 28427:7a8c69c4eb55 stable

convert obsolete octave_fcn_inline object to @inline class Use a legacy @class object to provide inline function objects instead of using a built-in class derived from the octave_function_handle class. * scripts/legacy/@inline: New directory containing the following files: argnames.m, char.m, feval.m, formula.m, inline.m, subsref.m, vectorize.m. * scripts/legacy/@inline/module.mk: New file. * scripts/legacy/module.mk, scripts/module.mk: Update. * test/inline-fcn.tst: New tests. * test/module.mk: Update. * ov-fcn-inline.h, ov-fcn-inline.cc: Delete. Remove all uses. * libinterp/octave-value/module.mk: Update. * external.txi, func.txi, octave.texi, plot.txi, quad.txi: Eliminate discusion of inline function objects. * fplot.m, __ezplot__.m: Use isa to identify inline objects instead of instead of checking typeinfo. * ov-fcn-handle.cc, ov-typeinfo.cc: Fix tests. * ov-base.h, ov-base.cc (octave_base_value::fcn_inline_value): Delete. * ov.h, ov.cc (octave_value::fcn_inline_value): Delete. (octave_value::xfcn_inline_value): Delete value extractor. * ov-class.cc, ov-class.h (octave_inline, octave_inline_fcn): New classes that allow us to preserve the is_inline_function and function_value methods that were previously available in the octave_fcn_inline class. (F__inline_ctor__): New function for final construction of inline objects. * ls-hdf5.cc, ls-mat5.cc, ls-oct-binary.cc, ls-oct-text.cc: Handle loading of inline function objects from old files as special cases. New inline function objects will be saved and loaded as ordinary @class objects.
author John W. Eaton <jwe@octave.org>
date Mon, 23 Mar 2020 15:29:49 -0400
parents
children 53d8e7ca99c5
line wrap: on
line source

## -*- texinfo -*-
## @deftypefn {} {} vectorize (@var{fun})
## Create a vectorized version of the inline function @var{fun} by
## replacing all occurrences of @code{*}, @code{/}, etc., with
## @code{.*}, @code{./}, etc.
##
## This may be useful, for example, when using inline functions with
## numerical integration or optimization where a vector-valued function
## is expected.
##
## @example
## @group
## fcn = vectorize (inline ("x^2 - 1"))
##    @result{} fcn = f(x) = x.^2 - 1
## quadv (fcn, 0, 3)
##    @result{} 6
## @end group
## @end example
## @seealso{inline, formula, argnames}
## @end deftypefn

## The following function was translated directly from the original C++
## version.  Yes, it will be slow, but the use of inline functions is
## strongly discouraged anyway, and most expressions will probably be
## short.  It may also be buggy.  Well, don't use this object!  Use
## function handles instead!

function fcn = vectorize (obj)

  if (nargin != 1)
    print_usage ();
  endif

  new_expr = "";

  expr = obj.expr;
  len = length (expr);
  i = 1;

  while (i <= len)
    c = expr(i);

    if (c == "*" || c == "/" || c == "\\" || c == "^")
      if (i > 1 && expr(i-1) != ".")
        new_expr(end+1) = ".";
      endif

      ## Special case for ** operator.
      if (c == '*' && i < (len - 1) && expr(i+1) == '*')
        new_expr(end+1) = "*";
        i++;
      endif
    endif

    new_expr(end+1) = c;
    i++;

  endwhile

  fcn = inline (new_expr);

endfunction