def box_to_affine(xc, yc, angle, scalex, aspect):
m = np.zeros((2, 3), np.double )
m[0,0] = scalex * math.cos(angle)
m[1,0] = scalex * math.sin(angle)
m[0,1] = - scalex * math.sin(angle) * aspect
m[1,1] = scalex * math.cos(angle) * aspect
m[0,2] = xc
m[1,2] = yc
return m
python类double()的实例源码
def from_pb_tile(tile, no_data_value=None, data_type=None):
"""Creates a ``Tile`` from ``ProtoTile``.
Args:
tile (ProtoTile): The ``ProtoTile`` instance to be converted.
Returns:
:class:`~geopyspark.geotrellis.Tile`
"""
if not data_type:
data_type = _mapped_data_types[tile.cellType.dataType]
if data_type == 'BIT':
cells = np.int8(tile.uint32Cells[:])
elif data_type == 'BYTE':
cells = np.int8(tile.sint32Cells[:])
elif data_type == 'UBYTE':
cells = np.uint8(tile.uint32Cells[:])
elif data_type == 'SHORT':
cells = np.int16(tile.sint32Cells[:])
elif data_type == 'USHORT':
cells = np.uint16(tile.uint32Cells[:])
elif data_type == 'INT':
cells = np.int32(tile.sint32Cells[:])
elif data_type == 'FLOAT':
cells = np.float32(tile.floatCells[:])
else:
cells = np.double(tile.doubleCells[:])
return cells.reshape(tile.rows, tile.cols)
def get_coef(self, X):
qr, qraux = self.qr, self.qraux
n, p = qr.shape
# sanity check
assert isinstance(qr, np.ndarray), 'internal error: QR should be a np.ndarray but got %s' % type(qr)
assert isinstance(qraux, np.ndarray), 'internal error: qraux should be a np.ndarray but got %s' % type(qraux)
# validate input array
X = check_array(X, dtype='numeric', copy=True, order='F')
nx, ny = X.shape
if nx != n:
raise ValueError('qr and X must have same number of rows')
# check on size
_validate_matrix_size(n, p)
# get the rank of the decomposition
k = self.rank
# get ix vector
# if p > n:
# ix = np.ones(n + (p - n)) * np.nan
# ix[:n] = np.arange(n) # i.e., array([0,1,2,nan,nan,nan])
# else:
# ix = np.arange(n)
# set up the structures to alter
coef, info = (np.zeros((k, ny), dtype=np.double, order='F'),
np.zeros(1, dtype=np.int, order='F'))
# call the fortran module IN PLACE
_safecall(dqrsl.dqrcf, qr, n, k, qraux, X, ny, coef, 0)
# post-processing
# if k < p:
# cf = np.ones((p,ny)) * np.nan
# cf[self.pivot[np.arange(k)], :] = coef
return coef if not k < p else coef[self.pivot[np.arange(k)], :]
def _prep_X_Y_for_cython(X, Y):
X, Y = check_pairwise_arrays(X, Y)
X, Y = X.astype(np.double, order='C'), Y.astype(np.double, order='C').T # transposing Y here!
res = np.zeros((X.shape[0], Y.shape[1]), dtype=X.dtype)
return X, Y, res
# Cython proxies
def _hilbert_dot(x, y, scalar=1.0):
# return ``2 * safe_sparse_dot(x, y) - safe_sparse_dot(x, x.T) - safe_sparse_dot(y, y.T)``
x, y = x.astype(np.double, order='C'), y.astype(np.double, order='C')
return _hilbert_dot_fast(x, y, scalar)
def _hilbert_matrix(X, Y=None, scalar=1.0):
X, Y, res = _prep_X_Y_for_cython(X, Y)
_hilbert_matrix_fast(X, Y, res, np.double(scalar))
return res
def test_floats_from_string(self, level=rlevel):
# Ticket #640, floats from string
fsingle = np.single('1.234')
fdouble = np.double('1.234')
flongdouble = np.longdouble('1.234')
assert_almost_equal(fsingle, 1.234)
assert_almost_equal(fdouble, 1.234)
assert_almost_equal(flongdouble, 1.234)
def test_byteswap_complex_scalar(self):
# Ticket #1259 and gh-441
for dtype in [np.dtype('<'+t) for t in np.typecodes['Complex']]:
z = np.array([2.2-1.1j], dtype)
x = z[0] # always native-endian
y = x.byteswap()
if x.dtype.byteorder == z.dtype.byteorder:
# little-endian machine
assert_equal(x, np.fromstring(y.tobytes(), dtype=dtype.newbyteorder()))
else:
# big-endian machine
assert_equal(x, np.fromstring(y.tobytes(), dtype=dtype))
# double check real and imaginary parts:
assert_equal(x.real, y.real.byteswap())
assert_equal(x.imag, y.imag.byteswap())
def test_float_types():
""" Check formatting.
This is only for the str function, and only for simple types.
The precision of np.float and np.longdouble aren't the same as the
python float precision.
"""
for t in [np.float32, np.double, np.longdouble]:
yield check_float_type, t
def test_nan_inf_float():
""" Check formatting of nan & inf.
This is only for the str function, and only for simple types.
The precision of np.float and np.longdouble aren't the same as the
python float precision.
"""
for t in [np.float32, np.double, np.longdouble]:
yield check_nan_inf_float, t
def test_float_type_print():
"""Check formatting when using print """
for t in [np.float32, np.double, np.longdouble]:
yield check_float_type_print, t
def test_export_record(self):
dt = [('a', 'b'),
('b', 'h'),
('c', 'i'),
('d', 'l'),
('dx', 'q'),
('e', 'B'),
('f', 'H'),
('g', 'I'),
('h', 'L'),
('hx', 'Q'),
('i', np.single),
('j', np.double),
('k', np.longdouble),
('ix', np.csingle),
('jx', np.cdouble),
('kx', np.clongdouble),
('l', 'S4'),
('m', 'U4'),
('n', 'V3'),
('o', '?'),
('p', np.half),
]
x = np.array(
[(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
asbytes('aaaa'), 'bbbb', asbytes(' '), True, 1.0)],
dtype=dt)
y = memoryview(x)
assert_equal(y.shape, (1,))
assert_equal(y.ndim, 1)
assert_equal(y.suboffsets, EMPTY)
sz = sum([np.dtype(b).itemsize for a, b in dt])
if np.dtype('l').itemsize == 4:
assert_equal(y.format, 'T{b:a:=h:b:i:c:l:d:q:dx:B:e:@H:f:=I:g:L:h:Q:hx:f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}')
else:
assert_equal(y.format, 'T{b:a:=h:b:i:c:q:d:q:dx:B:e:@H:f:=I:g:Q:h:Q:hx:f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}')
# Cannot test if NPY_RELAXED_STRIDES_CHECKING changes the strides
if not (np.ones(1).strides[0] == np.iinfo(np.intp).max):
assert_equal(y.strides, (sz,))
assert_equal(y.itemsize, sz)
def test_1darray(self):
array = np.arange(24, dtype=np.double)
from_c = test_as_c_array(array, 3)
assert_equal(array[3], from_c)
def test_2darray(self):
array = np.arange(24, dtype=np.double).reshape(3, 8)
from_c = test_as_c_array(array, 2, 4)
assert_equal(array[2, 4], from_c)
def test_3darray(self):
array = np.arange(24, dtype=np.double).reshape(2, 3, 4)
from_c = test_as_c_array(array, 1, 2, 3)
assert_equal(array[1, 2, 3], from_c)
def test_matrix_multiply(self):
self.compare_matrix_multiply_results(np.long)
self.compare_matrix_multiply_results(np.double)
def test_float_repr(self):
# long double test cannot work, because eval goes through a python
# float
for t in [np.float32, np.float64]:
yield self._test_type_repr, t
def mirr(values, finance_rate, reinvest_rate):
"""
Modified internal rate of return.
Parameters
----------
values : array_like
Cash flows (must contain at least one positive and one negative
value) or nan is returned. The first value is considered a sunk
cost at time zero.
finance_rate : scalar
Interest rate paid on the cash flows
reinvest_rate : scalar
Interest rate received on the cash flows upon reinvestment
Returns
-------
out : float
Modified internal rate of return
"""
values = np.asarray(values, dtype=np.double)
n = values.size
pos = values > 0
neg = values < 0
if not (pos.any() and neg.any()):
return np.nan
numer = np.abs(npv(reinvest_rate, values*pos))
denom = np.abs(npv(finance_rate, values*neg))
return (numer/denom)**(1.0/(n - 1))*(1 + reinvest_rate) - 1
def test_right_left_behavior(self):
# Needs range of sizes to test different code paths.
# size ==1 is special cased, 1 < size < 5 is linear search, and
# size >= 5 goes through local search and possibly binary search.
for size in range(1, 10):
xp = np.arange(size, dtype=np.double)
yp = np.ones(size, dtype=np.double)
incpts = np.array([-1, 0, size - 1, size], dtype=np.double)
decpts = incpts[::-1]
incres = interp(incpts, xp, yp)
decres = interp(decpts, xp, yp)
inctgt = np.array([1, 1, 1, 1], dtype=np.float)
dectgt = inctgt[::-1]
assert_equal(incres, inctgt)
assert_equal(decres, dectgt)
incres = interp(incpts, xp, yp, left=0)
decres = interp(decpts, xp, yp, left=0)
inctgt = np.array([0, 1, 1, 1], dtype=np.float)
dectgt = inctgt[::-1]
assert_equal(incres, inctgt)
assert_equal(decres, dectgt)
incres = interp(incpts, xp, yp, right=2)
decres = interp(decpts, xp, yp, right=2)
inctgt = np.array([1, 1, 1, 2], dtype=np.float)
dectgt = inctgt[::-1]
assert_equal(incres, inctgt)
assert_equal(decres, dectgt)
incres = interp(incpts, xp, yp, left=0, right=2)
decres = interp(decpts, xp, yp, left=0, right=2)
inctgt = np.array([0, 1, 1, 2], dtype=np.float)
dectgt = inctgt[::-1]
assert_equal(incres, inctgt)
assert_equal(decres, dectgt)
def get_complex_dtype(dtype):
return {single: csingle, double: cdouble,
csingle: csingle, cdouble: cdouble}[dtype]