26#if defined (HAVE_CONFIG_H)
76 bool execution_error =
false;
80 tmp = interp.
feval (fcn, inputlist, nargout);
91 execution_error =
true;
107 static_cast<double> (count
113 tmp = interp.
feval (error_handler, errlist, nargout);
125template <
typename BNDA,
typename NDA>
131 std::string name = args(0).string_value ();
137 if (name ==
"isempty")
139 BNDA result (f_args.
dims ());
144 else if (name ==
"islogical")
146 BNDA result (f_args.
dims ());
151 else if (name ==
"isnumeric")
153 BNDA result (f_args.
dims ());
158 else if (name ==
"isreal")
160 BNDA result (f_args.
dims ());
162 result(count) = f_args.
elem (count).
isreal ();
165 else if (name ==
"length")
167 NDA result (f_args.
dims ());
169 result(count) =
static_cast<double> (f_args.
elem (count).
length ());
172 else if (name ==
"ndims")
174 NDA result (f_args.
dims ());
176 result(count) =
static_cast<double> (f_args.
elem (count).
ndims ());
179 else if (name ==
"numel" || name ==
"prodofsize")
181 NDA result (f_args.
dims ());
183 result(count) =
static_cast<double> (f_args.
elem (count).
numel ());
186 else if (name ==
"size")
189 error (R
"(cellfun: not enough arguments for "size")");
191 int d = args(2).strict_int_value () - 1;
194 error (
"cellfun: K must be a positive integer");
196 NDA result (f_args.
dims ());
202 result(count) =
static_cast<double> (dv(
d));
209 else if (name ==
"isclass")
212 error (R
"(cellfun: not enough arguments for "isclass")");
214 std::string class_name = args(2).string_value ();
215 BNDA result (f_args.dims ());
217 result(count) = (f_args.
elem (count).
class_name () == class_name);
228 int& nargin,
bool& uniform_output,
231 while (nargin > 3 && args(nargin-2).is_string ())
233 std::string arg = args(nargin-2).string_value ();
235 std::size_t compare_len
236 = std::max (arg.length (),
static_cast<std::size_t
> (2));
238 if (string::strncmpi (arg,
"uniformoutput", compare_len))
239 uniform_output = args(nargin-1).bool_value ();
240 else if (string::strncmpi (arg,
"errorhandler", compare_len))
242 if (args(nargin-1).is_function_handle ()
243 || args(nargin-1).is_inline_function ())
245 error_handler = args(nargin-1);
247 else if (args(nargin-1).is_string ())
249 std::string err_name = args(nargin-1).string_value ();
254 error (
"cellfun: invalid function NAME: %s",
258 error (
"cellfun: invalid value for 'ErrorHandler' function");
261 error (
"cellfun: unrecognized parameter %s", arg.c_str ());
269DEFMETHOD (cellfun, interp, args, nargout,
408 int nargin = args.
length ();
413 if (! args(1).iscell ())
414 error (
"cellfun: C must be a cell array");
417 int nargout1 = (nargout < 1 ? 1 : nargout);
425 retval = try_cellfun_internal_ops<boolNDArray, NDArray> (args, nargin);
427 if (! retval.
empty ())
432 std::string name = args(0).string_value ();
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;
452 get_mapper_fun_options (symtab, args, nargin, uniform_output, error_handler);
474 if (name !=
"size" && name !=
"class")
481 retval = try_cellfun_internal_ops<boolNDArray, NDArray> (tmp_args, nargin);
483 retval = try_cellfun_internal_ops<Cell, Cell> (tmp_args, nargin);
485 if (! retval.
empty ())
505 const Cell *cinputs = inputs;
513 for (
int j = 0; j < nargin; j++)
515 if (! args(j+1).iscell ())
516 error (
"cellfun: arguments must be cells");
519 mask[j] = inputs[j].
numel () != 1;
521 inputlist(j) = cinputs[j](0);
524 for (
int j = 0; j < nargin; j++)
528 fdims = inputs[j].dims ();
529 k = inputs[j].numel ();
530 for (
int i = j+1; i < nargin; i++)
532 if (mask[i] && inputs[i].dims () != fdims)
533 error (
"cellfun: dimensions mismatch");
543 std::list<octave_value_list> idx_list (1);
544 idx_list.front ().resize (1);
545 std::string idx_type =
"(";
549 int expected_nargout;
552 for (
int j = 0; j < nargin; j++)
555 inputlist.
xelem (j) = cinputs[j](count);
559 = get_output_list (interp, count, nargout, inputlist, fcn,
562 int tmp_numel = tmp.
length ();
564 expected_nargout = tmp_numel;
565 else if (tmp_numel != expected_nargout)
566 error (
"cellfun: function returned unexpected number of values");
568 if (nargout > 0 && tmp_numel < nargout)
569 error (
"cellfun: function returned fewer than nargout values");
572 || (nargout == 0 && tmp_numel > 0 && tmp(0).is_defined ()))
574 int num_to_copy = tmp.
length ();
576 if (num_to_copy > nargout1)
577 num_to_copy = nargout1;
581 for (
int j = 0; j < num_to_copy; j++)
583 if (tmp(j).is_defined ())
587 if (val.
numel () != 1)
588 error (
"cellfun: all values must be scalars when UniformOutput = true");
590 retv[j] = val.
resize (fdims);
593 error (
"cellfun: function returned fewer than nargout values");
598 for (
int j = 0; j < num_to_copy; j++)
600 if (tmp(j).is_defined ())
604 if (! retv[j].fast_elem_insert (count, val))
606 if (val.
numel () != 1)
607 error (
"cellfun: all values must be scalars when UniformOutput = true");
609 idx_list.front ()(0) = count + 1.0;
611 idx_type, idx_list, val);
615 error (
"cellfun: function returned fewer than nargout values");
623 for (
int j = 0; j < nargout1; j++)
625 if (nargout > 0 && retv[j].is_undefined ())
635 for (
int j = 0; j < nargout1; j++)
636 results[j].resize (fdims,
Matrix ());
638 bool have_some_output =
false;
642 for (
int j = 0; j < nargin; j++)
645 inputlist.
xelem (j) = cinputs[j](count);
649 = get_output_list (interp, count, nargout, inputlist, fcn,
652 if (nargout > 0 && tmp.
length () < nargout)
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++)
669 if (tmp(j).is_undefined ())
670 error (
"cellfun: function returned fewer than nargout values");
671 results[j](count) = tmp(j);
676 if (have_some_output || fdims.
any_zero ())
680 for (
int j = 0; j < nargout1; j++)
681 retval(j) = results[j];
1068DEFMETHOD (arrayfun, interp, args, nargout,
1188 int nargin = args.
length ();
1194 int nargout1 = (nargout < 1 ? 1 : nargout);
1195 bool symbol_table_lookup =
false;
1203 std::string name = args(0).string_value ();
1213 "arrayfun: invalid function NAME: %s",
1216 symbol_table_lookup =
true;
1227 if (! symbol_table_lookup)
1243 if (
f.is_defined ())
1249 bool uniform_output =
true;
1252 get_mapper_fun_options (symtab, args, nargin, uniform_output,
1266 for (
int j = 0; j < nargin; j++)
1268 inputs[j] = args(j+1);
1269 mask[j] = inputs[j].numel () != 1;
1272 inputlist(j) = inputs[j];
1275 for (
int j = 0; j < nargin; j++)
1279 fdims = inputs[j].dims ();
1280 k = inputs[j].numel ();
1282 for (
int i = j+1; i < nargin; i++)
1284 if (mask[i] && inputs[i].dims () != fdims)
1286 "arrayfun: dimensions mismatch");
1296 std::list<octave_value_list> idx_list (1);
1297 idx_list.front ().resize (1);
1298 std::string idx_type =
"(";
1304 idx_list.front ()(0) = count + 1.0;
1306 for (
int j = 0; j < nargin; j++)
1313 = get_output_list (interp, count, nargout, inputlist, fcn,
1316 if (nargout > 0 && tmp.
length () < nargout)
1318 "arrayfun: function returned fewer than nargout values");
1322 && tmp.
length () > 0 && tmp(0).is_defined ()))
1324 int num_to_copy = tmp.
length ();
1326 if (num_to_copy > nargout1)
1327 num_to_copy = nargout1;
1331 for (
int j = 0; j < num_to_copy; j++)
1333 if (tmp(j).is_defined ())
1337 if (val.
numel () == 1)
1338 retv[j] = val.
resize (fdims);
1341 "arrayfun: all values must be scalars when UniformOutput = true");
1344 error (
"arrayfun: function returned fewer than nargout values");
1349 for (
int j = 0; j < num_to_copy; j++)
1351 if (tmp(j).is_defined ())
1355 if (! retv[j].fast_elem_insert (count, val))
1357 if (val.
numel () == 1)
1359 idx_list.front ()(0) = count + 1.0;
1361 idx_type, idx_list, val);
1365 "arrayfun: all values must be scalars when UniformOutput = true");
1369 error (
"arrayfun: function returned fewer than nargout values");
1375 retval.
resize (nargout1);
1377 for (
int j = 0; j < nargout1; j++)
1379 if (nargout > 0 && retv[j].is_undefined ())
1382 retval(j) = retv[j];
1387 std::list<octave_value_list> idx_list (1);
1388 idx_list.front ().resize (1);
1389 std::string idx_type =
"(";
1393 for (
int j = 0; j < nargout1; j++)
1394 results[j].resize (fdims,
Matrix ());
1396 bool have_some_output =
false;
1400 idx_list.front ()(0) = count + 1.0;
1402 for (
int j = 0; j < nargin; j++)
1409 = get_output_list (interp, count, nargout, inputlist, fcn,
1412 if (nargout > 0 && tmp.
length () < nargout)
1414 "arrayfun: function returned fewer than nargout values");
1418 && tmp.
length () > 0 && tmp(0).is_defined ()))
1420 int num_to_copy = tmp.
length ();
1422 if (num_to_copy > nargout1)
1423 num_to_copy = nargout1;
1425 if (num_to_copy > 0)
1426 have_some_output =
true;
1428 for (
int j = 0; j < num_to_copy; j++)
1430 if (tmp(j).is_undefined ())
1431 error (
"arrayfun: function returned fewer than nargout values");
1432 results[j](count) = tmp(j);
1437 if (have_some_output || fdims.
any_zero ())
1439 retval.
resize (nargout1);
1441 for (
int j = 0; j < nargout1; j++)
1442 retval(j) = results[j];
1448 "arrayfun: argument NAME must be a string or function handle");
1709 int dvl = dimv.
numel ();
1710 int maxd = dv.
ndims ();
1712 for (
int i = 0; i < dvl; i++)
1713 maxd = std::max (maxd, dimv(i));
1714 if (maxd > dv.
ndims ())
1720 perm.
clear (maxd, 1);
1721 for (
int i = 0; i < dvl; i++)
1723 int k = dimv(i) - 1;
1725 error (
"num2cell: dimension indices must be positive");
1727 if (i > 0 && k < dimv(i-1) - 1)
1728 error (
"num2cell: dimension indices must be strictly increasing");
1734 for (
int k = 0, i = dvl; k < maxd; k++)
1738 for (
int i = 0; i < maxd; i++)
1745template <
typename NDA>
1746static inline typename NDA::element_type
1752{
return Cell (array(i)); }
1754template <
typename NDA>
1756do_num2cell (
const NDA& array,
const Array<int>& dimv)
1760 Cell retval (array.dims ());
1763 retval.
xelem (i) = do_num2cell_elem (array, i);
1771 do_num2cell_helper (array.dims (), dimv, celldv, arraydv, perm);
1773 NDA parray = array.permute (perm);
1777 parray = parray.reshape (
dim_vector (nela, nelc));
1779 Cell retval (celldv);
1782 retval.
xelem (i) = NDA (parray.column (i).reshape (arraydv));
1804 for (
int i = 0; i < n; i++)
1820 error (
"num2cell (A, dim) not implemented for class objects");
1822 const dim_vector& dv = get_object_dims (array);
1840DEFUN (num2cell, args, ,
1904 int nargin = args.
length ();
1906 if (nargin < 1 || nargin > 2)
1915 dimv = args(1).int_vector_value (
true);
1954 retval = do_num2cell (array.
array_value (), dimv);
1958 retval = do_object2cell (array, dimv);
1960 retval = do_num2cell (array.
map_value (), dimv);
1961 else if (array.
iscell ())
1962 retval = do_num2cell (array.
cell_value (), dimv);
1979 for (
int i = 0; i < nd; i++)
1988 error (
"mat2cell: mismatch on dimension %d (%" OCTAVE_IDX_TYPE_FORMAT
1989 " != %" OCTAVE_IDX_TYPE_FORMAT
")", i+1, r, s);
1995template <
typename container>
1997prepare_idx (container *idx,
int idim,
int nd,
2018template <
typename Array2D>
2027 if (mat2cell_mismatch (a.dims (),
d, nd))
2032 retval.
clear (nridx, ncidx);
2035 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1)
2037 else if (a.rows () == 1 && nridx == 1 && nd == 2)
2056 prepare_idx (ridx, 0, nd,
d);
2059 prepare_idx (cidx, 1, nd,
d);
2066 retval.
xelem (i, j) = a.index (ridx[i], cidx[j]);
2076template <
typename ArrayND>
2084 if (mat2cell_mismatch (a.dims (),
d, nd))
2093 for (
int i = 0; i < nd; i++)
2095 retdv(i) = nidx[i] =
d[i].
numel ();
2101 retval.
clear (retdv);
2111 for (
int i = 0; i < nd; i++)
2113 idx[i] = xidx + idxtot;
2114 prepare_idx (idx[i], i, nd,
d);
2131 for (
int i = 0; i < nd; i++)
2132 ra_idx.xelem (i) = idx[i][ridx[i]];
2139 if (j < (retnumel - 1))
2147template <
typename ArrayND>
2151 if (a.ndims () == 2 && nd <= 2)
2152 return do_mat2cell_2d (a,
d, nd);
2167 if (mat2cell_mismatch (a.
dims (),
d, nd))
2173 for (
int i = 0; i < nd; i++)
2175 rdv(i) = nidx[i] =
d[i].
numel ();
2185 for (
int i = 0; i < nd; i++)
2187 idx[i] = xidx + idxtot;
2188 prepare_idx (idx[i], i, nd,
d);
2200 for (
int i = 0; i < nd; i++)
2201 ra_idx(i) = idx[i][ridx[i]];
2211DEFUN (mat2cell, args, ,
2300 int nargin = args.
length ();
2310 for (
int i = 1; i < nargin; i++)
2311 d[i-1] = args(i).octave_idx_type_vector_value (
true);
2315 if (sparse && nargin > 3)
2316 error (
"mat2cell: sparse arguments only support 2-D indexing");
2339#define BTYP_BRANCH(X, Y) \
2341 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \
2398template <
typename NDA>
2400do_cellslices_nda (
const NDA& array,
2407 if (array.isvector () && (dim == -1
2408 || (dim == 0 && array.columns () == 1)
2409 || (dim == 1 && array.rows () == 1)))
2417 int ndims = dv.
ndims ();
2420 ndims = std::max (ndims, dim + 1);
2427 retval.
xelem (i) = array.index (idx);
2434DEFUN (cellslices, args, ,
2458 int nargin = args.
length ();
2460 if (nargin < 3 || nargin > 4)
2469 dim = args(3).int_value () - 1;
2471 error (
"cellslices: DIM must be a valid dimension");
2475 error (
"cellslices: the lengths of LB and UB must match");
2478 if (!
x.issparse () &&
x.is_matrix_type ())
2482 retcell = do_cellslices_nda (
x.bool_array_value (),
2484 else if (
x.is_char_matrix ())
2485 retcell = do_cellslices_nda (
x.char_array_value (),
2487 else if (
x.isinteger ())
2489 if (
x.is_int8_type ())
2490 retcell = do_cellslices_nda (
x.int8_array_value (),
2492 else if (
x.is_int16_type ())
2493 retcell = do_cellslices_nda (
x.int16_array_value (),
2495 else if (
x.is_int32_type ())
2496 retcell = do_cellslices_nda (
x.int32_array_value (),
2498 else if (
x.is_int64_type ())
2499 retcell = do_cellslices_nda (
x.int64_array_value (),
2501 else if (
x.is_uint8_type ())
2502 retcell = do_cellslices_nda (
x.uint8_array_value (),
2504 else if (
x.is_uint16_type ())
2505 retcell = do_cellslices_nda (
x.uint16_array_value (),
2507 else if (
x.is_uint32_type ())
2508 retcell = do_cellslices_nda (
x.uint32_array_value (),
2510 else if (
x.is_uint64_type ())
2511 retcell = do_cellslices_nda (
x.uint64_array_value (),
2514 else if (
x.iscomplex ())
2516 if (
x.is_single_type ())
2517 retcell = do_cellslices_nda (
x.float_complex_array_value (),
2520 retcell = do_cellslices_nda (
x.complex_array_value (),
2525 if (
x.is_single_type ())
2526 retcell = do_cellslices_nda (
x.float_array_value (),
2529 retcell = do_cellslices_nda (
x.array_value (),
2537 retcell =
Cell (1, n);
2539 int ndims = dv.
ndims ();
2542 ndims = std::max (ndims, dim + 1);
2551 return ovl (retcell);
2561DEFUN (cellindexmat, args, ,
2586 const Cell x = args(0).xcell_value (
"cellindexmat: X must be a cell");
2604OCTAVE_END_NAMESPACE(octave)
Cell do_mat2cell(const ArrayND &a, const Array< octave_idx_type > *d, int nd)
#define BTYP_BRANCH(X, Y)
Cell do_mat2cell_nd(const ArrayND &a, const Array< octave_idx_type > *d, int nd)
N Dimensional Array with copy-on-write semantics.
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
T & xelem(octave_idx_type n)
Size of the specified dimension.
T & elem(octave_idx_type n)
Size of the specified dimension.
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
bool isempty() const
Size of the specified dimension.
octave_idx_type numel() const
Number of elements in the array.
Vector representing the dimensions (size) of an Array.
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
void resize(int n, int fill_value=0)
static dim_vector alloc(int n)
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...
octave_idx_type ndims() const
Number of dimensions.
int first_non_singleton(int def=0) const
octave_value last_error_id(const octave_value_list &args, int nargout)
void save_exception(const execution_exception &ee)
octave_value last_error_message(const octave_value_list &args, int nargout)
static const idx_vector colon
error_system & get_error_system()
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
void recover_from_exception()
symbol_table & get_symbol_table()
void assign(const std::string &k, const octave_value &val)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_value & xelem(octave_idx_type i)
octave_value_list slice(octave_idx_type offset, octave_idx_type len, bool tags=false) const
octave_idx_type length() const
octave_value_list & prepend(const octave_value &val)
bool is_function_handle() const
bool is_undefined() const
bool is_uint32_type() const
boolNDArray bool_array_value(bool warn=false) const
SparseMatrix sparse_matrix_value(bool frc_str_conv=false) const
std::string class_name() const
bool is_inline_function() const
octave_function * function_value(bool silent=false) const
int32NDArray int32_array_value() const
uint16NDArray uint16_array_value() const
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
int16NDArray int16_array_value() const
int8NDArray int8_array_value() const
bool is_int8_type() const
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
bool is_single_type() const
charNDArray char_array_value(bool frc_str_conv=false) const
bool is_uint8_type() const
bool is_uint64_type() const
bool is_uint16_type() const
uint64NDArray uint64_array_value() const
bool is_int16_type() const
bool is_char_matrix() const
octave_idx_type numel() const
octave_map map_value() const
bool is_int64_type() const
octave_value single_subsref(const std::string &type, const octave_value_list &idx)
int64NDArray int64_array_value() const
NDArray array_value(bool frc_str_conv=false) const
octave_idx_type length() const
bool is_int32_type() const
uint8NDArray uint8_array_value() const
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
octave_value resize(const dim_vector &dv, bool fill=false) const
FloatNDArray float_array_value(bool frc_str_conv=false) const
uint32NDArray uint32_array_value() const
builtin_type_t builtin_type() const
SparseComplexMatrix sparse_complex_matrix_value(bool frc_str_conv=false) const
octave_value find_function(const std::string &name, const symbol_scope &search_scope=symbol_scope::invalid())
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error_with_id(const char *id, const char *fmt,...)
void error(const char *fmt,...)
void err_wrong_type_arg(const char *name, const char *s)
octave_value get_function_handle(interpreter &interp, const octave_value &arg, const std::string ¶meter_name)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
#define OCTAVE_LOCAL_BUFFER_INIT(T, buf, size, value)
const octave_base_value const Array< octave_idx_type > & ra_idx
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
#define panic_unless(cond)
bool valid_identifier(const char *s)