コード例 #1
0
def test_nonzero():
    x, y = symbols('x,y')
    assert ask(x, Q.nonzero) == None
    assert ask(x, Q.nonzero, Assume(x, Q.real)) == None
    assert ask(x, Q.nonzero, Assume(x, Q.positive)) == True
    assert ask(x, Q.nonzero, Assume(x, Q.negative)) == True
    assert ask(x, Q.nonzero,
               Assume(x, Q.negative) | Assume(x, Q.positive)) == True

    assert ask(x + y, Q.nonzero) == None
    assert ask(x + y, Q.nonzero,
               Assume(x, Q.positive) & Assume(y, Q.positive)) == True
    assert ask(x + y, Q.nonzero,
               Assume(x, Q.positive) & Assume(y, Q.negative)) == None
    assert ask(x + y, Q.nonzero,
               Assume(x, Q.negative) & Assume(y, Q.negative)) == True

    assert ask(2 * x, Q.nonzero) == None
    assert ask(2 * x, Q.nonzero, Assume(x, Q.positive)) == True
    assert ask(2 * x, Q.nonzero, Assume(x, Q.negative)) == True
    assert ask(x * y, Q.nonzero, Assume(x, Q.nonzero)) == None
    assert ask(x * y, Q.nonzero,
               Assume(x, Q.nonzero) & Assume(y, Q.nonzero)) == True

    assert ask(Abs(x), Q.nonzero) == None
    assert ask(Abs(x), Q.nonzero, Assume(x, Q.nonzero)) == True
コード例 #2
0
ファイル: test_query.py プロジェクト: yuri-karadzhov/sympy
def test_positive():
    x, y, z, w = symbols('xyzw')
    assert ask(x, Q.positive, Assume(x, Q.positive)) == True
    assert ask(x, Q.positive, Assume(x, Q.negative)) == False
    assert ask(x, Q.positive, Assume(x, Q.nonzero)) == None

    assert ask(-x, Q.positive, Assume(x, Q.positive)) == False
    assert ask(-x, Q.positive, Assume(x, Q.negative)) == True

    assert ask(x+y, Q.positive, Assume(x, Q.positive) & \
                     Assume(y, Q.positive)) == True
    assert ask(x+y, Q.positive, Assume(x, Q.positive) & \
                     Assume(y, Q.negative)) == None

    assert ask(2*x,  Q.positive, Assume(x, Q.positive)) == True
    assumptions =  Assume(x, Q.positive) & Assume(y, Q.negative) & \
                    Assume(z, Q.negative) & Assume(w, Q.positive)
    assert ask(x*y*z,  Q.positive)  == None
    assert ask(x*y*z,  Q.positive, assumptions) == True
    assert ask(-x*y*z, Q.positive, assumptions) == False

    assert ask(x**2, Q.positive, Assume(x, Q.positive)) == True
    assert ask(x**2, Q.positive, Assume(x, Q.negative)) == True

    #exponential
    assert ask(exp(x),     Q.positive, Assume(x, Q.real)) == True
    assert ask(x + exp(x), Q.positive, Assume(x, Q.real)) == None

    #absolute value
    assert ask(Abs(x), Q.positive) == None # Abs(0) = 0
    assert ask(Abs(x), Q.positive, Assume(x, Q.positive)) == True
コード例 #3
0
def test_spinors():

    p1 = Symbol('p1', real=True)
    p2 = Symbol('p2', real=True)
    p3 = Symbol('p3', real=True)
    M  = Symbol('M',  real=True)
    p0 = sqrt(p1**2+p2**2+p3**2+M**2)

    dct = {var:uniform(1.,100.) for var in [p1,p2,p3,M]}
    
    # Check normalization of spinors: u*ubar = +2M, v*vbar = -2M
    assert(evaluate((uminus(p0,p1,p2,p3,'a')*uminusbar(p0,p1,p2,p3,'a') )._array[0].subs(dct)) - 2.0*M.subs(dct) < 1.e-11)
    assert(evaluate((vminus(p0,p1,p2,p3,'a')*vminusbar(p0,p1,p2,p3,'a') )._array[0].subs(dct)) + 2.0*M.subs(dct) < 1.e-11)
    assert(evaluate((uplus (p0,p1,p2,p3,'a')*uplusbar (p0,p1,p2,p3,'a') )._array[0].subs(dct)) - 2.0*M.subs(dct) < 1.e-11)
    assert(evaluate((vplus (p0,p1,p2,p3,'a')*vplusbar (p0,p1,p2,p3,'a') )._array[0].subs(dct)) + 2.0*M.subs(dct) < 1.e-11)

    # Check if spinors satisfy Dirac's equation
    dop_u = dirac_op([p0,p1,p2,p3], +M, 'a', 'b')
    dop_v = dirac_op([p0,p1,p2,p3], -M, 'a', 'b')

    d1 = dop_u *(uplus    (p0,p1,p2,p3,'b'))
    d2 = dop_u *(uminus   (p0,p1,p2,p3,'b'))
    d3 = dop_v *(vplus    (p0,p1,p2,p3,'b'))
    d4 = dop_v *(vminus   (p0,p1,p2,p3,'b'))

    for i in range(4):
        assert(Abs(evaluate(d1._array[i]._array[0].subs(dct))) < 1.e-11)
        assert(Abs(evaluate(d2._array[i]._array[0].subs(dct))) < 1.e-11)
        assert(Abs(evaluate(d3._array[i]._array[0].subs(dct))) < 1.e-11)
        assert(Abs(evaluate(d4._array[i]._array[0].subs(dct))) < 1.e-11)
