Python numpy.complexfloating() Examples
The following are 30
code examples of numpy.complexfloating().
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 also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: test_base.py From Computable with 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 #2
Source File: test_base.py From GraphicDesignPatternByPython with 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 #3
Source File: test_linsolve.py From GraphicDesignPatternByPython with 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 #4
Source File: test_linalg.py From recruit with 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 #5
Source File: base.py From GraphicDesignPatternByPython with 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 #6
Source File: test_basic.py From GraphicDesignPatternByPython with 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
Source File: test_real_transforms.py From GraphicDesignPatternByPython with 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 #8
Source File: test_linalg.py From GraphicDesignPatternByPython with 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 #9
Source File: base.py From lambda-packs with 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 #10
Source File: multiarray.py From afnumpy with 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 #11
Source File: test_util.py From trax with 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 #12
Source File: utils.py From nufhe with 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 #13
Source File: test_linalg.py From Mastering-Elasticsearch-7.0 with 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
Source File: test_mpmath.py From Computable with 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 #15
Source File: test_real_transforms.py From Computable with 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 #16
Source File: test_basic.py From Computable with 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 #17
Source File: test_linalg.py From vnpy_crypto with 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 #18
Source File: multiarray.py From afnumpy with 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 #19
Source File: arpack.py From GraphicDesignPatternByPython with 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 #20
Source File: test_waveforms.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_dtype(self): imp = waveforms.unit_impulse(7) assert_(np.issubdtype(imp.dtype, np.floating)) imp = waveforms.unit_impulse(5, 3, dtype=int) assert_(np.issubdtype(imp.dtype, np.integer)) imp = waveforms.unit_impulse((5, 2), (3, 1), dtype=complex) assert_(np.issubdtype(imp.dtype, np.complexfloating))
Example #21
Source File: test_linsolve.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def _smoketest(self, spxlu, check, dtype): if np.issubdtype(dtype, np.complexfloating): A = self.A + 1j*self.A.T else: A = self.A A = A.astype(dtype) lu = spxlu(A) rng = random.RandomState(1234) # Input shapes for k in [None, 1, 2, self.n, self.n+2]: msg = "k=%r" % (k,) if k is None: b = rng.rand(self.n) else: b = rng.rand(self.n, k) if np.issubdtype(dtype, np.complexfloating): b = b + 1j*rng.rand(*b.shape) b = b.astype(dtype) x = lu.solve(b) check(A, b, x, msg) x = lu.solve(b, 'T') check(A.T, b, x, msg) x = lu.solve(b, 'H') check(A.T.conj(), b, x, msg)
Example #22
Source File: arpack.py From Computable with MIT License | 5 votes |
def _matvec(self, x): # careful here: splu.solve will throw away imaginary # part of x if M is real 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 #23
Source File: test_pseudo_diffs.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def _check_1d(self, routine, dtype, shape, *args, **kwargs): 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) self._check(data, routine, *args, **kwargs)
Example #24
Source File: arpack.py From Computable with 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
Example #25
Source File: arpack.py From GraphicDesignPatternByPython with 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 #26
Source File: _norm.py From GraphicDesignPatternByPython with 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
Source File: arrayprint.py From Mastering-Elasticsearch-7.0 with 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 #28
Source File: test_abc.py From Mastering-Elasticsearch-7.0 with MIT License | 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 #29
Source File: qobj.py From qiskit-aer with Apache License 2.0 | 5 votes |
def __rmul__(self, other): """ MULTIPLICATION with Qobj on RIGHT [ ex. 4*Qobj ] """ if isinstance(other, np.ndarray): if other.dtype == 'object': return np.array([item * self for item in other], dtype=object) else: return other * self.data elif isinstance(other, list): # if other is a list, do element-wise multiplication return np.array([item * self for item in other], dtype=object) elif isinstance(other, (int, float, complex, np.integer, np.floating, np.complexfloating)): out = Qobj() out.data = other * self.data out.dims = self.dims out.superrep = self.superrep if isinstance(other, complex): out._isherm = out.isherm else: out._isherm = self._isherm return out else: raise TypeError("Incompatible object for multiplication") # keep for now
Example #30
Source File: polyint.py From Computable with 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_