26#if defined (HAVE_CONFIG_H)
76 bool& allflag,
const char *fcn)
82 error (
"%s: invalid dimension DIM = %d", fcn, dim + 1);
87 std::vector<int> vecdim;
89 int ndims = arg.
ndims ();
91 for (
int i = 0; i < vec.
numel (); i++)
95 error (
"%s: invalid dimension in VECDIM = %d", fcn, vec(i)+1);
97 vecdim.push_back (vec(i));
99 int n = vecdim.size ();
109 std::sort (vecdim.begin (), vecdim.end ());
111 auto dup = std::adjacent_find (vecdim.begin (), vecdim.end ());
112 if (dup != vecdim.end ())
113 error (
"%s: duplicate dimension in VECDIM = %d", fcn, *dup + 1);
116 int out_pos = ndims - n;
120 perm_vec(out_pos++) =
d;
133 new_sz.
resize (ndims - n + 1);
136 for (
int i = 0; i < ndims; i++)
138 if (std::find (vecdim.begin (), vecdim.end (), i)
146 new_sz(idx) = szvecdim;
147 arg = arg.
permute (perm_vec,
false);
190 int nargin = args.length ();
192 bool do_perm =
false;
193 bool allflag =
false;
195 while (nargin > 1 && args(nargin - 1).is_string ())
197 std::string str = args(nargin - 1).string_value ();
202 error (
"all: unrecognized optional argument '%s'", str.c_str ());
207 if (nargin < 1 || nargin > 2)
209 if (allflag && nargin > 1)
210 error (
"all: cannot set DIM or VECDIM with 'all' flag");
220 get_dim_vecdim_all (dimarg, arg, dim, perm_vec, do_perm, allflag,
"all");
230 retval = retval.
permute (perm_vec,
true);
354 int nargin = args.
length ();
356 bool do_perm =
false;
357 bool allflag =
false;
359 while (nargin > 1 && args(nargin - 1).is_string ())
361 std::string str = args(nargin - 1).string_value ();
366 error (
"any: unrecognized optional argument '%s'", str.c_str ());
371 if (nargin < 1 || nargin > 2)
373 if (allflag && nargin > 1)
374 error (
"any: cannot set DIM or VECDIM with 'all' flag");
384 get_dim_vecdim_all (dimarg, arg, dim, perm_vec, do_perm, allflag,
"any");
394 retval = retval.
permute (perm_vec,
true);
500 if (args.length () != 2)
505 if (! args(0).isnumeric ())
508 if (! args(1).isnumeric ())
511 if (args(0).iscomplex () || args(1).iscomplex ())
512 error (
"atan2: not defined for complex numbers");
514 if (args(0).is_single_type () || args(1).is_single_type ())
516 if (args(0).is_scalar_type () && args(1).is_scalar_type ())
517 retval = atan2f (args(0).float_value (), args(1).float_value ());
522 retval = binmap<float> (a0, a1, std::atan2,
"atan2");
527 if (args(0).is_scalar_type () && args(1).is_scalar_type ())
528 retval = atan2 (args(0).scalar_value (), args(1).scalar_value ());
529 else if (args(0).issparse ())
533 retval = binmap<double> (m0, m1, std::atan2,
"atan2");
537 NDArray a0 = args(0).array_value ();
538 NDArray a1 = args(1).array_value ();
539 retval = binmap<double> (a0, a1, std::atan2,
"atan2");
635 retval = binmap<float> (a0, a1, std::hypot,
"hypot");
646 retval = binmap<double> (m0, m1, std::hypot,
"hypot");
652 retval = binmap<double> (a0, a1, std::hypot,
"hypot");
681 int nargin = args.
length ();
689 retval = do_hypot (args(0), args(1));
694 for (
int i = 1; i < nargin; i++)
695 retval = do_hypot (retval, args(i));
740template <
typename T,
typename ET>
749 f.xelem (i) = math::log2 (
x(i), exp);
793 if (args.length () != 1)
799 retval =
ovl (args(0).
log2 ());
800 else if (args(0).is_single_type ())
802 if (args(0).isreal ())
811 else if (args(0).iscomplex ())
821 else if (args(0).isreal ())
830 else if (args(0).iscomplex ())
903 if (args.length () != 2)
908 if (! args(0).isnumeric ())
911 if (! args(1).isnumeric ())
914 if (args(0).iscomplex () || args(1).iscomplex ())
915 error (
"rem: not defined for complex numbers");
927 error (
"rem: cannot combine %s and %s",
928 args(0).class_name ().c_str (),
929 args(1).class_name ().c_str ());
933#define MAKE_INT_BRANCH(X) \
936 X##NDArray a0 = args(0).X##_array_value (); \
937 X##NDArray a1 = args(1).X##_array_value (); \
938 retval = binmap<octave_##X,octave_##X,octave_##X> (a0, a1, rem, "rem"); \
951#undef MAKE_INT_BRANCH
963 error (
"rem: unexpected: found %s instead of integer - please report this bug",
btyp_class_name[btyp0].c_str ());
972 else if (args(0).is_single_type () || args(1).is_single_type ())
974 if (args(0).is_scalar_type () && args(1).is_scalar_type ())
975 retval = math::rem (args(0).float_value (), args(1).float_value ());
980 retval = binmap<float> (a0, a1, math::rem<float>,
"rem");
985 if (args(0).is_scalar_type () && args(1).is_scalar_type ())
986 retval = math::rem (args(0).scalar_value (), args(1).scalar_value ());
987 else if (args(0).issparse () || args(1).issparse ())
991 retval = binmap<double> (m0, m1, math::rem<double>,
"rem");
995 NDArray a0 = args(0).array_value ();
996 NDArray a1 = args(1).array_value ();
997 retval = binmap<double> (a0, a1, math::rem<double>,
"rem");
1109 if (args.length () != 2)
1114 if (! args(0).isnumeric ())
1117 if (! args(1).isnumeric ())
1120 if (args(0).iscomplex () || args(1).iscomplex ())
1121 error (
"mod: not defined for complex numbers");
1133 error (
"mod: cannot combine %s and %s",
1134 args(0).class_name ().c_str (),
1135 args(1).class_name ().c_str ());
1139#define MAKE_INT_BRANCH(X) \
1142 X##NDArray a0 = args(0).X##_array_value (); \
1143 X##NDArray a1 = args(1).X##_array_value (); \
1144 retval = binmap<octave_##X,octave_##X,octave_##X> (a0, a1, mod, "mod"); \
1157#undef MAKE_INT_BRANCH
1169 error (
"mod: unexpected: found %s instead of integer - please report this bug",
btyp_class_name[btyp0].c_str ());
1178 else if (args(0).is_single_type () || args(1).is_single_type ())
1180 if (args(0).is_scalar_type () && args(1).is_scalar_type ())
1181 retval = math::mod (args(0).float_value (), args(1).float_value ());
1186 retval = binmap<float> (a0, a1, math::mod<float>,
"mod");
1191 if (args(0).is_scalar_type () && args(1).is_scalar_type ())
1192 retval = math::mod (args(0).scalar_value (), args(1).scalar_value ());
1193 else if (args(0).issparse () || args(1).issparse ())
1197 retval = binmap<double> (m0, m1, math::mod<double>,
"mod");
1201 NDArray a0 = args(0).array_value ();
1202 NDArray a1 = args(1).array_value ();
1203 retval = binmap<double> (a0, a1, math::mod<double>,
"mod");
1270DEFUN (cumprod, args, ,
1331 int nargin = args.
length ();
1333 bool direction =
false;
1334 bool do_perm =
false;
1335 bool allflag =
false;
1336 bool nanflag =
false;
1338 while (nargin > 1 && args(nargin - 1).is_string ())
1340 std::string str = args(nargin - 1).string_value ();
1342 if (str ==
"forward")
1344 else if (str ==
"reverse")
1346 else if (str ==
"all")
1348 else if (str ==
"omitnan" || str ==
"omitmissing")
1350 if (args(0).is_double_type () || args(0).is_single_type ())
1353 else if (str ==
"includenan" || str ==
"includemissing")
1356 error (
"cumprod: unrecognized optional argument '%s'", str.c_str ());
1361 if (nargin < 1 || nargin > 2)
1363 if (allflag && nargin > 1)
1364 error (
"cumprod: cannot set DIM or VECDIM with 'all' flag");
1374 get_dim_vecdim_all (dimarg, arg, dim, perm_vec, do_perm, allflag,
"cumprod");
1389 error (
"cumprod: DIRECTION is not supported for sparse matrices");
1406 error (
"cumprod: DIRECTION is not supported for sparse matrices");
1433#define MAKE_INT_BRANCH(X) \
1436 retval = arg.X ## _array_value ().flip (dim).cumprod (dim).flip (dim); \
1438 retval = arg.X ## _array_value ().cumprod (dim); \
1450#undef MAKE_INT_BRANCH
1456 error (
"cumprod: DIRECTION is not supported for sparse matrices");
1473 retval = retval.
permute (perm_vec,
true);
1638DEFUN (cumsum, args, ,
1699 int nargin = args.
length ();
1701 bool direction =
false;
1702 bool do_perm =
false;
1703 bool allflag =
false;
1704 bool nanflag =
false;
1706 while (nargin > 1 && args(nargin - 1).is_string ())
1708 std::string str = args(nargin - 1).string_value ();
1710 if (str ==
"forward")
1712 else if (str ==
"reverse")
1714 else if (str ==
"all")
1716 else if (str ==
"omitnan" || str ==
"omitmissing")
1718 if (args(0).is_double_type () || args(0).is_single_type ())
1721 else if (str ==
"includenan" || str ==
"includemissing")
1724 error (
"cumsum: unrecognized optional argument '%s'", str.c_str ());
1729 if (nargin < 1 || nargin > 2)
1731 if (allflag && nargin > 1)
1732 error (
"cumsum: cannot set DIM or VECDIM with 'all' flag");
1742 get_dim_vecdim_all (dimarg, arg, dim, perm_vec, do_perm, allflag,
"cumsum");
1757 error (
"cumsum: DIRECTION is not supported for sparse matrices");
1774 error (
"cumsum: DIRECTION is not supported for sparse matrices");
1801#define MAKE_INT_BRANCH(X) \
1804 retval = arg.X ## _array_value ().flip (dim).cumsum (dim).flip (dim); \
1806 retval = arg.X ## _array_value ().cumsum (dim); \
1818#undef MAKE_INT_BRANCH
1824 error (
"cumsum: DIRECTION is not supported for sparse matrices");
1835 if (dim >= m.
ndims ())
1844 for (
int i = 0; i < dim; i++)
1850 for (
int i = dim + 1; i < dv.
ndims (); i++)
1854 const bool *data = m.
data ();
1855 double *r = result.
rwdata ();
1865 double cumval = 0.0;
1872 cumval += data[idx];
1886 double cumval = 0.0;
1891 cumval += data[idx];
1907 retval = retval.
permute (perm_vec,
true);
2117 int nargin = args.
length ();
2119 if (nargin < 1 || nargin > 3)
2125 retval = args(0).
diag ();
2126 else if (nargin == 2)
2128 octave_idx_type k = args(1).strict_idx_type_value (
"diag: invalid argument K");
2130 retval = args(0).
diag (k);
2137 error (
"diag: V must be a vector");
2139 octave_idx_type m = args(1).strict_idx_type_value (
"diag: invalid dimension M");
2140 octave_idx_type n = args(2).strict_idx_type_value (
"diag: invalid dimension N");
2142 retval = arg0.
diag (m, n);
2299 int nargin = args.
length ();
2301 bool isnative =
false;
2302 bool isdouble =
false;
2303 bool do_perm =
false;
2304 bool allflag =
false;
2305 bool nanflag =
false;
2307 while (nargin > 1 && args(nargin - 1).is_string ())
2309 std::string str = args(nargin - 1).string_value ();
2311 if (str ==
"native")
2313 else if (str ==
"double")
2315 else if (str ==
"all")
2317 else if (str ==
"omitnan" || str ==
"omitmissing")
2319 if (args(0).is_double_type () || args(0).is_single_type ())
2322 else if (str ==
"includenan" || str ==
"includemissing")
2324 else if (str !=
"default")
2325 error (
"prod: unrecognized optional argument '%s'", str.c_str ());
2330 if (nargin < 1 || nargin > 2)
2332 if (allflag && nargin > 1)
2333 error (
"prod: cannot set DIM or VECDIM with 'all' flag");
2343 get_dim_vecdim_all (dimarg, arg, dim, perm_vec, do_perm, allflag,
"prod");
2382#define MAKE_INT_BRANCH(X) \
2385 retval = arg.X ## _array_value ().prod (dim); \
2387 retval = arg.array_value ().prod (dim); \
2399#undef MAKE_INT_BRANCH
2425 retval = retval.
permute (perm_vec,
true);
2622 int n_args = args.
length ();
2623 for (
int i = 0; i < n_args; i++)
2624 if (args(i).
numel () != 1)
2630template <
typename TYPE,
typename T>
2632single_type_concat (
Array<T>& result,
2636 int n_args = args.
length ();
2639 && all_scalar_1x1 (args))
2643 if (dim == -1 || dim == -2)
2646 dv.resize (dim+1, 1);
2651 for (
int j = 0; j < n_args; j++)
2655 result(j) = octave_value_extract<T> (args(j));
2662 for (
int j = 0; j < n_args; j++)
2666 array_list[j] = octave_value_extract<TYPE> (args(j));
2673template <
typename TYPE,
typename T>
2686 sparse_list[j] = octave_value_extract<TYPE> (args(j));
2693template <
typename TYPE>
2699 single_type_concat<TYPE, typename TYPE::element_type> (result, args, dim);
2704template <
typename MAP>
2710 int n_args = args.
length ();
2713 for (
int j = 0; j < n_args; j++)
2717 map_list[j] = octave_value_extract<MAP> (args(j));
2728 if (all_scalar_1x1 (args))
2729 single_type_concat_map<octave_scalar_map> (result, args, dim);
2731 single_type_concat_map<octave_map> (result, args, dim);
2743 int n_args = args.
length ();
2745 if (all_scalar_1x1 (args))
2749 if (dim == -1 || dim == -2)
2752 dv.resize (dim+1, 1);
2757 for (
int j = 0; j < n_args; j++)
2761 arr(j) = args(j).classdef_object_value ()->get_object ();
2768 for (
int j = 0; j < n_args; j++)
2772 cdef_object obj = args(j).classdef_object_value ()->get_object ();
2783 obj_result.
set_class (arr(0).get_class ());
2785 return to_ov (obj_result);
2789attempt_type_conversion (
const octave_value& ov, std::string dtype)
2806 if (! fcn.is_defined ())
2809 if (fcn.is_defined ())
2815 result = interp.
feval (fcn,
ovl (ov), 1);
2819 error (ee,
"conversion from %s to %s failed", dtype.c_str (),
2823 if (result.
empty ())
2824 error (
"conversion from %s to %s failed", dtype.c_str (),
2837 if (! ctor.is_defined ())
2844 if (ctor.is_defined () && ctor.is_function ())
2851 octave::unwind_protect frame;
2853 octave::interpreter_try (frame);
2857 result = interp.
feval (ctor,
ovl (ov), 1);
2859 catch (
const octave::execution_exception&)
2871 result =
to_ov (cls.construct_object (
ovl (ov)));
2875 error (ee,
"%s constructor failed for %s argument",
2876 dtype.c_str (), cname.c_str ());
2890 const std::string& cattype,
int dim)
2914 tmp2 = interp.
feval (ov_fcn,
ovl, 1);
2918 error (ee,
"cat: %s/%s method failed", dtype.c_str (), cattype.c_str ());
2922 error (
"cat: %s/%s method did not return a value", dtype.c_str (),
2942 if (t1_type == dtype)
2946 auto ov = attempt_type_conversion (elt, dtype);
2949 error (
"cat: cannot convert from type \"%s\" to type \"%s\"",
2950 t1_type.c_str (), dtype.c_str ());
2962 retval = do_single_type_concat_cdef (tmp, dim);
2966 octave_map m = do_single_type_concat_map (tmp, dim);
2968 std::string cname = tmp(0).class_name ();
2969 std::list<std::string> parents = tmp(0).parent_class_name_list ();
2988 int n_args = args.
length ();
2992 else if (n_args == 1)
2994 else if (n_args > 1)
2996 std::string result_type;
2998 bool all_strings_p =
true;
2999 bool all_sq_strings_p =
true;
3000 bool all_dq_strings_p =
true;
3001 bool all_real_p =
true;
3002 bool all_cmplx_p =
true;
3003 bool any_sparse_p =
false;
3004 bool any_cell_p =
false;
3005 bool any_class_p =
false;
3007 bool first_elem_is_struct =
false;
3009 for (
int i = 0; i < n_args; i++)
3013 result_type = args(i).class_name ();
3015 first_elem_is_struct = args(i).isstruct ();
3020 if (all_strings_p && ! args(i).is_string ())
3021 all_strings_p =
false;
3022 if (all_sq_strings_p && ! args(i).is_sq_string ())
3023 all_sq_strings_p =
false;
3024 if (all_dq_strings_p && ! args(i).is_dq_string ())
3025 all_dq_strings_p =
false;
3026 if (all_real_p && ! args(i).isreal ())
3028 if (all_cmplx_p && ! (args(i).iscomplex ()
3029 || args(i).isreal ()))
3030 all_cmplx_p =
false;
3031 if (! any_sparse_p && args(i).issparse ())
3032 any_sparse_p =
true;
3033 if (! any_cell_p && args(i).iscell ())
3035 if (! any_class_p && args(i).isobject ())
3039 if (any_cell_p && ! any_class_p && ! first_elem_is_struct)
3042 for (
int i = 0; i < n_args; i++)
3044 if (args(i).iscell ())
3045 args(j++) = args(i);
3048 if (args(i).isempty ())
3051 args(j++) =
Cell (args(i));
3060 else if (result_type ==
"double")
3065 retval = do_single_type_concat<SparseMatrix> (args, dim);
3067 retval = do_single_type_concat<SparseComplexMatrix> (args, dim);
3072 retval = do_single_type_concat<NDArray> (args, dim);
3074 retval = do_single_type_concat<ComplexNDArray> (args, dim);
3077 else if (result_type ==
"single")
3080 retval = do_single_type_concat<FloatNDArray> (args, dim);
3082 retval = do_single_type_concat<FloatComplexNDArray> (args, dim);
3084 else if (result_type ==
"char")
3086 char type = (all_dq_strings_p ?
'"' :
'\'');
3088 if (! all_strings_p)
3090 "numeric", result_type);
3094 charNDArray result = do_single_type_concat<charNDArray> (args, dim);
3098 else if (result_type ==
"logical")
3101 retval = do_single_type_concat<SparseBoolMatrix> (args, dim);
3103 retval = do_single_type_concat<boolNDArray> (args, dim);
3105 else if (result_type ==
"int8")
3106 retval = do_single_type_concat<int8NDArray> (args, dim);
3107 else if (result_type ==
"int16")
3108 retval = do_single_type_concat<int16NDArray> (args, dim);
3109 else if (result_type ==
"int32")
3110 retval = do_single_type_concat<int32NDArray> (args, dim);
3111 else if (result_type ==
"int64")
3112 retval = do_single_type_concat<int64NDArray> (args, dim);
3113 else if (result_type ==
"uint8")
3114 retval = do_single_type_concat<uint8NDArray> (args, dim);
3115 else if (result_type ==
"uint16")
3116 retval = do_single_type_concat<uint16NDArray> (args, dim);
3117 else if (result_type ==
"uint32")
3118 retval = do_single_type_concat<uint32NDArray> (args, dim);
3119 else if (result_type ==
"uint64")
3120 retval = do_single_type_concat<uint64NDArray> (args, dim);
3121 else if (result_type ==
"cell")
3122 retval = do_single_type_concat<Cell> (args, dim);
3123 else if (result_type ==
"struct")
3124 retval = do_single_type_concat_map (args, dim);
3133 if (dim == -1 || dim == -2)
3139 for (
int i = 1; i < args.
length (); i++)
3141 if (! (dv.*concat_rule) (args(i).dims (), dim))
3142 error (
"cat: dimension mismatch");
3163 int dv_len = dv.
ndims ();
3166 for (
int j = 0; j < n_args; j++)
3178 error (
"%s: indexing error", fname.c_str ());
3183 ra_idx(dim) += (dim < dv_tmp.
ndims () ? dv_tmp(dim) : 1);
3194DEFUN (horzcat, args, ,
3212 return do_cat (args, -2,
"horzcat");
3436DEFUN (vertcat, args, ,
3454 return do_cat (args, -1,
"vertcat");
3512 int dim = args(0).strict_int_value (
"cat: DIM must be an integer") - 1;
3515 error (
"cat: DIM must be a valid dimension");
3517 return ovl (do_cat (args.
slice (1, args.
length () - 1), dim,
"cat"));
3684 if (args.
length () != 2 || args(1).length () < args(1).ndims ())
3687 Array<int> vec = args(1).int_vector_value ();
3691 int n = vec.
numel ();
3692 for (
int i = 0; i < n; i++)
3698DEFUN (permute, args, ,
3728 return do_permute (args,
false);
3731DEFUN (ipermute, args, ,
3747 return do_permute (args,
true);
3750DEFUN (length, args, ,
3765 return ovl (args(0).length ());
3768DEFUN (ndims, args, ,
3796 while ((ndims > 2) && (sz(ndims - 1) == 1))
3851 int nargin = args.
length ();
3859 retval = args(0).numel ();
3860 else if (nargin > 1)
3870DEFUN (size, args, nargout,
3943 int nargin = args.
length ();
3953 int ndims = dimensions.
ndims ();
3959 dimensions = dimensions.
redim (nargout);
3960 ndims = dimensions.
ndims ();
3966 m(i) = dimensions(i);
3977 query_dims(i) = args(i+1).idx_type_value (
true);
3980 query_dims = args(1).octave_idx_type_vector_value (
true);
3982 if (nargout > 1 && nargout != query_dims.
numel ())
3983 error (
"size: nargout > 1 but does not match number of requested dimensions");
3994 error (
"size: requested dimension DIM (= %"
3995 OCTAVE_IDX_TYPE_FORMAT
") out of range", nd);
3997 m(i) = nd <= ndims ? dimensions (nd-1) : 1;
4091DEFUN (size_equal, args, ,
4102 int nargin = args.
length ();
4108 for (
int i = 1; i < nargin; ++i)
4112 if (a_dims != b_dims)
4130 return ovl (args(0).nnz ());
4149DEFUN (nzmax, args, ,
4166 return ovl (args(0).nzmax ());
4221DEFUN (columns, args, ,
4315 int nargin = args.
length ();
4317 bool isnative =
false;
4318 bool isdouble =
false;
4319 bool isextra =
false;
4320 bool do_perm =
false;
4321 bool allflag =
false;
4322 bool nanflag =
false;
4324 while (nargin > 1 && args(nargin - 1).is_string ())
4326 std::string str = args(nargin - 1).string_value ();
4328 if (str ==
"native")
4330 else if (str ==
"double")
4332 else if (str ==
"extra")
4334 else if (str ==
"all")
4336 else if (str ==
"omitnan" || str ==
"omitmissing")
4338 if (args(0).is_double_type () || args(0).is_single_type ())
4341 else if (str ==
"includenan" || str ==
"includemissing")
4343 else if (str !=
"default")
4344 error (
"sum: unrecognized optional argument '%s'", str.c_str ());
4349 if (nargin < 1 || nargin > 2)
4351 if (allflag && nargin > 1)
4352 error (
"sum: cannot set DIM or VECDIM with 'all' flag");
4362 get_dim_vecdim_all (dimarg, arg, dim, perm_vec, do_perm, allflag,
"sum");
4408 if (isdouble || isextra)
4415 if (isdouble || isextra)
4421#define MAKE_INT_BRANCH(X) \
4424 retval = arg.X ## _array_value ().sum (dim); \
4426 retval = arg.X ## _array_value ().dsum (dim); \
4438#undef MAKE_INT_BRANCH
4470 retval = retval.
permute (perm_vec,
true);
4809DEFUN (sumsq, args, ,
4884 int nargin = args.
length ();
4886 bool isnative =
false;
4887 bool isdouble =
false;
4888 bool do_perm =
false;
4889 bool allflag =
false;
4890 bool nanflag =
false;
4892 while (nargin > 1 && args(nargin - 1).is_string ())
4894 std::string str = args(nargin - 1).string_value ();
4896 if (str ==
"native")
4898 else if (str ==
"double")
4900 else if (str ==
"all")
4902 else if (str ==
"omitnan" || str ==
"omitmissing")
4904 if (args(0).is_double_type () || args(0).is_single_type ())
4907 else if (str ==
"includenan" || str ==
"includemissing")
4909 else if (str !=
"default")
4910 error (
"sumsq: unrecognized optional argument '%s'", str.c_str ());
4915 if (nargin < 1 || nargin > 2)
4917 if (allflag && nargin > 1)
4918 error (
"sumsq: cannot set DIM or VECDIM with 'all' flag");
4928 get_dim_vecdim_all (dimarg, arg, dim, perm_vec, do_perm, allflag,
"sumsq");
4967#define MAKE_INT_BRANCH(X) \
4970 retval = arg.X ## _array_value ().sumsq (dim); \
4972 retval = arg.array_value ().sumsq (dim); \
4984#undef MAKE_INT_BRANCH
5014 retval = retval.
permute (perm_vec,
true);
5193DEFUN (islogical, args, ,
5207 return ovl (args(0).islogical ());
5284DEFUN (iscomplex, args, ,
5294 return ovl (args(0).iscomplex ());
5314DEFUN (isfloat, args, ,
5326 return ovl (args(0).isfloat ());
5332DEFUN (complex, args, ,
5362 int nargin = args.
length ();
5364 if (nargin < 1 || nargin > 2)
5385 if (arg.
numel () == 1)
5400 if (arg.
numel () == 1)
5425 if (re.
numel () == 1)
5428 if (re_val.
nnz () == 0)
5440 i < im_val.
cidx (j + 1); i++)
5441 result.
data (im_val.
ridx (i) + off)
5447 else if (im.
numel () == 1)
5450 if (im_val.
nnz () == 0)
5463 i < re_val.
cidx (j + 1); i++)
5464 result.
data (re_val.
ridx (i) + off)
5472 if (re_val.
dims () != im_val.
dims ())
5473 error (
"complex: dimension mismatch");
5483 if (re.
numel () == 1)
5487 if (im.
numel () == 1)
5511 if (im.
numel () == 1)
5527 if (re_val.
dims () != im_val.
dims ())
5528 error (
"complex: dimension mismatch");
5541 else if (re.
numel () == 1)
5545 if (im.
numel () == 1)
5568 if (im.
numel () == 1)
5583 if (re_val.
dims () != im_val.
dims ())
5584 error (
"complex: dimension mismatch");
5643DEFUN (isreal, args, ,
5656 return ovl (args(0).isreal ());
5659DEFUN (isempty, args, ,
5670 return ovl (args(0).isempty ());
5678DEFUN (isnumeric, args, ,
5692 return ovl (args(0).isnumeric ());
5711DEFUN (isscalar, args, ,
5728 return ovl (sz.
numel () == 2 && sz(0) == 1 && sz(1) == 1);
5768 return ovl (sz.
numel () == 2 && (sz(0) == 1 || sz(1) == 1));
5791DEFUN (isrow, args, ,
5808 return ovl (sz.
numel () == 2 && sz(0) == 1);
5840DEFUN (iscolumn, args, ,
5857 return ovl (sz.
numel () == 2 && sz(1) == 1);
5889DEFUN (ismatrix, args, ,
5907 return ovl (sz.
numel () == 2 && sz(0) >= 0 && sz(1) >= 0);
5938DEFUN (issquare, args, ,
5955 return ovl (sz.
numel () == 2 && sz(0) == sz(1));
5984 int nargin = args.
length ();
5989 bool issparse =
false;
5990 bool iscomplex =
false;
5992 if (nargin > 0 && args(nargin-1).is_string ())
5994 std::string nm = args(nargin-1).string_value ();
6000 if (nargin > 1 && args(nargin-2).is_string ()
6001 && args(nargin-2).string_value () ==
"like")
6003 std::string nm = args(nargin-1).class_name ();
6004 issparse = args(nargin-1).issparse ();
6005 iscomplex = args(nargin-1).iscomplex ();
6021 dims.resize (nargin);
6023 for (
int i = 0; i < nargin; i++)
6025 if (args(i).
numel () > 1)
6026 error (
"%s: dimensions must be scalars.", fcn);
6028 dims(i) = (args(i).isempty () ? 0 : args(i).idx_type_value (
true));
6034 dims.chop_trailing_singletons ();
6046 if (dims.ndims () > 2)
6047 error (
"%s: sparse ND arrays not supported.", fcn);
6055 retval =
SparseMatrix (dims(0), dims(1),
static_cast<double> (val));
6064 error (
"%s: invalid class name for sparse", fcn);
6123 error (
"%s: invalid class name", fcn);
6136 int nargin = args.
length ();
6141 bool issparse =
false;
6142 bool iscomplex =
false;
6144 if (nargin > 0 && args(nargin-1).is_string ())
6146 std::string nm = args(nargin-1).string_value ();
6152 if (nargin > 1 && args(nargin-2).is_string ()
6153 && args(nargin-2).string_value () ==
"like"
6154 && (std::string(fcn) ==
"Inf"
6155 || std::string(fcn) ==
"NaN" || std::string(fcn) ==
"NA"))
6157 if (! args(nargin-1).isfloat ())
6158 error (
"%s: input followed by 'like' must be floating point", fcn);
6159 std::string nm = args(nargin-1).class_name ();
6160 issparse = args(nargin-1).issparse ();
6161 iscomplex = args(nargin-1).iscomplex ();
6177 dims.resize (nargin);
6179 for (
int i = 0; i < nargin; i++)
6181 if (args(i).
numel () > 1)
6182 error (
"%s: dimensions must be scalars.", fcn);
6184 dims(i) = (args(i).isempty () ? 0 : args(i).idx_type_value (
true));
6190 dims.chop_trailing_singletons ();
6199 if (dims.ndims () > 2)
6200 error (
"%s: sparse ND arrays not supported", fcn);
6205 retval =
SparseMatrix (dims(0), dims(1),
static_cast<double> (val));
6227 error (
"%s: invalid class name", fcn);
6239 int nargin = args.
length ();
6245 if (nargin > 0 && args(nargin-1).is_string ())
6247 std::string nm = args(nargin-1).string_value ();
6264 dims.resize (nargin);
6266 for (
int i = 0; i < nargin; i++)
6268 if (args(i).
numel () > 1)
6269 error (
"%s: dimensions must be scalars.", fcn);
6271 dims(i) = (args(i).isempty () ? 0 : args(i).idx_type_value (
true));
6277 dims.chop_trailing_singletons ();
6287 retval =
FloatNDArray (dims,
static_cast<float> (val));
6295 error (
"%s: invalid class name", fcn);
6308 int nargin = args.
length ();
6314 if (nargin > 0 && args(nargin-1).is_string ())
6316 std::string nm = args(nargin-1).string_value ();
6333 dims.resize (nargin);
6335 for (
int i = 0; i < nargin; i++)
6337 if (args(i).
numel () > 1)
6338 error (
"%s: dimensions must be scalars.", fcn);
6340 dims(i) = (args(i).isempty () ? 0 : args(i).idx_type_value (
true));
6346 dims.chop_trailing_singletons ();
6365 error (
"%s: invalid class name", fcn);
6377 int nargin = args.
length ();
6384 if (nargin > 0 && args(nargin-1).is_string ())
6386 std::string nm = args(nargin-1).string_value ();
6390 error (
"%s: invalid data type '%s'", fcn, nm.c_str ());
6393 bool issparse =
false;
6395 if (nargin > 1 && args(nargin-2).is_string ()
6396 && args(nargin-2).string_value () ==
"like")
6398 if (! args(nargin-1).islogical ())
6399 error (R
"(%s: input followed by "like" must be logical)", fcn);
6401 issparse = args(nargin-1).issparse ();
6416 dims.resize (nargin);
6418 for (
int i = 0; i < nargin; i++)
6420 if (args(i).
numel () > 1)
6421 error (
"%s: dimensions must be scalars.", fcn);
6423 dims(i) = (args(i).isempty () ? 0 : args(i).idx_type_value (
true));
6429 dims.chop_trailing_singletons ();
6438 if (dims.ndims () > 2)
6439 error (
"%s: sparse ND arrays not supported", fcn);
6492 return fill_matrix (args, 1,
"ones");
6555DEFUN (zeros, args, ,
6592 return fill_matrix (args, 0,
"zeros");
6835 double e_val = exp (1.0);
6838 return fill_matrix (args, e_val,
"e");
6841template <
typename T>
6845 T epsval =
x.abs ();
6846 typedef typename T::value_type P;
6849 P val = epsval.xelem (i);
6850 if (math::isnan (val) || math::isinf (val))
6851 epsval(i) = numeric_limits<P>::NaN ();
6852 else if (val < std::numeric_limits<P>::min ())
6853 epsval(i) = std::numeric_limits<P>::denorm_min ();
6857 math::frexp (val, &exponent);
6858 const P digits = std::numeric_limits<P>::digits;
6859 epsval(i) = std::pow (
static_cast<P
> (2.0),
6860 static_cast<P
> (exponent - digits));
6906 if (args.
length () == 1 && ! args(0).is_string ())
6920 error (
"eps: X must be of a floating point type");
6923 retval = fill_matrix (args, std::numeric_limits<double>::epsilon (),
6924 std::numeric_limits<float>::epsilon (),
"eps");
6999 double pi_val = M_PI;
7001 double pi_val = 4.0 *
atan (1.0);
7004 return fill_matrix (args, pi_val,
"pi");
7007DEFUN (realmax, args, ,
7045 return fill_matrix (args, std::numeric_limits<double>::max (),
7046 std::numeric_limits<float>::max (),
"realmax");
7049DEFUN (realmin, args, ,
7087 return fill_matrix (args, std::numeric_limits<double>::min (),
7088 std::numeric_limits<float>::min (),
"realmin");
7129 return fill_matrix (args,
Complex (0.0, 1.0),
"I");
7185DEFUN (
false, args, ,
7214 return fill_matrix (args,
false,
"false");
7264 return fill_matrix (args,
true,
"true");
7286template <
typename MT>
7292 typename MT::element_type one (1);
7294 if (nr == 1 && nc == 1)
7300 typename MT::element_type zero (0);
7304 if (nr > 0 && nc > 0)
7306 int n = std::min (nr, nc);
7308 for (
int i = 0; i < n; i++)
7318#define INSTANTIATE_EYE(T) \
7319 template octave_value identity_matrix<T> (int, int)
7388 error (
"eye: invalid class name");
7395#undef INT_EYE_MATRIX
7451 int nargin = args.
length ();
7457 if (nargin > 0 && args(nargin-1).is_string ())
7459 std::string nm = args(nargin-1).string_value ();
7472 else if (nargin == 1)
7510template <
typename MT>
7515 typedef typename MT::column_vector_type CVT;
7516 typedef typename MT::element_type T;
7522 T bs = octave_value_extract<T> (base);
7525 T ls = octave_value_extract<T> (limit);
7530 CVT lv = octave_value_extract<CVT> (limit);
7531 CVT bv (lv.numel (), bs);
7537 CVT bv = octave_value_extract<CVT> (base);
7540 T ls = octave_value_extract<T> (limit);
7541 CVT lv (bv.numel (), ls);
7546 CVT lv = octave_value_extract<CVT> (limit);
7580 int nargin = args.
length ();
7582 if (nargin != 2 && nargin != 3)
7595 error (
"linspace: N must be a scalar");
7605 bool isvector1 = sz1.
ndims () == 2 && (sz1(0) == 1 || sz1(1) == 1);
7607 bool isvector2 = sz2.
ndims () == 2 && (sz2(0) == 1 || sz2(1) == 1);
7609 if (! isvector1 || ! isvector2)
7610 error (
"linspace: START, END must be scalars or vectors");
7617 retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints);
7619 retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints);
7624 retval = do_linspace<ComplexMatrix> (arg_1, arg_2, npoints);
7626 retval = do_linspace<Matrix> (arg_1, arg_2, npoints);
7748DEFUN (resize, args, ,
7794 int nargin = args.
length ();
7804 int ndim = vec.
numel ();
7815 for (
int i = 0; i < ndim; i++)
7818 retval = retval.
resize (dv,
true);
7829 retval = retval.
resize (dv,
true);
7837DEFUN (reshape, args, ,
7871 int nargin = args.
length ();
7884 if (new_size.
numel () < 2)
7885 error (
"reshape: SIZE must have 2 or more dimensions");
7891 if (new_size(i) < 0)
7892 error (
"reshape: SIZE must be non-negative");
7894 new_dims(i) = new_size(i);
7902 for (
int i = 1; i < nargin; i++)
7904 if (args(i).isempty ())
7907 error (
"reshape: only a single dimension can be unknown");
7914 new_dims(i-1) = args(i).idx_type_value ();
7916 if (new_dims(i-1) < 0)
7917 error (
"reshape: SIZE must be non-negative");
7926 new_dims(empty_dim-1) = 0;
7932 if (a_nel != size_empty_dim * nel)
7933 error (
"reshape: SIZE is not divisible by the product of "
7934 "known dimensions (= %" OCTAVE_IDX_TYPE_FORMAT
")",
7937 new_dims(empty_dim-1) = size_empty_dim;
7942 retval = args(0).reshape (new_dims);
7988 int nargin = args.
length ();
7990 if (nargin < 1 || nargin > 2)
7996 dim = args(1).idx_type_value ();
7999 error (
"vec: DIM must be greater than zero");
8011 for (
int i = 0; i < dim-1; i++)
8014 new_dims(dim-1) = retval.
numel ();
8016 retval = retval.
reshape (new_dims);
8037DEFUN (squeeze, args, ,
8050 return ovl (args(0).squeeze ());
8064 return ovl (args(0).full_value ());
8130 int nargin = args.
length ();
8132 if (nargin < 1 || nargin > 3)
8137 if (x_arg.
ndims () != 2)
8138 error (
"norm: only valid for 2-D objects");
8140 enum {sfmatrix, sfcols, sfrows, sffrob, sfinf, sfneginf} strflag = sfmatrix;
8141 if (nargin > 1 && args(nargin-1).is_string ())
8143 std::string str = args(nargin-1).string_value ();
8144 std::transform (str.begin (), str.end (), str.begin (), tolower);
8145 if (str ==
"cols" || str ==
"columns")
8147 else if (str ==
"rows")
8149 else if (str ==
"fro")
8151 else if (str ==
"inf")
8153 else if (str ==
"-inf")
8156 error (
"norm: unrecognized option: %s", str.c_str ());
8169 std::transform (str.begin (), str.end (), str.begin (), tolower);
8170 if (strflag != sfcols && strflag != sfrows)
8171 error (
"norm: invalid combination of options");
8173 if (str ==
"cols" || str ==
"columns" || str ==
"rows")
8174 error (
"norm: invalid combination of options");
8178 else if (str ==
"inf")
8179 p_arg = numeric_limits<double>::Inf ();
8180 else if (str ==
"-inf")
8181 p_arg = -numeric_limits<double>::Inf ();
8183 error (
"norm: unrecognized option: %s", str.c_str ());
8193 retval =
xnorm (x_arg, p_arg);
8209 retval =
xnorm (x_arg, numeric_limits<double>::Inf ());
8213 retval =
xnorm (x_arg, -numeric_limits<double>::Inf ());
8428DEFUN (uplus, args, ,
8438DEFUN (uminus, args, ,
8448DEFUN (transpose, args, ,
8480DEFUN (ctranspose, args, ,
8519 return binary_op (op, args(0), args(1));
8527 int nargin = args.
length ();
8535 retval =
binary_op (op, args(0), args(1));
8538 retval =
binary_op (op, args(0), args(1));
8540 for (
int i = 2; i < nargin; i++)
8541 retval.
assign (aop, args(i));
8578DEFUN (minus, args, ,
8588DEFUN (mtimes, args, ,
8609DEFUN (mrdivide, args, ,
8624DEFUN (mpower, args, ,
8639DEFUN (mldivide, args, ,
8718DEFUN (times, args, ,
8739DEFUN (rdivide, args, ,
8751DEFUN (power, args, ,
8772DEFUN (ldivide, args, ,
8827DEFUN (colon, args, ,
8840 int nargin = args.
length ();
8842 if (nargin < 2 || nargin > 3)
8847 :
colon_op (args(0), args(1), args(2)));
8850static double tic_toc_timestamp = -1.0;
8852DEFUN (tic, args, nargout,
8905 warning (
"tic: ignoring extra arguments");
8914 double frac = std::modf (tmp, &ip);
8915 uint64_t microsecs =
static_cast<uint64_t
> (CLOCKS_PER_SEC * frac);
8916 microsecs += CLOCKS_PER_SEC *
static_cast<uint64_t
> (ip);
8920 tic_toc_timestamp = tmp;
8925DEFUN (toc, args, nargout,
8943 int nargin = args.
length ();
8948 double start_time = tic_toc_timestamp;
8952 octave_uint64 id = args(0).xuint64_scalar_value (
"toc: invalid ID");
8954 uint64_t val =
id.value ();
8957 = (
static_cast<double> (val / CLOCKS_PER_SEC)
8958 +
static_cast<double> (val % CLOCKS_PER_SEC)
8966 error (
"toc: function called before timer initialization with tic()");
8970 double etime = now.double_value () - start_time;
8976 octave_stdout <<
"Elapsed time is " << etime <<
" seconds.\n";
8988DEFUN (cputime, args, ,
9010 sys::cpu_time cpu_tm;
9012 double usr = cpu_tm.user ();
9013 double sys = cpu_tm.system ();
9015 return ovl (usr + sys, usr, sys);
9018DEFUN (sort, args, nargout,
9088 int nargin = args.
length ();
9090 if (nargin < 1 || nargin > 3)
9094 bool return_idx = (nargout > 1);
9095 bool have_sortmode = (nargin > 1 && args(1).is_string ());
9103 std::string mode = args(1).string_value ();
9104 if (mode ==
"ascend")
9106 else if (mode ==
"descend")
9109 error (R
"(sort: MODE must be either "ascend" or "descend")");
9114 if (! args(1).is_scalar_type () || args(1).iscomplex ()
9115 || args(1).double_value () <= 0)
9116 error (
"sort: DIM must be a positive scalar integer");
9119 dim = args(1).strict_int_value (
"sort: DIM must be a positive scalar integer") - 1;
9126 error (
"sort: DIM argument must precede MODE argument");
9128 std::string mode = args(2).xstring_value (
"sort: MODE must be a string");
9130 if (mode ==
"ascend")
9132 else if (mode ==
"descend")
9135 error (R
"(sort: MODE must be either "ascend" or "descend")");
9139 if (nargin == 1 || have_sortmode)
9152 retval(0) = arg.
sort (sidx, dim, smode);
9157 retval =
ovl (arg.
sort (dim, smode));
9416DEFUN (__sort_rows_idx__, args, ,
9422 int nargin = args.
length ();
9424 if (nargin < 1 || nargin > 2)
9427 if (nargin == 2 && ! args(1).is_string ())
9428 error (
"__sort_rows_idx__: second argument must be a string");
9433 std::string mode = args(1).string_value ();
9434 if (mode ==
"ascend")
9436 else if (mode ==
"descend")
9439 error (R
"(__sort_rows_idx__: MODE must be either "ascend" or "descend")");
9445 error (
"__sort_rows_idx__: sparse matrices not yet supported");
9447 if (arg.
ndims () != 2)
9448 error (
"__sort_rows_idx__: needs a 2-D object");
9464 std::string mode = arg.
xstring_value (
"issorted: MODE must be a string");
9466 if (mode ==
"ascend")
9468 else if (mode ==
"descend")
9470 else if (mode ==
"either" || mode ==
"monotonic")
9473 error (R
"(issorted: MODE must be "ascend", "descend", "monotonic", or "either")");
9478DEFUN (issorted, args, ,
9497 int nargin = args.
length ();
9499 if (nargin < 1 || nargin > 3)
9502 bool by_rows =
false;
9509 smode = get_sort_mode_option (args(2));
9511 std::string tmp = args(1).xstring_value (
"issorted: second argument must be a string");
9515 smode = get_sort_mode_option (args(1));
9527 error (
"issorted: sparse matrices not yet supported");
9529 if (arg.
ndims () != 2)
9530 error (
"issorted: A must be a 2-D object");
9537 error (
"issorted: needs a vector");
9539 retval = args(0).issorted (smode) !=
UNSORTED;
9614DEFUN (nth_element, args, ,
9638 int nargin = args.
length ();
9640 if (nargin < 2 || nargin > 3)
9646 dim = args(2).int_value (
true) - 1;
9648 error (
"nth_element: DIM must be a valid dimension");
9676#define MAKE_INT_BRANCH(X) \
9678 retval = argx.X ## _array_value ().nth_element (n, dim); \
9691#undef MAKE_INT_BRANCH
9702 error (
"nth_element: invalid index %s", ie.what ());
9723template <
typename NDT>
9725do_accumarray_sum (
const idx_vector& idx,
const NDT& vals,
9728 typedef typename NDT::element_type T;
9731 else if (idx.
extent (n) > n)
9732 error (
"accumarray: index out of range");
9736 if (vals.numel () == 1)
9737 retval.idx_add (idx, vals (0));
9738 else if (vals.numel () == idx.
length (n))
9739 retval.idx_add (idx, vals);
9741 error (
"accumarray: dimensions mismatch");
9746DEFUN (__accumarray_sum__, args, ,
9752 int nargin = args.
length ();
9754 if (nargin < 2 || nargin > 3)
9757 if (! args(0).isnumeric ())
9758 error (
"__accumarray_sum__: first argument must be numeric");
9767 n = args(2).idx_type_value (
true);
9774 if (r.increment () == 0)
9781 retval = do_accumarray_sum (idx,
9790 retval = do_accumarray_sum (idx,
9794 retval = do_accumarray_sum (idx, vals.
array_value (), n);
9801 error (
"__accumarray_sum__: invalid index %s", ie.what ());
9807template <
typename NDT>
9809do_accumarray_minmax (
const idx_vector& idx,
const NDT& vals,
9811 const typename NDT::element_type& zero_val)
9813 typedef typename NDT::element_type T;
9816 else if (idx.
extent (n) > n)
9817 error (
"accumarray: index out of range");
9823 = ismin ? (&
MArray<T>::idx_min) : (&
MArray<T>::idx_max);
9826 if (vals.numel () == 1)
9827 (retval.*op) (idx, NDT (
dim_vector (l, 1), vals(0)));
9828 else if (vals.numel () == l)
9829 (retval.*op) (idx, vals);
9831 error (
"accumarray: dimensions mismatch");
9840 int nargin = args.
length ();
9842 if (nargin < 3 || nargin > 4)
9845 if (! args(0).isnumeric ())
9846 error (
"accumarray: first argument must be numeric");
9855 n = args(3).idx_type_value (
true);
9863 retval = do_accumarray_minmax (idx, vals.
array_value (), n, ismin,
9878 retval = do_accumarray_minmax (idx,
9884#define MAKE_INT_BRANCH(X) \
9886 retval = do_accumarray_minmax (idx, vals.X ## _array_value (), \
9887 n, ismin, zero.X ## _scalar_value ()); \
9899#undef MAKE_INT_BRANCH
9902 retval = do_accumarray_minmax (idx, vals.
array_value (), n, ismin,
9912 error (
"do_accumarray_minmax_fcn: invalid index %s", ie.what ());
9918DEFUN (__accumarray_min__, args, ,
9924 return do_accumarray_minmax_fcn (args,
true);
9927DEFUN (__accumarray_max__, args, ,
9933 return do_accumarray_minmax_fcn (args,
false);
9936template <
typename NDT>
9938do_accumdim_sum (
const idx_vector& idx,
const NDT& vals,
9941 typedef typename NDT::element_type T;
9944 else if (idx.
extent (n) > n)
9945 error (
"accumdim: index out of range");
9951 dim = vals.dims ().first_non_singleton ();
9952 else if (dim >= rdv.
ndims ())
9957 NDT retval (rdv, T ());
9959 if (idx.
length () != vals_dim(dim))
9960 error (
"accumdim: dimension mismatch");
9962 retval.idx_add_nd (idx, vals, dim);
9967DEFUN (__accumdim_sum__, args, ,
9973 int nargin = args.
length ();
9975 if (nargin < 2 || nargin > 4)
9978 if (! args(0).isnumeric ())
9979 error (
"__accumdim_sum__: first argument must be numeric");
9988 dim = args(2).int_value () - 1;
9992 n = args(3).idx_type_value (
true);
9999 retval = do_accumdim_sum (idx,
10012 retval = do_accumdim_sum (idx, vals.
array_value (), dim, n);
10019 error (
"__accumdim_sum__: invalid index %s", ie.what ());
10025template <
typename NDT>
10028 const NDT& tval,
const NDT& fval)
10030 typedef typename NDT::element_type T;
10034 bool tscl = tval.numel () == 1;
10035 bool fscl = fval.numel () == 1;
10037 if ((! tscl && tval.dims () != dv) || (! fscl && fval.dims () != dv))
10038 error (
"merge: MASK, TVAL, and FVAL dimensions must match");
10040 T *rv = retval.rwdata ();
10043 const T *tv = tval.data ();
10044 const T *fv = fval.data ();
10045 const bool *mv = mask.
data ();
10054 rv[i] = (mv[i] ? ts : fs);
10060 rv[i] = (mv[i] ? ts : fv[i]);
10069 rv[i] = (mv[i] ? tv[i] : fs);
10074 rv[i] = (mv[i] ? tv[i] : fv[i]);
10081#define MAKE_INT_BRANCH(INTX) \
10082 else if (tval.is_ ## INTX ## _type () && fval.is_ ## INTX ## _type ()) \
10084 retval = do_merge (mask, \
10085 tval.INTX ## _array_value (), \
10086 fval.INTX ## _array_value ()); \
10089DEFUN (merge, args, ,
10121 if (args.
length () != 3)
10124 if (! (args(0).islogical () || args(0).isnumeric ()))
10125 error (
"merge: first argument must be logical or numeric");
10132 retval = (mask_val.
is_true () ? args(1) : args(2));
10143 retval = do_merge (mask,
10147 retval = do_merge (mask,
10154 retval = do_merge (mask,
10158 retval = do_merge (mask,
10168 sq_string ?
'\'' :
'"');
10172 retval = do_merge (mask,
10187 error (
"merge: cannot merge %s with %s with array mask",
10197#undef MAKE_INT_BRANCH
10199template <
typename SparseT>
10204 SparseT retval = array;
10208 while (order > 0 && k > 0)
10210 idx_vector col1 (
':'), col2 (
':'), sl1 (1, k), sl2 (0, k-1);
10211 retval = SparseT (retval.index (col1, sl1))
10212 - SparseT (retval.index (col2, sl2));
10221 while (order > 0 && k > 0)
10223 idx_vector col1 (
':'), col2 (
':'), sl1 (1, k), sl2 (0, k-1);
10224 retval = SparseT (retval.index (sl1, col1))
10225 - SparseT (retval.index (sl2, col2));
10247 if (dv(dim) <= order)
10256 if (dim == dv.
ndims ())
10258 retval = do_diff (array, order, dim - 1);
10261 else if (dv(dim) == 1)
10265 retval = do_diff (array, dv(dim) - 1, dim);
10266 order -= dv(dim) - 1;
10295 error (
"diff: unexpected integer type - please report this bug");
10323DEFUN (diff, args, ,
10353 int nargin = args.
length ();
10355 if (nargin < 1 || nargin > 3)
10358 if (! (args(0).isnumeric () || args(0).islogical ()))
10359 error (
"diff: X must be numeric or logical");
10365 if (args(1).is_scalar_type ())
10366 order = args(1).idx_type_value (
true,
false);
10367 else if (! args(1).is_zero_by_zero ())
10368 error (
"diff: order K must be a scalar or []");
10370 error (
"diff: order K must be non-negative");
10375 dim = args(2).int_value (
true,
false);
10376 if (dim < 1 || dim > args(0).ndims ())
10377 error (
"diff: DIM must be a valid dimension");
10382 return do_diff (args(0), order, dim);
10398template <
typename T>
10404 if (rep.
ndims () != 2 || rep.
rows () != 2)
10405 error (
"repelems: R must be a 2-row, N-column matrix of integers");
10413 error (
"repelems: second row must contain non-negative numbers");
10418 retval.
clear (1, l);
10419 T *dest = retval.
rwdata ();
10424 std::fill_n (dest, k, src.
checkelem (rep(0, i) - 1));
10431DEFUN (repelems, args, ,
10457 if (args.
length () != 2)
10462 const Matrix rm = args(1).matrix_value ();
10464 if (rm.
rows () != 2 || rm.
ndims () != 2)
10465 error (
"repelems: R must be a matrix with two rows");
10474 if (
static_cast<double> (rx) != rm(i))
10475 error (
"repelems: R must be a matrix of integers");
10480 switch (
x.builtin_type ())
10482#define BTYP_BRANCH(X, EX) \
10484 retval = do_repelems (x.EX ## _value (), r); \
10490 BTYP_BRANCH (float_complex, float_complex_array);
10504 retval =
Cell (do_repelems (
x.cell_value (), r));
10527 if (args.
length () != 1)
10530 if (! args(0).isnumeric ())
10531 error (
"base64_encode: encoding is supported only for numeric arrays");
10533 if (args(0).iscomplex () || args(0).issparse ())
10534 error (
"base64_encode: encoding complex or sparse data is not supported");
10540#define MAKE_INT_BRANCH(X) \
10541 if (args(0).is_ ## X ## _type ()) \
10543 const X##NDArray in = args(0). X## _array_value (); \
10544 std::size_t inlen = in.numel () * sizeof (X## _t) / sizeof (char); \
10545 const char *inc = reinterpret_cast<const char *> (in.data ()); \
10547 if (base64_encode (inc, inlen, &out)) \
10549 retval(0) = octave_value (out); \
10563#undef MAKE_INT_BRANCH
10566 error (
"base_64_decode: unexpected integer type - please report this bug");
10568 else if (args(0).is_single_type ())
10572 inlen = in.
numel () *
sizeof (
float) /
sizeof (
char);
10574 inc =
reinterpret_cast<const char *
> (in.
data ());
10586 inlen = in.
numel () *
sizeof (
double) /
sizeof (
char);
10588 inc =
reinterpret_cast<const char *
> (in.
data ());
10635 int nargin = args.
length ();
10637 if (nargin < 1 || nargin > 2)
10640 std::string str = args(0).xstring_value (
"base64_decode: first argument must be a character array");
10649 = args(1).octave_idx_type_vector_value ();
10655 retval = retval.
reshape (dims);
10658 return ovl (retval);
10679DEFUN (__base64_decode_bytes__, args, ,
10691 int nargin = args.
length ();
10693 if (nargin < 1 || nargin > 2)
10696 std::string str = args(0).xstring_value (
"__base64_decode_bytes__: first argument must be a character array");
10705 = args(1).octave_idx_type_vector_value ();
10711 retval = retval.
reshape (dims);
10714 return ovl (retval);
10733OCTAVE_END_NAMESPACE(octave)
bool isvector(const dim_vector &dim)
ComplexMatrix linspace(const ComplexColumnVector &x1, const ComplexColumnVector &x2, octave_idx_type n)
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
cdef_class lookup_class(const std::string &name, bool error_if_not_found, bool load_if_not_found)
octave_value to_ov(const cdef_object &obj)
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.
Array< T, Alloc > nth_element(const octave::idx_vector &n, int dim=0) const
Returns the n-th element in increasing order, using the same ordering as used for sort.
int ndims() const
Size of the specified dimension.
octave_idx_type rows() const
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type columns() const
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
const T * data() const
Size of the specified dimension.
T * rwdata()
Size of the specified dimension.
T & checkelem(octave_idx_type n)
Size of the specified dimension.
static Array< T, Alloc > cat(int dim, octave_idx_type n, const Array< T, Alloc > *array_list)
Concatenation along a specified (0-based) dimension, equivalent to cat().
octave_idx_type numel() const
Number of elements in the array.
ComplexNDArray xsum(int dim=-1, bool nanflag=false) const
ComplexNDArray cumprod(int dim=-1, bool nanflag=false) const
ComplexNDArray sumsq(int dim=-1, bool nanflag=false) const
ComplexNDArray diff(octave_idx_type order=1, int dim=-1) const
ComplexNDArray flip(int dim=-1) const
ComplexNDArray cumsum(int dim=-1, bool nanflag=false) const
ComplexNDArray sum(int dim=-1, bool nanflag=false) const
ComplexNDArray prod(int dim=-1, bool nanflag=false) const
ComplexNDArray dsumsq(int dim=-1, bool nanflag=false) const
ComplexNDArray dsum(int dim=-1, bool nanflag=false) const
ComplexNDArray dprod(int dim=-1, bool nanflag=false) const
FloatComplexNDArray cumsum(int dim=-1, bool nanflag=false) const
FloatComplexNDArray diff(octave_idx_type order=1, int dim=-1) const
FloatComplexNDArray cumprod(int dim=-1, bool nanflag=false) const
FloatComplexNDArray sumsq(int dim=-1, bool nanflag=false) const
FloatComplexNDArray prod(int dim=-1, bool nanflag=false) const
FloatComplexNDArray sum(int dim=-1, bool nanflag=false) const
FloatComplexNDArray flip(int dim=-1) const
NDArray dsum(int dim=-1, bool nanflag=false) const
NDArray dprod(int dim=-1, bool nanflag=false) const
FloatNDArray cumsum(int dim=-1, bool nanflag=false) const
NDArray dsumsq(int dim=-1, bool nanflag=false) const
FloatNDArray sumsq(int dim=-1, bool nanflag=false) const
FloatNDArray diff(octave_idx_type order=1, int dim=-1) const
FloatNDArray sum(int dim=-1, bool nanflag=false) const
FloatNDArray flip(int dim=-1) const
FloatNDArray cumprod(int dim=-1, bool nanflag=false) const
FloatNDArray prod(int dim=-1, bool nanflag=false) const
Template for N-dimensional array classes with like-type math operators.
MArray< T > reshape(const dim_vector &new_dims) const
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
NDArray xsum(int dim=-1, bool nanflag=false) const
NDArray flip(int dim=-1) const
NDArray diff(octave_idx_type order=1, int dim=-1) const
NDArray cumsum(int dim=-1, bool nanflag=false) const
NDArray cumprod(int dim=-1, bool nanflag=false) const
NDArray sum(int dim=-1, bool nanflag=false) const
NDArray sumsq(int dim=-1, bool nanflag=false) const
NDArray prod(int dim=-1, bool nanflag=false) const
SparseBoolMatrix all(int dim=-1) const
SparseMatrix sum(int dim=-1) const
SparseBoolMatrix any(int dim=-1) const
SparseComplexMatrix cumprod(int dim=-1, bool nanflag=false) const
SparseComplexMatrix cumsum(int dim=-1, bool nanflag=false) const
SparseComplexMatrix xsum(int dim=-1, bool nanflag=false) const
SparseComplexMatrix prod(int dim=-1, bool nanflag=false) const
SparseComplexMatrix sumsq(int dim=-1, bool nanflag=false) const
SparseComplexMatrix sum(int dim=-1, bool nanflag=false) const
SparseMatrix cumprod(int dim=-1, bool nanflag=false) const
SparseMatrix prod(int dim=-1, bool nanflag=false) const
SparseMatrix sum(int dim=-1, bool nanflag=false) const
SparseMatrix xsum(int dim=-1, bool nanflag=false) const
SparseMatrix sumsq(int dim=-1, bool nanflag=false) const
SparseMatrix cumsum(int dim=-1, bool nanflag=false) const
octave_idx_type cols() const
static Sparse< T, Alloc > cat(int dim, octave_idx_type n, const Sparse< T, Alloc > *sparse_list)
octave_idx_type nnz() const
Actual number of nonzero terms.
octave_idx_type rows() const
boolNDArray any(int dim=-1) const
boolNDArray all(int dim=-1) const
octave_value find_method_symbol(const std::string &method_name, const std::string &class_name)
cdef_class find_class(const std::string &name, bool error_if_not_found=true, bool load_if_not_found=true)
void set_class(const cdef_class &cls)
Array< cdef_object > array_value() const
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)
octave_idx_type ndims() const
Number of dimensions.
bool hvcat(const dim_vector &dvb, int dim)
This corresponds to [,] (horzcat, dim = 0) and [;] (vertcat, dim = 1).
int first_non_singleton(int def=0) const
dim_vector redim(int n) const
Force certain dimensionality, preserving numel ().
octave_idx_type length(octave_idx_type n=0) const
octave_idx_type extent(octave_idx_type n) const
intNDArray diff(octave_idx_type order=1, int dim=-1) const
cdef_manager & get_cdef_manager()
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()
static data_type string_to_data_type(const std::string &s)
virtual octave_function * function_value(bool silent=false)
virtual bool is_legacy_constructor(const std::string &="") const
static octave_map cat(int dim, octave_idx_type n, const octave_scalar_map *map_list)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Array< octave_value > array_value() const
octave_value_list slice(octave_idx_type offset, octave_idx_type len, bool tags=false) const
octave_idx_type length() 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 bool_value(bool warn=false) const
FloatComplexNDArray xfloat_complex_array_value(const char *fmt,...) const
int32NDArray int32_array_value() const
uint16NDArray uint16_array_value() const
int16NDArray int16_array_value() const
int int_value(bool req_int=false, bool frc_str_conv=false) const
int8NDArray int8_array_value() const
octave_idx_type rows() const
bool is_scalar_type() const
bool is_sq_string() const
Array< std::string > cellstr_value() const
octave_value permute(const Array< int > &vec, bool inv=false) const
bool is_int8_type() const
Complex complex_value(bool frc_str_conv=false) const
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
octave_idx_type idx_type_value(bool req_int=false, bool frc_str_conv=false) const
Complex xcomplex_value(const char *fmt,...) const
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) 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
FloatComplex float_complex_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
float float_value(bool frc_str_conv=false) const
SparseComplexMatrix xsparse_complex_matrix_value(const char *fmt,...) const
octave_value any(int dim=0) const
bool is_int16_type() const
octave_value reshape(const dim_vector &dv) const
double scalar_value(bool frc_str_conv=false) const
octave_idx_type numel() const
Array< int > int_vector_value(bool req_int=false, bool frc_str_conv=false, bool frc_vec_conv=false) const
std::string string_value(bool force=false) const
octave_value & assign(assign_op op, const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
bool is_int64_type() const
ComplexNDArray xcomplex_array_value(const char *fmt,...) const
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
octave_value single_subsref(const std::string &type, const octave_value_list &idx)
int64NDArray int64_array_value() const
SparseBoolMatrix sparse_bool_matrix_value(bool warn=false) const
NDArray array_value(bool frc_str_conv=false) const
FloatComplex xfloat_complex_value(const char *fmt,...) const
octave_idx_type length() const
bool is_double_type() const
bool is_int32_type() const
uint8NDArray uint8_array_value() const
octave_value all(int dim=0) const
std::string xstring_value(const char *fmt,...) 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
octave_idx_type columns() const
builtin_type_t builtin_type() const
octave_value diag(octave_idx_type k=0) const
octave::range< double > range_value() const
double double_value(bool frc_str_conv=false) const
SparseComplexMatrix sparse_complex_matrix_value(bool frc_str_conv=false) const
octave_value find_method(const std::string &name, const std::string &dispatch_type)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define INSTANTIATE_EYE(T)
template octave_value identity_matrix< int8NDArray >(int, int)
octave_value do_class_concat(const octave_value_list &ovl, const std::string &cattype, int dim)
template octave_value identity_matrix< uint64NDArray >(int, int)
template octave_value identity_matrix< int16NDArray >(int, int)
#define BTYP_BRANCH(X, EX)
void map_2_xlog2(const Array< T > &x, Array< T > &f, Array< ET > &e)
template octave_value identity_matrix< int64NDArray >(int, int)
template octave_value identity_matrix< int32NDArray >(int, int)
template octave_value identity_matrix< uint8NDArray >(int, int)
octave_value identity_matrix(int nr, int nc)
#define MAKE_INT_BRANCH(X)
template octave_value identity_matrix< uint16NDArray >(int, int)
template octave_value identity_matrix< boolNDArray >(int, int)
template octave_value identity_matrix< uint32NDArray >(int, int)
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
#define DEFALIAS(alias, name)
Macro to define an alias for another existing function name.
void warning(const char *fmt,...)
void error(const char *fmt,...)
void err_wrong_type_arg(const char *name, const char *s)
void warn_implicit_conversion(const char *id, const char *from, const char *to)
std::string get_dispatch_type(const octave_value_list &args, builtin_type_t &builtin_type)
double norm(const ColumnVector &v)
intNDArray< octave_int16 > int16NDArray
intNDArray< octave_int32 > int32NDArray
intNDArray< octave_int64 > int64NDArray
intNDArray< octave_int8 > int8NDArray
cdef_manager & __get_cdef_manager__()
interpreter & __get_interpreter__()
Complex log2(const Complex &x)
double lo_ieee_na_value()
float lo_ieee_float_nan_value()
float lo_ieee_float_na_value()
double lo_ieee_nan_value()
float lo_ieee_float_inf_value()
double lo_ieee_inf_value()
Complex atan(const Complex &x)
T mx_inline_count(const bool *v, octave_idx_type n)
bool base64_encode(const char *inc, const std::size_t inlen, char **out)
intNDArray< octave_uint8 > base64_decode_bytes(const std::string &str)
Array< double > base64_decode(const std::string &str)
std::complex< double > Complex
std::complex< float > FloatComplex
octave_int< uint64_t > octave_uint64
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
double xfrobnorm(const Matrix &x)
double xnorm(const ColumnVector &x, double p)
RowVector xcolnorms(const Matrix &m, double p)
ColumnVector xrownorms(const Matrix &m, double p)
T::size_type numel(const T &str)
const octave_base_value const Array< octave_idx_type > & ra_idx
std::string btyp_class_name[btyp_num_types+1]
octave_value colon_op(const octave_value &base, const octave_value &increment, const octave_value &limit, bool is_for_cmd_expr=false)
octave_value cat_op(type_info &ti, const octave_value &a, const octave_value &b, const Array< octave_idx_type > &ra_idx)
octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)
octave_value unary_op(type_info &ti, octave_value::unary_op op, const octave_value &a)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
#define panic_unless(cond)
void maybe_warn_string_concat(bool all_dq_strings_p, bool all_sq_strings_p)
std::string get_concat_class(const std::string &c1, const std::string &c2)
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
intNDArray< octave_uint16 > uint16NDArray
intNDArray< octave_uint32 > uint32NDArray
intNDArray< octave_uint64 > uint64NDArray
intNDArray< octave_uint8 > uint8NDArray
octave_idx_type dims_to_numel(const dim_vector &dims, const octave_value_list &idx_arg)
void get_dimensions(const octave_value &a, const char *warn_for, dim_vector &dim)
void check_dimensions(dim_vector &dim, const char *warnfor)