コード例 #4
0
ファイル: test_query.py プロジェクト: lazovich/sympy
def test_positive():
    x, y, z, w = symbols('x,y,z,w')
    assert ask(Q.positive(x), Q.positive(x)) == True
    assert ask(Q.positive(x), Q.negative(x)) == False
    assert ask(Q.positive(x), Q.nonzero(x)) == None

    assert ask(Q.positive(-x), Q.positive(x)) == False
    assert ask(Q.positive(-x), Q.negative(x)) == True

    assert ask(Q.positive(x + y), Q.positive(x) & Q.positive(y)) == True
    assert ask(Q.positive(x + y), Q.positive(x) & Q.negative(y)) == None

    assert ask(Q.positive(2 * x), Q.positive(x)) == True
    assumptions = Q.positive(x) & Q.negative(y) & Q.negative(z) & Q.positive(w)
    assert ask(Q.positive(x * y * z)) == None
    assert ask(Q.positive(x * y * z), assumptions) == True
    assert ask(Q.positive(-x * y * z), assumptions) == False

    assert ask(Q.positive(x**2), Q.positive(x)) == True
    assert ask(Q.positive(x**2), Q.negative(x)) == True

    #exponential
    assert ask(Q.positive(exp(x)), Q.real(x)) == True
    assert ask(Q.positive(x + exp(x)), Q.real(x)) == None

    #absolute value
    assert ask(Q.positive(Abs(x))) == None  # Abs(0) = 0
    assert ask(Q.positive(Abs(x)), Q.positive(x)) == True
コード例 #5
0
def P_l_m(m, l, theta):
    """Legendre polynomial"""
    #    eq = diff(P_l(l,theta),cos(theta),Abs(m))
    eq = P_l(l, theta)
    resultdiff = diff(eq.subs(cos(theta), ib), ib, Abs(m))
    eq = resultdiff.subs(ib, cos(theta))
    result = sin(theta)**Abs(m) * eq
    return result
コード例 #6
0
ファイル: test_c.py プロジェクト: rivlakmaster/sympy
def test_C99CodePrinter__precision():
    n = symbols('n', integer=True)
    f32_printer = C99CodePrinter(dict(type_aliases={real: float32}))
    f64_printer = C99CodePrinter(dict(type_aliases={real: float64}))
    f80_printer = C99CodePrinter(dict(type_aliases={real: float80}))
    assert f32_printer.doprint(sin(x+2.1)) == 'sinf(x + 2.1F)'
    assert f64_printer.doprint(sin(x+2.1)) == 'sin(x + 2.1000000000000001)'
    assert f80_printer.doprint(sin(x+Float('2.0'))) == 'sinl(x + 2.0L)'

    for printer, suffix in zip([f32_printer, f64_printer, f80_printer], ['f', '', 'l']):
        def check(expr, ref):
            assert printer.doprint(expr) == ref.format(s=suffix, S=suffix.upper())
        check(Abs(n), 'abs(n)')
        check(Abs(x + 2.0), 'fabs{s}(x + 2.0{S})')
        check(sin(x + 4.0)**cos(x - 2.0), 'pow{s}(sin{s}(x + 4.0{S}), cos{s}(x - 2.0{S}))')
        check(exp(x*8.0), 'exp{s}(8.0{S}*x)')
        check(exp2(x), 'exp2{s}(x)')
        check(expm1(x*4.0), 'expm1{s}(4.0{S}*x)')
        check(Mod(n, 2), '((n) % (2))')
        check(Mod(2*n + 3, 3*n + 5), '((2*n + 3) % (3*n + 5))')
        check(Mod(x + 2.0, 3.0), 'fmod{s}(1.0{S}*x + 2.0{S}, 3.0{S})')
        check(Mod(x, 2.0*x + 3.0), 'fmod{s}(1.0{S}*x, 2.0{S}*x + 3.0{S})')
        check(log(x/2), 'log{s}((1.0{S}/2.0{S})*x)')
        check(log10(3*x/2), 'log10{s}((3.0{S}/2.0{S})*x)')
        check(log2(x*8.0), 'log2{s}(8.0{S}*x)')
        check(log1p(x), 'log1p{s}(x)')
        check(2**x, 'pow{s}(2, x)')
        check(2.0**x, 'pow{s}(2.0{S}, x)')
        check(x**3, 'pow{s}(x, 3)')
        check(x**4.0, 'pow{s}(x, 4.0{S})')
        check(sqrt(3+x), 'sqrt{s}(x + 3)')
        check(Cbrt(x-2.0), 'cbrt{s}(x - 2.0{S})')
        check(hypot(x, y), 'hypot{s}(x, y)')
        check(sin(3.*x + 2.), 'sin{s}(3.0{S}*x + 2.0{S})')
        check(cos(3.*x - 1.), 'cos{s}(3.0{S}*x - 1.0{S})')
        check(tan(4.*y + 2.), 'tan{s}(4.0{S}*y + 2.0{S})')
        check(asin(3.*x + 2.), 'asin{s}(3.0{S}*x + 2.0{S})')
        check(acos(3.*x + 2.), 'acos{s}(3.0{S}*x + 2.0{S})')
        check(atan(3.*x + 2.), 'atan{s}(3.0{S}*x + 2.0{S})')
        check(atan2(3.*x, 2.*y), 'atan2{s}(3.0{S}*x, 2.0{S}*y)')

        check(sinh(3.*x + 2.), 'sinh{s}(3.0{S}*x + 2.0{S})')
        check(cosh(3.*x - 1.), 'cosh{s}(3.0{S}*x - 1.0{S})')
        check(tanh(4.0*y + 2.), 'tanh{s}(4.0{S}*y + 2.0{S})')
        check(asinh(3.*x + 2.), 'asinh{s}(3.0{S}*x + 2.0{S})')
        check(acosh(3.*x + 2.), 'acosh{s}(3.0{S}*x + 2.0{S})')
        check(atanh(3.*x + 2.), 'atanh{s}(3.0{S}*x + 2.0{S})')
        check(erf(42.*x), 'erf{s}(42.0{S}*x)')
        check(erfc(42.*x), 'erfc{s}(42.0{S}*x)')
        check(gamma(x), 'tgamma{s}(x)')
        check(loggamma(x), 'lgamma{s}(x)')

        check(ceiling(x + 2.), "ceil{s}(x + 2.0{S})")
        check(floor(x + 2.), "floor{s}(x + 2.0{S})")
        check(fma(x, y, -z), 'fma{s}(x, y, -z)')
        check(Max(x, 8.0, x**4.0), 'fmax{s}(8.0{S}, fmax{s}(x, pow{s}(x, 4.0{S})))')
        check(Min(x, 2.0), 'fmin{s}(2.0{S}, x)')
