我们从Python开源项目中,提取了以下28个代码示例,用于说明如何使用numpy.complexfloating()。
def _get_inplace_dtype(obj1, obj2): """ Returns the dtype of obj1, Raise error if 1) obj1 is real and obj2 is complex 2) obj1 is integer and obj2 is floating Parameters ---------- obj1 : numpy.ndarray like array obj2 : numpy.ndarray like array Returns ------- out : np.dtype """ if isrealobj(obj1): if iscomplexobj(obj2): raise TypeError("Cannot cast complex dtype to real dtype") if issubclass(obj1.dtype.type, np.integer): if issubclass(obj2.dtype.type, (np.floating, np.complexfloating)): raise TypeError("Cannot cast floating to integer") return obj1.dtype
def _get_common_dtype_with_scalar(scalar, obj1): """ return the common dtype between a native scalar (int, float, complex) and the dtype of an ndarray like array. Parameters ---------- scalar : { int, float, complex } obj1 : numpy.ndarray like array. """ if issubclass(type(scalar), (int, float, np.integer, np.floating)): return obj1.dtype elif issubclass(type(scalar), (complex, np.complexfloating)): if isrealobj(obj1): return floattocomplex(obj1.dtype) else: return obj1.dtype else: raise TypeError("scalar type is not supported")
def _can_hold_element(self, element): if is_list_like(element): element = np.array(element) return issubclass(element.dtype.type, (np.floating, np.integer, np.complexfloating)) return (isinstance(element, (float, int, complex, np.float_, np.int_)) and not isinstance(bool, np.bool_))
def should_store(self, value): return issubclass(value.dtype.type, np.complexfloating)
def should_store(self, value): return not (issubclass(value.dtype.type, (np.integer, np.floating, np.complexfloating, np.datetime64, np.bool_)) or is_internal_type(value))
def make_block(values, placement, klass=None, ndim=None, dtype=None, fastpath=False): if klass is None: dtype = dtype or values.dtype vtype = dtype.type if isinstance(values, SparseArray): klass = SparseBlock elif issubclass(vtype, np.floating): klass = FloatBlock elif (issubclass(vtype, np.integer) and issubclass(vtype, np.timedelta64)): klass = TimeDeltaBlock elif (issubclass(vtype, np.integer) and not issubclass(vtype, np.datetime64)): klass = IntBlock elif dtype == np.bool_: klass = BoolBlock elif issubclass(vtype, np.datetime64): if hasattr(values, 'tz'): klass = DatetimeTZBlock else: klass = DatetimeBlock elif is_datetimetz(values): klass = DatetimeTZBlock elif issubclass(vtype, np.complexfloating): klass = ComplexBlock elif is_categorical(values): klass = CategoricalBlock else: klass = ObjectBlock elif klass is DatetimeTZBlock and not is_datetimetz(values): return klass(values, ndim=ndim, fastpath=fastpath, placement=placement, dtype=dtype) return klass(values, ndim=ndim, fastpath=fastpath, placement=placement) # TODO: flexible with index=None and/or items=None
def is_complex_dtype(arr_or_dtype): tipo = _get_dtype_type(arr_or_dtype) return issubclass(tipo, np.complexfloating)
def test_convert_objects_complex_number(): for dtype in np.sctypes['complex']: arr = np.array(list(1j * np.arange(20, dtype=dtype)), dtype='O') assert (arr[0].dtype == np.dtype(dtype)) result = lib.maybe_convert_objects(arr) assert (issubclass(result.dtype.type, np.complexfloating))
def fft_convolve_1d(x: np.ndarray, h: np.ndarray): n = len(x) + len(h) - 1 n_opt = 1 << (n - 1).bit_length() # Get next power of 2 if np.issubdtype(x.dtype, np.complexfloating) or np.issubdtype(h.dtype, np.complexfloating): fft, ifft = np.fft.fft, np.fft.ifft # use complex fft else: fft, ifft = np.fft.rfft, np.fft.irfft # use real fft result = ifft(fft(x, n_opt) * fft(h, n_opt), n_opt)[0:n] too_much = (len(result) - len(x)) // 2 # Center result return result[too_much: -too_much]
def _get_inplace_dtype_with_scalar(scalar, obj1): """ Returns the dtype of obj1, Raise error if 1) obj1 is real and obj2 is complex 2) obj1 is integer and obj2 is floating Parameters ---------- obj1 : numpy.ndarray like array obj2 : numpy.ndarray like array Returns ------- out : np.dtype """ if isrealobj(obj1): if issubclass(type(scalar), (complex, np.complexfloating)): raise TypeError("Cannot cast complex dtype to real dtype") if issubclass(obj1.dtype.type, np.integer): if issubclass( type(scalar), (float, complex, np.floating, np.complexfloating)): raise TypeError("Cannot cast floating to integer") return obj1.dtype
def iscomplexobj(pary): """ Check if the array dtype is complex """ return issubclass(pary.dtype.type, np.complexfloating)
def complextofloat(dtype): """ convert dtype from complex to corresponding real dtype """ dtype = dtype.type if isinstance(dtype, np.dtype) else dtype if not issubclass(dtype, np.complexfloating): outdtype = dtype elif dtype == np.complex64: outdtype = np.float32 elif dtype == np.complex128: outdtype = np.float64 else: raise TypeError("input dtype " + str(dtype) + " cannot be translated to floating") return np.dtype(outdtype)
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)
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))
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 * identity(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
def nan_to_num(array, fill_value = 0.0, copy = True): """ Replace NaNs with another fill value. Parameters ---------- array : array_like Input data. fill_value : float, optional NaNs will be replaced by ``fill_value``. Default is 0.0, in keeping with ``numpy.nan_to_num``. copy : bool, optional Whether to create a copy of `array` (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Returns ------- out : ndarray Array without NaNs. If ``array`` was not of floating or complearray type, ``array`` is returned unchanged. Notes ----- Contrary to ``numpy.nan_to_num``, this functions does not handle infinite values. See Also -------- numpy.nan_to_num : replace NaNs and Infs with zeroes. """ array = np.array(array, subok = True, copy = copy) dtype = array.dtype.type # Non-inexact types do not have NaNs if not np.issubdtype(dtype, np.inexact): return array iscomplex = np.issubdtype(dtype, np.complexfloating) dest = (array.real, array.imag) if iscomplex else (array,) for d in dest: np.copyto(d, fill_value, where = np.isnan(d)) return array
def get_numeric_types(with_int=True, with_float=True, with_complex=False, only_theano_types=True): """ Return numpy numeric data types. :param with_int: Whether to include integer types. :param with_float: Whether to include floating point types. :param with_complex: Whether to include complex types. :param only_theano_types: If True, then numpy numeric data types that are not supported by Theano are ignored (i.e. those that are not declared in scalar/basic.py). :returns: A list of unique data type objects. Note that multiple data types may share the same string representation, but can be differentiated through their `num` attribute. Note that when `only_theano_types` is True we could simply return the list of types defined in the `scalar` module. However with this function we can test more unique dtype objects, and in the future we may use it to automatically detect new data types introduced in numpy. """ if only_theano_types: theano_types = [d.dtype for d in theano.scalar.all_types] rval = [] def is_within(cls1, cls2): # Return True if scalars defined from `cls1` are within the hierarchy # starting from `cls2`. # The third test below is to catch for instance the fact that # one can use ``dtype=numpy.number`` and obtain a float64 scalar, even # though `numpy.number` is not under `numpy.floating` in the class # hierarchy. return (cls1 is cls2 or issubclass(cls1, cls2) or isinstance(numpy.array([0], dtype=cls1)[0], cls2)) for cls in get_numeric_subclasses(): dtype = numpy.dtype(cls) if ((not with_complex and is_within(cls, numpy.complexfloating)) or (not with_int and is_within(cls, numpy.integer)) or (not with_float and is_within(cls, numpy.floating)) or (only_theano_types and dtype not in theano_types)): # Ignore this class. continue rval.append([str(dtype), dtype, dtype.num]) # We sort it to be deterministic, then remove the string and num elements. return [x[1] for x in sorted(rval, key=str)]
def __add__(self, other): """ Addition Parameters ---------- other: scalar or Pitcharray Returns ------- out : PitchArray A new PitchArray """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") result = self._new_like_me(_get_common_dtype(self, other)) if self.size: if self.M == 1: func = pu.get_addarray_function( self.dtype, other.dtype, result.dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_addarray_function( self.dtype, other.dtype, result.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other.gpudata, other.ld) return result elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_common_dtype_with_scalar(other, self) if other == 0: return self.astype(dtype) else: result = self._new_like_me(dtype) if self.size: if self.M == 1: func = pu.get_addscalar_function( self.dtype, dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other, self.size) else: func = pu.get_addscalar_function( self.dtype, dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other) return result else: raise TypeError("type of object to be added is not supported")
def __sub__(self, other): """ Substraction self - other Parameters ---------- other : scalar or Pitcharray Returns ------- out : PitchArray A new PitchArray """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") result = self._new_like_me(_get_common_dtype(self, other)) if self.size: if self.M == 1: func = pu.get_subarray_function( self.dtype, other.dtype, result.dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_subarray_function( self.dtype, other.dtype, result.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other.gpudata, other.ld) return result elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_common_dtype_with_scalar(other, self) if other == 0: return self.astype(dtype) else: result = self._new_like_me(dtype) if self.size: if self.M == 1: func = pu.get_subscalar_function( self.dtype, dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other, self.size) else: func = pu.get_subscalar_function( self.dtype, dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other) return result else: raise TypeError("type of object to be substracted " "is not supported")
def __mul__(self, other): """ Multiply Parameters ---------- other: scalar or Pitcharray Returns ------- out : PitchArray A new PitchArray """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") result = self._new_like_me(_get_common_dtype(self, other)) if self.size: if self.M == 1: func = pu.get_mularray_function( self.dtype, other.dtype, result.dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_mularray_function( self.dtype, other.dtype, result.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other.gpudata, other.ld) return result elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_common_dtype_with_scalar(other, self) if other == 1.0: return self.astype(dtype) else: result = self._new_like_me(dtype) if self.size: if self.M == 1: func = pu.get_mulscalar_function( self.dtype, dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other, self.size) else: func = pu.get_mulscalar_function( self.dtype, dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other) return result else: raise TypeError("type of object to be multiplied " "is not supported")
def __div__(self, other): """ Division self / other Parameters ---------- other: scalar or Pitcharray Returns ------- out : PitchArray A new PitchArray """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") result = self._new_like_me(_get_common_dtype(self, other)) if self.size: if self.M == 1: func = pu.get_divarray_function( self.dtype, other.dtype, result.dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_divarray_function( self.dtype, other.dtype, result.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other.gpudata, other.ld) return result elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_common_dtype_with_scalar(other, self) if other == 1.0: return self.astype(dtype) else: result = self._new_like_me(dtype) if self.size: if self.M == 1: func = pu.get_divscalar_function( self.dtype, dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other, self.size) else: func = pu.get_divscalar_function( self.dtype, dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other) return result else: raise TypeError("type of object to be divided " "is not supported")
def __rdiv__(self, other): """ Being divided other / self Parameters ---------- other: scalar or Pitcharray Returns ------- out : PitchArray A new PitchArray """ """ # this part it not necessary if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") result = self._new_like_me(_get_common_dtype(self, other)) if self.size: if self.M == 1: func = pu.get_divarray_function( other.dtype, self.dtype, result.dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, other.gpudata, self.gpudata, self.size) else: func = pu.get_divarray_function( other.dtype, self.dtype, result.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, other.gpudata, other.ld, self.gpudata, self.ld) return result """ if issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_common_dtype_with_scalar(other, self) result = self._new_like_me(dtype) if self.size: if self.M == 1: func = pu.get_scalardiv_function( self.dtype, dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other, self.size) else: func = pu.get_scalardiv_function( self.dtype, dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other) return result else: raise TypeError("type of object to be divided from" "is not supported")
def __pow__(self, other): if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") result = self._new_like_me(_get_common_dtype(self, other)) if self.size: if self.M == 1: func = pu.get_powarray_function( self.dtype, other.dtype, result.dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_powarray_function( self.dtype, other.dtype, result.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other.gpudata, other.ld) return result elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_common_dtype_with_scalar(other, self) if other == 0: return self.astype(dtype) else: result = self._new_like_me(dtype) if self.size: if self.M == 1: func = pu.get_powscalar_function( self.dtype, dtype, pitch = False) func.prepared_call( self._grid, self._block, result.gpudata, self.gpudata, other, self.size) else: func = pu.get_powscalar_function( self.dtype, dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, result.gpudata, result.ld, self.gpudata, self.ld, other) return result else: raise TypeError("type of object to be powered is not supported")
def add(self, other): """ add other to self inplace """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") dtype = _get_inplace_dtype(self, other) if self.size: if self.M == 1: func = pu.get_addarray_function( self.dtype, other.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_addarray_function( self.dtype, other.dtype, self.dtype, pitch = True) func.prepared_call(self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other.gpudata, other.ld) return self elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_inplace_dtype_with_scalar(other, self) if other != 0: if self.size: if self.M == 1: func = pu.get_addscalar_function( self.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other, self.size) else: func = pu.get_addscalar_function( self.dtype, self.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other) return self else: raise TypeError("type of object to be added" "is not supported")
def rsub(self, other): """ substract other by self inplace """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") dtype = _get_inplace_dtype(self, other) if self.size: if self.M == 1: func = pu.get_subarray_function( other.dtype, self.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, other.gpudata, self.gpudata, self.size) else: func = pu.get_subarray_function( other.dtype, self.dtype, self.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, self.gpudata, self.ld, other.gpudata, other.ld, self.gpudata, self.ld) return self elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_inplace_dtype_with_scalar(other, self) if self.size: if self.M == 1: func = pu.get_scalarsub_function( self.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other, self.size) else: func = pu.get_scalarsub_function( self.dtype, self.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other) return self else: raise TypeError("type of object to substract from" "is not supported")
def mul(self, other): """ multiply other with self inplace """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") dtype = _get_inplace_dtype(self, other) if self.size: if self.M == 1: func = pu.get_mularray_function( self.dtype, other.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_mularray_function( self.dtype, other.dtype, self.dtype, pitch = True) func.prepared_call(self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other.gpudata, other.ld) return self elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_inplace_dtype_with_scalar(other, self) if other != 1: if self.size: if self.M == 1: func = pu.get_mulscalar_function( self.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other, self.size) else: func = pu.get_mulscalar_function( self.dtype, self.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other) return self else: raise TypeError("type of object to be multiplied" "is not supported")
def div(self, other): """ divide other from self inplace """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") dtype = _get_inplace_dtype(self, other) if self.size: if self.M == 1: func = pu.get_divarray_function( self.dtype, other.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other.gpudata, self.size) else: func = pu.get_divarray_function( self.dtype, other.dtype, self.dtype, pitch = True) func.prepared_call(self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other.gpudata, other.ld) return self elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_inplace_dtype_with_scalar(other, self) if other != 1: if self.size: if self.M == 1: func = pu.get_divscalar_function( self.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other, self.size) else: func = pu.get_divscalar_function( self.dtype, self.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other) return self else: raise TypeError("type of object to be divided" "is not supported")
def rdiv(self, other): """ divide other by self inplace """ if isinstance(other, PitchArray): if self.shape != other.shape: raise ValueError("array dimension misaligned") dtype = _get_inplace_dtype(self, other) if self.size: if self.M == 1: func = pu.get_divarray_function( other.dtype, self.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, other.gpudata, self.gpudata, self.size) else: func = pu.get_divarray_function( other.dtype, self.dtype, self.dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, self.gpudata, self.ld, other.gpudata, other.ld, self.gpudata, self.ld) return self elif issubclass(type(other), (float, int, complex, np.integer, np.floating, np.complexfloating)): dtype = _get_inplace_dtype_with_scalar(other, self) if self.size: if self.M == 1: func = pu.get_scalardiv_function( self.dtype, self.dtype, pitch = False) func.prepared_call( self._grid, self._block, self.gpudata, self.gpudata, other, self.size) else: func = pu.get_scalardiv_function( self.dtype, dtype, pitch = True) func.prepared_call( self._grid, self._block, self.M, self.N, self.gpudata, self.ld, self.gpudata, self.ld, other) return self else: raise TypeError("type of object to divide from" "is not supported")