69 std::ostringstream buf;
86 if (parse_status == 0)
105 if (parent_scope < 0)
106 parent_scope = curr_fcn->
scope ();
115 error (
"inline: unable to define function");
125 m.
assign (
"version", 1.0);
126 m.
assign (
"isEmpty", 0.0);
134 std::ostringstream buf;
136 for (
int i = 0; i < args.
length (); i++)
137 buf << args(i) <<
" = INLINE_INPUTS_{" << i + 1 <<
"}; ";
139 m.
assign (
"inputExpr", buf.str ());
150 if (
nm.length () < 1)
166 for (
int i = 0; i < nargs; i++)
199 os.write (reinterpret_cast<char *> (&tmp), 4);
203 os.write (reinterpret_cast<char *> (&tmp), 4);
207 os.write (reinterpret_cast<char *> (&tmp), 4);
208 os.write (
nm.c_str (),
nm.length ());
210 os.write (reinterpret_cast<char *> (&tmp), 4);
220 if (! is.read (reinterpret_cast<char *> (&nargs), 4))
231 for (
int i = 0; i < nargs; i++)
233 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
240 ifargs(i) = std::string (ctmp);
246 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
252 is.read (ctmp1, tmp);
253 nm = std::string (ctmp1);
258 if (! is.read (reinterpret_cast<char *> (&tmp), 4))
264 is.read (ctmp2, tmp);
265 iftext = std::string (ctmp2);
282 #if defined (HAVE_HDF5)
284 hid_t group_hid = -1;
287 group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
289 group_hid = H5Gcreate (loc_id, name, 0);
291 if (group_hid < 0)
return false;
298 hid_t space_hid, data_hid, type_hid;
299 space_hid = data_hid = type_hid = -1;
310 space_hid = H5Screate_simple (2, hdims, 0);
313 H5Gclose (group_hid);
317 data_hid = H5Dcreate (group_hid,
"args", H5T_NATIVE_CHAR, space_hid,
318 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
320 data_hid = H5Dcreate (group_hid,
"args", H5T_NATIVE_CHAR, space_hid,
325 H5Sclose (space_hid);
326 H5Gclose (group_hid);
335 const char * cptr =
ifargs(i).c_str ();
337 s[i*(len+1)+j] = *cptr++;
341 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL,
342 H5P_DEFAULT, s) >= 0;
345 H5Sclose (space_hid);
349 H5Gclose (group_hid);
354 type_hid = H5Tcopy (H5T_C_S1);
355 H5Tset_size (type_hid,
nm.length () + 1);
358 H5Gclose (group_hid);
363 space_hid = H5Screate_simple (0 , hdims, 0);
367 H5Gclose (group_hid);
371 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid,
372 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
374 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid, H5P_DEFAULT);
376 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL,
377 H5P_DEFAULT,
nm.c_str ()) < 0)
379 H5Sclose (space_hid);
381 H5Gclose (group_hid);
387 H5Tset_size (type_hid,
iftext.length () + 1);
390 H5Gclose (group_hid);
395 data_hid = H5Dcreate (group_hid,
"iftext", type_hid, space_hid,
396 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
398 data_hid = H5Dcreate (group_hid,
"iftext", type_hid, space_hid,
401 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL,
402 H5P_DEFAULT,
iftext.c_str ()) < 0)
404 H5Sclose (space_hid);
406 H5Gclose (group_hid);
411 H5Sclose (space_hid);
413 H5Gclose (group_hid);
425 #if defined (HAVE_HDF5)
427 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id;
432 group_hid = H5Gopen (loc_id, name, H5P_DEFAULT);
434 group_hid = H5Gopen (loc_id, name);
436 if (group_hid < 0)
return false;
439 data_hid = H5Dopen (group_hid,
"args", H5P_DEFAULT);
441 data_hid = H5Dopen (group_hid,
"args");
443 space_hid = H5Dget_space (data_hid);
444 rank = H5Sget_simple_extent_ndims (space_hid);
449 H5Sclose (space_hid);
450 H5Gclose (group_hid);
457 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
463 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL,
464 H5P_DEFAULT, s1) < 0)
467 H5Sclose (space_hid);
468 H5Gclose (group_hid);
473 H5Sclose (space_hid);
475 for (
size_t i = 0; i < hdims[1]; i++)
476 ifargs(i) = std::string (s1 + i*hdims[0]);
479 data_hid = H5Dopen (group_hid,
"nm", H5P_DEFAULT);
481 data_hid = H5Dopen (group_hid,
"nm");
486 H5Gclose (group_hid);
490 type_hid = H5Dget_type (data_hid);
491 type_class_hid = H5Tget_class (type_hid);
493 if (type_class_hid != H5T_STRING)
497 H5Gclose (group_hid);
501 space_hid = H5Dget_space (data_hid);
502 rank = H5Sget_simple_extent_ndims (space_hid);
506 H5Sclose (space_hid);
509 H5Gclose (group_hid);
513 slen = H5Tget_size (type_hid);
516 H5Sclose (space_hid);
519 H5Gclose (group_hid);
526 st_id = H5Tcopy (H5T_C_S1);
527 H5Tset_size (st_id, slen);
529 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0)
531 H5Sclose (space_hid);
533 H5Gclose (group_hid);
541 data_hid = H5Dopen (group_hid,
"iftext", H5P_DEFAULT);
543 data_hid = H5Dopen (group_hid,
"iftext");
548 H5Gclose (group_hid);
552 type_hid = H5Dget_type (data_hid);
553 type_class_hid = H5Tget_class (type_hid);
555 if (type_class_hid != H5T_STRING)
559 H5Gclose (group_hid);
563 space_hid = H5Dget_space (data_hid);
564 rank = H5Sget_simple_extent_ndims (space_hid);
568 H5Sclose (space_hid);
571 H5Gclose (group_hid);
575 slen = H5Tget_size (type_hid);
578 H5Sclose (space_hid);
581 H5Gclose (group_hid);
588 st_id = H5Tcopy (H5T_C_S1);
589 H5Tset_size (st_id, slen);
591 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, iftext_tmp) < 0)
593 H5Sclose (space_hid);
595 H5Gclose (group_hid);
623 std::ostringstream buf;
652 @deftypefn {Built-in Function} {} inline (@var{str})\n\
653 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, @dots{})\n\
654 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\
655 Create an inline function from the character string @var{str}.\n\
657 If called with a single argument, the arguments of the generated function\n\
658 are extracted from the function itself. The generated function arguments\n\
659 will then be in alphabetical order. It should be noted that i and j are\n\
660 ignored as arguments due to the ambiguity between their use as a variable or\n\
661 their use as an built-in constant. All arguments followed by a parenthesis\n\
662 are considered to be functions. If no arguments are found, a function\n\
663 taking a single argument named @code{x} will be created.\n\
665 If the second and subsequent arguments are character strings, they are the\n\
666 names of the arguments of the function.\n\
668 If the second argument is an integer @var{n}, the arguments are\n\
669 @qcode{\"x\"}, @qcode{\"P1\"}, @dots{}, @qcode{\"P@var{N}\"}.\n\
671 Programming Note: The use of @code{inline} is discouraged and it may be\n\
672 removed from a future version of Octave. The preferred way to create\n\
673 functions from strings is through the use of anonymous functions\n\
674 (@pxref{Anonymous Functions}) or @code{str2func}.\n\
675 @seealso{argnames, formula, vectorize, str2func}\n\
680 int nargin = args.
length ();
684 if (args(0).is_string ())
686 std::string fun = args(0).string_value ();
692 bool in_string =
false;
695 size_t fun_length = fun.
length ();
697 while (i < fun_length)
699 bool terminate_arg =
false;
704 if (c ==
'\'' || c ==
'\"')
707 else if (c ==
'\'' || c ==
'\"')
711 terminate_arg =
true;
713 else if (! isalpha (c) && c !=
'_')
716 else if (isdigit (c))
717 tmp_arg.append (1, c);
721 while (i < fun_length && isspace (c))
726 terminate_arg =
true;
729 tmp_arg = std::string ();
735 if (c ==
'e' || c ==
'E')
739 || fun[i] ==
'-' || fun[i] ==
'+')
743 tmp_arg.append (1, c);
747 tmp_arg.append (1, c);
750 if (terminate_arg || (i == fun_length && is_arg))
752 bool have_arg =
false;
754 for (
int j = 0; j < fargs.
length (); j++)
755 if (tmp_arg == fargs (j))
761 if (! have_arg && tmp_arg !=
"i" && tmp_arg !=
"j"
762 && tmp_arg !=
"NaN" && tmp_arg !=
"nan"
763 && tmp_arg !=
"Inf" && tmp_arg !=
"inf"
764 && tmp_arg !=
"NA" && tmp_arg !=
"pi"
765 && tmp_arg !=
"e" && tmp_arg !=
"eps")
768 tmp_arg = std::string ();
777 fargs.
append (std::string (
"x"));
780 else if (nargin == 2 && args(1).is_numeric_type ())
782 if (! args(1).is_scalar_type ())
784 error (
"inline: N must be an integer");
798 for (
int i = 1; i < n+1; i++)
800 std::ostringstream buf;
802 fargs(i) = buf.str ();
807 error (
"inline: N must be a positive integer or zero");
813 error (
"inline: N must be an integer");
819 fargs.
resize (nargin - 1);
821 for (
int i = 1; i < nargin; i++)
823 if (args(i).is_string ())
825 std::string s = args(i).string_value ();
830 error (
"inline: expecting string arguments");
839 error (
"inline: STR argument must be a string");
864 DEFUN (formula, args, ,
866 @deftypefn {Built-in Function} {} formula (@var{fun})\n\
867 Return a character string representing the inline function @var{fun}.\n\
869 Note that @code{char (@var{fun})} is equivalent to\n\
870 @code{formula (@var{fun})}.\n\
871 @seealso{char, argnames, inline, vectorize}\n\
876 int nargin = args.
length ();
885 error (
"formula: FUN must be an inline function");
903 DEFUN (argnames, args, ,
905 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\
906 Return a cell array of character strings containing the names of the\n\
907 arguments of the inline function @var{fun}.\n\
908 @seealso{inline, formula, vectorize}\n\
913 int nargin = args.
length ();
925 for (
int i = 0; i < t1.
length (); i++)
931 error (
"argnames: FUN must be an inline function");
950 DEFUN (vectorize, args, ,
952 @deftypefn {Built-in Function} {} vectorize (@var{fun})\n\
953 Create a vectorized version of the inline function @var{fun} by replacing\n\
954 all occurrences of @code{*}, @code{/}, etc., with @code{.*}, @code{./}, etc.\n\
956 This may be useful, for example, when using inline functions with numerical\n\
957 integration or optimization where a vector-valued function is expected.\n\
961 fcn = vectorize (inline (\"x^2 - 1\"))\n\
962 @result{} fcn = f(x) = x.^2 - 1\n\
967 @seealso{inline, formula, argnames}\n\
972 int nargin = args.
length ();
976 std::string old_func;
978 bool func_is_string =
true;
980 if (args(0).is_string ())
985 func_is_string =
false;
990 error (
"vectorize: FUN must be a string or inline function");
995 std::string new_func;
998 while (i < old_func.length ())
1000 std::string t1 = old_func.substr (i, 1);
1002 if (t1 ==
"*" || t1 ==
"/" || t1 ==
"\\" || t1 ==
"^")
1004 if (i && old_func.substr (i-1, 1) !=
".")
1005 new_func.append (
".");
1008 if (t1 ==
"*" && i < (old_func.length () - 1)
1009 && old_func.substr (i+1, 1) ==
"*")
1011 new_func.append (
"*");
1015 new_func.append (t1);
std::string read_until_newline(std::istream &is, bool keep_newline)
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
OCTINTERP_API void print_usage(void)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
void gripe_load(const char *type) const
octave_map map_value(void) const
int int_value(bool req_int=false, bool frc_str_conv=false) const
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
OCTAVE_EXPORT octave_value_list Finline(const octave_value_list &args, int)
bool save_binary(std::ostream &os, bool &save_as_floats)
static octave_function * current(void)
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
void gripe_save(const char *type) const
void skip_preceeding_newline(std::istream &is)
octave_value_list eval_string(const std::string &eval_str, bool silent, int &parse_status, int nargout)
octave_value fcn_val(void) const
octave_fcn_handle * fcn_handle_value(bool silent=false) const
bool load_ascii(std::istream &is)
void newline(std::ostream &os) const
std::string fcn_text(void) const
void print(std::ostream &os, bool pr_as_read_syntax=false)
virtual symbol_table::scope_id scope(void)
std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only)
void swap_bytes< 4 >(void *ptr)
F77_RET_T const double const double * f
virtual std::string string_value(bool force=false) const
void resize(octave_idx_type n, const std::string &rfv=std::string())
octave_user_function * user_function_value(bool silent=false) const
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
string_vector & append(const std::string &s)
void stash_parent_fcn_scope(symbol_table::scope_id ps)
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
#define DEFUNX(name, fname, args_name, nargout_name, doc)
virtual symbol_table::scope_id parent_fcn_scope(void) const
string_vector & sort(bool make_uniq=false)
octave_idx_type length(void) const
octave_value convert_to_str_internal(bool, bool, char) const
friend class octave_value
bool save_ascii(std::ostream &os)
octave_idx_type length(void) const
Number of elements in the array.
int current_print_indent_level(void) const
void assign(const std::string &k, const octave_value &val)
void octave_print_internal(std::ostream &, char, bool)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
bool is_undefined(void) const
string_vector fcn_arg_names(void) const
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
octave_fcn_inline * fcn_inline_value(bool=false)