The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
def upcast_float16_ufunc(fn): """Decorator that enforces computation is not done in float16 by NumPy. Some ufuncs in NumPy will compute float values on int8 and uint8 in half-precision (float16), which is not enough, and not compatible with the C code. :param fn: numpy ufunc :returns: function similar to fn.__call__, computing the same value with a minimum floating-point precision of float32 """ def ret(*args, **kwargs): out_dtype = numpy.find_common_type( [a.dtype for a in args], [numpy.float16]) if out_dtype == 'float16': # Force everything to float32 sig = 'f' * fn.nin + '->' + 'f' * fn.nout kwargs.update(sig=sig) return fn(*args, **kwargs) return ret
Example 2
def __init__(self,value,site,tag,matrix): ''' Constructor. Parameters ---------- value : number The overall coefficient of the single site operator. site : Label The site label of the single site operator. tag : any hashable object The tag of the single operator. matrix : 2d ndarray The matrix of the single site operator. ''' assert matrix.ndim==2 dtype=np.find_common_type([np.asarray(value).dtype,np.asarray(matrix).dtype],[]) self.value=np.array(value,dtype=dtype) self.site=site self.tag=tag self.matrix=np.asarray(matrix,dtype=dtype)
Example 3
def __iadd__(self,other): ''' Overloaded self addition(+) operator, which supports the self addition by an instance of Opt. ''' assert other.site==self.site if self.tag=='0': return other elif other.tag=='0': return self else: if self.tag==other.tag: self.value+=other.value else: self.value=np.array(1.0,dtype=np.find_common_type([self.value.dtype,other.value.dtype],[])) self.tag='%s(%s)+%s(%s)'%(self.value,self.tag,other.value,other.tag) self.matrix*=self.value self.matrix+=other.value*other.matrix return self
Example 4
def block_diag(*ms): ''' Create a block diagonal matrix from provided ones. Parameters ---------- ms : list of 2d ndarray The input matrices. Returns ------- 2d ndarray The constructed block diagonal matrix. ''' if len(ms)==0: ms=[np.zeros((0,0))] shapes=np.array([a.shape for a in ms]) dtype=np.find_common_type([m.dtype for m in ms],[]) result=np.zeros(np.sum(shapes,axis=0),dtype=dtype) r,c=0,0 for i,(cr,cc) in enumerate(shapes): result[r:r+cr,c:c+cc]=ms[i] r+=cr c+=cc return result
Example 5
def test_find_common_type_boolean(self): # Ticket #1695 assert_(np.find_common_type([], ['?', '?']) == '?')
Example 6
def test_scalar_loses1(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['f8']) assert_(res == 'f4')
Example 7
def test_scalar_loses2(self): res = np.find_common_type(['f4', 'f4'], ['i8']) assert_(res == 'f4')
Example 8
def test_scalar_wins(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['c8']) assert_(res == 'c8')
Example 9
def test_scalar_wins2(self): res = np.find_common_type(['u4', 'i4', 'i4'], ['f4']) assert_(res == 'f8')
Example 10
def test_where_type(self): # Test the type conservation with where x = np.arange(4, dtype=np.int32) y = np.arange(4, dtype=np.float32) * 2.2 test = where(x > 1.5, y, x).dtype control = np.find_common_type([np.int32, np.float32], []) assert_equal(test, control)
Example 11
def _cast_common_type(*xs): dtypes = [x.dtype for x in xs if x is not None] dtype = numpy.find_common_type(dtypes, []) return [x.astype(dtype) if x is not None and x.dtype != dtype else x for x in xs]
Example 12
def slogdet(a): """Returns sign and logarithm of the determinat of an array. It calculates the natural logarithm of the deteminant of a given value. Args: a (cupy.ndarray): The input matrix with dimension ``(..., N, N)``. Returns: tuple of :class:`~cupy.ndarray`: It returns a tuple ``(sign, logdet)``. ``sign`` represents each sign of the deteminant as a real number ``0``, ``1`` or ``-1``. 'logdet' represents the natural logarithm of the absolute of the deteminant. If the deteninant is zero, ``sign`` will be ``0`` and ``logdet`` will be ``-inf``. The shapes of both ``sign`` and ``logdet`` are equal to ``a.shape[:-2]``. .. seealso:: :func:`numpy.linalg.slogdet` """ if not cuda.cusolver_enabled: raise RuntimeError('Current cupy only supports cusolver in CUDA 8.0') if a.ndim < 2: msg = ('%d-dimensional array given. ' 'Array must be at least two-dimensional' % a.ndim) raise linalg.LinAlgError(msg) dtype = numpy.find_common_type((a.dtype.char, 'f'), ()) shape = a.shape[:-2] sign = cupy.empty(shape, dtype) logdet = cupy.empty(shape, dtype) a = a.astype(dtype) for index in numpy.ndindex(*shape): s, l = _slogdet_one(a[index]) sign[index] = s logdet[index] = l return sign, logdet
Example 13
def _get_dtype(operators, dtypes=None): if dtypes is None: dtypes = [] for obj in operators: if obj is not None and hasattr(obj, 'dtype'): dtypes.append(obj.dtype) return np.find_common_type(dtypes, [])
Example 14
def _coerce_dtype(self, other_dtype): """Possibly change the bin content type to allow correct operations with other operand. Parameters ---------- other_dtype : np.dtype or type """ new_dtype = np.find_common_type([self.dtype, np.dtype(other_dtype)], []) if new_dtype != self.dtype: self.dtype = new_dtype
Example 15
def test_find_common_type_boolean(self): # Ticket #1695 assert_(np.find_common_type([], ['?', '?']) == '?')
Example 16
def test_scalar_loses1(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['f8']) assert_(res == 'f4')
Example 17
def test_scalar_loses2(self): res = np.find_common_type(['f4', 'f4'], ['i8']) assert_(res == 'f4')
Example 18
def test_scalar_wins(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['c8']) assert_(res == 'c8')
Example 19
def test_scalar_wins2(self): res = np.find_common_type(['u4', 'i4', 'i4'], ['f4']) assert_(res == 'f8')
Example 20
def test_where_type(self): # Test the type conservation with where x = np.arange(4, dtype=np.int32) y = np.arange(4, dtype=np.float32) * 2.2 test = where(x > 1.5, y, x).dtype control = np.find_common_type([np.int32, np.float32], []) assert_equal(test, control)
Example 21
def forward_cpu(self, x): a, b = x batch_size = a.shape[0] shape = self._output_shape(a, b) ret_dtype = numpy.find_common_type([a.dtype, b.dtype], []) ret = numpy.empty(shape, dtype=ret_dtype) for i in six.moves.range(batch_size): ret[i] = _matmul( a[i], b[i], transa=self.transa, transb=self.transb) return ret,
Example 22
def _arith_method_PANEL(op, name, str_rep=None, fill_zeros=None, default_axis=None, **eval_kwargs): # copied from Series na_op above, but without unnecessary branch for # non-scalar def na_op(x, y): try: result = expressions.evaluate(op, str_rep, x, y, raise_on_error=True, **eval_kwargs) except TypeError: # TODO: might need to find_common_type here? result = np.empty(len(x), dtype=x.dtype) mask = notnull(x) result[mask] = op(x[mask], y) result, changed = com._maybe_upcast_putmask(result, ~mask, np.nan) result = com._fill_zeros(result, x, y, name, fill_zeros) return result # work only for scalars def f(self, other): if not isscalar(other): raise ValueError('Simple arithmetic with %s can only be ' 'done with scalar values' % self._constructor.__name__) return self._combine(other, op) f.__name__ = name return f
Example 23
def test_find_common_type_boolean(self): # Ticket #1695 assert_(np.find_common_type([], ['?', '?']) == '?')
Example 24
def test_scalar_loses1(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['f8']) assert_(res == 'f4')
Example 25
def test_scalar_loses2(self): res = np.find_common_type(['f4', 'f4'], ['i8']) assert_(res == 'f4')
Example 26
def test_scalar_wins(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['c8']) assert_(res == 'c8')
Example 27
def test_scalar_wins3(self): # doesn't go up to 'f16' on purpose res = np.find_common_type(['u8', 'i8', 'i8'], ['f8']) assert_(res == 'f8')
Example 28
def test_where_type(self): # Test the type conservation with where x = np.arange(4, dtype=np.int32) y = np.arange(4, dtype=np.float32) * 2.2 test = where(x > 1.5, y, x).dtype control = np.find_common_type([np.int32, np.float32], []) assert_equal(test, control)
Example 29
def _common_dtype(x, y): """Determines common numpy DTYPE for arrays.""" dtype = np.find_common_type([x.dtype, y.dtype], []) if x.dtype != dtype: x = x.astype(dtype) if y.dtype != dtype: y = y.astype(dtype) return x, y
Example 30
def _common_dtype(x, y): dtype = np.find_common_type([x.dtype, y.dtype], []) if x.dtype != dtype: x = x.astype(dtype) if y.dtype != dtype: y = y.astype(dtype) return x, y
Example 31
def test_find_common_type_boolean(self): # Ticket #1695 assert_(np.find_common_type([], ['?', '?']) == '?')
Example 32
def test_scalar_loses1(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['f8']) assert_(res == 'f4')
Example 33
def test_scalar_loses2(self): res = np.find_common_type(['f4', 'f4'], ['i8']) assert_(res == 'f4')
Example 34
def test_scalar_wins(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['c8']) assert_(res == 'c8')
Example 35
def test_scalar_wins2(self): res = np.find_common_type(['u4', 'i4', 'i4'], ['f4']) assert_(res == 'f8')
Example 36
def test_where_type(self): # Test the type conservation with where x = np.arange(4, dtype=np.int32) y = np.arange(4, dtype=np.float32) * 2.2 test = where(x > 1.5, y, x).dtype control = np.find_common_type([np.int32, np.float32], []) assert_equal(test, control)
Example 37
def test_find_common_type_boolean(self): # Ticket #1695 assert_(np.find_common_type([], ['?', '?']) == '?')
Example 38
def test_scalar_loses1(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['f8']) assert_(res == 'f4')
Example 39
def test_scalar_loses2(self): res = np.find_common_type(['f4', 'f4'], ['i8']) assert_(res == 'f4')
Example 40
def test_scalar_wins(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['c8']) assert_(res == 'c8')
Example 41
def test_scalar_wins2(self): res = np.find_common_type(['u4', 'i4', 'i4'], ['f4']) assert_(res == 'f8')
Example 42
def test_where_type(self): # Test the type conservation with where x = np.arange(4, dtype=np.int32) y = np.arange(4, dtype=np.float32) * 2.2 test = where(x > 1.5, y, x).dtype control = np.find_common_type([np.int32, np.float32], []) assert_equal(test, control)
Example 43
def test_find_common_type_boolean(self): # Ticket #1695 assert_(np.find_common_type([], ['?', '?']) == '?')
Example 44
def test_scalar_loses1(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['f8']) assert_(res == 'f4')
Example 45
def test_scalar_loses2(self): res = np.find_common_type(['f4', 'f4'], ['i8']) assert_(res == 'f4')
Example 46
def test_scalar_wins(self): res = np.find_common_type(['f4', 'f4', 'i2'], ['c8']) assert_(res == 'c8')
Example 47
def test_scalar_wins2(self): res = np.find_common_type(['u4', 'i4', 'i4'], ['f4']) assert_(res == 'f8')
Example 48
def test_where_type(self): # Test the type conservation with where x = np.arange(4, dtype=np.int32) y = np.arange(4, dtype=np.float32) * 2.2 test = where(x > 1.5, y, x).dtype control = np.find_common_type([np.int32, np.float32], []) assert_equal(test, control)
Example 49
def directsum(tensors,labels,axes=()): ''' The directsum of a couple of tensors. Parameters ---------- tensors : list of Tensor The tensors to be directsummed. labels : list of Label The labels of the directsum. axes : list of integer, optional The axes along which the directsum is block diagonal. Returns ------- Tensor The directsum of the tensors. ''' for i,tensor in enumerate(tensors): if i==0: assert tensor.ndim>len(axes) ndim,qnon,shps=tensor.ndim,tensor.qnon,[tensor.shape[axis] for axis in axes] alters,shape,dtypes=set(xrange(ndim))-set(axes),list(tensor.shape),[tensor.dtype] else: assert tensor.ndim==ndim and tensor.qnon==qnon and [tensor.shape[axis] for axis in axes]==shps for alter in alters: shape[alter]+=tensor.shape[alter] dtypes.append(tensor.dtype) data=np.zeros(tuple(shape),dtype=np.find_common_type([],dtypes)) for i,tensor in enumerate(tensors): if i==0: slices=[slice(0,tensor.shape[axis]) if axis in alters else slice(None,None,None) for axis in xrange(ndim)] else: for alter in alters: slices[alter]=slice(slices[alter].stop,slices[alter].stop+tensor.shape[alter]) data[tuple(slices)]=tensor[...] if qnon: for alter in alters: labels[alter].qns=QuantumNumbers.union([tensor.labels[alter].qns for tensor in tensors]) for axis in axes: labels[axis].qns=next(iter(tensor)).labels[axis].qns return Tensor(data,labels=labels)
Example 50
def solve(A,b,rtol=10**-8): ''' Solve the matrix equation A*x=b by QR decomposition. Parameters ---------- A : 2d ndarray The coefficient matrix. b : 1d ndarray The ordinate values. rtol : np.float64 The relative tolerance of the solution. Returns ------- 1d ndarray The solution. Raises ------ LinAlgError When no solution exists. ''' assert A.ndim==2 nrow,ncol=A.shape if nrow>=ncol: result=np.zeros(ncol,dtype=np.find_common_type([],[A.dtype,b.dtype])) q,r=sl.qr(A,mode='economic',check_finite=False) temp=q.T.dot(b) for i,ri in enumerate(r[::-1]): result[-1-i]=(temp[-1-i]-ri[ncol-i:].dot(result[ncol-i:]))/ri[-1-i] else: temp=np.zeros(nrow,dtype=np.find_common_type([],[A.dtype,b.dtype])) q,r=sl.qr(dagger(A),mode='economic',check_finite=False) for i,ri in enumerate(dagger(r)): temp[i]=(b[i]-ri[:i].dot(temp[:i]))/ri[i] result=q.dot(temp) if not np.allclose(A.dot(result),b,rtol=rtol): raise sl.LinAlgError('solve error: no solution.') return result