summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2009-10-20 21:35:53 +0000
committerCharles Harris <charlesr.harris@gmail.com>2009-10-20 21:35:53 +0000
commit549d7bda0ba075538e803e836f66e9cfd6d85a53 (patch)
tree9a20b5872a2592cb00f35a2ce043ff058d34a596
parent4c297e11801dd9a27a35179b8ae2058a6d1c948e (diff)
downloadnumpy-549d7bda0ba075538e803e836f66e9cfd6d85a53.tar.gz
Hard tab removal.
Trailing whitespace removal. Some coding style cleanups.
-rw-r--r--numpy/fft/SConscript4
-rw-r--r--numpy/fft/fftpack_litemodule.c18
-rw-r--r--numpy/lib/tests/test__iotools.py10
-rw-r--r--numpy/random/mtrand/distributions.c4
-rw-r--r--numpy/random/mtrand/initarray.c110
-rw-r--r--numpy/random/mtrand/numpy.pxi20
-rw-r--r--numpy/random/mtrand/randomkit.c329
-rw-r--r--numpy/random/mtrand/randomkit.h56
8 files changed, 273 insertions, 278 deletions
diff --git a/numpy/fft/SConscript b/numpy/fft/SConscript
index 42764229a..adceea011 100644
--- a/numpy/fft/SConscript
+++ b/numpy/fft/SConscript
@@ -4,5 +4,5 @@ from numscons import GetNumpyEnvironment
env = GetNumpyEnvironment(ARGUMENTS)
-env.NumpyPythonExtension('fftpack_lite',
- source = ['fftpack_litemodule.c', 'fftpack.c'])
+env.NumpyPythonExtension('fftpack_lite',
+ source = ['fftpack_litemodule.c', 'fftpack.c'])
diff --git a/numpy/fft/fftpack_litemodule.c b/numpy/fft/fftpack_litemodule.c
index 123a44416..e6d0d2925 100644
--- a/numpy/fft/fftpack_litemodule.c
+++ b/numpy/fft/fftpack_litemodule.c
@@ -292,21 +292,19 @@ fftpack_rffti(PyObject *NPY_UNUSED(self), PyObject *args)
/* List of methods defined in the module */
static struct PyMethodDef fftpack_methods[] = {
- {"cfftf", fftpack_cfftf, 1, fftpack_cfftf__doc__},
- {"cfftb", fftpack_cfftb, 1, fftpack_cfftb__doc__},
- {"cffti", fftpack_cffti, 1, fftpack_cffti__doc__},
- {"rfftf", fftpack_rfftf, 1, fftpack_rfftf__doc__},
- {"rfftb", fftpack_rfftb, 1, fftpack_rfftb__doc__},
- {"rffti", fftpack_rffti, 1, fftpack_rffti__doc__},
- {NULL, NULL, 0, NULL} /* sentinel */
+ {"cfftf", fftpack_cfftf, 1, fftpack_cfftf__doc__},
+ {"cfftb", fftpack_cfftb, 1, fftpack_cfftb__doc__},
+ {"cffti", fftpack_cffti, 1, fftpack_cffti__doc__},
+ {"rfftf", fftpack_rfftf, 1, fftpack_rfftf__doc__},
+ {"rfftb", fftpack_rfftb, 1, fftpack_rfftb__doc__},
+ {"rffti", fftpack_rffti, 1, fftpack_rffti__doc__},
+ {NULL, NULL, 0, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initfftpack) */
-static char fftpack_module_documentation[] =
-""
-;
+static char fftpack_module_documentation[] = "" ;
PyMODINIT_FUNC initfftpack_lite(void)
{
diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py
index 11ce4047b..aa067115e 100644
--- a/numpy/lib/tests/test__iotools.py
+++ b/numpy/lib/tests/test__iotools.py
@@ -245,20 +245,20 @@ class TestMiscFunctions(TestCase):
# As list of tuples w/o names
ndtype = [('A', int), ('B', float)]
assert_equal(easy_dtype(ndtype), np.dtype([('A', int), ('B', float)]))
- # As list of tuples w/ names
+ # As list of tuples w/ names
assert_equal(easy_dtype(ndtype, names="a,b"),
np.dtype([('a', int), ('b', float)]))
- # As list of tuples w/ not enough names
+ # As list of tuples w/ not enough names
assert_equal(easy_dtype(ndtype, names="a"),
np.dtype([('a', int), ('f0', float)]))
- # As list of tuples w/ too many names
+ # As list of tuples w/ too many names
assert_equal(easy_dtype(ndtype, names="a,b,c"),
np.dtype([('a', int), ('b', float)]))
- # As list of types w/o names
+ # As list of types w/o names
ndtype = (int, float, float)
assert_equal(easy_dtype(ndtype),
np.dtype([('f0', int), ('f1', float), ('f2', float)]))
- # As list of types w names
+ # As list of types w names
ndtype = (int, float, float)
assert_equal(easy_dtype(ndtype, names="a, b, c"),
np.dtype([('a', int), ('b', float), ('c', float)]))
diff --git a/numpy/random/mtrand/distributions.c b/numpy/random/mtrand/distributions.c
index d138be948..39bd82f4a 100644
--- a/numpy/random/mtrand/distributions.c
+++ b/numpy/random/mtrand/distributions.c
@@ -575,7 +575,7 @@ double rk_vonmises(rk_state *state, double mu, double kappa)
while (1)
{
- U = rk_double(state);
+ U = rk_double(state);
Z = cos(M_PI*U);
W = (1 + s*Z)/(s + Z);
Y = kappa * (s - W);
@@ -591,7 +591,7 @@ double rk_vonmises(rk_state *state, double mu, double kappa)
result = acos(W);
if (U < 0.5)
{
- result = -result;
+ result = -result;
}
result += mu;
neg = (result < 0);
diff --git a/numpy/random/mtrand/initarray.c b/numpy/random/mtrand/initarray.c
index 07ad2cc4f..8e54a2c3f 100644
--- a/numpy/random/mtrand/initarray.c
+++ b/numpy/random/mtrand/initarray.c
@@ -9,23 +9,23 @@
------------------------------------------------------------------
The code in this module was based on a download from:
- http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
+ http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
It was modified in 2002 by Raymond Hettinger as follows:
- * the principal computational lines untouched except for tabbing.
+ * the principal computational lines untouched except for tabbing.
- * renamed genrand_res53() to random_random() and wrapped
- in python calling/return code.
+ * renamed genrand_res53() to random_random() and wrapped
+ in python calling/return code.
- * genrand_int32() and the helper functions, init_genrand()
- and init_by_array(), were declared static, wrapped in
- Python calling/return code. also, their global data
- references were replaced with structure references.
+ * genrand_int32() and the helper functions, init_genrand()
+ and init_by_array(), were declared static, wrapped in
+ Python calling/return code. also, their global data
+ references were replaced with structure references.
- * unused functions from the original were deleted.
- new, original C python code was added to implement the
- Random() interface.
+ * unused functions from the original were deleted.
+ new, original C python code was added to implement the
+ Random() interface.
The following are the verbatim comments from the original code:
@@ -43,15 +43,15 @@
are met:
1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
+ notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
- products derived from this software without specific prior written
- permission.
+ products derived from this software without specific prior written
+ permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -80,23 +80,23 @@ init_genrand(rk_state *self, unsigned long s);
static void
init_genrand(rk_state *self, unsigned long s)
{
- int mti;
- unsigned long *mt;
-
- mt = self->key;
- mt[0]= s & 0xffffffffUL;
- for (mti=1; mti<RK_STATE_LEN; mti++) {
- mt[mti] =
- (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
- /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
- /* In the previous versions, MSBs of the seed affect */
- /* only MSBs of the array mt[]. */
- /* 2002/01/09 modified by Makoto Matsumoto */
- mt[mti] &= 0xffffffffUL;
- /* for >32 bit machines */
- }
- self->pos = mti;
- return;
+ int mti;
+ unsigned long *mt;
+
+ mt = self->key;
+ mt[0]= s & 0xffffffffUL;
+ for (mti=1; mti<RK_STATE_LEN; mti++) {
+ mt[mti] =
+ (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
+ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
+ /* In the previous versions, MSBs of the seed affect */
+ /* only MSBs of the array mt[]. */
+ /* 2002/01/09 modified by Makoto Matsumoto */
+ mt[mti] &= 0xffffffffUL;
+ /* for >32 bit machines */
+ }
+ self->pos = mti;
+ return;
}
@@ -106,28 +106,28 @@ init_genrand(rk_state *self, unsigned long s)
extern void
init_by_array(rk_state *self, unsigned long init_key[], unsigned long key_length)
{
- unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */
- unsigned long *mt;
-
- mt = self->key;
- init_genrand(self, 19650218UL);
- i=1; j=0;
- k = (RK_STATE_LEN>key_length ? RK_STATE_LEN : key_length);
- for (; k; k--) {
- mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
- + init_key[j] + j; /* non linear */
- mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
- i++; j++;
- if (i>=RK_STATE_LEN) { mt[0] = mt[RK_STATE_LEN-1]; i=1; }
- if (j>=key_length) j=0;
- }
- for (k=RK_STATE_LEN-1; k; k--) {
- mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- - i; /* non linear */
- mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
- i++;
- if (i>=RK_STATE_LEN) { mt[0] = mt[RK_STATE_LEN-1]; i=1; }
- }
+ unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */
+ unsigned long *mt;
+
+ mt = self->key;
+ init_genrand(self, 19650218UL);
+ i=1; j=0;
+ k = (RK_STATE_LEN>key_length ? RK_STATE_LEN : key_length);
+ for (; k; k--) {
+ mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ + init_key[j] + j; /* non linear */
+ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
+ i++; j++;
+ if (i>=RK_STATE_LEN) { mt[0] = mt[RK_STATE_LEN-1]; i=1; }
+ if (j>=key_length) j=0;
+ }
+ for (k=RK_STATE_LEN-1; k; k--) {
+ mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
+ - i; /* non linear */
+ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
+ i++;
+ if (i>=RK_STATE_LEN) { mt[0] = mt[RK_STATE_LEN-1]; i=1; }
+ }
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
self->has_gauss = 0;
diff --git a/numpy/random/mtrand/numpy.pxi b/numpy/random/mtrand/numpy.pxi
index 11cb8fac9..3c5f6f956 100644
--- a/numpy/random/mtrand/numpy.pxi
+++ b/numpy/random/mtrand/numpy.pxi
@@ -7,15 +7,15 @@ cdef extern from "numpy/arrayobject.h":
NPY_BYTE
NPY_UBYTE
NPY_SHORT
- NPY_USHORT
+ NPY_USHORT
NPY_INT
- NPY_UINT
+ NPY_UINT
NPY_LONG
NPY_ULONG
NPY_LONGLONG
NPY_ULONGLONG
NPY_FLOAT
- NPY_DOUBLE
+ NPY_DOUBLE
NPY_LONGDOUBLE
NPY_CFLOAT
NPY_CDOUBLE
@@ -56,7 +56,7 @@ cdef extern from "numpy/arrayobject.h":
NPY_OUT_FARRAY
NPY_INOUT_FARRAY
- NPY_UPDATE_ALL
+ NPY_UPDATE_ALL
cdef enum defines:
NPY_MAXDIMS
@@ -69,7 +69,7 @@ cdef extern from "numpy/arrayobject.h":
double real
double imag
- ctypedef int npy_intp
+ ctypedef int npy_intp
ctypedef extern class numpy.dtype [object PyArray_Descr]:
cdef int type_num, elsize, alignment
@@ -90,7 +90,7 @@ cdef extern from "numpy/arrayobject.h":
cdef npy_intp index, size
cdef ndarray ao
cdef char *dataptr
-
+
ctypedef extern class numpy.broadcast [object PyArrayMultiIterObject]:
cdef int numiter
cdef npy_intp size, index
@@ -103,15 +103,15 @@ cdef extern from "numpy/arrayobject.h":
dtype PyArray_DescrFromTypeNum(NPY_TYPES type_num)
object PyArray_SimpleNew(int ndims, npy_intp* dims, NPY_TYPES type_num)
int PyArray_Check(object obj)
- object PyArray_ContiguousFromAny(object obj, NPY_TYPES type,
+ object PyArray_ContiguousFromAny(object obj, NPY_TYPES type,
int mindim, int maxdim)
- object PyArray_ContiguousFromObject(object obj, NPY_TYPES type,
+ object PyArray_ContiguousFromObject(object obj, NPY_TYPES type,
int mindim, int maxdim)
npy_intp PyArray_SIZE(ndarray arr)
npy_intp PyArray_NBYTES(ndarray arr)
void *PyArray_DATA(ndarray arr)
object PyArray_FromAny(object obj, dtype newtype, int mindim, int maxdim,
- int requirements, object context)
+ int requirements, object context)
object PyArray_FROMANY(object obj, NPY_TYPES type_num, int min,
int max, int requirements)
object PyArray_NewFromDescr(object subtype, dtype newtype, int nd,
@@ -124,7 +124,7 @@ cdef extern from "numpy/arrayobject.h":
object PyArray_MultiIterNew(int n, ...)
char *PyArray_MultiIter_DATA(broadcast multi, int i)
- void PyArray_MultiIter_NEXTi(broadcast multi, int i)
+ void PyArray_MultiIter_NEXTi(broadcast multi, int i)
void PyArray_MultiIter_NEXT(broadcast multi)
object PyArray_IterNew(object arr)
diff --git a/numpy/random/mtrand/randomkit.c b/numpy/random/mtrand/randomkit.c
index 0fbc40dfd..1426566b2 100644
--- a/numpy/random/mtrand/randomkit.c
+++ b/numpy/random/mtrand/randomkit.c
@@ -118,8 +118,8 @@
char *rk_strerror[RK_ERR_MAX] =
{
- "no error",
- "random device unvavailable"
+ "no error",
+ "random device unvavailable"
};
/* static functions */
@@ -127,67 +127,62 @@ static unsigned long rk_hash(unsigned long key);
void rk_seed(unsigned long seed, rk_state *state)
{
- int pos;
- seed &= 0xffffffffUL;
-
- /* Knuth's PRNG as used in the Mersenne Twister reference implementation */
- for (pos=0; pos<RK_STATE_LEN; pos++)
- {
- state->key[pos] = seed;
- seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
- }
-
- state->pos = RK_STATE_LEN;
- state->has_gauss = 0;
+ int pos;
+ seed &= 0xffffffffUL;
+
+ /* Knuth's PRNG as used in the Mersenne Twister reference implementation */
+ for (pos=0; pos<RK_STATE_LEN; pos++) {
+ state->key[pos] = seed;
+ seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
+ }
+ state->pos = RK_STATE_LEN;
+ state->has_gauss = 0;
state->has_binomial = 0;
}
/* Thomas Wang 32 bits integer hash function */
unsigned long rk_hash(unsigned long key)
{
- key += ~(key << 15);
- key ^= (key >> 10);
- key += (key << 3);
- key ^= (key >> 6);
- key += ~(key << 11);
- key ^= (key >> 16);
- return key;
+ key += ~(key << 15);
+ key ^= (key >> 10);
+ key += (key << 3);
+ key ^= (key >> 6);
+ key += ~(key << 11);
+ key ^= (key >> 16);
+ return key;
}
rk_error rk_randomseed(rk_state *state)
{
#ifndef _WIN32
- struct timeval tv;
+ struct timeval tv;
#else
- struct _timeb tv;
+ struct _timeb tv;
#endif
- int i;
-
- if(rk_devfill(state->key, sizeof(state->key), 0) == RK_NOERR)
- {
- state->key[0] |= 0x80000000UL; /* ensures non-zero key */
- state->pos = RK_STATE_LEN;
- state->has_gauss = 0;
- state->has_binomial = 0;
+ int i;
- for (i=0; i<624; i++)
- {
- state->key[i] &= 0xffffffffUL;
- }
+ if (rk_devfill(state->key, sizeof(state->key), 0) == RK_NOERR) {
+ state->key[0] |= 0x80000000UL; /* ensures non-zero key */
+ state->pos = RK_STATE_LEN;
+ state->has_gauss = 0;
+ state->has_binomial = 0;
- return RK_NOERR;
- }
+ for (i = 0; i < 624; i++) {
+ state->key[i] &= 0xffffffffUL;
+ }
+ return RK_NOERR;
+ }
#ifndef _WIN32
- gettimeofday(&tv, NULL);
- rk_seed(rk_hash(getpid()) ^ rk_hash(tv.tv_sec) ^ rk_hash(tv.tv_usec)
- ^ rk_hash(clock()), state);
+ gettimeofday(&tv, NULL);
+ rk_seed(rk_hash(getpid()) ^ rk_hash(tv.tv_sec) ^ rk_hash(tv.tv_usec)
+ ^ rk_hash(clock()), state);
#else
- _FTIME(&tv);
- rk_seed(rk_hash(tv.time) ^ rk_hash(tv.millitm) ^ rk_hash(clock()), state);
+ _FTIME(&tv);
+ rk_seed(rk_hash(tv.time) ^ rk_hash(tv.millitm) ^ rk_hash(clock()), state);
#endif
- return RK_ENODEV;
+ return RK_ENODEV;
}
/* Magic Mersenne Twister constants */
@@ -200,182 +195,184 @@ rk_error rk_randomseed(rk_state *state)
/* Slightly optimised reference implementation of the Mersenne Twister */
unsigned long rk_random(rk_state *state)
{
- unsigned long y;
-
- if (state->pos == RK_STATE_LEN)
- {
- int i;
-
- for (i=0;i<N-M;i++)
- {
- y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
- state->key[i] = state->key[i+M] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
- }
- for (;i<N-1;i++)
- {
- y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
- state->key[i] = state->key[i+(M-N)] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
+ unsigned long y;
+
+ if (state->pos == RK_STATE_LEN) {
+ int i;
+
+ for (i = 0; i < N - M; i++) {
+ y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
+ state->key[i] = state->key[i+M] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
+ }
+ for (; i < N - 1; i++) {
+ y = (state->key[i] & UPPER_MASK) | (state->key[i+1] & LOWER_MASK);
+ state->key[i] = state->key[i+(M-N)] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
+ }
+ y = (state->key[N-1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
+ state->key[N-1] = state->key[M-1] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
+
+ state->pos = 0;
}
- y = (state->key[N-1] & UPPER_MASK) | (state->key[0] & LOWER_MASK);
- state->key[N-1] = state->key[M-1] ^ (y>>1) ^ (-(y & 1) & MATRIX_A);
-
- state->pos = 0;
- }
-
- y = state->key[state->pos++];
-
- /* Tempering */
- y ^= (y >> 11);
- y ^= (y << 7) & 0x9d2c5680UL;
- y ^= (y << 15) & 0xefc60000UL;
- y ^= (y >> 18);
-
- return y;
+ y = state->key[state->pos++];
+
+ /* Tempering */
+ y ^= (y >> 11);
+ y ^= (y << 7) & 0x9d2c5680UL;
+ y ^= (y << 15) & 0xefc60000UL;
+ y ^= (y >> 18);
+
+ return y;
}
long rk_long(rk_state *state)
{
- return rk_ulong(state) >> 1;
+ return rk_ulong(state) >> 1;
}
unsigned long rk_ulong(rk_state *state)
{
#if ULONG_MAX <= 0xffffffffUL
- return rk_random(state);
+ return rk_random(state);
#else
- return (rk_random(state) << 32) | (rk_random(state));
+ return (rk_random(state) << 32) | (rk_random(state));
#endif
}
unsigned long rk_interval(unsigned long max, rk_state *state)
{
- unsigned long mask = max, value;
+ unsigned long mask = max, value;
- if (max == 0) return 0;
+ if (max == 0) return 0;
- /* Smallest bit mask >= max */
- mask |= mask >> 1;
- mask |= mask >> 2;
- mask |= mask >> 4;
- mask |= mask >> 8;
- mask |= mask >> 16;
+ /* Smallest bit mask >= max */
+ mask |= mask >> 1;
+ mask |= mask >> 2;
+ mask |= mask >> 4;
+ mask |= mask >> 8;
+ mask |= mask >> 16;
#if ULONG_MAX > 0xffffffffUL
- mask |= mask >> 32;
+ mask |= mask >> 32;
#endif
- /* Search a random value in [0..mask] <= max */
+ /* Search a random value in [0..mask] <= max */
#if ULONG_MAX > 0xffffffffUL
- if (max <= 0xffffffffUL) {
- while ((value = (rk_random(state) & mask)) > max);
- } else {
- while ((value = (rk_ulong(state) & mask)) > max);
- }
+ if (max <= 0xffffffffUL) {
+ while ((value = (rk_random(state) & mask)) > max);
+ }
+ else {
+ while ((value = (rk_ulong(state) & mask)) > max);
+ }
#else
- while ((value = (rk_ulong(state) & mask)) > max);
+ while ((value = (rk_ulong(state) & mask)) > max);
#endif
- return value;
+ return value;
}
double rk_double(rk_state *state)
{
- /* shifts : 67108864 = 0x4000000, 9007199254740992 = 0x20000000000000 */
- long a = rk_random(state) >> 5, b = rk_random(state) >> 6;
- return (a * 67108864.0 + b) / 9007199254740992.0;
+ /* shifts : 67108864 = 0x4000000, 9007199254740992 = 0x20000000000000 */
+ long a = rk_random(state) >> 5, b = rk_random(state) >> 6;
+ return (a * 67108864.0 + b) / 9007199254740992.0;
}
void rk_fill(void *buffer, size_t size, rk_state *state)
{
- unsigned long r;
- unsigned char *buf = buffer;
-
- for (; size >= 4; size -= 4)
- {
- r = rk_random(state);
- *(buf++) = r & 0xFF;
- *(buf++) = (r >> 8) & 0xFF;
- *(buf++) = (r >> 16) & 0xFF;
- *(buf++) = (r >> 24) & 0xFF;
- }
-
- if (!size) return;
-
- r = rk_random(state);
-
- for (; size; r >>= 8, size --)
- *(buf++) = (unsigned char)(r & 0xFF);
+ unsigned long r;
+ unsigned char *buf = buffer;
+
+ for (; size >= 4; size -= 4) {
+ r = rk_random(state);
+ *(buf++) = r & 0xFF;
+ *(buf++) = (r >> 8) & 0xFF;
+ *(buf++) = (r >> 16) & 0xFF;
+ *(buf++) = (r >> 24) & 0xFF;
+ }
+
+ if (!size) {
+ return;
+ }
+ r = rk_random(state);
+ for (; size; r >>= 8, size --) {
+ *(buf++) = (unsigned char)(r & 0xFF);
+ }
}
rk_error rk_devfill(void *buffer, size_t size, int strong)
{
#ifndef _WIN32
- FILE *rfile;
- int done;
-
- if (strong)
- rfile = fopen(RK_DEV_RANDOM, "rb");
- else
- rfile = fopen(RK_DEV_URANDOM, "rb");
- if (rfile == NULL)
- return RK_ENODEV;
- done = fread(buffer, size, 1, rfile);
- fclose(rfile);
- if (done)
- return RK_NOERR;
+ FILE *rfile;
+ int done;
+
+ if (strong) {
+ rfile = fopen(RK_DEV_RANDOM, "rb");
+ }
+ else {
+ rfile = fopen(RK_DEV_URANDOM, "rb");
+ }
+ if (rfile == NULL) {
+ return RK_ENODEV;
+ }
+ done = fread(buffer, size, 1, rfile);
+ fclose(rfile);
+ if (done) {
+ return RK_NOERR;
+ }
#else
#ifndef RK_NO_WINCRYPT
- HCRYPTPROV hCryptProv;
- BOOL done;
-
- if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
- CRYPT_VERIFYCONTEXT) || !hCryptProv)
- return RK_ENODEV;
- done = CryptGenRandom(hCryptProv, size, (unsigned char *)buffer);
- CryptReleaseContext(hCryptProv, 0);
- if (done)
- return RK_NOERR;
-#endif
+ HCRYPTPROV hCryptProv;
+ BOOL done;
+ if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
+ CRYPT_VERIFYCONTEXT) || !hCryptProv) {
+ return RK_ENODEV;
+ }
+ done = CryptGenRandom(hCryptProv, size, (unsigned char *)buffer);
+ CryptReleaseContext(hCryptProv, 0);
+ if (done) {
+ return RK_NOERR;
+ }
#endif
- return RK_ENODEV;
+#endif
+ return RK_ENODEV;
}
rk_error rk_altfill(void *buffer, size_t size, int strong, rk_state *state)
{
- rk_error err;
-
- err = rk_devfill(buffer, size, strong);
- if (err)
- rk_fill(buffer, size, state);
+ rk_error err;
- return err;
+ err = rk_devfill(buffer, size, strong);
+ if (err) {
+ rk_fill(buffer, size, state);
+ }
+ return err;
}
double rk_gauss(rk_state *state)
{
- if (state->has_gauss)
- {
- state->has_gauss = 0;
- return state->gauss;
- }
- else
- {
- double f, x1, x2, r2;
- do
- {
- x1 = 2.0*rk_double(state) - 1.0;
- x2 = 2.0*rk_double(state) - 1.0;
- r2 = x1*x1 + x2*x2;
- }
- while (r2 >= 1.0 || r2 == 0.0);
-
- f = sqrt(-2.0*log(r2)/r2); /* Box-Muller transform */
- state->has_gauss = 1;
- state->gauss = f*x1; /* Keep for next call */
- return f*x2;
- }
+ if (state->has_gauss) {
+ state->has_gauss = 0;
+ return state->gauss;
+ }
+ else {
+ double f, x1, x2, r2;
+
+ do {
+ x1 = 2.0*rk_double(state) - 1.0;
+ x2 = 2.0*rk_double(state) - 1.0;
+ r2 = x1*x1 + x2*x2;
+ }
+ while (r2 >= 1.0 || r2 == 0.0);
+
+ /* Box-Muller transform */
+ f = sqrt(-2.0*log(r2)/r2);
+ state->has_gauss = 1;
+ /* Keep for next call */
+ state->gauss = f*x1;
+ return f*x2;
+ }
}
diff --git a/numpy/random/mtrand/randomkit.h b/numpy/random/mtrand/randomkit.h
index 389666854..e049488ee 100644
--- a/numpy/random/mtrand/randomkit.h
+++ b/numpy/random/mtrand/randomkit.h
@@ -2,7 +2,7 @@
/*
* Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org)
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
@@ -10,10 +10,10 @@
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -29,30 +29,30 @@
* Typical use:
*
* {
- * rk_state state;
- * unsigned long seed = 1, random_value;
- *
- * rk_seed(seed, &state); // Initialize the RNG
- * ...
- * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
+ * rk_state state;
+ * unsigned long seed = 1, random_value;
+ *
+ * rk_seed(seed, &state); // Initialize the RNG
+ * ...
+ * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
* }
- *
+ *
* Instead of rk_seed, you can use rk_randomseed which will get a random seed
* from /dev/urandom (or the clock, if /dev/urandom is unavailable):
*
* {
- * rk_state state;
- * unsigned long random_value;
- *
- * rk_randomseed(&state); // Initialize the RNG with a random seed
- * ...
- * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
+ * rk_state state;
+ * unsigned long random_value;
+ *
+ * rk_randomseed(&state); // Initialize the RNG with a random seed
+ * ...
+ * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
* }
*/
-
+
/*
* Useful macro:
- * RK_DEV_RANDOM: the device used for random seeding.
+ * RK_DEV_RANDOM: the device used for random seeding.
* defaults to "/dev/urandom"
*/
@@ -61,21 +61,21 @@
#ifndef _RANDOMKIT_
#define _RANDOMKIT_
-#define RK_STATE_LEN 624
+#define RK_STATE_LEN 624
typedef struct rk_state_
{
- unsigned long key[RK_STATE_LEN];
- int pos;
- int has_gauss; /* !=0: gauss contains a gaussian deviate */
- double gauss;
+ unsigned long key[RK_STATE_LEN];
+ int pos;
+ int has_gauss; /* !=0: gauss contains a gaussian deviate */
+ double gauss;
/* The rk_state structure has been extended to store the following
* information for the binomial generator. If the input values of n or p
* are different than nsave and psave, then the other parameters will be
* recomputed. RTK 2005-09-02 */
- int has_binomial; /* !=0: following parameters initialized for
+ int has_binomial; /* !=0: following parameters initialized for
binomial */
double psave;
long nsave;
@@ -98,9 +98,9 @@ typedef struct rk_state_
rk_state;
typedef enum {
- RK_NOERR = 0, /* no error */
- RK_ENODEV = 1, /* no RK_DEV_RANDOM device */
- RK_ERR_MAX = 2
+ RK_NOERR = 0, /* no error */
+ RK_ENODEV = 1, /* no RK_DEV_RANDOM device */
+ RK_ERR_MAX = 2
} rk_error;
/* error strings */
@@ -175,7 +175,7 @@ extern rk_error rk_devfill(void *buffer, size_t size, int strong);
* Returns RK_ENODEV if the device is unavailable, or RK_NOERR if it is
*/
extern rk_error rk_altfill(void *buffer, size_t size, int strong,
- rk_state *state);
+ rk_state *state);
/*
* return a random gaussian deviate with variance unity and zero mean.