コード例 #7
0
def test_ccode_user_functions():
    x = symbols("x", integer=False)
    n = symbols("n", integer=True)
    custom_functions = {
        "ceiling": "ceil",
        "Abs": [(lambda x: not x.is_integer, "fabs"), (lambda x: x.is_integer, "abs")],
    }
    assert ccode(ceiling(x), user_functions=custom_functions) == "ceil(x)"
    assert ccode(Abs(x), user_functions=custom_functions) == "fabs(x)"
    assert ccode(Abs(n), user_functions=custom_functions) == "abs(n)"
コード例 #8
0
def test_user_functions():
    x = symbols('x', integer=False)
    n = symbols('n', integer=True)
    custom_functions = {
        "ceiling": "ceil",
        "Abs": [(lambda x: not x.is_integer, "fabs", 4), (lambda x: x.is_integer, "abs", 4)],
    }
    assert rust_code(ceiling(x), user_functions=custom_functions) == "x.ceil()"
    assert rust_code(Abs(x), user_functions=custom_functions) == "fabs(x)"
    assert rust_code(Abs(n), user_functions=custom_functions) == "abs(n)"
コード例 #9
0
def Y_l_m(l,m,phi,theta):
    """Spherical harmonics"""
    eq = P_l_m(m,l,theta)
    if m>0:
        pe=re(exp(I*m*phi))*sqrt(2)
    elif m<0:
        pe=im(exp(I*m*phi))*sqrt(2)
    elif m==0:
        pe=1
    return abs(sqrt(((2*l+1)*fac(l-Abs(m)))/(4*pi*fac(l+Abs(m))))*pe*eq)
コード例 #10
0
ファイル: test_query.py プロジェクト: lazovich/sympy
def test_negative():
    x, y = symbols('x,y')
    assert ask(Q.negative(x), Q.negative(x)) == True
    assert ask(Q.negative(x), Q.positive(x)) == False
    assert ask(Q.negative(x), ~Q.real(x)) == False
    assert ask(Q.negative(x), Q.prime(x)) == False
    assert ask(Q.negative(x), ~Q.prime(x)) == None

    assert ask(Q.negative(-x), Q.positive(x)) == True
    assert ask(Q.negative(-x), ~Q.positive(x)) == None
    assert ask(Q.negative(-x), Q.negative(x)) == False
    assert ask(Q.negative(-x), Q.positive(x)) == True

    assert ask(Q.negative(x - 1), Q.negative(x)) == True
    assert ask(Q.negative(x + y)) == None
    assert ask(Q.negative(x + y), Q.negative(x)) == None
    assert ask(Q.negative(x + y), Q.negative(x) & Q.negative(y)) == True

    assert ask(Q.negative(x**2)) == None
    assert ask(Q.negative(x**2), Q.real(x)) == False
    assert ask(Q.negative(x**1.4), Q.real(x)) == None

    assert ask(Q.negative(x * y)) == None
    assert ask(Q.negative(x * y), Q.positive(x) & Q.positive(y)) == False
    assert ask(Q.negative(x * y), Q.positive(x) & Q.negative(y)) == True
    assert ask(Q.negative(x * y), Q.complex(x) & Q.complex(y)) == None

    assert ask(Q.negative(x**y)) == None
    assert ask(Q.negative(x**y), Q.negative(x) & Q.even(y)) == False
    assert ask(Q.negative(x**y), Q.negative(x) & Q.odd(y)) == True
    assert ask(Q.negative(x**y), Q.positive(x) & Q.integer(y)) == False

    assert ask(Q.negative(Abs(x))) == False
コード例 #11
0
ファイル: hyper.py プロジェクト: vishalbelsare/sympy
 def eval(cls, ap, bq, z):
     from sympy.functions.elementary.complexes import unpolarify
     if len(ap) <= len(bq) or (len(ap) == len(bq) + 1 and
                               (Abs(z) <= 1) == True):
         nz = unpolarify(z)
         if z != nz:
             return hyper(ap, bq, nz)
