26 #if defined (HAVE_CONFIG_H)
79 bool execution_error =
false;
83 tmp =
feval (fcn, inputlist, nargout);
94 execution_error =
true;
108 static_cast<double> (count
114 tmp =
feval (error_handler, errlist, nargout);
126 template <
typename BNDA,
typename NDA>
132 std::string name = args(0).string_value ();
138 if (name ==
"isempty")
140 BNDA result (f_args.
dims ());
145 else if (name ==
"islogical")
147 BNDA result (f_args.
dims ());
152 else if (name ==
"isnumeric")
154 BNDA result (f_args.
dims ());
159 else if (name ==
"isreal")
161 BNDA result (f_args.
dims ());
163 result(count) = f_args.
elem (count).
isreal ();
166 else if (name ==
"length")
168 NDA result (f_args.
dims ());
170 result(count) =
static_cast<double> (f_args.
elem (count).
length ());
173 else if (name ==
"ndims")
175 NDA result (f_args.
dims ());
177 result(count) =
static_cast<double> (f_args.
elem (count).
ndims ());
180 else if (name ==
"numel" || name ==
"prodofsize")
182 NDA result (f_args.
dims ());
184 result(count) =
static_cast<double> (f_args.
elem (count).
numel ());
187 else if (name ==
"size")
190 error (R
"(cellfun: not enough arguments for "size")");
192 int d = args(2).nint_value () - 1;
195 error (
"cellfun: K must be a positive integer");
197 NDA result (f_args.
dims ());
203 result(count) =
static_cast<double> (dv(
d));
210 else if (name ==
"isclass")
213 error (R
"(cellfun: not enough arguments for "isclass")");
215 std::string class_name = args(2).string_value ();
216 BNDA result (f_args.dims ());
218 result(count) = (f_args.
elem (count).
class_name () == class_name);
229 int& nargin,
bool& uniform_output,
232 while (nargin > 3 && args(nargin-2).is_string ())
234 std::string arg = args(nargin-2).string_value ();
236 std::size_t compare_len
237 =
std::max (arg.length (),
static_cast<std::size_t
> (2));
240 uniform_output = args(nargin-1).bool_value ();
243 if (args(nargin-1).is_function_handle ()
244 || args(nargin-1).is_inline_function ())
246 error_handler = args(nargin-1);
248 else if (args(nargin-1).is_string ())
250 std::string err_name = args(nargin-1).string_value ();
255 error (
"cellfun: invalid function NAME: %s",
259 error (
"cellfun: invalid value for 'ErrorHandler' function");
262 error (
"cellfun: unrecognized parameter %s", arg.c_str ());
270 DEFMETHOD (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];
1056 DEFMETHOD (arrayfun, interp, args, nargout,
1176 int nargin = args.length ();
1182 int nargout1 = (nargout < 1 ? 1 : nargout);
1183 bool symbol_table_lookup =
false;
1191 std::string name = args(0).string_value ();
1201 "arrayfun: invalid function NAME: %s",
1204 symbol_table_lookup =
true;
1215 if (! symbol_table_lookup)
1231 if (
f.is_defined ())
1237 bool uniform_output =
true;
1254 for (
int j = 0; j < nargin; j++)
1256 inputs[j] = args(j+1);
1257 mask[j] = inputs[j].numel () != 1;
1260 inputlist(j) = inputs[j];
1263 for (
int j = 0; j < nargin; j++)
1267 fdims = inputs[j].dims ();
1268 k = inputs[j].numel ();
1270 for (
int i = j+1; i < nargin; i++)
1272 if (mask[i] && inputs[i].dims () != fdims)
1274 "arrayfun: dimensions mismatch");
1286 std::list<octave_value_list> idx_list (1);
1287 idx_list.front ().resize (1);
1288 std::string idx_type =
"(";
1294 idx_list.front ()(0) = count + 1.0;
1296 for (
int j = 0; j < nargin; j++)
1306 if (nargout > 0 && tmp.
length () < nargout)
1308 "arrayfun: function returned fewer than nargout values");
1312 && tmp.
length () > 0 && tmp(0).is_defined ()))
1314 int num_to_copy = tmp.
length ();
1316 if (num_to_copy > nargout1)
1317 num_to_copy = nargout1;
1321 for (
int j = 0; j < num_to_copy; j++)
1323 if (tmp(j).is_defined ())
1327 if (val.
numel () == 1)
1328 retv[j] = val.
resize (fdims);
1331 "arrayfun: all values must be scalars when UniformOutput = true");
1337 for (
int j = 0; j < num_to_copy; j++)
1339 if (tmp(j).is_defined ())
1343 if (! retv[j].fast_elem_insert (count, val))
1345 if (val.
numel () == 1)
1347 idx_list.front ()(0) = count + 1.0;
1349 idx_type, idx_list, val);
1353 "arrayfun: all values must be scalars when UniformOutput = true");
1361 retval.
resize (nargout1);
1363 for (
int j = 0; j < nargout1; j++)
1365 if (nargout > 0 && retv[j].is_undefined ())
1368 retval(j) = retv[j];
1373 std::list<octave_value_list> idx_list (1);
1374 idx_list.front ().resize (1);
1375 std::string idx_type =
"(";
1379 for (
int j = 0; j < nargout1; j++)
1380 results[j].resize (fdims,
Matrix ());
1382 bool have_some_output =
false;
1386 idx_list.front ()(0) = count + 1.0;
1388 for (
int j = 0; j < nargin; j++)
1398 if (nargout > 0 && tmp.
length () < nargout)
1400 "arrayfun: function returned fewer than nargout values");
1404 && tmp.
length () > 0 && tmp(0).is_defined ()))
1406 int num_to_copy = tmp.
length ();
1408 if (num_to_copy > nargout1)
1409 num_to_copy = nargout1;
1411 if (num_to_copy > 0)
1412 have_some_output =
true;
1414 for (
int j = 0; j < num_to_copy; j++)
1415 results[j](count) = tmp(j);
1419 if (have_some_output || fdims.
any_zero ())
1421 retval.
resize (nargout1);
1423 for (
int j = 0; j < nargout1; j++)
1424 retval(j) = results[j];
1430 "arrayfun: argument NAME must be a string or function handle");
1684 int dvl = dimv.
numel ();
1685 int maxd = dv.
ndims ();
1687 for (
int i = 0; i < dvl; i++)
1689 if (maxd > dv.
ndims ())
1695 perm.
clear (maxd, 1);
1696 for (
int i = 0; i < dvl; i++)
1698 int k = dimv(i) - 1;
1700 error (
"num2cell: dimension indices must be positive");
1702 if (i > 0 && k < dimv(i-1) - 1)
1703 error (
"num2cell: dimension indices must be strictly increasing");
1709 for (
int k = 0, i = dvl; k < maxd; k++)
1713 for (
int i = 0; i < maxd; i++)
1720 template <
typename NDA>
1721 static inline typename NDA::element_type
1723 {
return array(i); }
1727 {
return Cell (array(i)); }
1729 template <
typename NDA>
1735 Cell retval (array.dims ());
1748 NDA parray = array.permute (perm);
1752 parray = parray.reshape (
dim_vector (nela, nelc));
1754 Cell retval (celldv);
1757 retval.
xelem (i) = NDA (parray.column (i).reshape (arraydv));
1779 for (
int i = 0; i <
n; i++)
1795 error (
"num2cell (A, dim) not implemented for class objects");
1807 idx(0) = double (i+1);
1815 DEFUN (num2cell, args, ,
1879 int nargin = args.length ();
1881 if (nargin < 1 || nargin > 2)
1890 dimv = args(1).int_vector_value (
true);
1936 else if (array.
iscell ())
1954 for (
int i = 0; i < nd; i++)
1963 error (
"mat2cell: mismatch on dimension %d (%" OCTAVE_IDX_TYPE_FORMAT
1964 " != %" OCTAVE_IDX_TYPE_FORMAT
")", i+1,
r, s);
1970 template <
typename container>
1993 template <
typename Array2D>
2006 retval.
clear (nridx, ncidx);
2009 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1)
2011 else if (a.rows () == 1 && nridx == 1 && nd == 2)
2040 retval.
xelem (i, j) = a.index (ridx[i], cidx[j]);
2050 template <
typename ArrayND>
2063 for (
int i = 0; i < nd; i++)
2065 rdv(i) = nidx[i] =
d[i].numel ();
2075 for (
int i = 0; i < nd; i++)
2077 idx[i] = xidx + idxtot;
2090 for (
int i = 0; i < nd; i++)
2091 ra_idx.xelem (i) = idx[i][ridx[i]];
2102 template <
typename ArrayND>
2106 if (a.ndims () == 2 && nd <= 2)
2127 for (
int i = 0; i < nd; i++)
2129 rdv(i) = nidx[i] =
d[i].numel ();
2139 for (
int i = 0; i < nd; i++)
2141 idx[i] = xidx + idxtot;
2154 for (
int i = 0; i < nd; i++)
2155 ra_idx(i) = idx[i][ridx[i]];
2165 DEFUN (mat2cell, args, ,
2254 int nargin = args.length ();
2264 for (
int i = 1; i < nargin; i++)
2265 d[i-1] = args(i).octave_idx_type_vector_value (
true);
2269 if (sparse && nargin > 3)
2270 error (
"mat2cell: sparse arguments only support 2-D indexing");
2293 #define BTYP_BRANCH(X, Y) \
2295 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \
2343 template <
typename NDA>
2352 if (array.isvector () && (dim == -1
2353 || (dim == 0 && array.columns () == 1)
2354 || (dim == 1 && array.rows () == 1)))
2362 int ndims = dv.
ndims ();
2372 retval.
xelem (i) = array.index (idx);
2379 DEFUN (cellslices, args, ,
2403 int nargin = args.length ();
2405 if (nargin < 3 || nargin > 4)
2414 dim = args(3).int_value () - 1;
2416 error (
"cellslices: DIM must be a valid dimension");
2420 error (
"cellslices: the lengths of LB and UB must match");
2423 if (!
x.issparse () &&
x.is_matrix_type ())
2429 else if (
x.is_char_matrix ())
2432 else if (
x.isinteger ())
2434 if (
x.is_int8_type ())
2437 else if (
x.is_int16_type ())
2440 else if (
x.is_int32_type ())
2443 else if (
x.is_int64_type ())
2446 else if (
x.is_uint8_type ())
2449 else if (
x.is_uint16_type ())
2452 else if (
x.is_uint32_type ())
2455 else if (
x.is_uint64_type ())
2459 else if (
x.iscomplex ())
2461 if (
x.is_single_type ())
2470 if (
x.is_single_type ())
2482 retcell =
Cell (1,
n);
2484 int ndims = dv.
ndims ();
2491 idx(dim) = range<double> (lb(i), ub(i));
2492 retcell.
xelem (i) =
x.index_op (idx);
2496 return ovl (retcell);
2506 DEFUN (cellindexmat, args, ,
2528 if (args.length () == 0)
2531 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_value_list get_output_list(error_system &es, octave_idx_type count, octave_idx_type nargout, const octave_value_list &inputlist, octave_value &fcn, 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)
OCTARRAY_API void clear(void)
OCTARRAY_OVERRIDABLE_FUNC_API bool isempty(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API T & elem(octave_idx_type n)
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API T & xelem(octave_idx_type n)
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)
static const idx_vector colon
void recover_from_exception(void)
void assign(const std::string &k, const octave_value &val)
Cell cell_value(void) const
octave_value & xelem(octave_idx_type i)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length(void) const
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
OCTINTERP_API octave_function * function_value(bool silent=false) 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)
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())
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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 error_unless(bool cond)
void err_wrong_type_arg(const char *name, const char *s)
octave::idx_vector idx_vector
interpreter & __get_interpreter__(void)
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)
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.
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.
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.
bool valid_identifier(const char *s)