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);
276 #if defined (HAVE_HDF5)
281 hid_t group_hid = -1;
283 group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
285 group_hid = H5Gcreate (loc_id, name, 0);
287 if (group_hid < 0 )
return false;
294 hid_t space_hid = -1, data_hid = -1, type_hid = -1;;
306 space_hid = H5Screate_simple (2, hdims, 0);
309 H5Gclose (group_hid);
313 data_hid = H5Dcreate (group_hid,
"args", H5T_NATIVE_CHAR, space_hid,
314 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
316 data_hid = H5Dcreate (group_hid,
"args", H5T_NATIVE_CHAR, space_hid,
321 H5Sclose (space_hid);
322 H5Gclose (group_hid);
331 const char * cptr =
ifargs(i).c_str ();
333 s[i*(len+1)+j] = *cptr++;
337 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL,
338 H5P_DEFAULT, s) >= 0;
341 H5Sclose (space_hid);
345 H5Gclose (group_hid);
350 type_hid = H5Tcopy (H5T_C_S1);
351 H5Tset_size (type_hid,
nm.length () + 1);
354 H5Gclose (group_hid);
359 space_hid = H5Screate_simple (0 , hdims, 0);
363 H5Gclose (group_hid);
367 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid,
368 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
370 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid, H5P_DEFAULT);
372 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL,
373 H5P_DEFAULT,
nm.c_str ()) < 0)
375 H5Sclose (space_hid);
377 H5Gclose (group_hid);
383 H5Tset_size (type_hid,
iftext.length () + 1);
386 H5Gclose (group_hid);
391 data_hid = H5Dcreate (group_hid,
"iftext", type_hid, space_hid,
392 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
394 data_hid = H5Dcreate (group_hid,
"iftext", type_hid, space_hid,
397 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL,
398 H5P_DEFAULT,
iftext.c_str ()) < 0)
400 H5Sclose (space_hid);
402 H5Gclose (group_hid);
407 H5Sclose (space_hid);
409 H5Gclose (group_hid);
417 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id;
422 group_hid = H5Gopen (loc_id, name, H5P_DEFAULT);
424 group_hid = H5Gopen (loc_id, name);
426 if (group_hid < 0 )
return false;
429 data_hid = H5Dopen (group_hid,
"args", H5P_DEFAULT);
431 data_hid = H5Dopen (group_hid,
"args");
433 space_hid = H5Dget_space (data_hid);
434 rank = H5Sget_simple_extent_ndims (space_hid);
439 H5Sclose (space_hid);
440 H5Gclose (group_hid);
447 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
453 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL,
454 H5P_DEFAULT, s1) < 0)
457 H5Sclose (space_hid);
458 H5Gclose (group_hid);
463 H5Sclose (space_hid);
465 for (
size_t i = 0; i < hdims[1]; i++)
466 ifargs(i) = std::string (s1 + i*hdims[0]);
469 data_hid = H5Dopen (group_hid,
"nm", H5P_DEFAULT);
471 data_hid = H5Dopen (group_hid,
"nm");
476 H5Gclose (group_hid);
480 type_hid = H5Dget_type (data_hid);
481 type_class_hid = H5Tget_class (type_hid);
483 if (type_class_hid != H5T_STRING)
487 H5Gclose (group_hid);
491 space_hid = H5Dget_space (data_hid);
492 rank = H5Sget_simple_extent_ndims (space_hid);
496 H5Sclose (space_hid);
499 H5Gclose (group_hid);
503 slen = H5Tget_size (type_hid);
506 H5Sclose (space_hid);
509 H5Gclose (group_hid);
516 st_id = H5Tcopy (H5T_C_S1);
517 H5Tset_size (st_id, slen);
519 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0)
521 H5Sclose (space_hid);
523 H5Gclose (group_hid);
531 data_hid = H5Dopen (group_hid,
"iftext", H5P_DEFAULT);
533 data_hid = H5Dopen (group_hid,
"iftext");
538 H5Gclose (group_hid);
542 type_hid = H5Dget_type (data_hid);
543 type_class_hid = H5Tget_class (type_hid);
545 if (type_class_hid != H5T_STRING)
549 H5Gclose (group_hid);
553 space_hid = H5Dget_space (data_hid);
554 rank = H5Sget_simple_extent_ndims (space_hid);
558 H5Sclose (space_hid);
561 H5Gclose (group_hid);
565 slen = H5Tget_size (type_hid);
568 H5Sclose (space_hid);
571 H5Gclose (group_hid);
578 st_id = H5Tcopy (H5T_C_S1);
579 H5Tset_size (st_id, slen);
581 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, iftext_tmp) < 0)
583 H5Sclose (space_hid);
585 H5Gclose (group_hid);
609 std::ostringstream buf;
638 @deftypefn {Built-in Function} {} inline (@var{str})\n\
639 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, @dots{})\n\
640 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\
641 Create an inline function from the character string @var{str}.\n\
642 If called with a single argument, the arguments of the generated\n\
643 function are extracted from the function itself. The generated\n\
644 function arguments will then be in alphabetical order. It should\n\
645 be noted that i, and j are ignored as arguments due to the\n\
646 ambiguity between their use as a variable or their use as an inbuilt\n\
647 constant. All arguments followed by a parenthesis are considered\n\
648 to be functions. If no arguments are found, a function taking a single\n\
649 argument named @code{x} will be created.\n\
651 If the second and subsequent arguments are character strings,\n\
652 they are the names of the arguments of the function.\n\
654 If the second argument is an integer @var{n}, the arguments are\n\
655 @qcode{\"x\"}, @qcode{\"P1\"}, @dots{}, @qcode{\"P@var{N}\"}.\n\
656 @seealso{argnames, formula, vectorize}\n\
661 int nargin = args.
length ();
665 if (args(0).is_string ())
667 std::string fun = args(0).string_value ();
673 bool in_string =
false;
676 size_t fun_length = fun.
length ();
678 while (i < fun_length)
680 bool terminate_arg =
false;
685 if (c ==
'\'' || c ==
'\"')
688 else if (c ==
'\'' || c ==
'\"')
692 terminate_arg =
true;
694 else if (! isalpha (c) && c !=
'_')
697 else if (isdigit (c))
698 tmp_arg.append (1, c);
702 while (i < fun_length && isspace (c))
707 terminate_arg =
true;
710 tmp_arg = std::string ();
716 if (c ==
'e' || c ==
'E')
720 || fun[i] ==
'-' || fun[i] ==
'+')
724 tmp_arg.append (1, c);
728 tmp_arg.append (1, c);
731 if (terminate_arg || (i == fun_length && is_arg))
733 bool have_arg =
false;
735 for (
int j = 0; j < fargs.
length (); j++)
736 if (tmp_arg == fargs (j))
742 if (! have_arg && tmp_arg !=
"i" && tmp_arg !=
"j" &&
743 tmp_arg !=
"NaN" && tmp_arg !=
"nan" &&
744 tmp_arg !=
"Inf" && tmp_arg !=
"inf" &&
745 tmp_arg !=
"NA" && tmp_arg !=
"pi" &&
746 tmp_arg !=
"e" && tmp_arg !=
"eps")
749 tmp_arg = std::string ();
758 fargs.
append (std::string (
"x"));
761 else if (nargin == 2 && args(1).is_numeric_type ())
763 if (! args(1).is_scalar_type ())
765 error (
"inline: N must be an integer");
779 for (
int i = 1; i < n+1; i++)
781 std::ostringstream buf;
783 fargs(i) = buf.str ();
788 error (
"inline: N must be a positive integer or zero");
794 error (
"inline: N must be an integer");
800 fargs.
resize (nargin - 1);
802 for (
int i = 1; i < nargin; i++)
804 if (args(i).is_string ())
806 std::string s = args(i).string_value ();
811 error (
"inline: expecting string arguments");
820 error (
"inline: STR argument must be a string");
847 @deftypefn {Built-in Function} {} formula (@var{fun})\n\
848 Return a character string representing the inline function @var{fun}.\n\
849 Note that @code{char (@var{fun})} is equivalent to\n\
850 @code{formula (@var{fun})}.\n\
851 @seealso{argnames, inline, vectorize}\n\
856 int nargin = args.
length ();
865 error (
"formula: FUN must be an inline function");
885 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\
886 Return a cell array of character strings containing the names of\n\
887 the arguments of the inline function @var{fun}.\n\
888 @seealso{inline, formula, vectorize}\n\
893 int nargin = args.
length ();
905 for (
int i = 0; i < t1.
length (); i++)
911 error (
"argnames: FUN must be an inline function");
932 @deftypefn {Built-in Function} {} vectorize (@var{fun})\n\
933 Create a vectorized version of the inline function @var{fun}\n\
934 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\
935 @code{.*}, @code{./}, etc.\n\
937 This may be useful, for example, when using inline functions with\n\
938 numerical integration or optimization where a vector-valued function\n\
943 fcn = vectorize (inline (\"x^2 - 1\"))\n\
944 @result{} fcn = f(x) = x.^2 - 1\n\
949 @seealso{inline, formula, argnames}\n\
954 int nargin = args.
length ();
958 std::string old_func;
960 bool func_is_string =
true;
962 if (args(0).is_string ())
967 func_is_string =
false;
972 error (
"vectorize: FUN must be a string or inline function");
977 std::string new_func;
980 while (i < old_func.length ())
982 std::string t1 = old_func.substr (i, 1);
984 if (t1 ==
"*" || t1 ==
"/" || t1 ==
"\\" || t1 ==
"^")
986 if (i && old_func.substr (i-1, 1) !=
".")
987 new_func.append (
".");
990 if (t1 ==
"*" && i < (old_func.length () - 1)
991 && old_func.substr (i+1, 1) ==
"*")
993 new_func.append (
"*");
997 new_func.append (t1);