/usr/share/slsh/rand.sl is in slsh 2.3.0-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | % Copyright (C) 2012-2014 John E. Davis
%
% This file is part of the S-Lang Library and may be distributed under the
% terms of the GNU General Public License. See the file COPYING for
% more information.
%---------------------------------------------------------------------------
import ("rand");
private define get_generator_args (nargs, num_parms, parmsp, rtp, nump,
usage_str)
{
@rtp = NULL;
@nump = NULL;
if (nargs == num_parms)
{
@parmsp = __pop_list (num_parms);
return;
}
if (nargs == num_parms + 1)
{
@nump = ();
variable parms = __pop_list (num_parms);
if (typeof (parms[0]) == Rand_Type)
{
@rtp = list_pop (parms);
list_append (parms, @nump);
@nump = NULL;
}
@parmsp = parms;
return;
}
if (nargs == num_parms + 2)
{
@nump = ();
@parmsp = __pop_list (num_parms);
variable rt = ();
if (typeof (rt) == Rand_Type)
{
@rtp = rt;
return;
}
}
else _pop_n (nargs);
usage (usage_str);
}
private define call_rand_func ()
{
variable num = ();
variable args = __pop_list (_NARGS-3);
variable rt, func;
(func, rt) = ();
if (rt == NULL)
{
if (num == NULL)
return (@func) (__push_list(args));
return (@func) (__push_list(args), num);
}
if (num == NULL)
return (@func)(rt, __push_list(args));
return (@func)(rt, __push_list(args), num);
}
define rand_flat ()
{
variable parms, rt, num;
get_generator_args (_NARGS, 2, &parms, &rt, &num,
"r = rand_flat ([Rand_Type,] xmin, xmax [,num])");
variable r = call_rand_func (&rand_uniform, rt, num);
return parms[0] + (parms[1] - parms[0])*__tmp(r);
}
define rand_chisq ()
{
variable parms, rt, num;
get_generator_args (_NARGS, 1, &parms, &rt, &num,
"r = rand_chisq ([Rand_Type,] nu [,num])");
return 2.0 * call_rand_func (&rand_gamma, rt, 0.5*parms[0], 1.0, num);
}
define rand_fdist ()
{
variable parms, rt, num;
get_generator_args (_NARGS, 2, &parms, &rt, &num,
"r = rand_fdist ([Rand_Type,] nu1, nu2 [,num])");
variable nu1 = parms[0], nu2 = parms[1];
return (call_rand_func (&rand_gamma, rt, 0.5*nu1, 1.0, num)/nu1)
/ (call_rand_func(&rand_gamma, rt, 0.5*nu2, 1.0, num)/nu2);
}
define rand_tdist ()
{
variable parms, rt, num;
get_generator_args (_NARGS, 1, &parms, &rt, &num,
"r = rand_tdist ([Rand_Type,] nu, [,num])");
variable nu = parms[0];
return call_rand_func (&rand_gauss, rt, 1.0, num)
/ sqrt(call_rand_func(&rand_chisq, rt, nu, num)/nu);
}
define rand_int ()
{
variable parms, rt, num;
get_generator_args (_NARGS, 2, &parms, &rt, &num,
"r = rand_int ([Rand_Type,] imin, imax [,num])");
variable
imin = typecast (parms[0], Int32_Type),
imax = typecast (parms[1], Int32_Type), di;
if (imin > imax)
throw InvalidParmError, "rand_int: imax < imin";
di = typecast (imax - imin, UInt32_Type);
variable r = call_rand_func (&rand, rt, num);
if (di + 1 != 0) % UINT32_MAX does not exist
r = __tmp(r) mod (di + 1);
return typecast (imin + r, Int32_Type);
}
define rand_exp ()
{
variable parms, rt, num;
get_generator_args (_NARGS, 1, &parms, &rt, &num,
"r = rand_exp ([Rand_Type,] beta [,num])");
return (-parms[0]) * log (call_rand_func (&rand_uniform_pos, rt, num));
}
private define make_indices (a, d, i)
{
_for (0, length(array_shape(a))-1, 1)
{
variable j = ();
if (j == d)
i;
else
[:];
}
}
define rand_sample ()
{
if (_NARGS < 2)
{
_pop_n (_NARGS);
usage ("(B1 [,B2,...]) = rand_sample ([Rand_Type,] A1 [A2,...], num)");
}
variable num = ();
variable arrays = __pop_list (_NARGS-1);
variable rt = NULL;
if (typeof (arrays[0]) == Rand_Type)
rt = list_pop (arrays);
variable n0 = NULL, dim0;
variable a, indices;
foreach a (arrays)
{
dim0 = array_shape (a)[0];
if (n0 == NULL)
{
n0 = dim0;
continue;
}
if (n0 != dim0)
throw TypeMismatchError, "The arrays passed to rand_sample must have the same leading dimension";
}
if (num > n0)
num = n0;
if (rt == NULL)
indices = rand_permutation (n0);
else
indices = rand_permutation (rt, n0);
if (num < n0)
indices = indices[[0:num-1]];
foreach a (arrays)
a[make_indices(a, 0, indices)];
}
|