我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.longdouble()。
def test_basic(self): dts = [np.bool, np.int16, np.int32, np.int64, np.double, np.complex128, np.longdouble, np.clongdouble] for dt in dts: c = np.ones(53, dtype=np.bool) assert_equal(np.where( c, dt(0), dt(1)), dt(0)) assert_equal(np.where(~c, dt(0), dt(1)), dt(1)) assert_equal(np.where(True, dt(0), dt(1)), dt(0)) assert_equal(np.where(False, dt(0), dt(1)), dt(1)) d = np.ones_like(c).astype(dt) e = np.zeros_like(d) r = d.astype(dt) c[7] = False r[7] = e[7] assert_equal(np.where(c, e, e), e) assert_equal(np.where(c, d, e), r) assert_equal(np.where(c, d, e[0]), r) assert_equal(np.where(c, d[0], e), r) assert_equal(np.where(c[::2], d[::2], e[::2]), r[::2]) assert_equal(np.where(c[1::2], d[1::2], e[1::2]), r[1::2]) assert_equal(np.where(c[::3], d[::3], e[::3]), r[::3]) assert_equal(np.where(c[1::3], d[1::3], e[1::3]), r[1::3]) assert_equal(np.where(c[::-2], d[::-2], e[::-2]), r[::-2]) assert_equal(np.where(c[::-3], d[::-3], e[::-3]), r[::-3]) assert_equal(np.where(c[1::-3], d[1::-3], e[1::-3]), r[1::-3])
def test_sum(self): for dt in (np.int, np.float16, np.float32, np.float64, np.longdouble): for v in (0, 1, 2, 7, 8, 9, 15, 16, 19, 127, 128, 1024, 1235): tgt = dt(v * (v + 1) / 2) d = np.arange(1, v + 1, dtype=dt) assert_almost_equal(np.sum(d), tgt) assert_almost_equal(np.sum(d[::-1]), tgt) d = np.ones(500, dtype=dt) assert_almost_equal(np.sum(d[::2]), 250.) assert_almost_equal(np.sum(d[1::2]), 250.) assert_almost_equal(np.sum(d[::3]), 167.) assert_almost_equal(np.sum(d[1::3]), 167.) assert_almost_equal(np.sum(d[::-2]), 250.) assert_almost_equal(np.sum(d[-1::-2]), 250.) assert_almost_equal(np.sum(d[::-3]), 167.) assert_almost_equal(np.sum(d[-1::-3]), 167.) # sum with first reduction entry != 0 d = np.ones((1,), dtype=dt) d += d assert_almost_equal(d, 2.)
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 _check_ldexp(self, tp): assert_almost_equal(ncu.ldexp(np.array(2., np.float32), np.array(3, tp)), 16.) assert_almost_equal(ncu.ldexp(np.array(2., np.float64), np.array(3, tp)), 16.) assert_almost_equal(ncu.ldexp(np.array(2., np.longdouble), np.array(3, tp)), 16.)
def test_nextafterl(): return _test_nextafter(np.longdouble)
def test_spacingl(): return _test_spacing(np.longdouble)
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_complex_types(): """Check formatting of complex types. 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.complex64, np.cdouble, np.clongdouble]: yield check_complex_type, 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_scalar_format(): """Test the str.format method with NumPy scalar types""" tests = [('{0}', True, np.bool_), ('{0}', False, np.bool_), ('{0:d}', 130, np.uint8), ('{0:d}', 50000, np.uint16), ('{0:d}', 3000000000, np.uint32), ('{0:d}', 15000000000000000000, np.uint64), ('{0:d}', -120, np.int8), ('{0:d}', -30000, np.int16), ('{0:d}', -2000000000, np.int32), ('{0:d}', -7000000000000000000, np.int64), ('{0:g}', 1.5, np.float16), ('{0:g}', 1.5, np.float32), ('{0:g}', 1.5, np.float64), ('{0:g}', 1.5, np.longdouble)] # Python 2.6 doesn't implement complex.__format__ if sys.version_info[:2] > (2, 6): tests += [('{0:g}', 1.5+0.5j, np.complex64), ('{0:g}', 1.5+0.5j, np.complex128), ('{0:g}', 1.5+0.5j, np.clongdouble)] for (fmat, val, valtype) in tests: try: assert_equal(fmat.format(val), fmat.format(valtype(val)), "failed with val %s, type %s" % (val, valtype)) except ValueError as e: assert_(False, "format raised exception (fmt='%s', val=%s, type=%s, exc='%s')" % (fmat, repr(val), repr(valtype), str(e))) # Locale tests: scalar types formatting should be independent of the locale
def test_locale_longdouble(): assert_equal(str(np.longdouble(1.2)), str(float(1.2)))
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_einsum_sums_longdouble(self): self.check_einsum_sums(np.longdouble)
def test_singleton(self,level=2): ftype = finfo(longdouble) ftype2 = finfo(longdouble) assert_equal(id(ftype), id(ftype2))
def test_str(self): svals = [0.0, -0.0, 1, -1, np.inf, -np.inf, np.nan] styps = [np.float16, np.float32, np.float64, np.longdouble] actual = [str(f(c)) for c in svals for f in styps] wanted = [ '0.0', '0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '-0.0', '1.0', '1.0', '1.0', '1.0', '-1.0', '-1.0', '-1.0', '-1.0', 'inf', 'inf', 'inf', 'inf', '-inf', '-inf', '-inf', '-inf', 'nan', 'nan', 'nan', 'nan'] for res, val in zip(actual, wanted): assert_(res == val)
def test_large_types(self): for t in [np.int32, np.int64, np.float32, np.float64, np.longdouble]: a = t(51) b = a ** 4 msg = "error with %r: got %r" % (t, b) if np.issubdtype(t, np.integer): assert_(b == 6765201, msg) else: assert_almost_equal(b, 6765201, err_msg=msg)
def test_longdouble_int(self): # gh-627 x = np.longdouble(np.inf) assert_raises(OverflowError, x.__int__) x = np.clongdouble(np.inf) assert_raises(OverflowError, x.__int__)
def test_scalar_extraction(): """Confirm that extracting a value doesn't convert to python float""" o = 1 + np.finfo(np.longdouble).eps a = np.array([o, o, o]) assert_equal(a[1], o) # Conversions string -> long double
def test_repr_roundtrip(): o = 1 + np.finfo(np.longdouble).eps assert_equal(np.longdouble(repr(o)), o, "repr was %s" % repr(o))
def test_string(): np.longdouble("1.2")
def test_bytes(): np.longdouble(b"1.2")
def test_repr_roundtrip_bytes(): o = 1 + np.finfo(np.longdouble).eps assert_equal(np.longdouble(repr(o).encode("ascii")), o)
def test_repr_roundtrip_foreign(): o = 1.5 assert_equal(o, np.longdouble(repr(o)))
def test_bogus_string(): assert_raises(ValueError, np.longdouble, "spam") assert_raises(ValueError, np.longdouble, "1.0 flub")
def test_fromstring_best_effort(): assert_equal(np.fromstring("1,234", dtype=np.longdouble, sep=" "), np.array([1.]))
def test_fromfile(self): with temppath() as path: with open(path, 'wt') as f: f.write(self.out) res = np.fromfile(path, dtype=np.longdouble, sep="\n") assert_equal(res, self.tgt)
def test_genfromtxt(self): with temppath() as path: with open(path, 'wt') as f: f.write(self.out) res = np.genfromtxt(path, dtype=np.longdouble) assert_equal(res, self.tgt)
def test_loadtxt(self): with temppath() as path: with open(path, 'wt') as f: f.write(self.out) res = np.loadtxt(path, dtype=np.longdouble) assert_equal(res, self.tgt)
def test_tofile_roundtrip(self): with temppath() as path: self.tgt.tofile(path, sep=" ") res = np.fromfile(path, dtype=np.longdouble, sep=" ") assert_equal(res, self.tgt)
def test_fromstring_foreign_sep(): a = np.array([1, 2, 3, 4]) b = np.fromstring("1,2,3,4,", dtype=np.longdouble, sep=",") assert_array_equal(a, b)
def test_fromstring_foreign_value(): b = np.fromstring("1,234", dtype=np.longdouble, sep=" ") assert_array_equal(b[0], 1) # Conversions long double -> string
def test_repr_exact(): o = 1 + np.finfo(np.longdouble).eps assert_(repr(o) != '1')
def test_format(): o = 1 + np.finfo(np.longdouble).eps assert_("{0:.40g}".format(o) != '1')
def test_percent(): o = 1 + np.finfo(np.longdouble).eps assert_("%.40g" % o != '1')
def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ def floatconv(x): x.lower() if b'0x' in x: return float.fromhex(asstr(x)) return float(x) typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) if issubclass(typ, np.uint64): return np.uint64 if issubclass(typ, np.int64): return np.int64 if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.longdouble): return np.longdouble elif issubclass(typ, np.floating): return floatconv elif issubclass(typ, np.complex): return lambda x: complex(asstr(x)) elif issubclass(typ, np.bytes_): return bytes else: return str
def test_longdouble_norm(self): # Non-regression test: p-norm of longdouble would previously raise # UnboundLocalError. x = np.arange(10, dtype=np.longdouble) old_assert_almost_equal(norm(x, ord=3), 12.65, decimal=2)
def get_samples_per_second(self): """Return the sample rate in Hz as a numpy.longdouble.""" return self._samples_per_second