コード例 #12
0
ファイル: test_beam.py プロジェクト: yogesh1997/sympy
def test_variable_moment():
    E = Symbol('E')
    I = Symbol('I')

    b = Beam(4, E, 2*(4 - x))
    b.apply_load(20, 4, -1)
    R, M = symbols('R, M')
    b.apply_load(R, 0, -1)
    b.apply_load(M, 0, -2)
    b.bc_deflection = [(0, 0)]
    b.bc_slope = [(0, 0)]
    b.solve_for_reaction_loads(R, M)
    assert b.slope().expand() == ((10*x*SingularityFunction(x, 0, 0)
        - 10*(x - 4)*SingularityFunction(x, 4, 0))/E).expand()
    assert b.deflection().expand() == ((5*x**2*SingularityFunction(x, 0, 0)
        - 10*Piecewise((0, Abs(x)/4 < 1), (16*meijerg(((3, 1), ()), ((), (2, 0)), x/4), True))
        + 40*SingularityFunction(x, 4, 1))/E).expand()

    b = Beam(4, E - x, I)
    b.apply_load(20, 4, -1)
    R, M = symbols('R, M')
    b.apply_load(R, 0, -1)
    b.apply_load(M, 0, -2)
    b.bc_deflection = [(0, 0)]
    b.bc_slope = [(0, 0)]
    b.solve_for_reaction_loads(R, M)
    assert b.slope().expand() == ((-80*(-log(-E) + log(-E + x))*SingularityFunction(x, 0, 0)
        + 80*(-log(-E + 4) + log(-E + x))*SingularityFunction(x, 4, 0) + 20*(-E*log(-E)
        + E*log(-E + x) + x)*SingularityFunction(x, 0, 0) - 20*(-E*log(-E + 4) + E*log(-E + x)
        + x - 4)*SingularityFunction(x, 4, 0))/I).expand()
コード例 #13
0
ファイル: fancysets.py プロジェクト: quangpq/sympy
    def _contains(self, other):
        from sympy.functions import arg, Abs
        other = sympify(other)
        isTuple = isinstance(other, Tuple)
        if isTuple and len(other) != 2:
            raise ValueError('expecting Tuple of length 2')

        # If the other is not an Expression, and neither a Tuple
        if not isinstance(other, (Expr, Tuple)):
            return S.false
        # self in rectangular form
        if not self.polar:
            re, im = other if isTuple else other.as_real_imag()
            return fuzzy_or(fuzzy_and([
                pset.args[0]._contains(re),
                pset.args[1]._contains(im)])
                for pset in self.psets)

        # self in polar form
        elif self.polar:
            if other.is_zero:
                # ignore undefined complex argument
                return fuzzy_or(pset.args[0]._contains(S.Zero)
                    for pset in self.psets)
            if isTuple:
                r, theta = other
            else:
                r, theta = Abs(other), arg(other)
            if theta.is_real and theta.is_number:
                # angles in psets are normalized to [0, 2pi)
                theta %= 2*S.Pi
                return fuzzy_or(fuzzy_and([
                    pset.args[0]._contains(r),
                    pset.args[1]._contains(theta)])
                    for pset in self.psets)
コード例 #14
0
ファイル: fancysets.py プロジェクト: hridog00/Proyecto
    def _contains(self, other):
        from sympy.functions import arg, Abs
        from sympy.core.containers import Tuple
        other = sympify(other)
        isTuple = isinstance(other, Tuple)
        if isTuple and len(other) != 2:
            raise ValueError('expecting Tuple of length 2')

        # If the other is not an Expression, and neither a Tuple
        if not isinstance(other, Expr) and not isinstance(other, Tuple):
            return S.false
        # self in rectangular form
        if not self.polar:
            re, im = other if isTuple else other.as_real_imag()
            for element in self.psets:
                if And(element.args[0]._contains(re),
                       element.args[1]._contains(im)):
                    return True
            return False

        # self in polar form
        elif self.polar:
            if isTuple:
                r, theta = other
            elif other.is_zero:
                r, theta = S.Zero, S.Zero
            else:
                r, theta = Abs(other), arg(other)
            for element in self.psets:
                if And(element.args[0]._contains(r),
                       element.args[1]._contains(theta)):
                    return True
            return False
コード例 #15
0
def _solve_abs(f, symbol, domain):
    """ Helper function to solve equation involving absolute value function """
    if not domain.is_subset(S.Reals):
        raise ValueError(
            filldedent('''
            Absolute values cannot be inverted in the
            complex domain.'''))
    p, q, r = Wild('p'), Wild('q'), Wild('r')
    pattern_match = f.match(p * Abs(q) + r) or {}
    if not pattern_match.get(p, S.Zero).is_zero:
        f_p, f_q, f_r = pattern_match[p], pattern_match[q], pattern_match[r]
        q_pos_cond = solve_univariate_inequality(f_q >= 0,
                                                 symbol,
                                                 relational=False)
        q_neg_cond = solve_univariate_inequality(f_q < 0,
                                                 symbol,
                                                 relational=False)

        sols_q_pos = solveset_real(f_p * f_q + f_r,
                                   symbol).intersect(q_pos_cond)
        sols_q_neg = solveset_real(f_p * (-f_q) + f_r,
                                   symbol).intersect(q_neg_cond)
        return Union(sols_q_pos, sols_q_neg)
    else:
        return ConditionSet(symbol, Eq(f, 0), domain)
コード例 #16
0
 def eval(cls, ap, bq, z):
     from sympy import unpolarify
     if len(ap) <= len(bq) or (len(ap) == len(bq) + 1 and
                               (Abs(z) <= 1) == True):
         nz = unpolarify(z)
         if z != nz:
             return hyper(ap, bq, nz)
コード例 #17
0
ファイル: sympyextensions.py プロジェクト: mabau/pystencils
def extract_most_common_factor(term):
    """Processes a sum of fractions: determines the most common factor and splits term in common factor and rest"""
    coefficient_dict = term.as_coefficients_dict()
    counter = Counter([Abs(v) for v in coefficient_dict.values()])
    common_factor, occurrences = max(counter.items(), key=operator.itemgetter(1))
    if occurrences == 1 and (1 in counter):
        common_factor = 1
    return common_factor, term / common_factor
