26 #if defined (HAVE_CONFIG_H) 75 bool execution_error =
false;
81 catch (
const octave::execution_exception&
e)
87 execution_error =
true;
101 static_cast<double> (count
102 + static_cast<octave_idx_type>(1)));
121 template <
typename BNDA,
typename NDA>
133 if (
name ==
"isempty")
140 else if (
name ==
"islogical")
147 else if (
name ==
"isnumeric")
154 else if (
name ==
"isreal")
161 else if (
name ==
"length")
168 else if (
name ==
"ndims")
175 else if (
name ==
"numel" ||
name ==
"prodofsize")
182 else if (
name ==
"size")
185 error (R
"(cellfun: not enough arguments for "size")"); 187 int d = args(2).nint_value () - 1;
190 error (
"cellfun: K must be a positive integer");
198 result(count) =
static_cast<double> (
dv(
d));
205 else if (
name ==
"isclass")
208 error (R
"(cellfun: not enough arguments for "isclass")"); 224 int&
nargin,
bool& uniform_output,
234 uniform_output = args(
nargin-1).bool_value ();
237 if (args(
nargin-1).is_function_handle ()
238 || args(
nargin-1).is_inline_function ())
240 error_handler = args(
nargin-1);
242 else if (args(
nargin-1).is_string ())
249 error (
"cellfun: invalid function NAME: %s",
253 error (
"cellfun: invalid value for 'ErrorHandler' function");
256 error (
"cellfun: unrecognized parameter %s",
arg.c_str ());
402 int nargin = args.length ();
407 if (! args(1).iscell ())
408 error (
"cellfun: C must be a cell array");
419 retval = try_cellfun_internal_ops<boolNDArray,NDArray> (args,
nargin);
435 fname,
"; endfunction");
445 error (
"cellfun: invalid function NAME: %s",
name.c_str ());
451 error (
"cellfun: argument NAME must be a string or function handle");
453 bool uniform_output =
true;
469 if (
f -> is_overloaded ())
479 if (
name !=
"size" &&
name !=
"class")
487 try_cellfun_internal_ops<boolNDArray, NDArray> (tmp_args,
491 try_cellfun_internal_ops<Cell, Cell> (tmp_args,
nargin);
513 const Cell *cinputs = inputs;
521 for (
int j = 0; j <
nargin; j++)
523 if (! args(j+1).iscell ())
524 error (
"cellfun: arguments must be cells");
526 inputs[j] = args(j+1).cell_value ();
527 mask[j] = inputs[j].numel () != 1;
529 inputlist(j) = cinputs[j](0);
532 for (
int j = 0; j <
nargin; j++)
536 fdims = inputs[j].
dims ();
537 k = inputs[j].numel ();
540 if (mask[
i] && inputs[
i].
dims () != fdims)
541 error (
"cellfun: dimensions mismatch");
557 std::list<octave_value_list> idx_list (1);
558 idx_list.front ().resize (1);
565 for (
int j = 0; j <
nargin; j++)
568 inputlist.xelem (j) = cinputs[j](count);
576 error (
"cellfun: function returned fewer than nargout values");
580 &&
tmp.length () > 0 &&
tmp(0).is_defined ()))
582 int num_to_copy =
tmp.length ();
584 if (num_to_copy > nargout1)
585 num_to_copy = nargout1;
589 for (
int j = 0; j < num_to_copy; j++)
591 if (
tmp(j).is_defined ())
595 if (
val.numel () != 1)
596 error (
"cellfun: all values must be scalars when UniformOutput = true");
598 retv[j] =
val.resize (fdims);
604 for (
int j = 0; j < num_to_copy; j++)
606 if (
tmp(j).is_defined ())
610 if (! retv[j].fast_elem_insert (count,
val))
612 if (
val.numel () != 1)
613 error (
"cellfun: all values must be scalars when UniformOutput = true");
615 idx_list.front ()(0) = count + 1.0;
617 idx_type, idx_list,
val);
627 for (
int j = 0; j < nargout1; j++)
629 if (
nargout > 0 && retv[j].is_undefined ())
639 for (
int j = 0; j < nargout1; j++)
642 bool have_some_output =
false;
646 for (
int j = 0; j <
nargin; j++)
649 inputlist.xelem (j) = cinputs[j](count);
657 error (
"cellfun: function returned fewer than nargout values");
661 &&
tmp.length () > 0 &&
tmp(0).is_defined ()))
663 int num_to_copy =
tmp.length ();
665 if (num_to_copy > nargout1)
666 num_to_copy = nargout1;
669 have_some_output =
true;
671 for (
int j = 0; j < num_to_copy; j++)
672 results[j](count) =
tmp(j);
676 if (have_some_output || fdims.any_zero ())
680 for (
int j = 0; j < nargout1; j++)
1118 int nargin = args.length ();
1125 bool symbol_table_lookup =
false;
1142 fname,
"; endfunction");
1153 "arrayfun: invalid function NAME: %s",
1156 symbol_table_lookup =
true;
1167 if (! symbol_table_lookup)
1176 if (
f -> is_overloaded ())
1182 if (
f.is_defined ())
1188 bool uniform_output =
true;
1205 for (
int j = 0; j <
nargin; j++)
1207 inputs[j] = args(j+1);
1208 mask[j] = inputs[j].numel () != 1;
1211 inputlist(j) = inputs[j];
1214 for (
int j = 0; j <
nargin; j++)
1218 fdims = inputs[j].dims ();
1219 k = inputs[j].numel ();
1223 if (mask[
i] && inputs[
i].
dims () != fdims)
1225 "arrayfun: dimensions mismatch");
1241 std::list<octave_value_list> idx_list (1);
1242 idx_list.front ().resize (1);
1249 idx_list.front ()(0) = count + 1.0;
1251 for (
int j = 0; j <
nargin; j++)
1254 inputlist.xelem (j) = inputs[j].do_index_op (idx_list);
1263 "arrayfun: function returned fewer than nargout values");
1267 &&
tmp.length () > 0 &&
tmp(0).is_defined ()))
1269 int num_to_copy =
tmp.length ();
1271 if (num_to_copy > nargout1)
1272 num_to_copy = nargout1;
1276 for (
int j = 0; j < num_to_copy; j++)
1278 if (
tmp(j).is_defined ())
1282 if (
val.numel () == 1)
1283 retv[j] =
val.resize (fdims);
1286 "arrayfun: all values must be scalars when UniformOutput = true");
1292 for (
int j = 0; j < num_to_copy; j++)
1294 if (
tmp(j).is_defined ())
1298 if (! retv[j].fast_elem_insert (count,
val))
1300 if (
val.numel () == 1)
1302 idx_list.front ()(0) = count + 1.0;
1304 idx_type, idx_list,
val);
1308 "arrayfun: all values must be scalars when UniformOutput = true");
1318 for (
int j = 0; j < nargout1; j++)
1320 if (
nargout > 0 && retv[j].is_undefined ())
1328 std::list<octave_value_list> idx_list (1);
1329 idx_list.front ().resize (1);
1334 for (
int j = 0; j < nargout1; j++)
1337 bool have_some_output =
false;
1341 idx_list.front ()(0) = count + 1.0;
1343 for (
int j = 0; j <
nargin; j++)
1346 inputlist.xelem (j) = inputs[j].do_index_op (idx_list);
1355 "arrayfun: function returned fewer than nargout values");
1359 &&
tmp.length () > 0 &&
tmp(0).is_defined ()))
1361 int num_to_copy =
tmp.length ();
1363 if (num_to_copy > nargout1)
1364 num_to_copy = nargout1;
1366 if (num_to_copy > 0)
1367 have_some_output =
true;
1369 for (
int j = 0; j < num_to_copy; j++)
1370 results[j](count) =
tmp(j);
1374 if (have_some_output || fdims.any_zero ())
1378 for (
int j = 0; j < nargout1; j++)
1385 "arrayfun: argument NAME must be a string or function handle");
1612 int dvl = dimv.
numel ();
1615 for (
int i = 0;
i < dvl;
i++)
1623 perm.
clear (maxd, 1);
1624 for (
int i = 0;
i < dvl;
i++)
1626 int k = dimv(
i) - 1;
1628 error (
"num2cell: dimension indices must be positive");
1630 if (
i > 0 &&
k < dimv(
i-1) - 1)
1631 error (
"num2cell: dimension indices must be strictly increasing");
1637 for (
int k = 0,
i = dvl;
k < maxd;
k++)
1641 for (
int i = 0;
i < maxd;
i++)
1648 template <
typename NDA>
1649 static inline typename NDA::element_type
1657 template <
typename NDA>
1674 do_num2cell_helper (
array.dims (), dimv, celldv, arraydv, perm);
1676 NDA parray =
array.permute (perm);
1680 parray = parray.reshape (
dim_vector (nela, nelc));
1685 retval.xelem (
i) = NDA (parray.column (
i).reshape (arraydv));
1707 for (
int i = 0;
i < n;
i++)
1723 error (
"num2cell (A, dim) not implemented for class objects");
1743 DEFUN (num2cell, args, ,
1779 int nargin = args.length ();
1790 dimv = args(1).int_vector_value (
true);
1792 if (
array.islogical ())
1793 retval = do_num2cell (
array.bool_array_value (), dimv);
1794 else if (
array.is_char_matrix ())
1795 retval = do_num2cell (
array.char_array_value (), dimv);
1796 else if (
array.isnumeric ())
1798 if (
array.isinteger ())
1800 if (
array.is_int8_type ())
1801 retval = do_num2cell (
array.int8_array_value (), dimv);
1802 else if (
array.is_int16_type ())
1803 retval = do_num2cell (
array.int16_array_value (), dimv);
1804 else if (
array.is_int32_type ())
1805 retval = do_num2cell (
array.int32_array_value (), dimv);
1806 else if (
array.is_int64_type ())
1807 retval = do_num2cell (
array.int64_array_value (), dimv);
1808 else if (
array.is_uint8_type ())
1809 retval = do_num2cell (
array.uint8_array_value (), dimv);
1810 else if (
array.is_uint16_type ())
1811 retval = do_num2cell (
array.uint16_array_value (), dimv);
1812 else if (
array.is_uint32_type ())
1813 retval = do_num2cell (
array.uint32_array_value (), dimv);
1814 else if (
array.is_uint64_type ())
1815 retval = do_num2cell (
array.uint64_array_value (), dimv);
1817 else if (
array.iscomplex ())
1819 if (
array.is_single_type ())
1820 retval = do_num2cell (
array.float_complex_array_value (), dimv);
1822 retval = do_num2cell (
array.complex_array_value (), dimv);
1826 if (
array.is_single_type ())
1827 retval = do_num2cell (
array.float_array_value (), dimv);
1829 retval = do_num2cell (
array.array_value (), dimv);
1832 else if (
array.isobject ())
1834 else if (
array.isstruct ())
1836 else if (
array.iscell ())
1854 for (
int i = 0;
i < nd;
i++)
1863 error (
"mat2cell: mismatch on dimension %d (%d != %d)",
i+1, r,
s);
1869 template <
typename container>
1871 prepare_idx (container *idx,
int idim,
int nd,
1892 template <
typename Array2D>
1897 assert (nd == 1 || nd == 2);
1898 assert (
a.ndims () == 2);
1900 if (mat2cell_mismatch (
a.dims (),
d, nd))
1905 retval.clear (nridx, ncidx);
1908 if (
a.rows () > 1 &&
a.cols () == 1 && ncidx == 1)
1910 else if (
a.rows () == 1 && nridx == 1 && nd == 2)
1929 prepare_idx (ridx, 0, nd,
d);
1932 prepare_idx (cidx, 1, nd,
d);
1939 retval(
i,j) =
a.index (ridx[
i], cidx[j]);
1949 template <
typename ArrayND>
1956 if (mat2cell_mismatch (
a.dims (),
d, nd))
1962 for (
int i = 0;
i < nd;
i++)
1964 rdv(
i) = nidx[
i] =
d[
i].numel ();
1974 for (
int i = 0;
i < nd;
i++)
1976 idx[
i] = xidx + idxtot;
1977 prepare_idx (idx[
i],
i, nd,
d);
1989 for (
int i = 0;
i < nd;
i++)
2001 template <
typename ArrayND>
2005 if (
a.ndims () == 2 && nd <= 2)
2006 return do_mat2cell_2d (
a,
d, nd);
2008 return do_mat2cell_nd (
a,
d, nd);
2020 if (mat2cell_mismatch (
a.dims (),
d, nd))
2026 for (
int i = 0;
i < nd;
i++)
2028 rdv(
i) = nidx[
i] =
d[
i].numel ();
2038 for (
int i = 0;
i < nd;
i++)
2040 idx[
i] = xidx + idxtot;
2041 prepare_idx (idx[
i],
i, nd,
d);
2053 for (
int i = 0;
i < nd;
i++)
2064 DEFUN (mat2cell, args, ,
2109 int nargin = args.length ();
2120 d[
i-1] = args(
i).octave_idx_type_vector_value (
true);
2125 error (
"mat2cell: sparse arguments only support 2-D indexing");
2127 switch (
a.builtin_type ())
2141 retval = do_mat2cell_2d (
a.sparse_complex_matrix_value (),
d,
2148 #define BTYP_BRANCH(X, Y) \ 2150 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \ 2198 template <
typename NDA>
2200 do_cellslices_nda (
const NDA&
array,
2207 if (
array.isvector () && (dim == -1
2208 || (dim == 0 &&
array.columns () == 1)
2209 || (dim == 1 &&
array.rows () == 1)))
2234 DEFUN (cellslices, args, ,
2258 int nargin = args.length ();
2269 dim = args(3).int_value () - 1;
2271 error (
"cellslices: DIM must be a valid dimension");
2275 error (
"cellslices: the lengths of LB and UB must match");
2278 if (!
x.issparse () &&
x.is_matrix_type ())
2282 retcell = do_cellslices_nda (
x.bool_array_value (),
2284 else if (
x.is_char_matrix ())
2285 retcell = do_cellslices_nda (
x.char_array_value (),
2287 else if (
x.isinteger ())
2289 if (
x.is_int8_type ())
2290 retcell = do_cellslices_nda (
x.int8_array_value (),
2292 else if (
x.is_int16_type ())
2293 retcell = do_cellslices_nda (
x.int16_array_value (),
2295 else if (
x.is_int32_type ())
2296 retcell = do_cellslices_nda (
x.int32_array_value (),
2298 else if (
x.is_int64_type ())
2299 retcell = do_cellslices_nda (
x.int64_array_value (),
2301 else if (
x.is_uint8_type ())
2302 retcell = do_cellslices_nda (
x.uint8_array_value (),
2304 else if (
x.is_uint16_type ())
2305 retcell = do_cellslices_nda (
x.uint16_array_value (),
2307 else if (
x.is_uint32_type ())
2308 retcell = do_cellslices_nda (
x.uint32_array_value (),
2310 else if (
x.is_uint64_type ())
2311 retcell = do_cellslices_nda (
x.uint64_array_value (),
2314 else if (
x.iscomplex ())
2316 if (
x.is_single_type ())
2317 retcell = do_cellslices_nda (
x.float_complex_array_value (),
2320 retcell = do_cellslices_nda (
x.complex_array_value (),
2325 if (
x.is_single_type ())
2326 retcell = do_cellslices_nda (
x.float_array_value (),
2329 retcell = do_cellslices_nda (
x.array_value (),
2337 retcell =
Cell (1, n);
2346 idx(dim) =
Range (lb(
i), ub(
i));
2347 retcell(
i) =
x.do_index_op (idx);
2351 return ovl (retcell);
2361 DEFUN (cellindexmat, args, ,
2383 if (args.length () == 0)
2386 const Cell x = args(0).xcell_value (
"cellindexmat: X must be a cell");
2398 y(
i) =
tmp.do_index_op (idx);
OCTINTERP_API octave_value_list feval(const std::string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
octave_value find_function(const std::string &name, const octave_value_list &args=octave_value_list(), bool local_funcs=true)
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
const octave_base_value const Array< octave_idx_type > & ra_idx
#define OCTAVE_LOCAL_BUFFER_INIT(T, buf, size, value)
static void recover_from_exception(void)
bool islogical(void) const
static const idx_vector colon
OCTINTERP_API void print_usage(void)
octave_value_list slice(octave_idx_type offset, octave_idx_type len, bool tags=false) const
identity matrix If supplied two scalar respectively For allows like xample val
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
void resize(int n, int fill_value=0)
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error(const char *fmt,...)
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
bool is_defined(void) const
T & elem(octave_idx_type n)
static void get_mapper_fun_options(octave::symbol_table &symtab, const octave_value_list &args, int &nargin, bool &uniform_output, octave_value &error_handler)
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
is the outputs are concatenated into a cell array(or cell arrays). For example endfunction cellfun("factorial", -1, 2, "ErrorHandler", @foo) esult
octave_value resize(const dim_vector &dv, bool fill=false) const
std::string unique_symbol_name(const std::string &basename)
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
then the function must return scalars which will be concatenated into the return array(s). If code
int buffer_error_messages
nd deftypefn *std::string name
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
bool is_function_handle(void) const
int increment_index(octave_idx_type *idx, int start=0) const
Increment a multi-dimensional index tuple, optionally starting from an offset position and return the...
bool valid_identifier(const char *s)
octave_idx_type numel(const octave_value_list &idx)
void error_with_id(const char *id, const char *fmt,...)
returns the type of the matrix and caches it for future use Called with more than one the function will not attempt to guess the type if it is still unknown This is useful for debugging purposes The possible matrix types depend on whether the matrix is full or sparse
int first_non_singleton(int def=0) const
dim_vector dims(void) const
OCTINTERP_API std::string last_error_id(void)
octave_function * function_value(bool silent=false) const
std::string class_name(void) const
bool is_function(void) const
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
static dim_vector alloc(int n)
void err_wrong_type_arg(const char *name, const char *s)
With real return the complex result
octave::unwind_protect frame
OCTINTERP_API std::string last_error_message(void)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
charNDArray max(char d, const charNDArray &m)
octave_fcn_handle * fcn_handle_value(bool silent=false) const
This is a simple wrapper template that will subclass an Array<T> type or any later type derived from ...
static octave_value_list try_cellfun_internal_ops(const octave_value_list &args, int nargin)
bool is_undefined(void) const
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
#define BTYP_BRANCH(X, Y)
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
void assign(const std::string &k, const octave_value &val)
Cell cell_value(void) const
octave_function * extract_function(const octave_value &arg, const std::string &warn_for, const std::string &fname, const std::string &header, const std::string &trailer)
the element is set to zero In other the statement xample y
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
bool is_string(void) const
octave_idx_type ndims(void) const
Number of dimensions.
octave_idx_type numel(void) const
Number of elements in the array.
octave_idx_type length(void) const
bool strncmpi(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same, ignoring case.
Vector representing the dimensions (size) of an Array.
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
octave_value_list & prepend(const octave_value &val)
bool isnumeric(void) const
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
bool is_inline_function(void) const
static octave_value_list get_output_list(octave_idx_type count, octave_idx_type nargout, const octave_value_list &inputlist, octave_value &func, octave_value &error_handler)