Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[math] Add vector filling overload for scalar distributions of TRandom #17077

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ericcano
Copy link

[math] Add new array filling functions for distributions delivering scalars in TRandom

Changes or fixes:

Addition of TRandom::<Distribution>N() array filler functions.

>>> vd = np.zeros(100, dtype=np.float64)
>>> r = ROOT.TRandom2(123)
>>> r.GausN(100, vd, 0, 1.0)
>>> vd
array([-0.51452618,  0.11320471, -0.2552159 , -0.9487521 ,  1.25042753,
        -0.34611829,  2.3636373 ,  1.52963734,  0.95990059,  0.21563747,
        -0.48241846,  1.51473977, -1.19058241, -1.62088594,  0.57052152,
         0.15026036,  1.58667136,  0.36062987, -1.23310079,  1.20035556,
        -0.42508447,  0.13440585,  0.63766076,  0.59627148, -0.46659047,
        -1.57966641, -0.23080729, -0.05128992,  1.27449527, -0.3358339 ,
        -2.53090467, -1.54203658, -0.84014657,  0.24656162,  0.08736865,
         0.66275005, -1.47442608, -0.48096595,  0.72262188,  1.07080308,
         0.24917973, -0.4976157 ,  0.47648086, -0.89377689,  0.82566228,
        -1.26325485, -0.17795292,  0.74986627,  1.16974563,  1.41039828,
        -0.01727918, -0.08254512,  0.39879297,  0.73254844,  0.80943978,
         1.79774612,  1.80436324, -1.13506493, -1.18507172, -0.63729355,
         1.08810798,  0.40591267,  0.03376047,  0.49296303,  2.89776279,
        -1.77849263, -0.3847957 , -0.15786366, -0.21491428, -0.9693155 ,
         1.50609877,  0.6414143 , -0.54620441,  1.363164  , -0.63699385,
         1.77694564,  0.62645766,  0.46607116,  1.12608175,  1.67122243,
        -0.40426394,  0.50792749,  0.49501287,  0.05635704,  1.66376808,
         0.06030797, -1.79686209,  1.9065689 , -0.60956203,  0.32817868,
        -1.14337713, -0.020879  ,  1.19166155, -1.55478791, -0.07289054,
         0.77477693, -0.93212149, -1.65628565,  0.29406467,  0.48126101])

This pythonisation is validated in unit tests.

We could not overload the existing functions as default values would create ambiguous cases.

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

Add an overload for RNGs that loops on on an array to fill it. This can
be used in Python:

```python
>>> vd = np.zeros(100, dtype=np.float64)
>>> r = ROOT.TRandom2(123)
>>> r.GausN(100, vd, 0, 1.0)
>>> vd
array([-0.51452618,  0.11320471, -0.2552159 , -0.9487521 ,  1.25042753,
        -0.34611829,  2.3636373 ,  1.52963734,  0.95990059,  0.21563747,
        -0.48241846,  1.51473977, -1.19058241, -1.62088594,  0.57052152,
         0.15026036,  1.58667136,  0.36062987, -1.23310079,  1.20035556,
        -0.42508447,  0.13440585,  0.63766076,  0.59627148, -0.46659047,
        -1.57966641, -0.23080729, -0.05128992,  1.27449527, -0.3358339 ,
        -2.53090467, -1.54203658, -0.84014657,  0.24656162,  0.08736865,
         0.66275005, -1.47442608, -0.48096595,  0.72262188,  1.07080308,
         0.24917973, -0.4976157 ,  0.47648086, -0.89377689,  0.82566228,
        -1.26325485, -0.17795292,  0.74986627,  1.16974563,  1.41039828,
        -0.01727918, -0.08254512,  0.39879297,  0.73254844,  0.80943978,
         1.79774612,  1.80436324, -1.13506493, -1.18507172, -0.63729355,
         1.08810798,  0.40591267,  0.03376047,  0.49296303,  2.89776279,
        -1.77849263, -0.3847957 , -0.15786366, -0.21491428, -0.9693155 ,
         1.50609877,  0.6414143 , -0.54620441,  1.363164  , -0.63699385,
         1.77694564,  0.62645766,  0.46607116,  1.12608175,  1.67122243,
        -0.40426394,  0.50792749,  0.49501287,  0.05635704,  1.66376808,
         0.06030797, -1.79686209,  1.9065689 , -0.60956203,  0.32817868,
        -1.14337713, -0.020879  ,  1.19166155, -1.55478791, -0.07289054,
         0.77477693, -0.93212149, -1.65628565,  0.29406467,  0.48126101])
```

This pythonisation is validated in unit tests
def test_uniform_2_params(self):
r = ROOT.TRandom2(123)
vd = np.zeros(100, dtype=np.float64)
r.UniformN(100, vd, 5, 1.5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! We can use something like:

 def _TRandom_Uniform(self, ...):

     import ROOT
     import numpy
 
    #  if user expects a vector:
         return self.UniformN(...)
    # if matches original Uniform implementation
        return self._Uniform(...)
    
   @pythonization('TRandom')
 def pythonize_trandom(klass):
     klass._Uniform = klass.Uniform
     klass.Uniform = _TRandom_Uniform

To automatically forward to the loop wrappers when the user expects an array of random numbers, or to the original generation function when the input signatures follow the traditional format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants