26 #if defined (HAVE_CONFIG_H)
77 bool execution_error =
false;
83 catch (
const octave::execution_exception& ee)
93 execution_error =
true;
107 static_cast<double> (count
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 size_t compare_len =
std::max (arg.length (),
static_cast<size_t> (2));
238 uniform_output = args(nargin-1).bool_value ();
241 if (args(nargin-1).is_function_handle ()
242 || args(nargin-1).is_inline_function ())
244 error_handler = args(nargin-1);
246 else if (args(nargin-1).is_string ())
248 std::string err_name = args(nargin-1).string_value ();
253 error (
"cellfun: invalid function NAME: %s",
257 error (
"cellfun: invalid value for 'ErrorHandler' function");
260 error (
"cellfun: unrecognized parameter %s", arg.c_str ());
268 DEFMETHOD (cellfun, interp, args, nargout,
406 int nargin = args.length ();
411 if (! args(1).iscell ())
412 error (
"cellfun: C must be a cell array");
415 int nargout1 = (nargout < 1 ? 1 : nargout);
423 retval = try_cellfun_internal_ops<boolNDArray,NDArray> (args, nargin);
430 std::string
name = args(0).string_value ();
439 error (
"cellfun: invalid function NAME: %s",
name.c_str ());
445 error (
"cellfun: argument NAME must be a string or function handle");
447 bool uniform_output =
true;
472 if (
name !=
"size" &&
name !=
"class")
479 retval = try_cellfun_internal_ops<boolNDArray, NDArray> (tmp_args, nargin);
481 retval = try_cellfun_internal_ops<Cell, Cell> (tmp_args, nargin);
503 const Cell *cinputs = inputs;
511 for (
int j = 0; j < nargin; j++)
513 if (! args(j+1).iscell ())
514 error (
"cellfun: arguments must be cells");
516 inputs[j] = args(j+1).cell_value ();
517 mask[j] = inputs[j].numel () != 1;
519 inputlist(j) = cinputs[j](0);
522 for (
int j = 0; j < nargin; j++)
526 fdims = inputs[j].dims ();
527 k = inputs[j].numel ();
528 for (
int i = j+1; i < nargin; i++)
530 if (mask[i] && inputs[i].dims () != fdims)
531 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);
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);
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++)
1024 DEFMETHOD (arrayfun, interp, args, nargout,
1139 int nargin = args.length ();
1145 int nargout1 = (nargout < 1 ? 1 : nargout);
1146 bool symbol_table_lookup =
false;
1154 std::string
name = args(0).string_value ();
1164 "arrayfun: invalid function NAME: %s",
1167 symbol_table_lookup =
true;
1178 if (! symbol_table_lookup)
1194 if (
f.is_defined ())
1200 bool uniform_output =
true;
1217 for (
int j = 0; j < nargin; j++)
1219 inputs[j] = args(j+1);
1220 mask[j] = inputs[j].numel () != 1;
1223 inputlist(j) = inputs[j];
1226 for (
int j = 0; j < nargin; j++)
1230 fdims = inputs[j].dims ();
1231 k = inputs[j].numel ();
1233 for (
int i = j+1; i < nargin; i++)
1235 if (mask[i] && inputs[i].dims () != fdims)
1237 "arrayfun: dimensions mismatch");
1249 std::list<octave_value_list> idx_list (1);
1250 idx_list.front ().resize (1);
1251 std::string idx_type =
"(";
1257 idx_list.front ()(0) = count + 1.0;
1259 for (
int j = 0; j < nargin; j++)
1269 if (nargout > 0 && tmp.
length () < nargout)
1271 "arrayfun: function returned fewer than nargout values");
1275 && tmp.
length () > 0 && tmp(0).is_defined ()))
1277 int num_to_copy = tmp.
length ();
1279 if (num_to_copy > nargout1)
1280 num_to_copy = nargout1;
1284 for (
int j = 0; j < num_to_copy; j++)
1286 if (tmp(j).is_defined ())
1290 if (val.
numel () == 1)
1291 retv[j] = val.
resize (fdims);
1294 "arrayfun: all values must be scalars when UniformOutput = true");
1300 for (
int j = 0; j < num_to_copy; j++)
1302 if (tmp(j).is_defined ())
1306 if (! retv[j].fast_elem_insert (count, val))
1308 if (val.
numel () == 1)
1310 idx_list.front ()(0) = count + 1.0;
1312 idx_type, idx_list, val);
1316 "arrayfun: all values must be scalars when UniformOutput = true");
1326 for (
int j = 0; j < nargout1; j++)
1328 if (nargout > 0 && retv[j].is_undefined ())
1336 std::list<octave_value_list> idx_list (1);
1337 idx_list.front ().resize (1);
1338 std::string idx_type =
"(";
1342 for (
int j = 0; j < nargout1; j++)
1343 results[j].resize (fdims,
Matrix ());
1345 bool have_some_output =
false;
1349 idx_list.front ()(0) = count + 1.0;
1351 for (
int j = 0; j < nargin; j++)
1361 if (nargout > 0 && tmp.
length () < nargout)
1363 "arrayfun: function returned fewer than nargout values");
1367 && tmp.
length () > 0 && tmp(0).is_defined ()))
1369 int num_to_copy = tmp.
length ();
1371 if (num_to_copy > nargout1)
1372 num_to_copy = nargout1;
1374 if (num_to_copy > 0)
1375 have_some_output =
true;
1377 for (
int j = 0; j < num_to_copy; j++)
1378 results[j](count) = tmp(j);
1382 if (have_some_output || fdims.
any_zero ())
1386 for (
int j = 0; j < nargout1; j++)
1393 "arrayfun: argument NAME must be a string or function handle");
1630 int dvl = dimv.
numel ();
1631 int maxd = dv.
ndims ();
1633 for (
int i = 0; i < dvl; i++)
1635 if (maxd > dv.
ndims ())
1641 perm.
clear (maxd, 1);
1642 for (
int i = 0; i < dvl; i++)
1644 int k = dimv(i) - 1;
1646 error (
"num2cell: dimension indices must be positive");
1648 if (i > 0 && k < dimv(i-1) - 1)
1649 error (
"num2cell: dimension indices must be strictly increasing");
1655 for (
int k = 0, i = dvl; k < maxd; k++)
1659 for (
int i = 0; i < maxd; i++)
1666 template <
typename NDA>
1667 static inline typename NDA::element_type
1669 {
return array(i); }
1673 {
return Cell (array(i)); }
1675 template <
typename NDA>
1694 NDA parray = array.permute (perm);
1698 parray = parray.reshape (
dim_vector (nela, nelc));
1703 retval.
xelem (i) = NDA (parray.column (i).reshape (arraydv));
1725 for (
int i = 0; i <
n; i++)
1741 error (
"num2cell (A, dim) not implemented for class objects");
1753 idx(0) = double (i+1);
1761 DEFUN (num2cell, args, ,
1825 int nargin = args.length ();
1827 if (nargin < 1 || nargin > 2)
1836 dimv = args(1).int_vector_value (
true);
1882 else if (array.
iscell ())
1900 for (
int i = 0; i < nd; i++)
1909 error (
"mat2cell: mismatch on dimension %d (%" OCTAVE_IDX_TYPE_FORMAT
1910 " != %" OCTAVE_IDX_TYPE_FORMAT
")", i+1,
r, s);
1916 template <
typename container>
1939 template <
typename Array2D>
1944 assert (nd == 1 || nd == 2);
1945 assert (a.ndims () == 2);
1955 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1)
1957 else if (a.rows () == 1 && nridx == 1 && nd == 2)
1996 template <
typename ArrayND>
2009 for (
int i = 0; i < nd; i++)
2011 rdv(i) = nidx[i] =
d[i].numel ();
2021 for (
int i = 0; i < nd; i++)
2023 idx[i] = xidx + idxtot;
2036 for (
int i = 0; i < nd; i++)
2037 ra_idx.xelem (i) = idx[i][ridx[i]];
2048 template <
typename ArrayND>
2052 if (a.ndims () == 2 && nd <= 2)
2073 for (
int i = 0; i < nd; i++)
2075 rdv(i) = nidx[i] =
d[i].numel ();
2085 for (
int i = 0; i < nd; i++)
2087 idx[i] = xidx + idxtot;
2100 for (
int i = 0; i < nd; i++)
2101 ra_idx(i) = idx[i][ridx[i]];
2111 DEFUN (mat2cell, args, ,
2200 int nargin = args.length ();
2210 for (
int i = 1; i < nargin; i++)
2211 d[i-1] = args(i).octave_idx_type_vector_value (
true);
2215 if (sparse && nargin > 3)
2216 error (
"mat2cell: sparse arguments only support 2-D indexing");
2239 #define BTYP_BRANCH(X, Y) \
2241 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \
2289 template <
typename NDA>
2298 if (array.isvector () && (dim == -1
2299 || (dim == 0 && array.columns () == 1)
2300 || (dim == 1 && array.rows () == 1)))
2308 int ndims = dv.
ndims ();
2325 DEFUN (cellslices, args, ,
2349 int nargin = args.length ();
2351 if (nargin < 3 || nargin > 4)
2360 dim = args(3).int_value () - 1;
2362 error (
"cellslices: DIM must be a valid dimension");
2366 error (
"cellslices: the lengths of LB and UB must match");
2369 if (!
x.issparse () &&
x.is_matrix_type ())
2375 else if (
x.is_char_matrix ())
2378 else if (
x.isinteger ())
2380 if (
x.is_int8_type ())
2383 else if (
x.is_int16_type ())
2386 else if (
x.is_int32_type ())
2389 else if (
x.is_int64_type ())
2392 else if (
x.is_uint8_type ())
2395 else if (
x.is_uint16_type ())
2398 else if (
x.is_uint32_type ())
2401 else if (
x.is_uint64_type ())
2405 else if (
x.iscomplex ())
2407 if (
x.is_single_type ())
2416 if (
x.is_single_type ())
2428 retcell =
Cell (1,
n);
2430 int ndims = dv.
ndims ();
2437 idx(dim) =
Range (lb(i), ub(i));
2438 retcell.
xelem (i) =
x.do_index_op (idx);
2442 return ovl (retcell);
2452 DEFUN (cellindexmat, args, ,
2474 if (args.length () == 0)
2477 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 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 void get_mapper_fun_options(octave::symbol_table &symtab, const octave_value_list &args, int &nargin, bool &uniform_output, octave_value &error_handler)
static octave_value_list get_output_list(octave::error_system &es, octave_idx_type count, octave_idx_type nargout, const octave_value_list &inputlist, octave_value &func, octave_value &error_handler)
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 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)
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
T & xelem(octave_idx_type n)
Size of the specified dimension.
octave_idx_type numel(void) const
Number of elements in the array.
T & elem(octave_idx_type n)
Size of the specified dimension.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
bool isempty(void) const
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
static const idx_vector colon
octave_value last_error_message(const octave_value_list &args, int nargout)
void save_exception(const execution_exception &e)
octave_value last_error_id(const octave_value_list &args, int nargout)
void recover_from_exception(void)
octave_value find_function(const std::string &name, const symbol_scope &search_scope=symbol_scope())
void assign(const std::string &k, const octave_value &val)
Cell cell_value(void) const
octave_value & xelem(octave_idx_type i)
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
octave_idx_type length(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
bool is_uint16_type(void) const
builtin_type_t builtin_type(void) const
bool is_char_matrix(void) const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
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
octave_function * function_value(bool silent=false) 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
octave_value single_subsref(const std::string &type, const octave_value_list &idx)
NDArray array_value(bool frc_str_conv=false) const
bool is_single_type(void) const
uint32NDArray uint32_array_value(void) const
bool isobject(void) const
octave_map map_value(void) const
bool is_undefined(void) 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
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
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)
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 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 valid_identifier(const char *s)
interpreter & __get_interpreter__(const std::string &who)
octave_value get_function_handle(interpreter &interp, const octave_value &arg, const std::string ¶meter_name)
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.
#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
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value::octave_value(const Array< char > &chm, char type) return retval
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.