コード例 #18
0
def run():
    A, lam, x, om, t = symbols("A lam x om t", real=True)
    assumptions.assume.global_assumptions.add(Q.positive(lam))
    psi = Function('psi')(x)
    psi = A * exp(-lam * Abs(x) - I * om * t)
    ret = simplify(conjugate(psi) * psi)  # 简化
    print(ret)  # A**2*exp(-2*lam*Abs(x))

    ret = integrate(conjugate(psi) * psi, (x, -oo, oo))
    print(ret)  # (A**2/lam, Abs(arg(lam)) < pi/2)
    # 所以求得系数 A = sqrt(lam)

    psi = sqrt(lam) * exp(-lam * Abs(x) - I * om * t)

    # 已知波函数了,求解<x>和<x**2>
    print(integrate(conjugate(psi) * x * psi, (x, -oo, oo)))  # 0
    print(integrate(conjugate(psi) * x * x * psi, (x, -oo, oo)))  #
コード例 #19
0
def test_even():
    x, y, z, t = symbols('x,y,z,t')
    assert ask(x, Q.even) == None
    assert ask(x, Q.even, Assume(x, Q.integer)) == None
    assert ask(x, Q.even, Assume(x, Q.integer, False)) == False
    assert ask(x, Q.even, Assume(x, Q.rational)) == None
    assert ask(x, Q.even, Assume(x, Q.positive)) == None

    assert ask(2 * x, Q.even) == None
    assert ask(2 * x, Q.even, Assume(x, Q.integer)) == True
    assert ask(2 * x, Q.even, Assume(x, Q.even)) == True
    assert ask(2 * x, Q.even, Assume(x, Q.irrational)) == False
    assert ask(2 * x, Q.even, Assume(x, Q.odd)) == True
    assert ask(2 * x, Q.even, Assume(x, Q.integer, False)) == None
    assert ask(3 * x, Q.even, Assume(x, Q.integer)) == None
    assert ask(3 * x, Q.even, Assume(x, Q.even)) == True
    assert ask(3 * x, Q.even, Assume(x, Q.odd)) == False

    assert ask(x + 1, Q.even, Assume(x, Q.odd)) == True
    assert ask(x + 1, Q.even, Assume(x, Q.even)) == False
    assert ask(x + 2, Q.even, Assume(x, Q.odd)) == False
    assert ask(x + 2, Q.even, Assume(x, Q.even)) == True
    assert ask(7 - x, Q.even, Assume(x, Q.odd)) == True
    assert ask(7 + x, Q.even, Assume(x, Q.odd)) == True
    assert ask(x + y, Q.even, Assume(x, Q.odd) & Assume(y, Q.odd)) == True
    assert ask(x + y, Q.even, Assume(x, Q.odd) & Assume(y, Q.even)) == False
    assert ask(x + y, Q.even, Assume(x, Q.even) & Assume(y, Q.even)) == True

    assert ask(2 * x + 1, Q.even, Assume(x, Q.integer)) == False
    assert ask(2 * x * y, Q.even,
               Assume(x, Q.rational) & Assume(x, Q.rational)) == None
    assert ask(2 * x * y, Q.even,
               Assume(x, Q.irrational) & Assume(x, Q.irrational)) == None

    assert ask(x+y+z, Q.even, Assume(x, Q.odd) & Assume(y, Q.odd) & \
                     Assume(z, Q.even)) == True
    assert ask(x+y+z+t, Q.even, Assume(x, Q.odd) & Assume(y, Q.odd) & \
                     Assume(z, Q.even) & Assume(t, Q.integer)) == None

    assert ask(Abs(x), Q.even, Assume(x, Q.even)) == True
    assert ask(Abs(x), Q.even, Assume(x, Q.even, False)) == None
    assert ask(re(x), Q.even, Assume(x, Q.even)) == True
    assert ask(re(x), Q.even, Assume(x, Q.even, False)) == None
    assert ask(im(x), Q.even, Assume(x, Q.even)) == True
    assert ask(im(x), Q.even, Assume(x, Q.real)) == True
コード例 #20
0
def test_ccode_exceptions():
    assert ccode(gamma(x), standard='C99') == "tgamma(x)"
    assert 'not supported in c' in ccode(gamma(x), standard='C89').lower()
    assert ccode(ceiling(x)) == "ceil(x)"
    assert ccode(Abs(x)) == "fabs(x)"
    assert ccode(gamma(x)) == "tgamma(x)"
    r, s = symbols('r,s', real=True)
    assert ccode(Mod(ceiling(r), ceiling(s))) == "((ceil(r)) % (ceil(s)))"
    assert ccode(Mod(r, s)) == "fmod(r, s)"
