[Numpy-svn] r8442 - trunk/numpy/random/mtrand

numpy-svn at scipy.org numpy-svn at scipy.org
Tue May 25 22:26:12 EDT 2010


Author: charris
Date: 2010-05-25 21:26:12 -0500 (Tue, 25 May 2010)
New Revision: 8442

Modified:
   trunk/numpy/random/mtrand/initarray.c
   trunk/numpy/random/mtrand/randomkit.c
Log:
STY: Some c coding style cleanups.

Modified: trunk/numpy/random/mtrand/initarray.c
===================================================================
--- trunk/numpy/random/mtrand/initarray.c	2010-05-25 17:38:56 UTC (rev 8441)
+++ trunk/numpy/random/mtrand/initarray.c	2010-05-26 02:26:12 UTC (rev 8442)
@@ -1,76 +1,77 @@
-/* These function have been adapted from Python 2.4.1's _randommodule.c
+/*
+ * These function have been adapted from Python 2.4.1's _randommodule.c
+ *
+ * The following changes have been made to it in 2005 by Robert Kern:
+ *
+ *   * init_by_array has been declared extern, has a void return, and uses the
+ *     rk_state structure to hold its data.
+ *
+ *  The original file has the following verbatim comments:
+ *
+ *  ------------------------------------------------------------------
+ *  The code in this module was based on a download from:
+ *     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.
+ *
+ *   * 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.
+ *
+ *   * 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:
+ *
+ *  A C-program for MT19937, with initialization improved 2002/1/26.
+ *  Coded by Takuji Nishimura and Makoto Matsumoto.
+ *
+ *  Before using, initialize the state by using init_genrand(seed)
+ *  or init_by_array(init_key, key_length).
+ *
+ *  Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *
+ *    1. Redistributions of source code must retain the above copyright
+ *   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.
+ *
+ *    3. The names of its contributors may not be used to endorse or promote
+ *   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
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ *  Any feedback is very welcome.
+ *  http://www.math.keio.ac.jp/matumoto/emt.html
+ *  email: matumoto at math.keio.ac.jp
+ */
 
-  The following changes have been made to it in 2005 by Robert Kern:
-
-    * init_by_array has been declared extern, has a void return, and uses the
-      rk_state structure to hold its data.
-
-   The original file has the following verbatim comments: 
-
-   ------------------------------------------------------------------
-   The code in this module was based on a download from:
-      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.
-
-    * 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.
-
-    * 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:
-
-   A C-program for MT19937, with initialization improved 2002/1/26.
-   Coded by Takuji Nishimura and Makoto Matsumoto.
-
-   Before using, initialize the state by using init_genrand(seed)
-   or init_by_array(init_key, key_length).
-
-   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
-   All rights reserved.
-
-   Redistribution and use in source and binary forms, with or without
-   modification, are permitted provided that the following conditions
-   are met:
-
-     1. Redistributions of source code must retain the above copyright
-    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.
-
-     3. The names of its contributors may not be used to endorse or promote
-    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
-   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
-   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-
-   Any feedback is very welcome.
-   http://www.math.keio.ac.jp/matumoto/emt.html
-   email: matumoto at math.keio.ac.jp
-*/
-
 #include "initarray.h"
 
 static void
@@ -81,56 +82,69 @@
 init_genrand(rk_state *self, unsigned long s)
 {
     int mti;
-    unsigned long *mt;
+    unsigned long *mt = self->key;
 
-    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[0] = s & 0xffffffffUL;
+    for (mti = 1; mti < RK_STATE_LEN; 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] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
+        /* for > 32 bit machines */
         mt[mti] &= 0xffffffffUL;
-        /* for >32 bit machines */
     }
     self->pos = mti;
     return;
 }
 
 
-/* initialize by an array with array-length */
-/* init_key is the array for initializing keys */
-/* key_length is its length */
+/*
+ * initialize by an array with array-length
+ * init_key is the array for initializing keys
+ * key_length is its length
+ */
 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;
+    /* was signed in the original code. RDH 12/16/2002 */
+    unsigned int i = 1;
+    unsigned int j = 0;
+    unsigned long *mt = self->key;
+    unsigned int k;
 
-    mt = self->key;
     init_genrand(self, 19650218UL);
-    i=1; j=0;
-    k = (RK_STATE_LEN>key_length ? RK_STATE_LEN : key_length);
+    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;
+        /* non linear */
+        mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL))
+            + init_key[j] + j;
+        /* for > 32 bit machines */
+        mt[i] &= 0xffffffffUL;
+        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--) {
+    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; }
+        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;
     self->has_binomial = 0;
 }
-

