我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用types.ComplexType()。
def assertEquals( exp, got ): """assertEquals(exp, got) Two objects test as "equal" if: * they are the same object as tested by the 'is' operator. * either object is a float or complex number and the absolute value of the difference between the two is less than 1e-8. * applying the equals operator ('==') returns True. """ from types import FloatType, ComplexType if exp is got: r = True elif ( type( exp ) in ( FloatType, ComplexType ) or type( got ) in ( FloatType, ComplexType ) ): r = abs( exp - got ) < 1e-8 else: r = ( exp == got ) if not r: print >>sys.stderr, "Error: expected <%s> but got <%s>" % ( repr( exp ), repr( got ) ) traceback.print_stack()
def assertEquals( exp, got, msg = None ): """assertEquals( exp, got[, message] ) Two objects test as "equal" if: * they are the same object as tested by the 'is' operator. * either object is a float or complex number and the absolute value of the difference between the two is less than 1e-8. * applying the equals operator ('==') returns True. """ if exp is got: r = True elif ( type( exp ) in ( FloatType, ComplexType ) or type( got ) in ( FloatType, ComplexType ) ): r = abs( exp - got ) < 1e-8 else: r = ( exp == got ) if not r: print >>sys.stderr, "Error: expected <%s> but got <%s>%s" % ( repr( exp ), repr( got ), colon( msg ) ) traceback.print_stack()
def is_scalar_element(self, x): """Is x a scalar By default a scalar is an element in the complex number field. A class that wants to perform linear algebra on things other than numbers may override this function. """ return isinstance(x, types.IntType) or \ isinstance(x, types.FloatType) or \ isinstance(x, types.ComplexType)
def is_probably_safe(x): ''' Objects are probably "safe" (unlikly to change) if they are -- immutable -- functions -- modules Obviously, the latter two may change, but in practice it is likely ok. Still, risky! ''' if is_immutable(x): return True if sys.version_info > (3,): probably_fine = (\ types.LambdaType, types.BuiltinMethodType, types.BuiltinFunctionType, types.FunctionType, types.ModuleType, types.MethodType) else: probably_fine = (\ types.LambdaType, types.InstanceType, types.NoneType, types.NotImplementedType, types.TypeType, types.UnicodeType, types.ComplexType, types.ClassType, types.BuiltinMethodType, types.BuiltinFunctionType, types.FunctionType, types.ModuleType, types.MethodType) if type(x) in probably_fine: return True if hasattr(x,'__call__'): return True return False
def displayNumType(num): print num, "is", if type(num) is types.IntType: # or : == types.IntType: print 'an integer' elif type(num) is long: print 'a long' elif type(num) is float: print 'a float' elif type(num) is types.ComplexType: print 'a complex number' else: print 'not a number at all!!'
def realpolyroots(*cs): """returns the roots of a polynom with given coefficients polynomial with coefficients given in cs: 0 = \sum_i cs[i] * x^(len(cs)-i-1) """ if not cs: return [0] try: f = 1.0/cs[0] cs = [f*c for c in cs[1:]] except ArithmeticError: return realpolyroots(*cs[1:]) else: n = len(cs) if n == 0: return [] elif n == 1: return [-cs[0]] elif n == 2: return _realroots_quadratic(*cs) elif n == 3: return _realroots_cubic(*cs) elif n == 4: return _realroots_quartic(*cs) else: raise RuntimeError("realpolyroots solver currently limited to polynoms up to the power of 4") # def realpolyroots_eigenvalue(*cs): # # as realpolyroots but using an equivalent eigenvalue problem # # (this code is currently used for functional tests only) # if not _has_numeric: # raise RuntimeError("realpolyroots_eigenvalue depends on Numeric") # if not cs: # return [0] # try: # f = 1.0/cs[0] # cs = [f*c for c in cs[1:]] # except ArithmeticError: # return realpolyroots_eigenvalue(*cs[1:]) # else: # if not cs: # return [] # n = len(cs) # a = Numeric.zeros((n, n), Numeric.Float) # for i in range(n-1): # a[i+1][i] = 1 # for i in range(n): # a[0][i] = -cs[i] # rs = [] # for r in LinearAlgebra.eigenvalues(a): # if type(r) == types.ComplexType: # if not r.imag: # rs.append(r.real) # else: # rs.append(r) # return rs #