def test_isfinite(self):
     real_vals = [float('-inf'), -2.3, -0.0,
                  0.0, 2.3, float('inf'), float('nan')]
     for x in real_vals:
         for y in real_vals:
             z = complex(x, y)
             self.assertEqual(cmath.isfinite(z),
                               math.isfinite(x) and math.isfinite(y))
Beispiel #2
0
def f2bin(f):
    if not cmath.isfinite(f):
        return repr(f)
    sign = '-' * (copysign(1.0, f) < 0)
    frac, fint = modf(fabs(f))
    n, d = frac.as_integer_ratio()
    assert d & (d - 1) == 0
    return f'{sign}{floor(fint):b}.{n:0{d.bit_length()-1}b}'
Beispiel #3
0
 def test_isfinite(self):
     real_vals = [float('-inf'), -2.3, -0.0,
                  0.0, 2.3, float('inf'), float('nan')]
     for x in real_vals:
         for y in real_vals:
             z = complex(x, y)
             self.assertEqual(cmath.isfinite(z),
                               math.isfinite(x) and math.isfinite(y))
Beispiel #4
0
    def test_isfinite(self):
        import cmath
        import math

        real_vals = [
            float('-inf'), -2.3, -0.0, 0.0, 2.3, float('inf'), float('nan')
        ]
        for x in real_vals:
            for y in real_vals:
                z = complex(x, y)
                assert cmath.isfinite(z) == (math.isfinite(x) and math.isfinite(y))
Beispiel #5
0
    def test_isfinite(self):
        import cmath
        import math

        real_vals = [
            float('-inf'), -2.3, -0.0, 0.0, 2.3, float('inf'), float('nan')
        ]
        for x in real_vals:
            for y in real_vals:
                z = complex(x, y)
                assert cmath.isfinite(z) == (math.isfinite(x) and math.isfinite(y))
Beispiel #6
0
def isfinite(x, /):
    '''Return True if x is neither an infinity nor a NaN and False otherwise'''
    try:
        return _math.isfinite(x)
    except Exception:
        pass
    try:
        return _cmath.isfinite(x)
    except Exception:
        pass

    # allow for customized types
    try:
        return x.isfinite()
    except Exception:
        pass
    try:
        return not (isnan(x) or isinf(x))
    except Exception:
        pass
    raise TypeError(f'invalid type, type {type(x).__name__}')
Beispiel #7
0
def isfinite_usecase(x):
    return cmath.isfinite(x)
# 常量
c.tau # 2pi
c.e  
c.pi
c.inf
c.infj #实部为0,虚部为无穷
c.nan

c.sqrt
c.exp
c.log(x,base=e)
c.log10(x)

c.isclose(a,b,rel_tol=1e-9,abs_tol=0) #判断a和b是否足够近,rel_tol为相对公差, abs_tol绝对公差
c.isfinite(z) #real 和image都为有限
c.isinf  #real 和image有一个为无穷
c.isnan

c.phase(z:complex)->float #返回辐角 phase 相位角
c.polar(z:complex) ->tuple[float,float] #直角坐标转换为极坐标
c.rect(r:float,phi:float)->complex  #极坐标转换直角坐标

# 三角函数
c.acos
c.asin
c.atan
c.cos
c.sin
c.tan
print('sine =', cmath.sin(c))
print('cosine =', cmath.cos(c))
print('tangent =', cmath.tan(c))

# hyperbolic functions
c = 2 + 2j
print('inverse hyperbolic sine =', cmath.asinh(c))
print('inverse hyperbolic cosine =', cmath.acosh(c))
print('inverse hyperbolic tangent =', cmath.atanh(c))

print('hyperbolic sine =', cmath.sinh(c))
print('hyperbolic cosine =', cmath.cosh(c))
print('hyperbolic tangent =', cmath.tanh(c))

# classification functions
print(cmath.isfinite(2 + 2j))  # True
print(cmath.isfinite(cmath.inf + 2j))  # False

print(cmath.isinf(2 + 2j))  # False
print(cmath.isinf(cmath.inf + 2j))  # True
print(cmath.isinf(cmath.nan + 2j))  # False


print(cmath.isnan(2 + 2j))  # False
print(cmath.isnan(cmath.inf + 2j))  # False
print(cmath.isnan(cmath.nan + 2j))  # True

