Python numpy.complexfloating() Examples
The following are 30 code examples for showing how to use numpy.complexfloating(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: recruit Author: Frank-qlu File: test_linalg.py License: Apache License 2.0 | 6 votes |
def test_basic_property(self): # Check A = L L^H shapes = [(1, 1), (2, 2), (3, 3), (50, 50), (3, 10, 10)] dtypes = (np.float32, np.float64, np.complex64, np.complex128) for shape, dtype in itertools.product(shapes, dtypes): np.random.seed(1) a = np.random.randn(*shape) if np.issubdtype(dtype, np.complexfloating): a = a + 1j*np.random.randn(*shape) t = list(range(len(shape))) t[-2:] = -1, -2 a = np.matmul(a.transpose(t).conj(), a) a = np.asarray(a, dtype=dtype) c = np.linalg.cholesky(a) b = np.matmul(c, c.transpose(t).conj()) assert_allclose(b, a, err_msg="{} {}\n{}\n{}".format(shape, dtype, a, c), atol=500 * a.shape[0] * np.finfo(dtype).eps)
Example 2
Project: lambda-packs Author: ryfeus File: base.py License: MIT License | 6 votes |
def check_arguments(fun, y0, support_complex): """Helper function for checking arguments common to all solvers.""" y0 = np.asarray(y0) if np.issubdtype(y0.dtype, np.complexfloating): if not support_complex: raise ValueError("`y0` is complex, but the chosen solver does " "not support integration in a complex domain.") dtype = complex else: dtype = float y0 = y0.astype(dtype, copy=False) if y0.ndim != 1: raise ValueError("`y0` must be 1-dimensional.") def fun_wrapped(t, y): return np.asarray(fun(t, y), dtype=dtype) return fun_wrapped, y0
Example 3
Project: vnpy_crypto Author: birforce File: test_linalg.py License: MIT License | 6 votes |
def test_basic_property(self): # Check A = L L^H shapes = [(1, 1), (2, 2), (3, 3), (50, 50), (3, 10, 10)] dtypes = (np.float32, np.float64, np.complex64, np.complex128) for shape, dtype in itertools.product(shapes, dtypes): np.random.seed(1) a = np.random.randn(*shape) if np.issubdtype(dtype, np.complexfloating): a = a + 1j*np.random.randn(*shape) t = list(range(len(shape))) t[-2:] = -1, -2 a = np.matmul(a.transpose(t).conj(), a) a = np.asarray(a, dtype=dtype) c = np.linalg.cholesky(a) b = np.matmul(c, c.transpose(t).conj()) assert_allclose(b, a, err_msg="{} {}\n{}\n{}".format(shape, dtype, a, c), atol=500 * a.shape[0] * np.finfo(dtype).eps)
Example 4
Project: nufhe Author: nucypher File: utils.py License: GNU General Public License v3.0 | 6 votes |
def get_test_array(shape, tp, val_range=None): dtype = tp_dtype(tp) if val_range is None: nmin, nmax = tp_limits(tp) else: nmin, nmax = val_range if numpy.issubdtype(dtype, numpy.integer): return numpy.random.randint(nmin, nmax, dtype=dtype, size=shape) elif numpy.issubdtype(dtype, numpy.floating): return numpy.random.uniform(nmin, nmax, size=shape).astype(dtype) elif numpy.issubdtype(dtype, numpy.complexfloating): return ( numpy.random.uniform(nmin, nmax, size=shape) + 1j * numpy.random.uniform(nmin, nmax, size=shape)).astype(dtype) else: raise NotImplementedError(dtype)
Example 5
Project: Computable Author: ktraunmueller File: test_real_transforms.py License: MIT License | 6 votes |
def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes): np.random.seed(1234) if np.issubdtype(dtype, np.complexfloating): data = np.random.randn(*shape) + 1j*np.random.randn(*shape) else: data = np.random.randn(*shape) data = data.astype(dtype) for type in [1, 2, 3]: for overwrite_x in [True, False]: for norm in [None, 'ortho']: if type == 1 and norm == 'ortho': continue should_overwrite = (overwrite_x and dtype in overwritable_dtypes and (len(shape) == 1 or (axis % len(shape) == len(shape)-1 ))) self._check(data, routine, type, None, axis, norm, overwrite_x, should_overwrite)
Example 6
Project: Computable Author: ktraunmueller File: test_basic.py License: MIT License | 6 votes |
def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes): np.random.seed(1234) if np.issubdtype(dtype, np.complexfloating): data = np.random.randn(*shape) + 1j*np.random.randn(*shape) else: data = np.random.randn(*shape) data = data.astype(dtype) for fftsize in [8, 16, 32]: for overwrite_x in [True, False]: should_overwrite = (overwrite_x and dtype in overwritable_dtypes and fftsize <= shape[axis] and (len(shape) == 1 or (axis % len(shape) == len(shape)-1 and fftsize == shape[axis]))) self._check(data, routine, fftsize, axis, overwrite_x=overwrite_x, should_overwrite=should_overwrite)
Example 7
Project: Computable Author: ktraunmueller File: test_base.py License: MIT License | 6 votes |
def test_mu(self): self.__arith_init() # basic tests assert_array_equal((self.__Asp*self.__Bsp.T).todense(),self.__A*self.__B.T) for x in supported_dtypes: A = self.__A.astype(x) Asp = self.spmatrix(A) for y in supported_dtypes: if np.issubdtype(y, np.complexfloating): B = self.__B.astype(y) else: B = self.__B.real.astype(y) Bsp = self.spmatrix(B) D1 = A * B.T S1 = Asp * Bsp.T assert_array_equal(S1.todense(),D1) assert_equal(S1.dtype,D1.dtype)
Example 8
Project: Computable Author: ktraunmueller File: test_mpmath.py License: MIT License | 6 votes |
def __init__(self, scipy_func, mpmath_func, arg_spec, name=None, dps=None, prec=None, n=5000, rtol=1e-7, atol=1e-300, ignore_inf_sign=False): self.scipy_func = scipy_func self.mpmath_func = mpmath_func self.arg_spec = arg_spec self.dps = dps self.prec = prec self.n = n self.rtol = rtol self.atol = atol self.ignore_inf_sign = ignore_inf_sign if isinstance(self.arg_spec, np.ndarray): self.is_complex = np.issubdtype(self.arg_spec.dtype, np.complexfloating) else: self.is_complex = any([isinstance(arg, ComplexArg) for arg in self.arg_spec]) self.ignore_inf_sign = ignore_inf_sign if not name or name == '<lambda>': name = getattr(scipy_func, '__name__', None) if not name or name == '<lambda>': name = getattr(mpmath_func, '__name__', None) self.name = name
Example 9
Project: Mastering-Elasticsearch-7.0 Author: PacktPublishing File: test_linalg.py License: MIT License | 6 votes |
def test_basic_property(self): # Check A = L L^H shapes = [(1, 1), (2, 2), (3, 3), (50, 50), (3, 10, 10)] dtypes = (np.float32, np.float64, np.complex64, np.complex128) for shape, dtype in itertools.product(shapes, dtypes): np.random.seed(1) a = np.random.randn(*shape) if np.issubdtype(dtype, np.complexfloating): a = a + 1j*np.random.randn(*shape) t = list(range(len(shape))) t[-2:] = -1, -2 a = np.matmul(a.transpose(t).conj(), a) a = np.asarray(a, dtype=dtype) c = np.linalg.cholesky(a) b = np.matmul(c, c.transpose(t).conj()) assert_allclose(b, a, err_msg="{} {}\n{}\n{}".format(shape, dtype, a, c), atol=500 * a.shape[0] * np.finfo(dtype).eps)
Example 10
Project: trax Author: google File: test_util.py License: Apache License 2.0 | 6 votes |
def _rand_dtype(rand, shape, dtype, scale=1., post=lambda x: x): """Produce random values given shape, dtype, scale, and post-processor. Args: rand: a function for producing random values of a given shape, e.g. a bound version of either onp.RandomState.randn or onp.RandomState.rand. shape: a shape value as a tuple of positive integers. dtype: a numpy dtype. scale: optional, a multiplicative scale for the random values (default 1). post: optional, a callable for post-processing the random values (default identity). Returns: An ndarray of the given shape and dtype using random values based on a call to rand but scaled, converted to the appropriate dtype, and post-processed. """ r = lambda: onp.asarray(scale * rand(*_dims_of_shape(shape)), dtype) if onp.issubdtype(dtype, onp.complexfloating): vals = r() + 1.0j * r() else: vals = r() return _cast_to_shape(onp.asarray(post(vals), dtype), shape, dtype)
Example 11
Project: afnumpy Author: FilipeMaia File: multiarray.py License: BSD 2-Clause "Simplified" License | 6 votes |
def real(self): ret_type = numpy.real(numpy.zeros((),dtype=self.dtype)).dtype shape = list(self.shape) if not numpy.issubdtype(self.dtype, numpy.complexfloating): return self shape[-1] *= 2 dims = numpy.array(pu.c2f(shape),dtype=pu.dim_t) s = arrayfire.Array() arrayfire.backend.get().af_device_array(ctypes.pointer(s.arr), ctypes.c_void_p(self.d_array.device_ptr()), self.ndim, ctypes.c_void_p(dims.ctypes.data), pu.typemap(ret_type).value) arrayfire.backend.get().af_retain_array(ctypes.pointer(s.arr),s.arr) a = ndarray(shape, dtype=ret_type, af_array=s) ret = a[...,::2] ret._base = a ret._base_index = (Ellipsis, slice(None,None,2)) return ret
Example 12
Project: afnumpy Author: FilipeMaia File: multiarray.py License: BSD 2-Clause "Simplified" License | 6 votes |
def imag(self): ret_type = numpy.real(numpy.zeros((),dtype=self.dtype)).dtype shape = list(self.shape) if not numpy.issubdtype(self.dtype, numpy.complexfloating): return afnumpy.zeros(self.shape) shape[-1] *= 2 dims = numpy.array(pu.c2f(shape),dtype=pu.dim_t) s = arrayfire.Array() arrayfire.backend.get().af_device_array(ctypes.pointer(s.arr), ctypes.c_void_p(self.d_array.device_ptr()), self.ndim, ctypes.c_void_p(dims.ctypes.data), pu.typemap(ret_type).value) arrayfire.backend.get().af_retain_array(ctypes.pointer(s.arr),s.arr) a = ndarray(shape, dtype=ret_type, af_array=s) ret = a[...,1::2] ret._base = a ret._base_index = (Ellipsis, slice(1,None,2)) return ret
Example 13
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_linalg.py License: MIT License | 6 votes |
def test_basic_property(self): # Check A = L L^H shapes = [(1, 1), (2, 2), (3, 3), (50, 50), (3, 10, 10)] dtypes = (np.float32, np.float64, np.complex64, np.complex128) for shape, dtype in itertools.product(shapes, dtypes): np.random.seed(1) a = np.random.randn(*shape) if np.issubdtype(dtype, np.complexfloating): a = a + 1j*np.random.randn(*shape) t = list(range(len(shape))) t[-2:] = -1, -2 a = np.matmul(a.transpose(t).conj(), a) a = np.asarray(a, dtype=dtype) c = np.linalg.cholesky(a) b = np.matmul(c, c.transpose(t).conj()) assert_allclose(b, a, err_msg="{} {}\n{}\n{}".format(shape, dtype, a, c), atol=500 * a.shape[0] * np.finfo(dtype).eps)
Example 14
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_real_transforms.py License: MIT License | 6 votes |
def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes): np.random.seed(1234) if np.issubdtype(dtype, np.complexfloating): data = np.random.randn(*shape) + 1j*np.random.randn(*shape) else: data = np.random.randn(*shape) data = data.astype(dtype) for type in [1, 2, 3]: for overwrite_x in [True, False]: for norm in [None, 'ortho']: if type == 1 and norm == 'ortho': continue should_overwrite = (overwrite_x and dtype in overwritable_dtypes and (len(shape) == 1 or (axis % len(shape) == len(shape)-1 ))) self._check(data, routine, type, None, axis, norm, overwrite_x, should_overwrite)
Example 15
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_basic.py License: MIT License | 6 votes |
def _check_1d(self, routine, dtype, shape, axis, overwritable_dtypes): np.random.seed(1234) if np.issubdtype(dtype, np.complexfloating): data = np.random.randn(*shape) + 1j*np.random.randn(*shape) else: data = np.random.randn(*shape) data = data.astype(dtype) for fftsize in [8, 16, 32]: for overwrite_x in [True, False]: should_overwrite = (overwrite_x and dtype in overwritable_dtypes and fftsize <= shape[axis] and (len(shape) == 1 or (axis % len(shape) == len(shape)-1 and fftsize == shape[axis]))) self._check(data, routine, fftsize, axis, overwrite_x=overwrite_x, should_overwrite=should_overwrite)
Example 16
Project: GraphicDesignPatternByPython Author: Relph1119 File: base.py License: MIT License | 6 votes |
def conj(self, copy=True): """Element-wise complex conjugation. If the matrix is of non-complex data type and `copy` is False, this method does nothing and the data is not copied. Parameters ---------- copy : bool, optional If True, the result is guaranteed to not share data with self. Returns ------- A : The element-wise complex conjugate. """ if np.issubdtype(self.dtype, np.complexfloating): return self.tocsr(copy=copy).conj(copy=False) elif copy: return self.copy() else: return self
Example 17
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_base.py License: MIT License | 6 votes |
def test_mu(self): self.__arith_init() # basic tests assert_array_equal((self.__Asp*self.__Bsp.T).todense(),self.__A*self.__B.T) for x in supported_dtypes: A = self.__A.astype(x) Asp = self.spmatrix(A) for y in supported_dtypes: if np.issubdtype(y, np.complexfloating): B = self.__B.astype(y) else: B = self.__B.real.astype(y) Bsp = self.spmatrix(B) D1 = A * B.T S1 = Asp * Bsp.T assert_allclose(S1.todense(), D1, atol=1e-14*abs(D1).max()) assert_equal(S1.dtype,D1.dtype)
Example 18
Project: GraphicDesignPatternByPython Author: Relph1119 File: test_linsolve.py License: MIT License | 6 votes |
def test_dtype_cast(self): A_real = scipy.sparse.csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]]) A_complex = scipy.sparse.csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5 + 1j]]) b_real = np.array([1,1,1]) b_complex = np.array([1,1,1]) + 1j*np.array([1,1,1]) x = spsolve(A_real, b_real) assert_(np.issubdtype(x.dtype, np.floating)) x = spsolve(A_real, b_complex) assert_(np.issubdtype(x.dtype, np.complexfloating)) x = spsolve(A_complex, b_real) assert_(np.issubdtype(x.dtype, np.complexfloating)) x = spsolve(A_complex, b_complex) assert_(np.issubdtype(x.dtype, np.complexfloating))
Example 19
Project: recruit Author: Frank-qlu File: arrayprint.py License: Apache License 2.0 | 5 votes |
def _get_format_function(data, **options): """ find the right formatting function for the dtype_ """ dtype_ = data.dtype dtypeobj = dtype_.type formatdict = _get_formatdict(data, **options) if issubclass(dtypeobj, _nt.bool_): return formatdict['bool']() elif issubclass(dtypeobj, _nt.integer): if issubclass(dtypeobj, _nt.timedelta64): return formatdict['timedelta']() else: return formatdict['int']() elif issubclass(dtypeobj, _nt.floating): if issubclass(dtypeobj, _nt.longfloat): return formatdict['longfloat']() else: return formatdict['float']() elif issubclass(dtypeobj, _nt.complexfloating): if issubclass(dtypeobj, _nt.clongfloat): return formatdict['longcomplexfloat']() else: return formatdict['complexfloat']() elif issubclass(dtypeobj, (_nt.unicode_, _nt.string_)): return formatdict['numpystr']() elif issubclass(dtypeobj, _nt.datetime64): return formatdict['datetime']() elif issubclass(dtypeobj, _nt.object_): return formatdict['object']() elif issubclass(dtypeobj, _nt.void): if dtype_.names is not None: return StructuredVoidFormat.from_data(data, **options) else: return formatdict['void']() else: return formatdict['numpystr']()
Example 20
Project: recruit Author: Frank-qlu File: test_abc.py License: Apache License 2.0 | 5 votes |
def test_abstract(self): assert_(issubclass(np.number, numbers.Number)) assert_(issubclass(np.inexact, numbers.Complex)) assert_(issubclass(np.complexfloating, numbers.Complex)) assert_(issubclass(np.floating, numbers.Real)) assert_(issubclass(np.integer, numbers.Integral)) assert_(issubclass(np.signedinteger, numbers.Integral)) assert_(issubclass(np.unsignedinteger, numbers.Integral))
Example 21
Project: recruit Author: Frank-qlu File: test_eval.py License: Apache License 2.0 | 5 votes |
def _is_py3_complex_incompat(result, expected): return (PY3 and isinstance(expected, (complex, np.complexfloating)) and np.isnan(result))
Example 22
Project: recruit Author: Frank-qlu File: blocks.py License: Apache License 2.0 | 5 votes |
def _can_hold_element(self, element): tipo = maybe_infer_dtype_type(element) if tipo is not None: return issubclass(tipo.type, (np.floating, np.integer, np.complexfloating)) return ( isinstance( element, (float, int, complex, np.float_, np.int_, compat.long)) and not isinstance(element, (bool, np.bool_)))
Example 23
Project: recruit Author: Frank-qlu File: blocks.py License: Apache License 2.0 | 5 votes |
def should_store(self, value): return not (issubclass(value.dtype.type, (np.integer, np.floating, np.complexfloating, np.datetime64, np.bool_)) or # TODO(ExtensionArray): remove is_extension_type # when all extension arrays have been ported. is_extension_type(value) or is_extension_array_dtype(value))
Example 24
Project: recruit Author: Frank-qlu File: common.py License: Apache License 2.0 | 5 votes |
def is_complex_dtype(arr_or_dtype): """ Check whether the provided array or dtype is of a complex dtype. Parameters ---------- arr_or_dtype : array-like The array or dtype to check. Returns ------- boolean : Whether or not the array or dtype is of a compex dtype. Examples -------- >>> is_complex_dtype(str) False >>> is_complex_dtype(int) False >>> is_complex_dtype(np.complex) True >>> is_complex_dtype(np.array(['a', 'b'])) False >>> is_complex_dtype(pd.Series([1, 2])) False >>> is_complex_dtype(np.array([1 + 1j, 5])) True """ return _is_dtype_type(arr_or_dtype, classes(np.complexfloating))
Example 25
Project: lambda-packs Author: ryfeus File: arrayprint.py License: MIT License | 5 votes |
def _get_format_function(data, **options): """ find the right formatting function for the dtype_ """ dtype_ = data.dtype dtypeobj = dtype_.type formatdict = _get_formatdict(data, **options) if issubclass(dtypeobj, _nt.bool_): return formatdict['bool']() elif issubclass(dtypeobj, _nt.integer): if issubclass(dtypeobj, _nt.timedelta64): return formatdict['timedelta']() else: return formatdict['int']() elif issubclass(dtypeobj, _nt.floating): if issubclass(dtypeobj, _nt.longfloat): return formatdict['longfloat']() else: return formatdict['float']() elif issubclass(dtypeobj, _nt.complexfloating): if issubclass(dtypeobj, _nt.clongfloat): return formatdict['longcomplexfloat']() else: return formatdict['complexfloat']() elif issubclass(dtypeobj, (_nt.unicode_, _nt.string_)): return formatdict['numpystr']() elif issubclass(dtypeobj, _nt.datetime64): return formatdict['datetime']() elif issubclass(dtypeobj, _nt.object_): return formatdict['object']() elif issubclass(dtypeobj, _nt.void): if dtype_.names is not None: return StructuredVoidFormat.from_data(data, **options) else: return formatdict['void']() else: return formatdict['numpystr']()
Example 26
Project: lambda-packs Author: ryfeus File: _norm.py License: MIT License | 5 votes |
def _sparse_frobenius_norm(x): if np.issubdtype(x.dtype, np.complexfloating): sqnorm = abs(x).power(2).sum() else: sqnorm = x.power(2).sum() return sqrt(sqnorm)
Example 27
Project: lambda-packs Author: ryfeus File: arpack.py License: MIT License | 5 votes |
def __init__(self, M): self.M_lu = splu(M) self.shape = M.shape self.dtype = M.dtype self.isreal = not np.issubdtype(self.dtype, np.complexfloating)
Example 28
Project: lambda-packs Author: ryfeus File: arpack.py License: MIT License | 5 votes |
def _matvec(self, x): # careful here: splu.solve will throw away imaginary # part of x if M is real x = np.asarray(x) if self.isreal and np.issubdtype(x.dtype, np.complexfloating): return (self.M_lu.solve(np.real(x).astype(self.dtype)) + 1j * self.M_lu.solve(np.imag(x).astype(self.dtype))) else: return self.M_lu.solve(x.astype(self.dtype))
Example 29
Project: lambda-packs Author: ryfeus File: arpack.py License: MIT License | 5 votes |
def get_OPinv_matvec(A, M, sigma, symmetric=False, tol=0): if sigma == 0: return get_inv_matvec(A, symmetric=symmetric, tol=tol) if M is None: #M is the identity matrix if isdense(A): if (np.issubdtype(A.dtype, np.complexfloating) or np.imag(sigma) == 0): A = np.copy(A) else: A = A + 0j A.flat[::A.shape[1] + 1] -= sigma return LuInv(A).matvec elif isspmatrix(A): A = A - sigma * eye(A.shape[0]) if symmetric and isspmatrix_csr(A): A = A.T return SpLuInv(A.tocsc()).matvec else: return IterOpInv(_aslinearoperator_with_dtype(A), M, sigma, tol=tol).matvec else: if ((not isdense(A) and not isspmatrix(A)) or (not isdense(M) and not isspmatrix(M))): return IterOpInv(_aslinearoperator_with_dtype(A), _aslinearoperator_with_dtype(M), sigma, tol=tol).matvec elif isdense(A) or isdense(M): return LuInv(A - sigma * M).matvec else: OP = A - sigma * M if symmetric and isspmatrix_csr(OP): OP = OP.T return SpLuInv(OP.tocsc()).matvec # ARPACK is not threadsafe or reentrant (SAVE variables), so we need a # lock and a re-entering check.
Example 30
Project: lambda-packs Author: ryfeus File: polyint.py License: MIT License | 5 votes |
def _set_dtype(self, dtype, union=False): if np.issubdtype(dtype, np.complexfloating) \ or np.issubdtype(self.dtype, np.complexfloating): self.dtype = np.complex_ else: if not union or self.dtype != np.complex_: self.dtype = np.float_