コード例 #21
0
ファイル: test_query.py プロジェクト: lazovich/sympy
def test_even():
    x, y, z, t = symbols('x,y,z,t')
    assert ask(Q.even(x)) == None
    assert ask(Q.even(x), Q.integer(x)) == None
    assert ask(Q.even(x), ~Q.integer(x)) == False
    assert ask(Q.even(x), Q.rational(x)) == None
    assert ask(Q.even(x), Q.positive(x)) == None

    assert ask(Q.even(2 * x)) == None
    assert ask(Q.even(2 * x), Q.integer(x)) == True
    assert ask(Q.even(2 * x), Q.even(x)) == True
    assert ask(Q.even(2 * x), Q.irrational(x)) == False
    assert ask(Q.even(2 * x), Q.odd(x)) == True
    assert ask(Q.even(2 * x), ~Q.integer(x)) == None
    assert ask(Q.even(3 * x), Q.integer(x)) == None
    assert ask(Q.even(3 * x), Q.even(x)) == True
    assert ask(Q.even(3 * x), Q.odd(x)) == False

    assert ask(Q.even(x + 1), Q.odd(x)) == True
    assert ask(Q.even(x + 1), Q.even(x)) == False
    assert ask(Q.even(x + 2), Q.odd(x)) == False
    assert ask(Q.even(x + 2), Q.even(x)) == True
    assert ask(Q.even(7 - x), Q.odd(x)) == True
    assert ask(Q.even(7 + x), Q.odd(x)) == True
    assert ask(Q.even(x + y), Q.odd(x) & Q.odd(y)) == True
    assert ask(Q.even(x + y), Q.odd(x) & Q.even(y)) == False
    assert ask(Q.even(x + y), Q.even(x) & Q.even(y)) == True

    assert ask(Q.even(2 * x + 1), Q.integer(x)) == False
    assert ask(Q.even(2 * x * y), Q.rational(x) & Q.rational(x)) == None
    assert ask(Q.even(2 * x * y), Q.irrational(x) & Q.irrational(x)) == None

    assert ask(Q.even(x + y + z), Q.odd(x) & Q.odd(y) & Q.even(z)) == True
    assert ask(Q.even(x + y + z + t),
               Q.odd(x) & Q.odd(y) & Q.even(z) & Q.integer(t)) == None

    assert ask(Q.even(Abs(x)), Q.even(x)) == True
    assert ask(Q.even(Abs(x)), ~Q.even(x)) == None
    assert ask(Q.even(re(x)), Q.even(x)) == True
    assert ask(Q.even(re(x)), ~Q.even(x)) == None
    assert ask(Q.even(im(x)), Q.even(x)) == True
    assert ask(Q.even(im(x)), Q.real(x)) == True
コード例 #22
0
def coherent_state(n, alpha):
    """
    Returns <n|alpha> for the coherent states of 1D harmonic oscillator.
    See http://en.wikipedia.org/wiki/Coherent_states

    ``n``
        the "nodal" quantum number
    ``alpha``
        the eigen value of annihilation operator
    """

    return exp(- Abs(alpha)**2/2)*(alpha**n)/sqrt(factorial(n))
コード例 #23
0
ファイル: test_query.py プロジェクト: lazovich/sympy
def test_nonzero():
    x, y = symbols('x,y')
    assert ask(Q.nonzero(x)) == None
    assert ask(Q.nonzero(x), Q.real(x)) == None
    assert ask(Q.nonzero(x), Q.positive(x)) == True
    assert ask(Q.nonzero(x), Q.negative(x)) == True
    assert ask(Q.nonzero(x), Q.negative(x) | Q.positive(x)) == True

    assert ask(Q.nonzero(x + y)) == None
    assert ask(Q.nonzero(x + y), Q.positive(x) & Q.positive(y)) == True
    assert ask(Q.nonzero(x + y), Q.positive(x) & Q.negative(y)) == None
    assert ask(Q.nonzero(x + y), Q.negative(x) & Q.negative(y)) == True

    assert ask(Q.nonzero(2 * x)) == None
    assert ask(Q.nonzero(2 * x), Q.positive(x)) == True
    assert ask(Q.nonzero(2 * x), Q.negative(x)) == True
    assert ask(Q.nonzero(x * y), Q.nonzero(x)) == None
    assert ask(Q.nonzero(x * y), Q.nonzero(x) & Q.nonzero(y)) == True

    assert ask(Q.nonzero(Abs(x))) == None
    assert ask(Q.nonzero(Abs(x)), Q.nonzero(x)) == True
コード例 #24
0
def test_odd():
    x, y, z, t = symbols('x,y,z,t')
    assert ask(x, Q.odd) == None
    assert ask(x, Q.odd, Assume(x, Q.odd)) == True
    assert ask(x, Q.odd, Assume(x, Q.integer)) == None
    assert ask(x, Q.odd, Assume(x, Q.integer, False)) == False
    assert ask(x, Q.odd, Assume(x, Q.rational)) == None
    assert ask(x, Q.odd, Assume(x, Q.positive)) == None

    assert ask(-x, Q.odd, Assume(x, Q.odd)) == True

    assert ask(2 * x, Q.odd) == None
    assert ask(2 * x, Q.odd, Assume(x, Q.integer)) == False
    assert ask(2 * x, Q.odd, Assume(x, Q.odd)) == False
    assert ask(2 * x, Q.odd, Assume(x, Q.irrational)) == False
    assert ask(2 * x, Q.odd, Assume(x, Q.integer, False)) == None
    assert ask(3 * x, Q.odd, Assume(x, Q.integer)) == None

    assert ask(x / 3, Q.odd, Assume(x, Q.odd)) == None
    assert ask(x / 3, Q.odd, Assume(x, Q.even)) == None

    assert ask(x + 1, Q.odd, Assume(x, Q.even)) == True
    assert ask(x + 2, Q.odd, Assume(x, Q.even)) == False
    assert ask(x + 2, Q.odd, Assume(x, Q.odd)) == True
    assert ask(3 - x, Q.odd, Assume(x, Q.odd)) == False
    assert ask(3 - x, Q.odd, Assume(x, Q.even)) == True
    assert ask(3 + x, Q.odd, Assume(x, Q.odd)) == False
    assert ask(3 + x, Q.odd, Assume(x, Q.even)) == True
    assert ask(x + y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd)) == False
    assert ask(x + y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.even)) == True
    assert ask(x - y, Q.odd, Assume(x, Q.even) & Assume(y, Q.odd)) == True
    assert ask(x - y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd)) == False

    assert ask(x+y+z, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd) & \
                     Assume(z, Q.even)) == False
    assert ask(x+y+z+t, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd) & \
                     Assume(z, Q.even) & Assume(t, Q.integer)) == None

    assert ask(2 * x + 1, Q.odd, Assume(x, Q.integer)) == True
    assert ask(2 * x + y, Q.odd,
               Assume(x, Q.integer) & Assume(y, Q.odd)) == True
    assert ask(2 * x + y, Q.odd,
               Assume(x, Q.integer) & Assume(y, Q.even)) == False
    assert ask(2 * x + y, Q.odd,
               Assume(x, Q.integer) & Assume(y, Q.integer)) == None
    assert ask(x * y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.even)) == False
    assert ask(x * y, Q.odd, Assume(x, Q.odd) & Assume(y, Q.odd)) == True
    assert ask(2 * x * y, Q.odd,
               Assume(x, Q.rational) & Assume(x, Q.rational)) == None
    assert ask(2 * x * y, Q.odd,
               Assume(x, Q.irrational) & Assume(x, Q.irrational)) == None

    assert ask(Abs(x), Q.odd, Assume(x, Q.odd)) == True