print(cmath.isclose(2+2j, 2.01+1.9j, rel_tol=0.05))  # True
print(cmath.isclose(2+2j, 2.01+1.9j, abs_tol=0.005))  # False
Beispiel #10
0
            # we only *increment* the col index, so we have to reset it each row
            col = 0
            for x in range(0, width):
                if col != cellcount - 1 and x == xticks[col]:
                    col += 1

                # z_0 is based on x and y coords
                # remember, stgX and stgY take columns and rows into account
                z_p = z = stgX(x) + graphy
                i = 0
                # smooth coloring using exponents????
                color = math.exp(-abs(z))
                # iterate, breaking if we exceed the orbit threshold
                for i in range(0, iterations):
                    z = eval_fn(z, cgrid[col][row])
                    if cmath.isnan(z) or not cmath.isfinite(z):
                        # oh no
                        # i can blame this on the user right?
                        z = z_p
                        break
                    elif abs(z) > cutoff:
                        break
                    z_p = z
                    color += math.exp(-abs(z))

                # color is in the range of [0, iterations], so we have to
                # normalize it
                color /= iterations
                # we have way more magnitude info than can be expressed with 255
                # discrete values, so we oscillate light and dark as magnitude
                # increases, for a variety of colors. by scaling the blue
Beispiel #11
0
def isfinite_usecase(x):
    return cmath.isfinite(x)
Beispiel #12
0
def test_isfinite():
    assert not isfinite(sympy.oo)
    assert not isfinite(-sympy.oo)
    assert not isfinite(sympy.nan)
    if hasattr(cmath, 'isfinite'):
        assert cmath.isfinite(float('inf')) == isfinite(float('inf'))
        assert cmath.isfinite(-float('inf')) == isfinite(-float('inf'))
        assert cmath.isfinite(float('nan')) == isfinite(float('nan'))
        assert cmath.isfinite(1.0) == isfinite(1.0)
        assert cmath.isfinite(0) == isfinite(0)
        inf = float('inf')
        nan = float('nan')
        assert cmath.isfinite(complex(inf)) == isfinite(complex(inf))
        assert cmath.isfinite(complex(1, inf)) == isfinite(complex(1, inf))
        assert cmath.isfinite(complex(1, -inf)) == isfinite(complex(1, -inf))
        assert cmath.isfinite(complex(inf, 1)) == isfinite(complex(inf, 1))
        assert cmath.isfinite(complex(inf, inf)) == isfinite(complex(inf, inf))
        assert cmath.isfinite(complex(1, nan)) == isfinite(complex(1, nan))
        assert cmath.isfinite(complex(nan, 1)) == isfinite(complex(nan, 1))
        assert cmath.isfinite(complex(nan, nan)) == isfinite(complex(nan, nan))
        assert cmath.isfinite(complex(inf, nan)) == isfinite(complex(inf, nan))
        assert cmath.isfinite(complex(nan, inf)) == isfinite(complex(nan, inf))
        assert cmath.isfinite(complex(1, 2)) == cmath.isfinite(complex(1, 2))
    else:
        assert not isfinite(float('inf'))
        assert not isfinite(-float('inf'))
        assert not isfinite(float('nan'))
        assert isfinite(1.0)
        assert isfinite(0)
        inf = float('inf')
        nan = float('nan')
        assert not isfinite(complex(inf))
        assert not isfinite(complex(1, inf))
        assert not isfinite(complex(1, -inf))
        assert not isfinite(complex(inf, 1))
        assert not isfinite(complex(inf, inf))
        assert not isfinite(complex(1, nan))
        assert not isfinite(complex(nan, 1))
        assert not isfinite(complex(nan, nan))
        assert not isfinite(complex(inf, nan))
        assert not isfinite(complex(nan, inf))
        assert isfinite(complex(1, 2))
Beispiel #13
0
print('arc tangent =', cmath.atan(c))

print('sine =', cmath.sin(c))
print('cosine =', cmath.cos(c))
print('tangent =', cmath.tan(c))

# hyperbolic functions
c = 2 + 2j
print('inverse hyperbolic sine =', cmath.asinh(c))
print('inverse hyperbolic cosine =', cmath.acosh(c))
print('inverse hyperbolic tangent =', cmath.atanh(c))

