mirror of
https://github.com/triqs/dft_tools
synced 2025-01-12 05:58:18 +01:00
Cythonize random generators
A trivial cythonized version of the c++ random_generator class. Useful to get the names of the generators from the python. new file: pytriqs/random/* new file: doc/reference/python/random/*
This commit is contained in:
parent
7607d3963d
commit
2921cbcc06
@ -9,4 +9,5 @@ Python
|
||||
operators/operators
|
||||
lattice/lattice
|
||||
data_analysis/contents
|
||||
random/random
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
.. _operators:
|
||||
.. module:: pytriqs.random
|
||||
|
||||
.. _operators:
|
||||
|
||||
The Operator class
|
||||
===================
|
||||
|
7
doc/reference/python/random/random.py
Normal file
7
doc/reference/python/random/random.py
Normal file
@ -0,0 +1,7 @@
|
||||
from pytriqs.random import *
|
||||
from pytriqs.plot.mpl_interface import *
|
||||
|
||||
r = RandomGenerator("lagged_fibonacci607", 237489)
|
||||
l = []
|
||||
for i in range(10000): l += [r.rand(),]
|
||||
plt.hist(l, 30, normed=True);
|
48
doc/reference/python/random/random.rst
Normal file
48
doc/reference/python/random/random.rst
Normal file
@ -0,0 +1,48 @@
|
||||
.. index:: Random number generator
|
||||
|
||||
.. module:: pytriqs.random
|
||||
|
||||
.. _random_generator:
|
||||
|
||||
Random generator class
|
||||
======================
|
||||
|
||||
TRIQS has a simple random number generator class called ``RandomGenerator``. It is based on boost
|
||||
just like the C++ random generator provided by TRIQS.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
The generator is constructed from a name and a seed::
|
||||
|
||||
from pytriqs.random import *
|
||||
r = RandomGenerator("mt19937", 237849)
|
||||
|
||||
A list of possible random generator names is obtained with::
|
||||
|
||||
print available_generator_names()
|
||||
|
||||
Then you can either generate float number on the interval :math:`[0,1[` using
|
||||
the ``rand()`` method or integer numbers in the inverval :math:`[0,N-1]` using
|
||||
``int_rand(N)``::
|
||||
|
||||
print r.rand()
|
||||
print r.int_rand(10)
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
Here's a simple example showing how to use the generator.
|
||||
|
||||
.. plot:: reference/python/random/random.py
|
||||
:include-source:
|
||||
:scale: 70
|
||||
|
||||
|
||||
Complete reference
|
||||
------------------
|
||||
|
||||
.. autoclass:: pytriqs.random.RandomGenerator
|
||||
:members:
|
||||
|
||||
.. autofunction:: pytriqs.random.available_generator_names
|
8
pytriqs/random/CMakeLists.txt
Normal file
8
pytriqs/random/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
SET(PYTHON_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/__init__.py
|
||||
)
|
||||
|
||||
install (FILES ${PYTHON_SOURCES} DESTINATION ${TRIQS_PYTHON_LIB_DEST}/random)
|
||||
|
||||
# the c++ code
|
||||
cython_module(random random_generator random)
|
5
pytriqs/random/__init__.py
Normal file
5
pytriqs/random/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
from random_generator import RandomGenerator, available_generator_names
|
||||
|
||||
__all__ = ['RandomGenerator','available_generator_names']
|
||||
|
49
pytriqs/random/random_generator.pyx
Normal file
49
pytriqs/random/random_generator.pyx
Normal file
@ -0,0 +1,49 @@
|
||||
from dcomplex cimport *
|
||||
from libcpp.string cimport string
|
||||
|
||||
# The c++ random generator class
|
||||
# We wrap two members only
|
||||
cdef extern from "triqs/mc_tools/random_generator.hpp":
|
||||
|
||||
cdef cppclass random_generator "triqs::mc_tools::random_generator":
|
||||
|
||||
random_generator(string, long) except +
|
||||
double operator() () except +
|
||||
int operator() (int) except +
|
||||
|
||||
# This is the wrapping of the static member random_generator_names
|
||||
# It is a bit of a hack but there is no notion of ststic members in cython
|
||||
cdef extern from "triqs/mc_tools/random_generator.hpp" namespace "triqs::mc_tools::random_generator":
|
||||
|
||||
string random_generator_names(string) except +
|
||||
|
||||
|
||||
# The python RandomGenerator class
|
||||
cdef class RandomGenerator:
|
||||
|
||||
cdef random_generator * _c
|
||||
|
||||
def __init__(self, name, seed):
|
||||
"""This is a random number generator class based on boost.
|
||||
|
||||
:param name: (string) Name of the random number generator
|
||||
:param seed: (int) Random number seed
|
||||
"""
|
||||
self._c = new random_generator(name, seed)
|
||||
|
||||
def __dealloc__(self):
|
||||
del self._c
|
||||
|
||||
def rand(self):
|
||||
"""Generate a float random number in [0,1["""
|
||||
return self._c[0]()
|
||||
|
||||
def int_rand(self, N):
|
||||
"""Generate an integer random number in [0,N-1]"""
|
||||
return self._c[0](N)
|
||||
|
||||
# The list of generator names accessed through the static member
|
||||
def available_generator_names():
|
||||
"""Get a list of available random generator names"""
|
||||
return random_generator_names(",").split(',')
|
||||
|
Loading…
Reference in New Issue
Block a user