1
# $Id: BPyMathutils.py,v 1.1 2005/05/17 19:56:29 ianwill Exp $
3
# --------------------------------------------------------------------------
4
# helper functions to be used by other scripts
5
# --------------------------------------------------------------------------
6
# ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
22
# ***** END GPL LICENCE BLOCK *****
23
# --------------------------------------------------------------------------
26
from Blender.Mathutils import *
28
# ------ Mersenne Twister - start
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
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.
43
# So from the year above (1997), this code is under GPL.
48
MATRIX_A = 0x9908b0dfL # constant vector a
49
UPPER_MASK = 0x80000000L # most significant w-r bits
50
LOWER_MASK = 0x7fffffffL # least significant r bits
52
# Tempering parameters
53
TEMPERING_MASK_B = 0x9d2c5680L
54
TEMPERING_MASK_C = 0xefc60000L
56
def TEMPERING_SHIFT_U(y):
59
def TEMPERING_SHIFT_S(y):
62
def TEMPERING_SHIFT_T(y):
65
def TEMPERING_SHIFT_L(y):
68
mt = [] # the array for the state vector
69
mti = N+1 # mti==N+1 means mt[N] is not initialized
71
# initializing the array with a NONZERO 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]
82
mt.append(seed & 0xffffffffL)
83
for i in xrange(1, N + 1):
84
mt.append((69069 * mt[i-1]) & 0xffffffffL)
93
mag01 = [0x0L, MATRIX_A]
94
# mag01[x] = x * MATRIX_A for x=0,1
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
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]
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]
109
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK)
110
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]
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)
121
return ( float(y) / 0xffffffffL ) # reals
123
#------ Mersenne Twister -- end