26 #if defined (HAVE_CONFIG_H)
31 #include <unordered_map>
83 const std::string& distribution,
bool additional_arg =
false)
87 bool is_single =
false;
89 if (nargin > 0 && args(nargin-1).is_string ())
91 std::string s_arg = args(nargin-1).string_value ();
93 if (s_arg ==
"single")
98 else if (s_arg ==
"double")
105 error (
"%s: at least one argument is required", fcn);
106 else if (args(0).is_string ())
107 additional_arg =
false;
110 a = args(0).xarray_value (
"%s: dimension must be a scalar integer", fcn);
155 else if (s_arg ==
"seed")
157 else if (s_arg ==
"state" || s_arg ==
"twister")
159 else if (s_arg ==
"uniform")
161 else if (s_arg ==
"normal")
163 else if (s_arg ==
"exponential")
165 else if (s_arg ==
"poisson")
167 else if (s_arg ==
"gamma")
170 error (
"%s: unrecognized string argument", fcn);
178 dims(0) = dims(1) =
n;
186 if (!
r.all_elements_are_ints ())
187 error (
"%s: all elements of range must be integers", fcn);
199 dims(i) = (base >= 0 ? base : 0);
213 catch (octave::execution_exception& e)
215 error (e,
"%s: dimensions must be a scalar or array of integers", fcn);
226 dims(i) = (elt >=0 ? elt : 0);
246 if (args(idx+1).is_real_scalar ())
248 double d = args(idx+1).double_value ();
252 else if (args(idx+1).is_string ()
253 && args(idx+1).string_value () ==
"reset")
256 error (
"%s: seed must be a real scalar", fcn);
258 else if (ts ==
"state" || ts ==
"twister")
260 if (args(idx+1).is_string ()
261 && args(idx+1).string_value () ==
"reset")
266 =
ColumnVector (args(idx+1).vector_value (
false,
true));
278 error (
"%s: unrecognized string argument", fcn);
284 for (
int i = 0; i < nargin; i++)
289 dims(i) = (elt >= 0 ? elt : 0);
313 if (a.
dims () != dims)
314 error (
"%s: mismatch in argument size", fcn);
318 float *v =
m.fortran_vec ();
337 if (a.
dims () != dims)
338 error (
"%s: mismatch in argument size", fcn);
342 double *v =
m.fortran_vec ();
444 return do_rand (args, args.length (),
"rand",
"uniform");
546 DEFUN (randn, args, ,
579 return do_rand (args, args.length (),
"randn",
"normal");
613 DEFUN (rande, args, ,
646 return do_rand (args, args.length (),
"rande",
"exponential");
682 DEFUN (randg, args, ,
783 int nargin = args.length ();
786 error (
"randg: insufficient arguments");
788 return do_rand (args, nargin,
"randg",
"gamma",
true);
957 DEFUN (randp, args, ,
1008 int nargin = args.length ();
1011 error (
"randp: insufficient arguments");
1013 return do_rand (args, nargin,
"randp",
"poisson",
true);
1102 DEFUN (randperm, args, ,
1118 int nargin = args.length ();
1120 if (nargin < 1 || nargin > 2)
1127 error (
"randperm: M and N must be non-negative");
1130 error (
"randperm: M must be less than or equal to N");
1134 bool short_shuffle =
m <
n/5;
1138 double *rvec =
r.fortran_vec ();
1146 catch (
const std::bad_alloc&)
1151 short_shuffle =
true;
1162 std::unordered_map<octave_idx_type, octave_idx_type> map (
m);
1173 std::swap (ivec[i], ivec[k]);
1177 if (map.find (k) == map.end ())
1180 std::swap (ivec[i], map[k]);
1190 std::swap (ivec[i], ivec[k]);
1196 rvec[i] = ivec[i] + 1;
template class OCTAVE_API Array< octave_idx_type >
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.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
const T * fortran_vec(void) const
Size of the specified dimension.
Vector representing the dimensions (size) of an Array.
void resize(int n, int fill_value=0)
void chop_trailing_singletons(void)
void add_fcn(void(*fcn)(Params...), Args &&... args)
static NDArray nd_array(const dim_vector &dims, double a=1.0)
static uint32NDArray state(const std::string &d="")
static std::string distribution(void)
static void normal_distribution(void)
static void poisson_distribution(void)
static double scalar(double a=1.0)
static FloatNDArray float_nd_array(const dim_vector &dims, float a=1.0)
static float float_scalar(float a=1.0)
static void exponential_distribution(void)
static void gamma_distribution(void)
static void uniform_distribution(void)
octave_idx_type idx_type_value(bool req_int=false, bool frc_str_conv=false) const
bool is_scalar_type(void) const
Range range_value(void) const
bool is_string(void) 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(void) const
bool is_range(void) const
OCTINTERP_API void print_usage(void)
#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)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
octave_idx_type nint_big(double x)
std::complex< T > floor(const std::complex< T > &x)
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.
static octave_value do_rand(const octave_value_list &args, int nargin, const char *fcn, const std::string &distribution, bool additional_arg=false)
static std::string current_distribution