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>
129 const Cell f_args =
args(1).cell_value ();
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 (
"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 (
"cellfun: not enough arguments for \"isclass\"");
225 while (nargin > 3 &&
args(nargin-2).is_string ())
229 size_t compare_len =
std::max (arg.length (),
static_cast<size_t> (2));
232 uniform_output =
args(nargin-1).bool_value ();
235 if (
args(nargin-1).is_function_handle ()
236 ||
args(nargin-1).is_inline_function ())
238 error_handler =
args(nargin-1);
240 else if (
args(nargin-1).is_string ())
247 error (
"cellfun: invalid function NAME: %s",
251 error (
"cellfun: invalid value for 'ErrorHandler' function");
254 error (
"cellfun: unrecognized parameter %s", arg.c_str ());
406 error (
"cellfun: C must be a cell array");
415 retval = try_cellfun_internal_ops<boolNDArray,NDArray> (
args,
nargin);
417 if (! retval.
empty ())
431 fname,
"; endfunction");
441 error (
"cellfun: invalid function NAME: %s", name.c_str ());
447 error (
"cellfun: argument NAME must be a string or function handle");
449 bool uniform_output =
true;
465 if (f -> is_overloaded ())
475 if (name !=
"size" && name !=
"class")
483 try_cellfun_internal_ops<boolNDArray, NDArray> (tmp_args,
487 try_cellfun_internal_ops<Cell, Cell> (tmp_args,
nargin);
489 if (! retval.empty ())
509 const Cell *cinputs = inputs;
517 for (
int j = 0; j <
nargin; j++)
519 if (!
args(j+1).is_cell ())
520 error (
"cellfun: arguments must be cells");
522 inputs[j] =
args(j+1).cell_value ();
523 mask[j] = inputs[j].numel () != 1;
525 inputlist(j) = cinputs[j](0);
528 for (
int j = 0; j <
nargin; j++)
532 fdims = inputs[j].
dims ();
533 k = inputs[j].numel ();
536 if (mask[
i] && inputs[
i].
dims () != fdims)
537 error (
"cellfun: dimensions mismatch");
553 std::list<octave_value_list> idx_list (1);
554 idx_list.front ().resize (1);
561 for (
int j = 0; j <
nargin; j++)
564 inputlist.xelem (j) = cinputs[j](count);
572 error (
"cellfun: function returned fewer than nargout values");
576 && tmp.
length () > 0 &&
tmp(0).is_defined ()))
578 int num_to_copy = tmp.
length ();
580 if (num_to_copy > nargout1)
581 num_to_copy = nargout1;
585 for (
int j = 0; j < num_to_copy; j++)
587 if (
tmp(j).is_defined ())
591 if (val.
numel () != 1)
592 error (
"cellfun: all values must be scalars when UniformOutput = true");
594 retv[j] = val.
resize (fdims);
600 for (
int j = 0; j < num_to_copy; j++)
602 if (
tmp(j).is_defined ())
606 if (! retv[j].fast_elem_insert (count, val))
608 if (val.
numel () != 1)
609 error (
"cellfun: all values must be scalars when UniformOutput = true");
611 idx_list.front ()(0) = count + 1.0;
613 idx_type, idx_list, val);
623 for (
int j = 0; j < nargout1; j++)
625 if (
nargout > 0 && retv[j].is_undefined ())
635 for (
int j = 0; j < nargout1; j++)
638 bool have_some_output =
false;
642 for (
int j = 0; j <
nargin; j++)
645 inputlist.xelem (j) = cinputs[j](count);
653 error (
"cellfun: function returned fewer than nargout values");
657 && tmp.
length () > 0 &&
tmp(0).is_defined ()))
659 int num_to_copy = tmp.
length ();
661 if (num_to_copy > nargout1)
662 num_to_copy = nargout1;
665 have_some_output =
true;
667 for (
int j = 0; j < num_to_copy; j++)
668 results[j](count) =
tmp(j);
672 if (have_some_output || fdims.any_zero ())
676 for (
int j = 0; j < nargout1; j++)
1114 int nargin = args.length ();
1121 bool symbol_table_lookup =
false;
1132 std::string fname =
"function y = " + fcn_name +
"(x) y = ";
1136 fname,
"; endfunction");
1147 "arrayfun: invalid function NAME: %s",
1150 symbol_table_lookup =
true;
1161 if (! symbol_table_lookup)
1170 if (f -> is_overloaded ())
1182 bool uniform_output =
true;
1198 for (
int j = 0; j <
nargin; j++)
1200 inputs[j] =
args(j+1);
1201 mask[j] = inputs[j].numel () != 1;
1204 inputlist(j) = inputs[j];
1207 for (
int j = 0; j <
nargin; j++)
1211 fdims = inputs[j].dims ();
1212 k = inputs[j].numel ();
1216 if (mask[
i] && inputs[
i].
dims () != fdims)
1218 "arrayfun: dimensions mismatch");
1234 std::list<octave_value_list> idx_list (1);
1235 idx_list.front ().resize (1);
1242 idx_list.front ()(0) = count + 1.0;
1244 for (
int j = 0; j <
nargin; j++)
1247 inputlist.xelem (j) = inputs[j].do_index_op (idx_list);
1256 "arrayfun: function returned fewer than nargout values");
1260 && tmp.
length () > 0 &&
tmp(0).is_defined ()))
1262 int num_to_copy = tmp.
length ();
1264 if (num_to_copy > nargout1)
1265 num_to_copy = nargout1;
1269 for (
int j = 0; j < num_to_copy; j++)
1271 if (
tmp(j).is_defined ())
1275 if (val.
numel () == 1)
1276 retv[j] = val.
resize (fdims);
1279 "arrayfun: all values must be scalars when UniformOutput = true");
1285 for (
int j = 0; j < num_to_copy; j++)
1287 if (
tmp(j).is_defined ())
1291 if (! retv[j].fast_elem_insert (count, val))
1293 if (val.
numel () == 1)
1295 idx_list.front ()(0) = count + 1.0;
1297 idx_type, idx_list, val);
1301 "arrayfun: all values must be scalars when UniformOutput = true");
1309 retval.
resize (nargout1);
1311 for (
int j = 0; j < nargout1; j++)
1313 if (
nargout > 0 && retv[j].is_undefined ())
1321 std::list<octave_value_list> idx_list (1);
1322 idx_list.front ().resize (1);
1327 for (
int j = 0; j < nargout1; j++)
1330 bool have_some_output =
false;
1334 idx_list.front ()(0) = count + 1.0;
1336 for (
int j = 0; j <
nargin; j++)
1339 inputlist.xelem (j) = inputs[j].do_index_op (idx_list);
1348 "arrayfun: function returned fewer than nargout values");
1352 && tmp.
length () > 0 &&
tmp(0).is_defined ()))
1354 int num_to_copy = tmp.
length ();
1356 if (num_to_copy > nargout1)
1357 num_to_copy = nargout1;
1359 if (num_to_copy > 0)
1360 have_some_output =
true;
1362 for (
int j = 0; j < num_to_copy; j++)
1363 results[j](count) =
tmp(j);
1367 if (have_some_output || fdims.any_zero ())
1369 retval.
resize (nargout1);
1371 for (
int j = 0; j < nargout1; j++)
1378 "arrayfun: argument NAME must be a string or function handle");
1605 int dvl = dimv.
numel ();
1606 int maxd = dv.
ndims ();
1608 for (
int i = 0;
i < dvl;
i++)
1610 if (maxd > dv.
ndims ())
1616 perm.
clear (maxd, 1);
1617 for (
int i = 0;
i < dvl;
i++)
1619 int k = dimv(
i) - 1;
1621 error (
"num2cell: dimension indices must be positive");
1623 if (
i > 0 && k < dimv(
i-1) - 1)
1624 error (
"num2cell: dimension indices must be strictly increasing");
1630 for (
int k = 0,
i = dvl; k < maxd; k++)
1634 for (
int i = 0;
i < maxd;
i++)
1641 template <
typename NDA>
1642 static inline typename NDA::element_type
1644 {
return array(i); }
1650 template <
typename NDA>
1652 do_num2cell (
const NDA& array,
const Array<int>& dimv)
1659 retval.
xelem (i) = do_num2cell_elem (array, i);
1667 do_num2cell_helper (array.dims (), dimv, celldv, arraydv, perm);
1669 NDA parray = array.
permute (perm);
1673 parray = parray.reshape (
dim_vector (nela, nelc));
1678 retval.
xelem (i) = NDA (parray.column (i).reshape (arraydv));
1700 for (
int i = 0; i < n; i++)
1716 error (
"num2cell (A, dim) not implemented for class objects");
1736 DEFUN (num2cell, args, ,
1772 int nargin = args.length ();
1774 if (nargin < 1 || nargin > 2)
1783 dimv =
args(1).int_vector_value (
true);
1822 retval = do_num2cell (array.
array_value (), dimv);
1826 retval = do_object2cell (array, dimv);
1827 else if (array.
is_map ())
1828 retval = do_num2cell (array.
map_value (), dimv);
1830 retval = do_num2cell (array.
cell_value (), dimv);
1847 for (
int i = 0; i < nd; i++)
1856 error (
"mat2cell: mismatch on dimension %d (%d != %d)", i+1, r, s);
1862 template <
typename container>
1864 prepare_idx (container *idx,
int idim,
int nd,
1885 template <
typename Array2D>
1890 assert (nd == 1 || nd == 2);
1891 assert (a.ndims () == 2);
1893 if (mat2cell_mismatch (a.dims (),
d, nd))
1898 retval.clear (nridx, ncidx);
1901 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1)
1903 else if (a.rows () == 1 && nridx == 1 && nd == 2)
1922 prepare_idx (ridx, 0, nd, d);
1925 prepare_idx (cidx, 1, nd, d);
1932 retval(i,j) = a.index (ridx[i], cidx[j]);
1942 template <
typename ArrayND>
1949 if (mat2cell_mismatch (a.dims (),
d, nd))
1955 for (
int i = 0; i < nd; i++)
1957 rdv(i) = nidx[
i] = d[
i].
numel ();
1967 for (
int i = 0; i < nd; i++)
1969 idx[
i] = xidx + idxtot;
1970 prepare_idx (idx[i], i, nd, d);
1982 for (
int i = 0; i < nd; i++)
1994 template <
typename ArrayND>
1998 if (a.ndims () == 2 && nd <= 2)
1999 return do_mat2cell_2d (a, d, nd);
2001 return do_mat2cell_nd (a, d, nd);
2013 if (mat2cell_mismatch (a.
dims (),
d, nd))
2019 for (
int i = 0; i < nd; i++)
2021 rdv(i) = nidx[
i] = d[
i].
numel ();
2031 for (
int i = 0; i < nd; i++)
2033 idx[
i] = xidx + idxtot;
2034 prepare_idx (idx[i], i, nd, d);
2046 for (
int i = 0; i < nd; i++)
2057 DEFUN (mat2cell, args, ,
2102 int nargin = args.length ();
2112 for (
int i = 1; i <
nargin; i++)
2113 d[i-1] =
args(i).octave_idx_type_vector_value (
true);
2117 if (sparse && nargin > 3)
2118 error (
"mat2cell: sparse arguments only support 2-D indexing");
2127 retval = do_mat2cell (a.
array_value (),
d, nargin - 1);
2141 #define BTYP_BRANCH(X, Y) \
2143 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \
2170 retval = do_mat2cell (a, d, nargin-1);
2191 template <
typename NDA>
2193 do_cellslices_nda (
const NDA& array,
2200 if (array.is_vector () && (dim == -1
2201 || (dim == 0 && array.columns () == 1)
2202 || (dim == 1 && array.rows () == 1)))
2210 int ndims = dv.
ndims ();
2220 retval(i) = array.index (idx);
2227 DEFUN (cellslices, args, ,
2251 int nargin = args.length ();
2253 if (nargin < 3 || nargin > 4)
2262 dim =
args(3).int_value () - 1;
2264 error (
"cellslices: DIM must be a valid dimension");
2268 error (
"cellslices: the lengths of LB and UB must match");
2330 retcell =
Cell (1, n);
2332 int ndims = dv.
ndims ();
2339 idx(dim) =
Range (lb(i), ub(i));
2344 return ovl (retcell);
2354 DEFUN (cellindexmat, args, ,
2376 if (args.length () == 0)
2379 const Cell x =
args(0).xcell_value (
"cellindexmat: X must be a cell");
uint8NDArray uint8_array_value(void) const
bool is_object(void) const
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
bool is_empty(void) const
charNDArray char_array_value(bool frc_str_conv=false) const
bool is_real_type(void) const
const octave_base_value const Array< octave_idx_type > & ra_idx
octave_value_list slice(octave_idx_type offset, octave_idx_type len, bool tags=false) const
#define OCTAVE_LOCAL_BUFFER_INIT(T, buf, size, value)
int8NDArray int8_array_value(void) const
Array< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
static const idx_vector colon
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
bool is_uint16_type(void) const
OCTINTERP_API void print_usage(void)
bool is_function(void) const
octave_idx_type numel(void) const
Number of elements in the array.
OCTAVE_EXPORT octave_value_list uint16
identity matrix If supplied two scalar respectively For allows like xample val
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
int16NDArray int16_array_value(void) const
octave_idx_type length(void) const
octave_map map_value(void) const
bool is_numeric_type(void) const
bool is_defined(void) const
void resize(int n, int fill_value=0)
uint64NDArray uint64_array_value(void) const
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
int32NDArray int32_array_value(void) const
static void get_mapper_fun_options(const octave_value_list &args, int &nargin, bool &uniform_output, octave_value &error_handler)
bool is_int8_type(void) const
octave_value resize(const dim_vector &dv, bool fill=false) const
T & elem(octave_idx_type n)
bool is_int32_type(void) const
octave_fcn_handle * fcn_handle_value(bool silent=false) const
static octave_value find_function(const std::string &name, const octave_value_list &args=octave_value_list(), bool local_funcs=true)
int increment_index(octave_idx_type *idx, int start=0) const
boolNDArray bool_array_value(bool warn=false) const
int first_non_singleton(int def=0) const
bool is_function_handle(void) const
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
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
Cell cell_value(void) const
int64NDArray int64_array_value(void) const
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
octave_value single_subsref(const std::string &type, const octave_value_list &idx)
octave_value_list do_multi_index_op(int nargout, const octave_value_list &idx)
FloatNDArray float_array_value(bool frc_str_conv=false) const
int buffer_error_messages
bool is_char_matrix(void) const
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_sparse_type(void) const
bool valid_identifier(const char *s)
octave_idx_type numel(const octave_value_list &idx)
bool is_bool_type(void) const
OCTAVE_EXPORT octave_value_list uint32
void error_with_id(const char *id, const char *fmt,...)
nd deftypefn *octave_map m
then the function must return scalars which will be concatenated into the return array(s).If code
bool is_matrix_type(void) const
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
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
bool is_string(void) const
OCTAVE_EXPORT octave_value_list int16
OCTINTERP_API std::string last_error_id(void)
bool is_inline_function(void) const
bool is_complex_type(void) const
void resize(const dim_vector &dv, const T &rfv)
bool is_int64_type(void) const
OCTAVE_EXPORT octave_value_list int32
is the outputs are concatenated into a cell array(or cell arrays).For example endfunction cellfun("factorial",-1, 2,"ErrorHandler",@foo) esult
octave_idx_type length(void) const
SparseComplexMatrix sparse_complex_matrix_value(bool frc_str_conv=false) 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)
dim_vector dims(void) const
void err_wrong_type_arg(const char *name, const char *s)
With real return the complex result
octave_function * function_value(bool silent=false) const
T & xelem(octave_idx_type n)
bool is_int16_type(void) const
OCTAVE_EXPORT octave_value_list int64
octave::unwind_protect frame
void recover_from_exception(void)
OCTINTERP_API std::string last_error_message(void)
charNDArray max(char d, const charNDArray &m)
bool is_empty(void) const
This is a simple wrapper template that will subclass an Array type or any later type derived from ...
static octave_value_list try_cellfun_internal_ops(const octave_value_list &args, int nargin)
NDArray array_value(bool frc_str_conv=false) const
#define BTYP_BRANCH(X, Y)
=val(i)}if ode{val(i)}occurs in table i
bool is_uint8_type(void) const
void assign(const std::string &k, const octave_value &val)
issues an error eealso double
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)
octave_idx_type ndims(void) const
Number of dimensions.
std::string class_name(void) const
the element is set to zero In other the statement xample y
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
bool is_undefined(void) const
bool is_uint64_type(void) const
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
SparseMatrix sparse_matrix_value(bool frc_str_conv=false) const
bool is_single_type(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.
bool is_uint32_type(void) const
Vector representing the dimensions (size) of an Array.
octave_value & xelem(octave_idx_type i)
uint32NDArray uint32_array_value(void) const
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
builtin_type_t builtin_type(void) const
uint16NDArray uint16_array_value(void) const
octave_value_list & prepend(const octave_value &val)
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
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)
bool is_integer_type(void) const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)