Modified: trunk/numpy/random/mtrand/randomkit.c
===================================================================
--- trunk/numpy/random/mtrand/randomkit.c	2010-05-25 17:38:56 UTC (rev 8441)
+++ trunk/numpy/random/mtrand/randomkit.c	2010-05-26 02:26:12 UTC (rev 8442)
@@ -3,7 +3,7 @@
 /*
  * Copyright (c) 2003-2005, Jean-Sebastien Roy (js at jeannot.org)
  *
- * The rk_random and rk_seed functions algorithms and the original design of 
+ * The rk_random and rk_seed functions algorithms and the original design of
  * the Mersenne Twister RNG:
  *
  *   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
@@ -12,7 +12,7 @@
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *   1. Redistributions of source code must retain the above copyright
  *   notice, this list of conditions and the following disclaimer.
  *
@@ -35,13 +35,13 @@
  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- * 
+ *
  * Original algorithm for the implementation of rk_interval function from
  * Richard J. Wagner's implementation of the Mersenne Twister RNG, optimised by
  * Magnus Jonsson.
  *
  * Constants used in the rk_double implementation by Isaku Wada.
- * 
+ *
  * 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
@@ -49,10 +49,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.
@@ -72,16 +72,23 @@
 #include <math.h>
 
 #ifdef _WIN32
-/* Windows */
-/* XXX: we have to use this ugly defined(__GNUC__) because it is not easy to
- * detect the compiler used in distutils itself */
+/*
+ * Windows
+ * XXX: we have to use this ugly defined(__GNUC__) because it is not easy to
+ * detect the compiler used in distutils itself
+ */
 #if (defined(__GNUC__) && defined(NPY_NEEDS_MINGW_TIME_WORKAROUND))
-/* FIXME: ideally, we should set this to the real version of MSVCRT. We need
- * something higher than 0x601 to enable _ftime64 and co */
+
+/*
+ * FIXME: ideally, we should set this to the real version of MSVCRT. We need
+ * something higher than 0x601 to enable _ftime64 and co
+ */
 #define __MSVCRT_VERSION__ 0x0700
 #include <time.h>
 #include <sys/timeb.h>
-/* mingw msvcr lib import wrongly export _ftime, which does not exist in the
+
+/*
+ * mingw msvcr lib import wrongly export _ftime, which does not exist in the
  * actual msvc runtime for version >= 8; we make it an alias to _ftime64, which
  * is available in those versions of the runtime
  */
@@ -131,7 +138,7 @@
         seed &= 0xffffffffUL;
 
         /* Knuth's PRNG as used in the Mersenne Twister reference implementation */
-        for (pos=0; pos<RK_STATE_LEN; pos++) {
+        for (pos = 0; pos < RK_STATE_LEN; pos++) {
             state->key[pos] = seed;
             seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL;
         }
@@ -157,20 +164,21 @@
 #ifndef _WIN32
     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;
+        /* ensures non-zero key */
+        state->key[0] |= 0x80000000UL;
+        state->pos = RK_STATE_LEN;
+        state->has_gauss = 0;
+        state->has_binomial = 0;
 
-            for (i = 0; i < 624; i++) {
-                state->key[i] &= 0xffffffffUL;
-            }
-            return RK_NOERR;
+        for (i = 0; i < 624; i++) {
+            state->key[i] &= 0xffffffffUL;
+        }
+        return RK_NOERR;
     }
 
 #ifndef _WIN32
@@ -196,10 +204,10 @@
 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);
@@ -208,19 +216,19 @@
             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);
-       
+        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;
 }
 
@@ -242,8 +250,9 @@
 {
     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;
@@ -265,7 +274,6 @@
 #else
     while ((value = (rk_ulong(state) & mask)) > max);
 #endif
-
     return value;
 }
 
@@ -280,7 +288,7 @@
 {
     unsigned long r;
     unsigned char *buf = buffer;
-    
+
     for (; size >= 4; size -= 4) {
         r = rk_random(state);
         *(buf++) = r & 0xFF;
@@ -288,7 +296,7 @@
         *(buf++) = (r >> 16) & 0xFF;
         *(buf++) = (r >> 24) & 0xFF;
     }
-    
+
     if (!size) {
         return;
     }
@@ -298,7 +306,8 @@
     }
 }
 
-rk_error rk_devfill(void *buffer, size_t size, int strong)
+rk_error
+rk_devfill(void *buffer, size_t size, int strong)
 {
 #ifndef _WIN32
     FILE *rfile;
@@ -339,7 +348,8 @@
     return RK_ENODEV;
 }
 
-rk_error rk_altfill(void *buffer, size_t size, int strong, rk_state *state)
+rk_error
+rk_altfill(void *buffer, size_t size, int strong, rk_state *state)
 {
     rk_error err;
 
@@ -350,7 +360,8 @@
     return err;
 }
 
-double rk_gauss(rk_state *state)
+double
+rk_gauss(rk_state *state)
 {
     if (state->has_gauss) {
         state->has_gauss = 0;
@@ -365,7 +376,7 @@
             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;
@@ -374,5 +385,3 @@
         return f*x2;
     }
 }
-
-




More information about the Numpy-svn mailing list