コード例 #25
0
ファイル: test_c.py プロジェクト: liyanxp/NmercialExperiment
def test_ccode_exceptions():
    assert ccode(gamma(x), standard='C99') == "tgamma(x)"
    gamma_c89 = ccode(gamma(x), standard='C89')
    assert 'not supported in c' in gamma_c89.lower()
    gamma_c89 = ccode(gamma(x), standard='C89', allow_unknown_functions=False)
    assert 'not supported in c' in gamma_c89.lower()
    gamma_c89 = ccode(gamma(x), standard='C89', allow_unknown_functions=True)
    assert not 'not supported in c' in gamma_c89.lower()
    assert ccode(ceiling(x)) == "ceil(x)"
    assert ccode(Abs(x)) == "fabs(x)"
    assert ccode(gamma(x)) == "tgamma(x)"
    r, s = symbols('r,s', real=True)
    assert ccode(Mod(ceiling(r), ceiling(s))) == "((ceil(r)) % (ceil(s)))"
    assert ccode(Mod(r, s)) == "fmod(r, s)"
コード例 #26
0
def test_polvecs():
    
    # Outgoing gluon mom
    k1 = Symbol('k1', real=True)
    k2 = Symbol('k2', real=True)
    k3 = Symbol('k3', real=True)
    k0 = sqrt(k1**2+k2**2+k3**2)
    k  = tensor1d([k0,k1,k2,k3], 'nu')

    # Outgoing gluon reference mom
    g1 = Symbol('g1', real=True)
    g2 = Symbol('g2', real=True)
    g3 = Symbol('g3', real=True)
    g0 = sqrt(g1**2+g2**2+g3**2)

    dct = {var:uniform(1.,100.) for var in [k1,k2,k3,g1,g2,g3]}

    em = epsilonminus(k0,k1,k2,k3, 'mu', g0, g1, g2, g3)
    ep = epsilonplus (k0,k1,k2,k3, 'mu', g0, g1, g2, g3)

    # Test transversality wrt. gluon momentum
    assert(Abs(evaluate((em*mink_metric('mu','nu')*k)._array[0].subs(dct))) < 1.e-11)
    assert(Abs(evaluate((ep*mink_metric('mu','nu')*k)._array[0].subs(dct))) < 1.e-11)

    # Test complex conjugation relation between two helicity states
    for el in (em-conjugate_tensor1d(ep)).elements():
        assert(Abs(evaluate(el._array[0].subs(dct))) < 1.e-11)

    # Test the completeness relation: t and s should be equal
    t  = ep*conjugate_tensor1d(epsilonplus (k0,k1,k2,k3, 'nu', g0, g1, g2, g3))
    t += em*conjugate_tensor1d(epsilonminus(k0,k1,k2,k3, 'nu', g0, g1, g2, g3))

    s  = -mink_metric('mu','nu')
    s += (tensor1d([k0,k1,k2,k3],'mu')*tensor1d([g0,g1,g2,g3],'nu')+tensor1d([k0,k1,k2,k3],'nu')*tensor1d([g0,g1,g2,g3],'mu'))/(tensor1d([k0,k1,k2,k3],'nu')*mink_metric('mu','nu')*tensor1d([g0,g1,g2,g3],'mu'))

    for el in (t-s).elements():
        assert(Abs(evaluate(el._array[0].subs(dct))) < 1.e-11)