print('hyperbolic sine =', cmath.sinh(c))
print('hyperbolic cosine =', cmath.cosh(c))
print('hyperbolic tangent =', cmath.tanh(c))

# classification functions
print(cmath.isfinite(2 + 2j))  # True
print(cmath.isfinite(cmath.inf + 2j))  # False

print(cmath.isinf(2 + 2j))  # False
print(cmath.isinf(cmath.inf + 2j))  # True
print(cmath.isinf(cmath.nan + 2j))  # False

print(cmath.isnan(2 + 2j))  # False
print(cmath.isnan(cmath.inf + 2j))  # False
print(cmath.isnan(cmath.nan + 2j))  # True

print(cmath.isclose(2 + 2j, 2.01 + 1.9j, rel_tol=0.05))  # True
print(cmath.isclose(2 + 2j, 2.01 + 1.9j, abs_tol=0.005))  # False
Beispiel #14
0
import cmath
import math

x = 1.0
y = 1.0
a = math.inf
b = math.nan

z = complex(x, y)
w = complex(x, a)
v = complex(x, b)

# Check if both numbers are finite
if cmath.isfinite(z):
    print('Complex number is finite')
else:
    print('Complex number is infinite')

if cmath.isinf(w):
    print('Complex number is infinite')
else:
    print('Complex number is finite')

if cmath.isnan(v):
    print('Complex number is NaN')
else:
    print('Complex number is not NaN')
Beispiel #15
0
    def execute_pcode(z, code, const_tab):
        MAX_STACK: int32 = 1024
        stack = np.empty(MAX_STACK, dtype=complex64)

        sp: int32 = 0
        pc: int32 = 0
        cc: int8 = code[pc]
        zero: complex64 = 0 + 0j

        while cc != SEND:
            if cc == SPUSHC:
                stack[sp] = const_tab[code[pc + 1]]
                sp += 1
                pc += 1
            elif cc == SPUSHZ:
                stack[sp] = z
                sp += 1
            elif cc == SADD:
                sp -= 2
                stack[sp] += stack[sp + 1]
                sp += 1
            elif cc == SSUB:
                sp -= 2
                stack[sp] -= stack[sp + 1]
                sp += 1
            elif cc == SMUL:
                sp -= 2
                stack[sp] *= stack[sp + 1]
                sp += 1
            elif cc == SDIV:
                sp -= 2
                stack[sp] = stack[sp] / stack[
                    sp + 1] if stack[sp + 1] != zero and isfinite(
                        stack[sp + 1]) and isfinite(stack[sp]) else zero
                sp += 1

            elif cc == SPOW:
                sp -= 2
                stack[sp] = stack[sp]**stack[sp + 1]
                sp += 1

            elif cc == SNEG:
                stack[sp - 1] = -stack[sp - 1]
            elif cc == SSIN:
                stack[sp - 1] = sin(stack[sp - 1])
            elif cc == SCOS:
                stack[sp - 1] = cos(stack[sp - 1])
            elif cc == STAN:
                stack[sp - 1] = tan(stack[sp - 1])
            elif cc == SASIN:
                stack[sp - 1] = asin(stack[sp - 1])
            elif cc == SACOS:
                stack[sp - 1] = acos(stack[sp - 1])
            elif cc == SATAN:
                stack[sp - 1] = atan(stack[sp - 1])
            elif cc == SLOG:
                stack[sp -
                      1] = log(stack[sp -
                                     1]) if stack[sp - 1] != zero else zero
            elif cc == SEXP:
                stack[sp - 1] = exp(stack[sp - 1])

            pc += 1
            cc = code[pc]

        return stack[0]
#3.log10()- This function returns log base 10 of a complex number.
print(cmath.log10(c))

#4.sqrt()-This function returns the square root of a complex number.
import cmath
a = 1.0
b = 1.0
c = complex(a, b)
print(cmath.sqrt(c))

#5.isfinite()- Returns true if both real and imaginary part of complex number are finite, else returns false.

x = 1.0
y = 1.0
z = complex(x, y)
if (cmath.isfinite(z)):
    print('True')
else:
    print('False')

#6.isinf()- Returns True if either of real or complex part is infinite.
import math
x = 1.0
y = math.inf
a = complex(x, y)
if (cmath.isinf(a)):
    print('True')
else:
    print('False')

#7.isnan()- Returns True if either of real or complex number is nan.