26#if defined (HAVE_CONFIG_H)
79 bool execution_error =
false;
83 tmp =
feval (func, inputlist, nargout);
95 execution_error =
true;
109 static_cast<double> (count
115 tmp =
feval (error_handler, errlist, nargout);
127template <
typename BNDA,
typename NDA>
133 std::string
name = args(0).string_value ();
139 if (
name ==
"isempty")
141 BNDA result (f_args.
dims ());
146 else if (
name ==
"islogical")
148 BNDA result (f_args.
dims ());
153 else if (
name ==
"isnumeric")
155 BNDA result (f_args.
dims ());
160 else if (
name ==
"isreal")
162 BNDA result (f_args.
dims ());
164 result(count) = f_args.
elem (count).
isreal ();
167 else if (
name ==
"length")
169 NDA result (f_args.
dims ());
171 result(count) =
static_cast<double> (f_args.
elem (count).
length ());
174 else if (
name ==
"ndims")
176 NDA result (f_args.
dims ());
178 result(count) =
static_cast<double> (f_args.
elem (count).
ndims ());
181 else if (
name ==
"numel" ||
name ==
"prodofsize")
183 NDA result (f_args.
dims ());
185 result(count) =
static_cast<double> (f_args.
elem (count).
numel ());
188 else if (
name ==
"size")
191 error (R
"(cellfun: not enough arguments for "size")");
193 int d = args(2).nint_value () - 1;
196 error (
"cellfun: K must be a positive integer");
198 NDA result (f_args.
dims ());
204 result(count) =
static_cast<double> (dv(
d));
211 else if (
name ==
"isclass")
214 error (R
"(cellfun: not enough arguments for "isclass")");
216 std::string class_name = args(2).string_value ();
217 BNDA result (f_args.dims ());
219 result(count) = (f_args.
elem (count).
class_name () == class_name);
230 int& nargin,
bool& uniform_output,
233 while (nargin > 3 && args(nargin-2).is_string ())
235 std::string arg = args(nargin-2).string_value ();
237 std::size_t compare_len
238 =
std::max (arg.length (),
static_cast<std::size_t
> (2));
241 uniform_output = args(nargin-1).bool_value ();
244 if (args(nargin-1).is_function_handle ()
245 || args(nargin-1).is_inline_function ())
247 error_handler = args(nargin-1);
249 else if (args(nargin-1).is_string ())
251 std::string err_name = args(nargin-1).string_value ();
256 error (
"cellfun: invalid function NAME: %s",
260 error (
"cellfun: invalid value for 'ErrorHandler' function");
263 error (
"cellfun: unrecognized parameter %s", arg.c_str ());
271DEFMETHOD (cellfun, interp, args, nargout,
409 int nargin = args.length ();
414 if (! args(1).iscell ())
415 error (
"cellfun: C must be a cell array");
418 int nargout1 = (nargout < 1 ? 1 : nargout);
426 retval = try_cellfun_internal_ops<boolNDArray, NDArray> (args, nargin);
428 if (! retval.
empty ())
433 std::string
name = args(0).string_value ();
442 error (
"cellfun: invalid function NAME: %s",
name.c_str ());
448 error (
"cellfun: argument NAME must be a string or function handle");
450 bool uniform_output =
true;
475 if (
name !=
"size" &&
name !=
"class")
482 retval = try_cellfun_internal_ops<boolNDArray, NDArray> (tmp_args, nargin);
484 retval = try_cellfun_internal_ops<Cell, Cell> (tmp_args, nargin);
486 if (! retval.
empty ())
506 const Cell *cinputs = inputs;
514 for (
int j = 0; j < nargin; j++)
516 if (! args(j+1).iscell ())
517 error (
"cellfun: arguments must be cells");
519 inputs[j] = args(j+1).cell_value ();
520 mask[j] = inputs[j].numel () != 1;
522 inputlist(j) = cinputs[j](0);
525 for (
int j = 0; j < nargin; j++)
529 fdims = inputs[j].dims ();
530 k = inputs[j].numel ();
531 for (
int i = j+1; i < nargin; i++)
533 if (mask[i] && inputs[i].dims () != fdims)
534 error (
"cellfun: dimensions mismatch");
546 std::list<octave_value_list> idx_list (1);
547 idx_list.front ().resize (1);
548 std::string idx_type =
"(";
552 int expected_nargout;
555 for (
int j = 0; j < nargin; j++)
558 inputlist.
xelem (j) = cinputs[j](count);
565 int tmp_numel = tmp.
length ();
567 expected_nargout = tmp_numel;
568 else if (tmp_numel != expected_nargout)
569 error (
"cellfun: function returned unexpected number of values");
571 if (nargout > 0 && tmp_numel < nargout)
572 error (
"cellfun: function returned fewer than nargout values");
575 || (nargout == 0 && tmp_numel > 0 && tmp(0).is_defined ()))
577 int num_to_copy = tmp.
length ();
579 if (num_to_copy > nargout1)
580 num_to_copy = nargout1;
584 for (
int j = 0; j < num_to_copy; j++)
586 if (tmp(j).is_defined ())
590 if (val.
numel () != 1)
591 error (
"cellfun: all values must be scalars when UniformOutput = true");
593 retv[j] = val.
resize (fdims);
599 for (
int j = 0; j < num_to_copy; j++)
601 if (tmp(j).is_defined ())
605 if (! retv[j].fast_elem_insert (count, val))
607 if (val.
numel () != 1)
608 error (
"cellfun: all values must be scalars when UniformOutput = true");
610 idx_list.front ()(0) = count + 1.0;
612 idx_type, idx_list, val);
622 for (
int j = 0; j < nargout1; j++)
624 if (nargout > 0 && retv[j].is_undefined ())
634 for (
int j = 0; j < nargout1; j++)
635 results[j].resize (fdims,
Matrix ());
637 bool have_some_output =
false;
641 for (
int j = 0; j < nargin; j++)
644 inputlist.
xelem (j) = cinputs[j](count);
651 if (nargout > 0 && tmp.
length () < nargout)
652 error (
"cellfun: function returned fewer than nargout values");
656 && tmp.
length () > 0 && tmp(0).is_defined ()))
658 int num_to_copy = tmp.
length ();
660 if (num_to_copy > nargout1)
661 num_to_copy = nargout1;
664 have_some_output =
true;
666 for (
int j = 0; j < num_to_copy; j++)
667 results[j](count) = tmp(j);
671 if (have_some_output || fdims.
any_zero ())
675 for (
int j = 0; j < nargout1; j++)
676 retval(j) = results[j];
1027DEFMETHOD (arrayfun, interp, args, nargout,
1142 int nargin = args.length ();
1148 int nargout1 = (nargout < 1 ? 1 : nargout);
1149 bool symbol_table_lookup =
false;
1157 std::string
name = args(0).string_value ();
1167 "arrayfun: invalid function NAME: %s",
1170 symbol_table_lookup =
true;
1181 if (! symbol_table_lookup)
1197 if (
f.is_defined ())
1203 bool uniform_output =
true;
1220 for (
int j = 0; j < nargin; j++)
1222 inputs[j] = args(j+1);
1223 mask[j] = inputs[j].numel () != 1;
1226 inputlist(j) = inputs[j];
1229 for (
int j = 0; j < nargin; j++)
1233 fdims = inputs[j].dims ();
1234 k = inputs[j].numel ();
1236 for (
int i = j+1; i < nargin; i++)
1238 if (mask[i] && inputs[i].dims () != fdims)
1240 "arrayfun: dimensions mismatch");
1252 std::list<octave_value_list> idx_list (1);
1253 idx_list.front ().resize (1);
1254 std::string idx_type =
"(";
1260 idx_list.front ()(0) = count + 1.0;
1262 for (
int j = 0; j < nargin; j++)
1272 if (nargout > 0 && tmp.
length () < nargout)
1274 "arrayfun: function returned fewer than nargout values");
1278 && tmp.
length () > 0 && tmp(0).is_defined ()))
1280 int num_to_copy = tmp.
length ();
1282 if (num_to_copy > nargout1)
1283 num_to_copy = nargout1;
1287 for (
int j = 0; j < num_to_copy; j++)
1289 if (tmp(j).is_defined ())
1293 if (val.
numel () == 1)
1294 retv[j] = val.
resize (fdims);
1297 "arrayfun: all values must be scalars when UniformOutput = true");
1303 for (
int j = 0; j < num_to_copy; j++)
1305 if (tmp(j).is_defined ())
1309 if (! retv[j].fast_elem_insert (count, val))
1311 if (val.
numel () == 1)
1313 idx_list.front ()(0) = count + 1.0;
1315 idx_type, idx_list, val);
1319 "arrayfun: all values must be scalars when UniformOutput = true");
1327 retval.
resize (nargout1);
1329 for (
int j = 0; j < nargout1; j++)
1331 if (nargout > 0 && retv[j].is_undefined ())
1334 retval(j) = retv[j];
1339 std::list<octave_value_list> idx_list (1);
1340 idx_list.front ().resize (1);
1341 std::string idx_type =
"(";
1345 for (
int j = 0; j < nargout1; j++)
1346 results[j].resize (fdims,
Matrix ());
1348 bool have_some_output =
false;
1352 idx_list.front ()(0) = count + 1.0;
1354 for (
int j = 0; j < nargin; j++)
1364 if (nargout > 0 && tmp.
length () < nargout)
1366 "arrayfun: function returned fewer than nargout values");
1370 && tmp.
length () > 0 && tmp(0).is_defined ()))
1372 int num_to_copy = tmp.
length ();
1374 if (num_to_copy > nargout1)
1375 num_to_copy = nargout1;
1377 if (num_to_copy > 0)
1378 have_some_output =
true;
1380 for (
int j = 0; j < num_to_copy; j++)
1381 results[j](count) = tmp(j);
1385 if (have_some_output || fdims.
any_zero ())
1387 retval.
resize (nargout1);
1389 for (
int j = 0; j < nargout1; j++)
1390 retval(j) = results[j];
1396 "arrayfun: argument NAME must be a string or function handle");
1633 int dvl = dimv.
numel ();
1634 int maxd = dv.
ndims ();
1636 for (
int i = 0; i < dvl; i++)
1638 if (maxd > dv.
ndims ())
1644 perm.
clear (maxd, 1);
1645 for (
int i = 0; i < dvl; i++)
1647 int k = dimv(i) - 1;
1649 error (
"num2cell: dimension indices must be positive");
1651 if (i > 0 && k < dimv(i-1) - 1)
1652 error (
"num2cell: dimension indices must be strictly increasing");
1658 for (
int k = 0, i = dvl; k < maxd; k++)
1662 for (
int i = 0; i < maxd; i++)
1669template <
typename NDA>
1670static inline typename NDA::element_type
1676{
return Cell (array(i)); }
1678template <
typename NDA>
1684 Cell retval (array.dims ());
1697 NDA parray = array.permute (perm);
1701 parray = parray.reshape (
dim_vector (nela, nelc));
1703 Cell retval (celldv);
1706 retval.
xelem (i) = NDA (parray.column (i).reshape (arraydv));
1728 for (
int i = 0; i < n; i++)
1744 error (
"num2cell (A, dim) not implemented for class objects");
1764DEFUN (num2cell, args, ,
1828 int nargin = args.length ();
1830 if (nargin < 1 || nargin > 2)
1839 dimv = args(1).int_vector_value (
true);
1885 else if (array.
iscell ())
1903 for (
int i = 0; i < nd; i++)
1912 error (
"mat2cell: mismatch on dimension %d (%" OCTAVE_IDX_TYPE_FORMAT
1913 " != %" OCTAVE_IDX_TYPE_FORMAT
")", i+1, r, s);
1919template <
typename container>
1942template <
typename Array2D>
1947 assert (nd == 1 || nd == 2);
1948 assert (a.ndims () == 2);
1955 retval.
clear (nridx, ncidx);
1958 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1)
1960 else if (a.rows () == 1 && nridx == 1 && nd == 2)
1989 retval.
xelem (i, j) = a.index (ridx[i], cidx[j]);
1999template <
typename ArrayND>
2012 for (
int i = 0; i < nd; i++)
2014 rdv(i) = nidx[i] =
d[i].numel ();
2024 for (
int i = 0; i < nd; i++)
2026 idx[i] = xidx + idxtot;
2039 for (
int i = 0; i < nd; i++)
2040 ra_idx.xelem (i) = idx[i][ridx[i]];
2051template <
typename ArrayND>
2055 if (a.ndims () == 2 && nd <= 2)
2076 for (
int i = 0; i < nd; i++)
2078 rdv(i) = nidx[i] =
d[i].numel ();
2088 for (
int i = 0; i < nd; i++)
2090 idx[i] = xidx + idxtot;
2103 for (
int i = 0; i < nd; i++)
2104 ra_idx(i) = idx[i][ridx[i]];
2114DEFUN (mat2cell, args, ,
2203 int nargin = args.length ();
2213 for (
int i = 1; i < nargin; i++)
2214 d[i-1] = args(i).octave_idx_type_vector_value (
true);
2218 if (sparse && nargin > 3)
2219 error (
"mat2cell: sparse arguments only support 2-D indexing");
2242#define BTYP_BRANCH(X, Y) \
2244 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \
2292template <
typename NDA>
2301 if (array.isvector () && (dim == -1
2302 || (dim == 0 && array.columns () == 1)
2303 || (dim == 1 && array.rows () == 1)))
2311 int ndims = dv.
ndims ();
2321 retval.
xelem (i) = array.index (idx);
2328DEFUN (cellslices, args, ,
2352 int nargin = args.length ();
2354 if (nargin < 3 || nargin > 4)
2363 dim = args(3).int_value () - 1;
2365 error (
"cellslices: DIM must be a valid dimension");
2369 error (
"cellslices: the lengths of LB and UB must match");
2372 if (!
x.issparse () &&
x.is_matrix_type ())
2378 else if (
x.is_char_matrix ())
2381 else if (
x.isinteger ())
2383 if (
x.is_int8_type ())
2386 else if (
x.is_int16_type ())
2389 else if (
x.is_int32_type ())
2392 else if (
x.is_int64_type ())
2395 else if (
x.is_uint8_type ())
2398 else if (
x.is_uint16_type ())
2401 else if (
x.is_uint32_type ())
2404 else if (
x.is_uint64_type ())
2408 else if (
x.iscomplex ())
2410 if (
x.is_single_type ())
2419 if (
x.is_single_type ())
2431 retcell =
Cell (1, n);
2433 int ndims = dv.
ndims ();
2440 idx(dim) = range<double> (lb(i), ub(i));
2441 retcell.
xelem (i) =
x.index_op (idx);
2445 return ovl (retcell);
2455DEFUN (cellindexmat, args, ,
2477 if (args.length () == 0)
2480 const Cell x = args(0).xcell_value (
"cellindexmat: X must be a cell");
static dim_vector get_object_dims(octave_value &obj)
static void prepare_idx(container *idx, int idim, int nd, const Array< octave_idx_type > *d)
Cell do_mat2cell(const ArrayND &a, const Array< octave_idx_type > *d, int nd)
static octave_value_list try_cellfun_internal_ops(const octave_value_list &args, int nargin)
static Cell do_object2cell(const octave_value &obj, const Array< int > &dimv)
static void get_mapper_fun_options(symbol_table &symtab, const octave_value_list &args, int &nargin, bool &uniform_output, octave_value &error_handler)
static Cell do_mat2cell_2d(const Array2D &a, const Array< octave_idx_type > *d, int nd)
static Cell do_cellslices_nda(const NDA &array, const Array< octave_idx_type > &lb, const Array< octave_idx_type > &ub, int dim=-1)
static NDA::element_type do_num2cell_elem(const NDA &array, octave_idx_type i)
#define BTYP_BRANCH(X, Y)
static bool mat2cell_mismatch(const dim_vector &dv, const Array< octave_idx_type > *d, int nd)
static OCTAVE_NAMESPACE_BEGIN octave_value_list get_output_list(error_system &es, octave_idx_type count, octave_idx_type nargout, const octave_value_list &inputlist, octave_value &func, octave_value &error_handler)
static void do_num2cell_helper(const dim_vector &dv, const Array< int > &dimv, dim_vector &celldv, dim_vector &arraydv, Array< int > &perm)
static Cell do_num2cell(const NDA &array, const Array< int > &dimv)
Cell do_mat2cell_nd(const ArrayND &a, const Array< octave_idx_type > *d, int nd)
charNDArray max(char d, const charNDArray &m)
T & xelem(octave_idx_type n)
Size of the specified dimension.
OCTARRAY_API void clear(void)
octave_idx_type numel(void) const
Number of elements in the array.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
T & elem(octave_idx_type n)
Size of the specified dimension.
bool isempty(void) const
Size of the specified dimension.
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
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...
int first_non_singleton(int def=0) const
octave_idx_type ndims(void) const
Number of dimensions.
bool any_zero(void) const
OCTINTERP_API octave_value last_error_id(const octave_value_list &args, int nargout)
OCTINTERP_API void save_exception(const execution_exception &ee)
OCTINTERP_API octave_value last_error_message(const octave_value_list &args, int nargout)
void recover_from_exception(void)
static const idx_vector colon
void assign(const std::string &k, const octave_value &val)
Cell cell_value(void) const
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length(void) const
octave_value & xelem(octave_idx_type i)
octave_value_list slice(octave_idx_type offset, octave_idx_type len, bool tags=false) const
octave_value_list & prepend(const octave_value &val)
bool is_function(void) const
int32NDArray int32_array_value(void) const
boolNDArray bool_array_value(bool warn=false) const
uint16NDArray uint16_array_value(void) const
SparseMatrix sparse_matrix_value(bool frc_str_conv=false) const
bool issparse(void) const
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
OCTINTERP_API octave_function * function_value(bool silent=false) const
bool is_uint16_type(void) const
builtin_type_t builtin_type(void) const
bool is_char_matrix(void) const
bool is_int8_type(void) const
bool isnumeric(void) const
octave_idx_type numel(void) const
bool is_string(void) const
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
bool is_defined(void) const
charNDArray char_array_value(bool frc_str_conv=false) const
bool isinteger(void) const
Cell cell_value(void) const
bool is_function_handle(void) const
std::string class_name(void) const
bool is_uint32_type(void) const
int8NDArray int8_array_value(void) const
bool is_int64_type(void) const
int64NDArray int64_array_value(void) const
bool isstruct(void) const
uint8NDArray uint8_array_value(void) const
bool is_int32_type(void) const
bool is_uint64_type(void) const
bool is_int16_type(void) const
uint64NDArray uint64_array_value(void) const
NDArray array_value(bool frc_str_conv=false) const
bool is_single_type(void) const
uint32NDArray uint32_array_value(void) const
OCTINTERP_API octave_map map_value(void) const
bool isobject(void) const
bool is_undefined(void) const
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
OCTINTERP_API octave_value single_subsref(const std::string &type, const octave_value_list &idx)
octave_value resize(const dim_vector &dv, bool fill=false) const
OCTINTERP_API octave_idx_type length(void) const
FloatNDArray float_array_value(bool frc_str_conv=false) const
bool is_uint8_type(void) const
int16NDArray int16_array_value(void) const
bool is_inline_function(void) const
bool iscomplex(void) const
bool islogical(void) const
dim_vector dims(void) 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())
OCTINTERP_API void print_usage(void)
#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::idx_vector idx_vector
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
OCTAVE_API 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.
octave_value get_function_handle(interpreter &interp, const octave_value &arg, const std::string ¶meter_name)
interpreter & __get_interpreter__(const std::string &who)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
#define OCTAVE_LOCAL_BUFFER_INIT(T, buf, size, value)
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
const octave_base_value const Array< octave_idx_type > & ra_idx
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
OCTAVE_NAMESPACE_BEGIN bool valid_identifier(const char *s)