home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2005 November / WNnov2005.iso / Windows / Equipement / Blender / blender-2.37a-windows.exe / $_5_ / .blender / scripts / bpymodules / BPyMathutils.py < prev    next >
Text File  |  2005-05-17  |  4KB  |  124 lines

  1. # $Id: BPyMathutils.py,v 1.1 2005/05/17 19:56:29 ianwill Exp $
  2. #
  3. # --------------------------------------------------------------------------
  4. # helper functions to be used by other scripts
  5. # --------------------------------------------------------------------------
  6. # ***** BEGIN GPL LICENSE BLOCK *****
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software Foundation,
  20. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21. #
  22. # ***** END GPL LICENCE BLOCK *****
  23. # --------------------------------------------------------------------------
  24.  
  25. import Blender
  26. from Blender.Mathutils import *
  27.  
  28. # ------ Mersenne Twister - start
  29.  
  30. # Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.
  31. # Any feedback is very welcome. For any question, comments,
  32. # see http://www.math.keio.ac.jp/matumoto/emt.html or email
  33. # matumoto@math.keio.ac.jp
  34.  
  35. # The link above is dead, this is the new one:
  36. # http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/emt.html
  37. # And here the license info, from Mr. Matsumoto's site:
  38. # Until 2001/4/6, MT had been distributed under GNU Public License,
  39. # but after 2001/4/6, we decided to let MT be used for any purpose, including
  40. # commercial use. 2002-versions mt19937ar.c, mt19937ar-cok.c are considered
  41. # to be usable freely.
  42. #
  43. # So from the year above (1997), this code is under GPL.
  44.  
  45. # Period parameters
  46. N = 624
  47. M = 397
  48. MATRIX_A = 0x9908b0dfL   # constant vector a
  49. UPPER_MASK = 0x80000000L # most significant w-r bits
  50. LOWER_MASK = 0x7fffffffL # least significant r bits
  51.  
  52. # Tempering parameters
  53. TEMPERING_MASK_B = 0x9d2c5680L
  54. TEMPERING_MASK_C = 0xefc60000L
  55.  
  56. def TEMPERING_SHIFT_U(y):
  57.     return (y >> 11)
  58.  
  59. def TEMPERING_SHIFT_S(y):
  60.     return (y << 7)
  61.  
  62. def TEMPERING_SHIFT_T(y):
  63.     return (y << 15)
  64.  
  65. def TEMPERING_SHIFT_L(y):
  66.     return (y >> 18)
  67.  
  68. mt = []   # the array for the state vector
  69. mti = N+1 # mti==N+1 means mt[N] is not initialized
  70.  
  71. # initializing the array with a NONZERO seed
  72. def sgenrand(seed):
  73.   # setting initial seeds to mt[N] using
  74.   # the generator Line 25 of Table 1 in
  75.   # [KNUTH 1981, The Art of Computer Programming
  76.   #    Vol. 2 (2nd Ed.), pp102]
  77.  
  78.   global mt, mti
  79.  
  80.   mt = []
  81.   
  82.   mt.append(seed & 0xffffffffL)
  83.   for i in xrange(1, N + 1):
  84.     mt.append((69069 * mt[i-1]) & 0xffffffffL)
  85.  
  86.   mti = i
  87. # end sgenrand
  88.  
  89.  
  90. def genrand():
  91.   global mt, mti
  92.   
  93.   mag01 = [0x0L, MATRIX_A]
  94.   # mag01[x] = x * MATRIX_A  for x=0,1
  95.   y = 0
  96.   
  97.   if mti >= N: # generate N words at one time
  98.     if mti == N+1:   # if sgenrand() has not been called,
  99.       sgenrand(4357) # a default initial seed is used
  100.  
  101.     for kk in xrange((N-M) + 1):
  102.       y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)
  103.       mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1]
  104.  
  105.     for kk in xrange(kk, N):
  106.       y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)
  107.       mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1]
  108.  
  109.     y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK)
  110.     mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]
  111.  
  112.     mti = 0
  113.  
  114.   y = mt[mti]
  115.   mti += 1
  116.   y ^= TEMPERING_SHIFT_U(y)
  117.   y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B
  118.   y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C
  119.   y ^= TEMPERING_SHIFT_L(y)
  120.  
  121.   return ( float(y) / 0xffffffffL ) # reals
  122.  
  123. #------ Mersenne Twister -- end 
  124.