A (one dimensional) cellular automaton is a function1 F : Σ → Σ with the property that there is a K > 0 such that F (x)i depends only on the 2K + 1 coordinates xi−K , xi−K+1, . . . , xi−1, xi, xi+1, . . . , xi+K . A periodic point of σ is any x such that σ^p (x) = x for some p ∈ N, and a periodic point of F is any x such that F^q (x) = x for some q ∈ N. Given a cellular automaton F, a point x ∈ Σ is jointly periodic if there are p, q ∈ N such that σ^p (x) = F^q (x) = x, that is, it is a periodic point under both functions.
This project aims to explore the nature of one-dimensional Cellular Automata, in the hope of finding the structure of cellular automata through its periodic points.
License: MIT
ubuntu2004
// MersenneTwister.h1// Mersenne Twister random number generator -- a C++ class MTRand2// Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus3// Richard J. Wagner v1.0 15 May 2003 [email protected]45// The Mersenne Twister is an algorithm for generating random numbers. It6// was designed with consideration of the flaws in various other generators.7// The period, 2^19937-1, and the order of equidistribution, 623 dimensions,8// are far greater. The generator is also fast; it avoids multiplication and9// division, and it benefits from caches and pipelines. For more information10// see the inventors' web page at http://www.math.keio.ac.jp/~matumoto/emt.html1112// Reference13// M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally14// Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on15// Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30.1617// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,18// Copyright (C) 2000 - 2003, Richard J. Wagner19// All rights reserved.20//21// Redistribution and use in source and binary forms, with or without22// modification, are permitted provided that the following conditions23// are met:24//25// 1. Redistributions of source code must retain the above copyright26// notice, this list of conditions and the following disclaimer.27//28// 2. Redistributions in binary form must reproduce the above copyright29// notice, this list of conditions and the following disclaimer in the30// documentation and/or other materials provided with the distribution.31//32// 3. The names of its contributors may not be used to endorse or promote33// products derived from this software without specific prior written34// permission.35//36// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS37// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT38// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR39// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR40// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,41// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,42// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR43// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF44// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING45// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS46// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.4748// The original code included the following notice:49//50// When you use this, send an email to: [email protected]51// with an appropriate reference to your work.52//53// It would be nice to CC: [email protected] and [email protected]54// when you write.5556#ifndef MERSENNETWISTER_H57#define MERSENNETWISTER_H5859// Not thread safe (unless auto-initialization is avoided and each thread has60// its own MTRand object)6162#include <iostream>63#include <limits.h>64#include <stdio.h>65#include <time.h>66#include <math.h>6768class MTRand {69// Data70public:71typedef unsigned long uint32; // unsigned integer type, at least 32 bits7273enum { N = 624 }; // length of state vector74enum { SAVE = N + 1 }; // length of array for save()7576protected:77enum { M = 397 }; // period parameter7879uint32 state[N]; // internal state80uint32 *pNext; // next value to get from state81int left; // number of values left before reload needed828384//Methods85public:86MTRand( const uint32& oneSeed ); // initialize with a simple uint3287MTRand( uint32 *const bigSeed, uint32 const seedLength = N ); // or an array88MTRand(); // auto-initialize with /dev/urandom or time() and clock()8990// Do NOT use for CRYPTOGRAPHY without securely hashing several returned91// values together, otherwise the generator state can be learned after92// reading 624 consecutive values.9394// Access to 32-bit random numbers95double rand(); // real number in [0,1]96double rand( const double& n ); // real number in [0,n]97double randExc(); // real number in [0,1)98double randExc( const double& n ); // real number in [0,n)99double randDblExc(); // real number in (0,1)100double randDblExc( const double& n ); // real number in (0,n)101uint32 randInt(); // integer in [0,2^32-1]102uint32 randInt( const uint32& n ); // integer in [0,n] for n < 2^32103double operator()() { return rand(); } // same as rand()104105// Access to 53-bit random numbers (capacity of IEEE double precision)106double rand53(); // real number in [0,1)107108// Access to nonuniform random number distributions109double randNorm( const double& mean = 0.0, const double& variance = 0.0 );110111// Re-seeding functions with same behavior as initializers112void seed( const uint32 oneSeed );113void seed( uint32 *const bigSeed, const uint32 seedLength = N );114void seed();115116// Saving and loading generator state117void save( uint32* saveArray ) const; // to array of size SAVE118void load( uint32 *const loadArray ); // from such array119friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );120friend std::istream& operator>>( std::istream& is, MTRand& mtrand );121122protected:123void initialize( const uint32 oneSeed );124void reload();125uint32 hiBit( const uint32& u ) const { return u & 0x80000000UL; }126uint32 loBit( const uint32& u ) const { return u & 0x00000001UL; }127uint32 loBits( const uint32& u ) const { return u & 0x7fffffffUL; }128uint32 mixBits( const uint32& u, const uint32& v ) const129{ return hiBit(u) | loBits(v); }130uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const131{ return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); }132static uint32 hash( time_t t, clock_t c );133};134135136inline MTRand::MTRand( const uint32& oneSeed )137{ seed(oneSeed); }138139inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength )140{ seed(bigSeed,seedLength); }141142inline MTRand::MTRand()143{ seed(); }144145inline double MTRand::rand()146{ return double(randInt()) * (1.0/4294967295.0); }147148inline double MTRand::rand( const double& n )149{ return rand() * n; }150151inline double MTRand::randExc()152{ return double(randInt()) * (1.0/4294967296.0); }153154inline double MTRand::randExc( const double& n )155{ return randExc() * n; }156157inline double MTRand::randDblExc()158{ return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }159160inline double MTRand::randDblExc( const double& n )161{ return randDblExc() * n; }162163inline double MTRand::rand53()164{165uint32 a = randInt() >> 5, b = randInt() >> 6;166return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0); // by Isaku Wada167}168169inline double MTRand::randNorm( const double& mean, const double& variance )170{171// Return a real number from a normal (Gaussian) distribution with given172// mean and variance by Box-Muller method173double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance;174double phi = 2.0 * 3.14159265358979323846264338328 * randExc();175return mean + r * cos(phi);176}177178inline MTRand::uint32 MTRand::randInt()179{180// Pull a 32-bit integer from the generator state181// Every other access function simply transforms the numbers extracted here182183if( left == 0 ) reload();184--left;185186register uint32 s1;187s1 = *pNext++;188s1 ^= (s1 >> 11);189s1 ^= (s1 << 7) & 0x9d2c5680UL;190s1 ^= (s1 << 15) & 0xefc60000UL;191return ( s1 ^ (s1 >> 18) );192}193194inline MTRand::uint32 MTRand::randInt( const uint32& n )195{196// Find which bits are used in n197// Optimized by Magnus Jonsson ([email protected])198uint32 used = n;199used |= used >> 1;200used |= used >> 2;201used |= used >> 4;202used |= used >> 8;203used |= used >> 16;204205// Draw numbers until one is found in [0,n]206uint32 i;207do208i = randInt() & used; // toss unused bits to shorten search209while( i > n );210return i;211}212213214inline void MTRand::seed( const uint32 oneSeed )215{216// Seed the generator with a simple uint32217initialize(oneSeed);218reload();219}220221222inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength )223{224// Seed the generator with an array of uint32's225// There are 2^19937-1 possible initial states. This function allows226// all of those to be accessed by providing at least 19937 bits (with a227// default seed length of N = 624 uint32's). Any bits above the lower 32228// in each element are discarded.229// Just call seed() if you want to get array from /dev/urandom230initialize(19650218UL);231register int i = 1;232register uint32 j = 0;233register int k = ( N > seedLength ? N : seedLength );234for( ; k; --k )235{236state[i] =237state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );238state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;239state[i] &= 0xffffffffUL;240++i; ++j;241if( i >= N ) { state[0] = state[N-1]; i = 1; }242if( j >= seedLength ) j = 0;243}244for( k = N - 1; k; --k )245{246state[i] =247state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );248state[i] -= i;249state[i] &= 0xffffffffUL;250++i;251if( i >= N ) { state[0] = state[N-1]; i = 1; }252}253state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array254reload();255}256257258inline void MTRand::seed()259{260// Seed the generator with an array from /dev/urandom if available261// Otherwise use a hash of time() and clock() values262263// First try getting an array from /dev/urandom264FILE* urandom = fopen( "/dev/urandom", "rb" );265if( urandom )266{267uint32 bigSeed[N];268register uint32 *s = bigSeed;269register int i = N;270register bool success = true;271while( success && i-- )272success = fread( s++, sizeof(uint32), 1, urandom );273fclose(urandom);274if( success ) { seed( bigSeed, N ); return; }275}276277// Was not successful, so use time() and clock() instead278seed( hash( time(NULL), clock() ) );279}280281282inline void MTRand::initialize( const uint32 seed )283{284// Initialize generator state with seed285// See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.286// In previous versions, most significant bits (MSBs) of the seed affect287// only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto.288register uint32 *s = state;289register uint32 *r = state;290register int i = 1;291*s++ = seed & 0xffffffffUL;292for( ; i < N; ++i )293{294*s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;295r++;296}297}298299300inline void MTRand::reload()301{302// Generate N new values in state303// Made clearer and faster by Matthew Bellew ([email protected])304register uint32 *p = state;305register int i;306for( i = N - M; i--; ++p )307*p = twist( p[M], p[0], p[1] );308for( i = M; --i; ++p )309*p = twist( p[M-N], p[0], p[1] );310*p = twist( p[M-N], p[0], state[0] );311312left = N, pNext = state;313}314315316inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )317{318// Get a uint32 from t and c319// Better than uint32(x) in case x is floating point in [0,1]320// Based on code by Lawrence Kirby ([email protected])321322static uint32 differ = 0; // guarantee time-based seeds will change323324uint32 h1 = 0;325unsigned char *p = (unsigned char *) &t;326for( size_t i = 0; i < sizeof(t); ++i )327{328h1 *= UCHAR_MAX + 2U;329h1 += p[i];330}331uint32 h2 = 0;332p = (unsigned char *) &c;333for( size_t j = 0; j < sizeof(c); ++j )334{335h2 *= UCHAR_MAX + 2U;336h2 += p[j];337}338return ( h1 + differ++ ) ^ h2;339}340341342inline void MTRand::save( uint32* saveArray ) const343{344register uint32 *sa = saveArray;345register const uint32 *s = state;346register int i = N;347for( ; i--; *sa++ = *s++ ) {}348*sa = left;349}350351352inline void MTRand::load( uint32 *const loadArray )353{354register uint32 *s = state;355register uint32 *la = loadArray;356register int i = N;357for( ; i--; *s++ = *la++ ) {}358left = *la;359pNext = &state[N-left];360}361362363inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand )364{365register const MTRand::uint32 *s = mtrand.state;366register int i = mtrand.N;367for( ; i--; os << *s++ << "\t" ) {}368return os << mtrand.left;369}370371372inline std::istream& operator>>( std::istream& is, MTRand& mtrand )373{374register MTRand::uint32 *s = mtrand.state;375register int i = mtrand.N;376for( ; i--; is >> *s++ ) {}377is >> mtrand.left;378mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];379return is;380}381382#endif // MERSENNETWISTER_H383384// Change log:385//386// v0.1 - First release on 15 May 2000387// - Based on code by Makoto Matsumoto, Takuji Nishimura, and Shawn Cokus388// - Translated from C to C++389// - Made completely ANSI compliant390// - Designed convenient interface for initialization, seeding, and391// obtaining numbers in default or user-defined ranges392// - Added automatic seeding from /dev/urandom or time() and clock()393// - Provided functions for saving and loading generator state394//395// v0.2 - Fixed bug which reloaded generator one step too late396//397// v0.3 - Switched to clearer, faster reload() code from Matthew Bellew398//399// v0.4 - Removed trailing newline in saved generator format to be consistent400// with output format of built-in types401//402// v0.5 - Improved portability by replacing static const int's with enum's and403// clarifying return values in seed(); suggested by Eric Heimburg404// - Removed MAXINT constant; use 0xffffffffUL instead405//406// v0.6 - Eliminated seed overflow when uint32 is larger than 32 bits407// - Changed integer [0,n] generator to give better uniformity408//409// v0.7 - Fixed operator precedence ambiguity in reload()410// - Added access for real numbers in (0,1) and (0,n)411//412// v0.8 - Included time.h header to properly support time_t and clock_t413//414// v1.0 - Revised seeding to match 26 Jan 2002 update of Nishimura and Matsumoto415// - Allowed for seeding with arrays of any length416// - Added access for real numbers in [0,1) with 53-bit resolution417// - Added access for real numbers from normal (Gaussian) distributions418// - Increased overall speed by optimizing twist()419// - Doubled speed of integer [0,n] generation420// - Fixed out-of-range number generation on 64-bit machines421// - Improved portability by substituting literal constants for long enum's422// - Changed license from GNU LGPL to BSD423424425426