Here are the examples of the python api numpy.frexp taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
5 Examples
3
Example 1
Project: iris Source File: test_basic_maths.py
def test_apply_ufunc_fail(self):
a = self.cube
# should fail because 'blah' is a string, not a np.ufunc
self.assertRaises(TypeError, iris.analysis.maths.apply_ufunc, 'blah', a)
# should fail because math.sqrt is not a np.ufunc
self.assertRaises(TypeError, iris.analysis.maths.apply_ufunc, math.sqrt, a)
# should fail because np.frexp gives 2 arrays as output
self.assertRaises(ValueError, iris.analysis.maths.apply_ufunc, np.frexp, a)
3
Example 2
Project: dask Source File: ufunc.py
def frexp(x):
# Not actually object dtype, just need to specify something
tmp = elemwise(np.frexp, x, dtype=object)
left = 'mantissa-' + tmp.name
right = 'exponent-' + tmp.name
ldsk = dict(((left,) + key[1:], (getitem, key, 0))
for key in core.flatten(tmp._keys()))
rdsk = dict(((right,) + key[1:], (getitem, key, 1))
for key in core.flatten(tmp._keys()))
a = np.empty((1, ), dtype=x.dtype)
l, r = np.frexp(a)
ldt = l.dtype
rdt = r.dtype
L = Array(merge(tmp.dask, ldsk), left, chunks=tmp.chunks, dtype=ldt)
R = Array(merge(tmp.dask, rdsk), right, chunks=tmp.chunks, dtype=rdt)
return L, R
3
Example 3
Project: veusz Source File: utilfuncs.py
def nextfloat(fin):
"""Return (approximately) next float value (for f>0)."""
d = 2**-52
split = N.frexp(fin)
while True:
fout = N.ldexp(split[0] + d, split[1])
if fin != fout:
return fout
d *= 2
0
Example 4
Project: iris Source File: test_basic_maths.py
def test_ifunc_init_fail(self):
# should fail because 'blah' is a string not a python function
self.assertRaises(TypeError, iris.analysis.maths.IFunc, 'blah',
lambda cube: cf_units.Unit('1'))
# should fail because math.sqrt is built-in function, which can not be
# used in inspect.getargspec
self.assertRaises(TypeError, iris.analysis.maths.IFunc, math.sqrt,
lambda cube: cf_units.Unit('1'))
# should fail because np.frexp gives 2 arrays as output
self.assertRaises(ValueError, iris.analysis.maths.IFunc, np.frexp,
lambda cube: cf_units.Unit('1'))
# should fail because data function has 3 arguments
self.assertRaises(ValueError, iris.analysis.maths.IFunc,
lambda a, b, c: a + b + c,
lambda cube: cf_units.Unit('1')
)
0
Example 5
Project: pyDOE Source File: doe_plackett_burman.py
def pbdesign(n):
"""
Generate a Plackett-Burman design
Parameter
---------
n : int
The number of factors to create a matrix for.
Returns
-------
H : 2d-array
An orthogonal design matrix with n columns, one for each factor, and
the number of rows being the next multiple of 4 higher than n (e.g.,
for 1-3 factors there are 4 rows, for 4-7 factors there are 8 rows,
etc.)
Example
-------
A 3-factor design::
>>> pbdesign(3)
array([[-1., -1., 1.],
[ 1., -1., -1.],
[-1., 1., -1.],
[ 1., 1., 1.]])
A 5-factor design::
>>> pbdesign(5)
array([[-1., -1., 1., -1., 1.],
[ 1., -1., -1., -1., -1.],
[-1., 1., -1., -1., 1.],
[ 1., 1., 1., -1., -1.],
[-1., -1., 1., 1., -1.],
[ 1., -1., -1., 1., 1.],
[-1., 1., -1., 1., -1.],
[ 1., 1., 1., 1., 1.]])
"""
assert n>0, 'Number of factors must be a positive integer'
keep = int(n)
n = 4*(int(n/4) + 1) # calculate the correct number of rows (multiple of 4)
f, e = np.frexp([n, n/12., n/20.])
k = [idx for idx, val in enumerate(np.logical_and(f==0.5, e>0)) if val]
assert isinstance(n, int) and k!=[], 'Invalid inputs. n must be a multiple of 4.'
k = k[0]
e = e[k] - 1
if k==0: # N = 1*2**e
H = np.ones((1, 1))
elif k==1: # N = 12*2**e
H = np.vstack((np.ones((1, 12)), np.hstack((np.ones((11, 1)),
toeplitz([-1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1],
[-1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1])))))
elif k==2: # N = 20*2**e
H = np.vstack((np.ones((1, 20)), np.hstack((np.ones((19, 1)),
hankel(
[-1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1],
[1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1])
))))
# Kronecker product construction
for i in range(e):
H = np.vstack((np.hstack((H, H)), np.hstack((H, -H))))
# Reduce the size of the matrix as needed
H = H[:, 1:(keep + 1)]
return np.flipud(H)