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);
125 template <
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).nint_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));
239 uniform_output = args(nargin-1).bool_value ();
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 ());
269 DEFMETHOD (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);
596 for (
int j = 0; j < num_to_copy; j++)
598 if (tmp(j).is_defined ())
602 if (! retv[j].fast_elem_insert (count, val))
604 if (val.
numel () != 1)
605 error (
"cellfun: all values must be scalars when UniformOutput = true");
607 idx_list.front ()(0) = count + 1.0;
609 idx_type, idx_list, val);
619 for (
int j = 0; j < nargout1; j++)
621 if (nargout > 0 && retv[j].is_undefined ())
631 for (
int j = 0; j < nargout1; j++)
632 results[j].resize (fdims,
Matrix ());
634 bool have_some_output =
false;
638 for (
int j = 0; j < nargin; j++)
641 inputlist.
xelem (j) = cinputs[j](count);
645 = get_output_list (interp, count, nargout, inputlist, fcn,
648 if (nargout > 0 && tmp.
length () < nargout)
649 error (
"cellfun: function returned fewer than nargout values");
653 && tmp.
length () > 0 && tmp(0).is_defined ()))
655 int num_to_copy = tmp.
length ();
657 if (num_to_copy > nargout1)
658 num_to_copy = nargout1;
661 have_some_output =
true;
663 for (
int j = 0; j < num_to_copy; j++)
664 results[j](count) = tmp(j);
668 if (have_some_output || fdims.
any_zero ())
672 for (
int j = 0; j < nargout1; j++)
673 retval(j) = results[j];
1053 DEFMETHOD (arrayfun, interp, args, nargout,
1173 int nargin = args.
length ();
1179 int nargout1 = (nargout < 1 ? 1 : nargout);
1180 bool symbol_table_lookup =
false;
1188 std::string name = args(0).string_value ();
1198 "arrayfun: invalid function NAME: %s",
1201 symbol_table_lookup =
true;
1212 if (! symbol_table_lookup)
1228 if (
f.is_defined ())
1234 bool uniform_output =
true;
1237 get_mapper_fun_options (symtab, args, nargin, uniform_output,
1251 for (
int j = 0; j < nargin; j++)
1253 inputs[j] = args(j+1);
1254 mask[j] = inputs[j].numel () != 1;
1257 inputlist(j) = inputs[j];
1260 for (
int j = 0; j < nargin; j++)
1264 fdims = inputs[j].dims ();
1265 k = inputs[j].numel ();
1267 for (
int i = j+1; i < nargin; i++)
1269 if (mask[i] && inputs[i].dims () != fdims)
1271 "arrayfun: dimensions mismatch");
1281 std::list<octave_value_list> idx_list (1);
1282 idx_list.front ().resize (1);
1283 std::string idx_type =
"(";
1289 idx_list.front ()(0) = count + 1.0;
1291 for (
int j = 0; j < nargin; j++)
1298 = get_output_list (interp, count, nargout, inputlist, fcn,
1301 if (nargout > 0 && tmp.
length () < nargout)
1303 "arrayfun: function returned fewer than nargout values");
1307 && tmp.
length () > 0 && tmp(0).is_defined ()))
1309 int num_to_copy = tmp.
length ();
1311 if (num_to_copy > nargout1)
1312 num_to_copy = nargout1;
1316 for (
int j = 0; j < num_to_copy; j++)
1318 if (tmp(j).is_defined ())
1322 if (val.
numel () == 1)
1323 retv[j] = val.
resize (fdims);
1326 "arrayfun: all values must be scalars when UniformOutput = true");
1332 for (
int j = 0; j < num_to_copy; j++)
1334 if (tmp(j).is_defined ())
1338 if (! retv[j].fast_elem_insert (count, val))
1340 if (val.
numel () == 1)
1342 idx_list.front ()(0) = count + 1.0;
1344 idx_type, idx_list, val);
1348 "arrayfun: all values must be scalars when UniformOutput = true");
1356 retval.
resize (nargout1);
1358 for (
int j = 0; j < nargout1; j++)
1360 if (nargout > 0 && retv[j].is_undefined ())
1363 retval(j) = retv[j];
1368 std::list<octave_value_list> idx_list (1);
1369 idx_list.front ().resize (1);
1370 std::string idx_type =
"(";
1374 for (
int j = 0; j < nargout1; j++)
1375 results[j].resize (fdims,
Matrix ());
1377 bool have_some_output =
false;
1381 idx_list.front ()(0) = count + 1.0;
1383 for (
int j = 0; j < nargin; j++)
1390 = get_output_list (interp, count, nargout, inputlist, fcn,
1393 if (nargout > 0 && tmp.
length () < nargout)
1395 "arrayfun: function returned fewer than nargout values");
1399 && tmp.
length () > 0 && tmp(0).is_defined ()))
1401 int num_to_copy = tmp.
length ();
1403 if (num_to_copy > nargout1)
1404 num_to_copy = nargout1;
1406 if (num_to_copy > 0)
1407 have_some_output =
true;
1409 for (
int j = 0; j < num_to_copy; j++)
1410 results[j](count) = tmp(j);
1414 if (have_some_output || fdims.
any_zero ())
1416 retval.
resize (nargout1);
1418 for (
int j = 0; j < nargout1; j++)
1419 retval(j) = results[j];
1425 "arrayfun: argument NAME must be a string or function handle");
1679 int dvl = dimv.
numel ();
1680 int maxd = dv.
ndims ();
1682 for (
int i = 0; i < dvl; i++)
1684 if (maxd > dv.
ndims ())
1690 perm.
clear (maxd, 1);
1691 for (
int i = 0; i < dvl; i++)
1693 int k = dimv(i) - 1;
1695 error (
"num2cell: dimension indices must be positive");
1697 if (i > 0 && k < dimv(i-1) - 1)
1698 error (
"num2cell: dimension indices must be strictly increasing");
1704 for (
int k = 0, i = dvl; k < maxd; k++)
1708 for (
int i = 0; i < maxd; i++)
1715 template <
typename NDA>
1716 static inline typename NDA::element_type
1718 {
return array(i); }
1722 {
return Cell (array(i)); }
1724 template <
typename NDA>
1726 do_num2cell (
const NDA& array,
const Array<int>& dimv)
1730 Cell retval (array.dims ());
1733 retval.
xelem (i) = do_num2cell_elem (array, i);
1741 do_num2cell_helper (array.dims (), dimv, celldv, arraydv, perm);
1743 NDA parray = array.permute (perm);
1747 parray = parray.reshape (
dim_vector (nela, nelc));
1749 Cell retval (celldv);
1752 retval.
xelem (i) = NDA (parray.column (i).reshape (arraydv));
1774 for (
int i = 0; i <
n; i++)
1790 error (
"num2cell (A, dim) not implemented for class objects");
1802 idx(0) = double (i+1);
1810 DEFUN (num2cell, args, ,
1874 int nargin = args.
length ();
1876 if (nargin < 1 || nargin > 2)
1885 dimv = args(1).int_vector_value (
true);
1924 retval = do_num2cell (array.
array_value (), dimv);
1928 retval = do_object2cell (array, dimv);
1930 retval = do_num2cell (array.
map_value (), dimv);
1931 else if (array.
iscell ())
1932 retval = do_num2cell (array.
cell_value (), dimv);
1949 for (
int i = 0; i < nd; i++)
1958 error (
"mat2cell: mismatch on dimension %d (%" OCTAVE_IDX_TYPE_FORMAT
1959 " != %" OCTAVE_IDX_TYPE_FORMAT
")", i+1,
r, s);
1965 template <
typename container>
1967 prepare_idx (container *idx,
int idim,
int nd,
1988 template <
typename Array2D>
1996 if (mat2cell_mismatch (a.dims (),
d, nd))
2001 retval.
clear (nridx, ncidx);
2004 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1)
2006 else if (a.rows () == 1 && nridx == 1 && nd == 2)
2025 prepare_idx (ridx, 0, nd,
d);
2028 prepare_idx (cidx, 1, nd,
d);
2035 retval.
xelem (i, j) = a.index (ridx[i], cidx[j]);
2045 template <
typename ArrayND>
2052 if (mat2cell_mismatch (a.dims (),
d, nd))
2061 for (
int i = 0; i < nd; i++)
2063 retdv(i) = nidx[i] =
d[i].numel ();
2069 retval.
clear (retdv);
2079 for (
int i = 0; i < nd; i++)
2081 idx[i] = xidx + idxtot;
2082 prepare_idx (idx[i], i, nd,
d);
2099 for (
int i = 0; i < nd; i++)
2100 ra_idx.xelem (i) = idx[i][ridx[i]];
2107 if (j < (retnumel - 1))
2115 template <
typename ArrayND>
2119 if (a.ndims () == 2 && nd <= 2)
2120 return do_mat2cell_2d (a,
d, nd);
2134 if (mat2cell_mismatch (a.
dims (),
d, nd))
2140 for (
int i = 0; i < nd; i++)
2142 rdv(i) = nidx[i] =
d[i].numel ();
2152 for (
int i = 0; i < nd; i++)
2154 idx[i] = xidx + idxtot;
2155 prepare_idx (idx[i], i, nd,
d);
2167 for (
int i = 0; i < nd; i++)
2168 ra_idx(i) = idx[i][ridx[i]];
2178 DEFUN (mat2cell, args, ,
2267 int nargin = args.
length ();
2277 for (
int i = 1; i < nargin; i++)
2278 d[i-1] = args(i).octave_idx_type_vector_value (
true);
2282 if (sparse && nargin > 3)
2283 error (
"mat2cell: sparse arguments only support 2-D indexing");
2306 #define BTYP_BRANCH(X, Y) \
2308 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \
2365 template <
typename NDA>
2367 do_cellslices_nda (
const NDA& array,
2374 if (array.isvector () && (dim == -1
2375 || (dim == 0 && array.columns () == 1)
2376 || (dim == 1 && array.rows () == 1)))
2384 int ndims = dv.
ndims ();
2394 retval.
xelem (i) = array.index (idx);
2401 DEFUN (cellslices, args, ,
2425 int nargin = args.
length ();
2427 if (nargin < 3 || nargin > 4)
2436 dim = args(3).int_value () - 1;
2438 error (
"cellslices: DIM must be a valid dimension");
2442 error (
"cellslices: the lengths of LB and UB must match");
2445 if (!
x.issparse () &&
x.is_matrix_type ())
2449 retcell = do_cellslices_nda (
x.bool_array_value (),
2451 else if (
x.is_char_matrix ())
2452 retcell = do_cellslices_nda (
x.char_array_value (),
2454 else if (
x.isinteger ())
2456 if (
x.is_int8_type ())
2457 retcell = do_cellslices_nda (
x.int8_array_value (),
2459 else if (
x.is_int16_type ())
2460 retcell = do_cellslices_nda (
x.int16_array_value (),
2462 else if (
x.is_int32_type ())
2463 retcell = do_cellslices_nda (
x.int32_array_value (),
2465 else if (
x.is_int64_type ())
2466 retcell = do_cellslices_nda (
x.int64_array_value (),
2468 else if (
x.is_uint8_type ())
2469 retcell = do_cellslices_nda (
x.uint8_array_value (),
2471 else if (
x.is_uint16_type ())
2472 retcell = do_cellslices_nda (
x.uint16_array_value (),
2474 else if (
x.is_uint32_type ())
2475 retcell = do_cellslices_nda (
x.uint32_array_value (),
2477 else if (
x.is_uint64_type ())
2478 retcell = do_cellslices_nda (
x.uint64_array_value (),
2481 else if (
x.iscomplex ())
2483 if (
x.is_single_type ())
2484 retcell = do_cellslices_nda (
x.float_complex_array_value (),
2487 retcell = do_cellslices_nda (
x.complex_array_value (),
2492 if (
x.is_single_type ())
2493 retcell = do_cellslices_nda (
x.float_array_value (),
2496 retcell = do_cellslices_nda (
x.array_value (),
2504 retcell =
Cell (1,
n);
2506 int ndims = dv.
ndims ();
2514 retcell.
xelem (i) =
x.index_op (idx);
2518 return ovl (retcell);
2528 DEFUN (cellindexmat, args, ,
2553 const Cell x = args(0).xcell_value (
"cellindexmat: X must be a cell");
2571 OCTAVE_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)
charNDArray max(char d, const charNDArray &m)
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.
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.
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
symbol_table & get_symbol_table()
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()
void assign(const std::string &k, const octave_value &val)
octave_value & xelem(octave_idx_type i)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
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
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
octave_function * function_value(bool silent=false) 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,...)
#define error_unless(cond)
void err_wrong_type_arg(const char *name, const char *s)
octave::idx_vector idx_vector
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
bool valid_identifier(const char *s)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
#define OCTAVE_LOCAL_BUFFER_INIT(T, buf, size, value)
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.