コード例 #27
0
ファイル: test_query.py プロジェクト: lazovich/sympy
def test_odd():
    x, y, z, t = symbols('x,y,z,t')
    assert ask(Q.odd(x)) == None
    assert ask(Q.odd(x), Q.odd(x)) == True
    assert ask(Q.odd(x), Q.integer(x)) == None
    assert ask(Q.odd(x), ~Q.integer(x)) == False
    assert ask(Q.odd(x), Q.rational(x)) == None
    assert ask(Q.odd(x), Q.positive(x)) == None

    assert ask(Q.odd(-x), Q.odd(x)) == True

    assert ask(Q.odd(2 * x)) == None
    assert ask(Q.odd(2 * x), Q.integer(x)) == False
    assert ask(Q.odd(2 * x), Q.odd(x)) == False
    assert ask(Q.odd(2 * x), Q.irrational(x)) == False
    assert ask(Q.odd(2 * x), ~Q.integer(x)) == None
    assert ask(Q.odd(3 * x), Q.integer(x)) == None

    assert ask(Q.odd(x / 3), Q.odd(x)) == None
    assert ask(Q.odd(x / 3), Q.even(x)) == None

    assert ask(Q.odd(x + 1), Q.even(x)) == True
    assert ask(Q.odd(x + 2), Q.even(x)) == False
    assert ask(Q.odd(x + 2), Q.odd(x)) == True
    assert ask(Q.odd(3 - x), Q.odd(x)) == False
    assert ask(Q.odd(3 - x), Q.even(x)) == True
    assert ask(Q.odd(3 + x), Q.odd(x)) == False
    assert ask(Q.odd(3 + x), Q.even(x)) == True
    assert ask(Q.odd(x + y), Q.odd(x) & Q.odd(y)) == False
    assert ask(Q.odd(x + y), Q.odd(x) & Q.even(y)) == True
    assert ask(Q.odd(x - y), Q.even(x) & Q.odd(y)) == True
    assert ask(Q.odd(x - y), Q.odd(x) & Q.odd(y)) == False

    assert ask(Q.odd(x + y + z), Q.odd(x) & Q.odd(y) & Q.even(z)) == False
    assert ask(Q.odd(x + y + z + t),
               Q.odd(x) & Q.odd(y) & Q.even(z) & Q.integer(t)) == None

    assert ask(Q.odd(2 * x + 1), Q.integer(x)) == True
    assert ask(Q.odd(2 * x + y), Q.integer(x) & Q.odd(y)) == True
    assert ask(Q.odd(2 * x + y), Q.integer(x) & Q.even(y)) == False
    assert ask(Q.odd(2 * x + y), Q.integer(x) & Q.integer(y)) == None
    assert ask(Q.odd(x * y), Q.odd(x) & Q.even(y)) == False
    assert ask(Q.odd(x * y), Q.odd(x) & Q.odd(y)) == True
    assert ask(Q.odd(2 * x * y), Q.rational(x) & Q.rational(x)) == None
    assert ask(Q.odd(2 * x * y), Q.irrational(x) & Q.irrational(x)) == None

    assert ask(Q.odd(Abs(x)), Q.odd(x)) == True
コード例 #28
0
def test_ccode_functions2():
    assert ccode(ceiling(x)) == "ceil(x)"
    assert ccode(Abs(x)) == "fabs(x)"
    assert ccode(gamma(x)) == "tgamma(x)"
    r, s = symbols('r,s', real=True)
    assert ccode(Mod(ceiling(r), ceiling(s))) == '((ceil(r) % ceil(s)) + '\
                                                 'ceil(s)) % ceil(s)'
    assert ccode(Mod(r, s)) == "fmod(r, s)"
    p1, p2 = symbols('p1 p2', integer=True, positive=True)
    assert ccode(Mod(p1, p2)) == 'p1 % p2'
    assert ccode(Mod(p1, p2 + 3)) == 'p1 % (p2 + 3)'
    assert ccode(Mod(-3, -7, evaluate=False)) == '(-3) % (-7)'
    assert ccode(-Mod(3, 7, evaluate=False)) == '-(3 % 7)'
    assert ccode(r * Mod(p1, p2)) == 'r*(p1 % p2)'
    assert ccode(Mod(p1, p2)**s) == 'pow(p1 % p2, s)'
    n = symbols('n', integer=True, negative=True)
    assert ccode(Mod(-n, p2)) == '(-n) % p2'
コード例 #29
0
def _solve_abs(f, symbol):
    """ Helper function to solve equation involving absolute value function """
    p, q, r = Wild('p'), Wild('q'), Wild('r')
    pattern_match = f.match(p*Abs(q) + r) or {}
    if not pattern_match.get(p, S.Zero).is_zero:
        f_p, f_q, f_r = pattern_match[p], pattern_match[q], pattern_match[r]
        q_pos_cond = solve_univariate_inequality(f_q >= 0, symbol,
                                                 relational=False)
        q_neg_cond = solve_univariate_inequality(f_q < 0, symbol,
                                                 relational=False)

        sols_q_pos = solveset_real(f_p*f_q + f_r,
                                           symbol).intersect(q_pos_cond)
        sols_q_neg = solveset_real(f_p*(-f_q) + f_r,
                                           symbol).intersect(q_neg_cond)
        return Union(sols_q_pos, sols_q_neg)
    else:
        return ConditionSet(symbol, Eq(f, 0), S.Complexes)
コード例 #30
0
ファイル: solveset.py プロジェクト: shahrk/sympy
def _invert_complex(f, g_ys, symbol):
    """ Helper function for invert_complex """

    if not f.has(symbol):
        raise ValueError("Inverse of constant function doesn't exist")

    if f is symbol:
        return (f, g_ys)

    n = Dummy('n')
    if f.is_Add:
        # f = g + h
        g, h = f.as_independent(symbol)
        if g != S.Zero:
            return _invert_complex(h, imageset(Lambda(n, n - g), g_ys), symbol)

    if f.is_Mul:
        # f = g*h
        g, h = f.as_independent(symbol)

        if g != S.One:
            return _invert_complex(h, imageset(Lambda(n, n / g), g_ys), symbol)

    if hasattr(f, 'inverse') and \
       not isinstance(f, TrigonometricFunction) and \
       not isinstance(f, exp):
        if len(f.args) > 1:
            raise ValueError("Only functions with one argument are supported.")
        return _invert_complex(f.args[0],
                               imageset(Lambda(n,
                                               f.inverse()(n)), g_ys), symbol)

    if isinstance(f, exp):
        if isinstance(g_ys, FiniteSet):
            exp_invs = Union(*[
                imageset(
                    Lambda(n,
                           I * (2 * n * pi + arg(g_y)) +
                           log(Abs(g_y))), S.Integers) for g_y in g_ys
                if g_y != 0
            ])
            return _invert_complex(f.args[0], exp_invs, symbol)
    return (f, g_ys)