26 #if defined (HAVE_CONFIG_H)
30 #include <unordered_map>
84 const std::string& distribution,
bool additional_arg =
false)
88 bool is_single =
false;
90 if (nargin > 0 && args(nargin-1).is_string ())
92 std::string s_arg = args(nargin-1).string_value ();
94 if (s_arg ==
"single")
99 else if (s_arg ==
"double")
106 error (
"%s: at least one argument is required", fcn);
107 else if (args(0).is_string ())
108 additional_arg =
false;
111 a = args(0).xarray_value (
"%s: dimension must be a scalar integer", fcn);
123 ([] (
const std::string& old_distribution)
158 else if (s_arg ==
"seed")
160 else if (s_arg ==
"state" || s_arg ==
"twister")
162 else if (s_arg ==
"uniform")
164 else if (s_arg ==
"normal")
166 else if (s_arg ==
"exponential")
168 else if (s_arg ==
"poisson")
170 else if (s_arg ==
"gamma")
173 error (
"%s: unrecognized string argument", fcn);
181 dims(0) = dims(1) =
n;
189 if (!
r.all_elements_are_ints ())
190 error (
"%s: all elements of range must be integers", fcn);
202 dims(i) = (base >= 0 ? base : 0);
218 error (ee,
"%s: dimensions must be a scalar or array of integers", fcn);
229 dims(i) = (elt >=0 ? elt : 0);
249 if (args(idx+1).is_real_scalar ())
251 double d = args(idx+1).double_value ();
255 else if (args(idx+1).is_string ()
256 && args(idx+1).string_value () ==
"reset")
259 error (
"%s: seed must be a real scalar", fcn);
261 else if (ts ==
"state" || ts ==
"twister")
263 if (args(idx+1).is_string ()
264 && args(idx+1).string_value () ==
"reset")
269 =
ColumnVector (args(idx+1).vector_value (
false,
true));
281 error (
"%s: unrecognized string argument", fcn);
287 for (
int i = 0; i < nargin; i++)
292 dims(i) = (elt >= 0 ? elt : 0);
316 if (a.
dims () != dims)
317 error (
"%s: mismatch in argument size", fcn);
321 float *v =
m.fortran_vec ();
340 if (a.
dims () != dims)
341 error (
"%s: mismatch in argument size", fcn);
345 double *v =
m.fortran_vec ();
449 return do_rand (args, args.
length (),
"rand",
"uniform");
551 DEFUN (randn, args, ,
584 return do_rand (args, args.
length (),
"randn",
"normal");
618 DEFUN (rande, args, ,
651 return do_rand (args, args.
length (),
"rande",
"exponential");
687 DEFUN (randg, args, ,
788 int nargin = args.
length ();
791 error (
"randg: insufficient arguments");
793 return do_rand (args, nargin,
"randg",
"gamma",
true);
962 DEFUN (randp, args, ,
1013 int nargin = args.
length ();
1016 error (
"randp: insufficient arguments");
1018 return do_rand (args, nargin,
"randp",
"poisson",
true);
1111 DEFUN (randperm, args, ,
1127 int nargin = args.
length ();
1129 if (nargin < 1 || nargin > 2)
1136 error (
"randperm: M and N must be non-negative");
1139 error (
"randperm: M must be less than or equal to N");
1143 bool short_shuffle =
m <
n/5;
1147 double *rvec =
r.fortran_vec ();
1155 catch (
const std::bad_alloc&)
1160 short_shuffle =
true;
1171 std::unordered_map<octave_idx_type, octave_idx_type> map (
m);
1182 std::swap (ivec[i], ivec[k]);
1186 if (map.find (k) == map.end ())
1189 std::swap (ivec[i], map[k]);
1199 std::swap (ivec[i], ivec[k]);
1205 rvec[i] = ivec[i] + 1;
1229 OCTAVE_END_NAMESPACE(
octave)
T * fortran_vec()
Size of the specified dimension.
void resize(const dim_vector &dv, const T &rfv)
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.
void chop_trailing_singletons()
void resize(int n, int fill_value=0)
octave_idx_type length() const
bool is_scalar_type() const
octave::range< double > range_value() const
octave_idx_type idx_type_value(bool req_int=false, bool frc_str_conv=false) const
Array< octave_idx_type > octave_idx_type_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
bool is_matrix_type() const
static FloatNDArray float_nd_array(const dim_vector &dims, float a=1.0)
static uint32NDArray state(const std::string &d="")
static NDArray nd_array(const dim_vector &dims, double a=1.0)
static void uniform_distribution()
static double scalar(double a=1.0)
static void exponential_distribution()
static float float_scalar(float a=1.0)
static void gamma_distribution()
static std::string distribution()
static void normal_distribution()
static void poisson_distribution()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void() error(const char *fmt,...)
void err_wrong_type_arg(const char *name, const char *s)
octave_idx_type nint_big(double x)
std::complex< T > floor(const std::complex< T > &x)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.