Commit 850b5070 authored by Martin Reinecke's avatar Martin Reinecke

tweaks to random

parent 7834aff5
Pipeline #18149 passed with stage
in 5 minutes and 34 seconds
......@@ -58,7 +58,7 @@ class Energy(with_metaclass(NiftyMeta, type('NewBase', (object,), {}))):
Memorizing the evaluations of some quantities (using the memo decorator)
minimizes the computational effort for multiple calls.
See also
See Also
--------
memo
......
......@@ -141,7 +141,7 @@ class Field(object):
# ---Factory methods---
@classmethod
def from_random(cls, random_type, domain, dtype=None, **kwargs):
def from_random(cls, random_type, domain, dtype=np.float64, **kwargs):
""" Draws a random field with the given parameters.
Parameters
......@@ -170,7 +170,7 @@ class Field(object):
generator_function = getattr(Random, random_type)
return Field(domain=domain,
val=generator_function(dtype=np.dtype(dtype),
val=generator_function(dtype=dtype,
shape=utilities.domains2shape(domain), **kwargs))
# ---Powerspectral methods---
......@@ -319,7 +319,7 @@ class Field(object):
return result_obj
def power_synthesize(self, spaces=None, real_power=True, real_signal=True,
mean=None, std=None):
mean=0., std=1.):
""" Yields a sampled field with `self`**2 as its power spectrum.
This method draws a Gaussian random field in the harmonic partner
......
......@@ -157,7 +157,6 @@ class HPLMTransformation(SlicingTransformation):
ri = map2alm(inp.imag, lmax, mmax)
ri = buildIdx(ri, lmax=lmax)
return rr + 1j*ri
else:
rr = map2alm(inp, lmax, mmax)
return buildIdx(rr, lmax=lmax)
......@@ -181,7 +180,6 @@ class LMHPTransformation(SlicingTransformation):
rr = alm2map(rr, lmax, mmax, nside)
ri = alm2map(ri, lmax, mmax, nside)
return rr + 1j*ri
else:
rr = buildLm(inp, lmax=lmax)
return alm2map(rr, lmax, mmax, nside)
......
......@@ -18,58 +18,42 @@
from builtins import object
import numpy as np
from functools import reduce
class Random(object):
@staticmethod
def pm1(dtype=np.dtype('int'), shape=1):
size = int(reduce(lambda x, y: x*y, shape))
if issubclass(dtype.type, np.complexfloating):
def pm1(dtype, shape):
if issubclass(dtype, (complex, np.complexfloating)):
x = np.array([1 + 0j, 0 + 1j, -1 + 0j, 0 - 1j], dtype=dtype)
x = x[np.random.randint(4, high=None, size=size)]
x = x[np.random.randint(4, size=shape)]
else:
x = 2 * np.random.randint(2, high=None, size=size) - 1
x = 2 * np.random.randint(2, size=shape) - 1
return x.astype(dtype).reshape(shape)
return x.astype(dtype)
@staticmethod
def normal(dtype=np.dtype('float64'), shape=(1,), mean=None, std=None):
size = int(reduce(lambda x, y: x*y, shape))
if issubclass(dtype.type, np.complexfloating):
x = np.empty(size, dtype=dtype)
x.real = np.random.normal(loc=0, scale=np.sqrt(0.5), size=size)
x.imag = np.random.normal(loc=0, scale=np.sqrt(0.5), size=size)
def normal(dtype, shape, mean=0., std=1.):
if issubclass(dtype, (complex, np.complexfloating)):
x = np.empty(shape, dtype=dtype)
x.real = np.random.normal(mean, std*np.sqrt(0.5), shape)
x.imag = np.random.normal(mean, std*np.sqrt(0.5), shape)
else:
x = np.random.normal(loc=0, scale=1, size=size)
x = np.random.normal(mean, std, shape)
x = x.astype(dtype, copy=False)
x = x.reshape(shape)
if std is not None:
x *= dtype.type(std)
if mean is not None:
x += dtype.type(mean)
return x
@staticmethod
def uniform(dtype=np.dtype('float64'), shape=1, low=0, high=1):
size = int(reduce(lambda x, y: x*y, shape))
if issubclass(dtype.type, np.complexfloating):
def uniform(dtype, shape, low=0., high=1.):
if issubclass(dtype, (complex, np.complexfloating)):
x = np.empty(size, dtype=dtype)
x.real = (high - low) * np.random.random(size=size) + low
x.imag = (high - low) * np.random.random(size=size) + low
x.real = (high - low) * np.random.random(shape) + low
x.imag = (high - low) * np.random.random(shape) + low
elif dtype in [np.dtype('int8'), np.dtype('int16'), np.dtype('int32'),
np.dtype('int64')]:
x = np.random.random_integers(min(low, high),
high=max(low, high),
size=size)
size=shape)
else:
x = (high - low) * np.random.random(size=size) + low
x = (high - low) * np.random.random(shape) + low
return x.astype(dtype, copy=False).reshape(shape)
return x.astype(dtype, copy=False)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment