示例#1
0
def test_real(self):
    z0 = Zero()
    z1 = as_ufl(1.0)
    z2 = ComplexValue(1j)
    z3 = ComplexValue(1+1j)
    assert Real(z1) == z1
    assert Real(z3) == z1
    assert Real(z2) == z0
示例#2
0
 def __new__(cls, argument):
     if isinstance(argument, (RealValue, Zero, numbers.Real)):
         if float(argument) < 0:
             return ComplexValue(cmath.sqrt(complex(argument)))
         else:
             return FloatValue(math.sqrt(float(argument)))
     if isinstance(argument, (ComplexValue, complex)):
         return ComplexValue(cmath.sqrt(complex(argument)))
     return MathFunction.__new__(cls)
示例#3
0
def test_complex(self):
    f1 = as_ufl(1 + 1j)
    f2 = as_ufl(1)
    f3 = as_ufl(1j)
    f4 = ComplexValue(1 + 1j)
    f5 = ComplexValue(1.0 + 1.0j)
    f6 = as_ufl(1.0)
    f7 = as_ufl(1.0j)

    assert f1 == f1
    assert f1 == f4
    assert f1 == f5  # ComplexValue uses floats
    assert f1 == f2 + f3  # Type promotion of IntValue to ComplexValue with arithmetic
    assert f4 == f2 + f3
    assert f5 == f2 + f3
    assert f4 == f5
    assert f6 + f7 == f2 + f3
示例#4
0
def test_imag(self):
    z0 = Zero()
    z1 = as_ufl(1.0)
    z2 = as_ufl(1j)
    z3 = ComplexValue(1+1j)

    assert Imag(z2) == z1
    assert Imag(z3) == z1
    assert Imag(z1) == z0
示例#5
0
 def __new__(cls, argument):
     if isinstance(argument, (RealValue, Zero)):
         erf = _find_erf()
         if erf is not None:
             return FloatValue(erf(float(argument)))
     if isinstance(argument, (ConstantValue)):
         erf = _find_erf()
         if erf is not None:
             return ComplexValue(erf(complex(argument)))
     return MathFunction.__new__(cls)
示例#6
0
def test_complex_algebra(self):
    z1 = ComplexValue(1j)
    z2 = ComplexValue(1+1j)

    # Remember that ufl.algebra functions return ComplexValues, but ufl.mathfunctions return complex Python scalar
    # Any operations with a ComplexValue and a complex Python scalar promote to ComplexValue
    assert z1*z2 == ComplexValue(-1+1j)
    assert z2/z1 == ComplexValue(1-1j)
    assert pow(z2, z1) == ComplexValue((1+1j)**1j)
    assert sqrt(z2) * as_ufl(1) == ComplexValue(cmath.sqrt(1+1j))
    assert ((sin(z2) + cosh(z2) - atan(z2)) * z1) == ComplexValue((cmath.sin(1+1j) + cmath.cosh(1+1j) - cmath.atan(1+1j))*1j)
    assert (abs(z2) - ln(z2))/exp(z1) == ComplexValue((abs(1+1j) - cmath.log(1+1j))/cmath.exp(1j))
示例#7
0
 def __new__(cls, argument):
     if isinstance(argument, (RealValue, Zero)):
         return FloatValue(math.exp(float(argument)))
     if isinstance(argument, (ComplexValue)):
         return ComplexValue(cmath.exp(complex(argument)))
     return MathFunction.__new__(cls)
示例#8
0
def test_conj(self):
    z1 = ComplexValue(1+2j)
    z2 = ComplexValue(1-2j)

    assert z1 == Conj(z2)
    assert z2 == Conj(z1)