Exemple #1
0
def test_issue_6194():
    x = Symbol('x')
    assert unchanged(Contains, x, Interval(0, 1))
    assert Interval(0, 1).contains(x) == (S.Zero <= x) & (x <= 1)
    assert Contains(x, FiniteSet(0)) != S.false
    assert Contains(x, Interval(1, 1)) != S.false
    assert Contains(x, S.Integers) != S.false
Exemple #2
0
def prove(Eq):
    n = Symbol.n(integer=True, positive=True)
    x = Symbol.x(complex=True, shape=(n, ))
    f = Function.f(nargs=(n, ), integer=True, shape=())
    g = Function.g(nargs=(n, ), integer=True, shape=())
    A = Symbol.A(definition=conditionset(x, Equality(f(x), 1)))
    B = Symbol.B(definition=conditionset(x, Equality(g(x), 1)))

    assert f.is_integer and g.is_integer
    assert f.shape == g.shape == ()

    Eq << apply(ForAll[x:A](Equality(g(x), 1)), ForAll[x:B](Equality(f(x), 1)))
    Eq << sets.imply.conditionset.apply(A)

    Eq << sets.imply.conditionset.apply(B)

    Eq << ForAll[x:A](Contains(x, B), plausible=True)

    Eq << Eq[-1].definition

    Eq << ForAll[x:B](Contains(x, A), plausible=True)

    Eq << Eq[-1].definition

    Eq << sets.forall_contains.forall_contains.imply.equality.apply(
        Eq[-2], Eq[-1])
Exemple #3
0
def test_GammaProcess_symbolic():
    t, d, x, y, g, l = symbols('t d x y g l', positive=True)
    X = GammaProcess("X", l, g)

    raises(NotImplementedError, lambda: X[t])
    raises(IndexError, lambda: X(-1))
    assert isinstance(X(t), RandomIndexedSymbol)
    assert X.state_space == Interval(0, oo)
    assert X.distribution(t) == GammaDistribution(g * t, 1 / l)
    assert X.joint_distribution(5, X(3)) == JointDistributionHandmade(
        Lambda(
            (X(5), X(3)),
            l**(8 * g) * exp(-l * X(3)) * exp(-l * X(5)) * X(3)**(3 * g - 1) *
            X(5)**(5 * g - 1) / (gamma(3 * g) * gamma(5 * g))))
    # property of the gamma process at any given timestamp
    assert E(X(t)) == g * t / l
    assert variance(X(t)).simplify() == g * t / l**2

    # Equivalent to E(2*X(1)) + E(X(1)**2) + E(X(1)**3), where E(X(1)) == g/l
    assert E(X(t)**2 + X(d)*2 + X(y)**3, Contains(t, Interval.Lopen(0, 1))
        & Contains(d, Interval.Lopen(1, 2)) & Contains(y, Interval.Ropen(3, 4))) == \
            2*g/l + (g**2 + g)/l**2 + (g**3 + 3*g**2 + 2*g)/l**3

    assert P(X(t) > 3, Contains(t, Interval.Lopen(3, 4))).simplify() == \
                                1 - lowergamma(g, 3*l)/gamma(g) # equivalent to P(X(1)>3)

    #test issue 20078
    assert (2 * X(t) + 3 * X(t)).simplify() == 5 * X(t)
    assert (2 * X(t) - 3 * X(t)).simplify() == -X(t)
    assert (2 * (0.25 * X(t))).simplify() == 0.5 * X(t)
    assert (2 * X(t) * 0.25 * X(t)).simplify() == 0.5 * X(t)**2
    assert (X(t)**2 + X(t)**3).simplify() == (X(t) + 1) * X(t)**2
Exemple #4
0
def test_contains_basic():
    raises(TypeError, lambda: Contains(S.Integers, 1))
    assert Contains(2, S.Integers) is S.true
    assert Contains(-2, S.Naturals) is S.false

    i = Symbol('i', integer=True)
    assert Contains(i, S.Naturals) == Contains(i, S.Naturals, evaluate=False)
Exemple #5
0
def prove(Eq):
    n = Symbol.n(integer=True, positive=True)
    m = Symbol.m(integer=True, positive=True)
    A = Symbol.A(dtype=dtype.integer * n)
    a = Symbol.a(integer=True, shape=(n, ))
    B = Symbol.B(dtype=dtype.integer * m)
    b = Symbol.b(integer=True, shape=(m, ))

    f = Function.f(nargs=(n, ), integer=True, shape=(m, ))
    g = Function.g(nargs=(m, ), integer=True, shape=(n, ))

    assert f.is_integer
    assert g.is_integer
    assert f.shape == (m, )
    assert g.shape == (n, )

    Eq << apply(ForAll[a:A](Contains(f(a), B)), ForAll[b:B](Contains(g(b), A)),
                ForAll[a:A](Equality(a, g(f(a)))), ForAll[b:B](Equality(
                    b, f(g(b)))))

    Eq << sets.forall_contains.forall_contains.forall_equality.imply.equality.apply(
        Eq[0], Eq[1], Eq[2])

    Eq << sets.forall_contains.forall_contains.forall_equality.imply.equality.apply(
        Eq[1], Eq[0], Eq[3])

    Eq << sets.equality.equality.imply.equality.abs.apply(Eq[-1],
                                                          Eq[-2]).reversed
Exemple #6
0
def test_powerset_contains():
    A = PowerSet(FiniteSet(1), evaluate=False)
    assert A.contains(2) == Contains(2, A)

    x = Symbol('x')

    A = PowerSet(FiniteSet(x), evaluate=False)
    assert A.contains(FiniteSet(1)) == Contains(FiniteSet(1), A)
Exemple #7
0
def prove(Eq):
    e = Symbol.e(integer=True)
    A = Symbol.A(dtype=dtype.integer)
    B = Symbol.B(dtype=dtype.integer)

    Eq << apply(Contains(e, A), Contains(e, B))

    Eq << Eq[-1].split()
Exemple #8
0
def prove(Eq):
    n = Symbol.n(domain=Interval(2, oo, integer=True))
    S = Symbol.S(dtype=dtype.integer * n)

    x = Symbol.x(**S.element_symbol().dtype.dict)

    i = Symbol.i(integer=True)
    j = Symbol.j(integer=True)

    given = [
        ForAll[j:1:n - 1, x:S](Contains(
            LAMBDA[i:n](Piecewise((x[0], Equality(i, j)),
                                  (x[j], Equality(i, 0)), (x[i], True))), S)),
        ForAll[x:S](Equality(abs(x.set_comprehension()), n))
    ]

    Eq << apply(given)

    Eq << discrete.combinatorics.permutation.adjacent.swap2.general.apply(
        Eq[0])

    Eq.permutation = discrete.combinatorics.permutation.adjacent.swapn.permutation.apply(
        Eq[-1])

    Eq << Eq.permutation.limits[0][1].this.definition

    Eq << discrete.combinatorics.permutation.factorial.definition.apply(n)

    Eq << Eq[-1].this.lhs.arg.limits_subs(Eq[-1].lhs.arg.variable,
                                          Eq[-2].rhs.variable)

    Eq <<= Eq[-1] & Eq[-2].abs()

    F = Function.F(nargs=(), dtype=dtype.integer * n)
    F.eval = lambda e: conditionset(x, Equality(x.set_comprehension(), e), S)

    e = Symbol.e(dtype=dtype.integer)
    Eq << Subset(F(e), S, plausible=True)
    Eq << Eq[-1].this.lhs.definition

    Eq << sets.subset.forall.imply.forall.apply(Eq[-1], Eq.permutation)

    Eq.forall_x = ForAll(Contains(Eq[-1].lhs, F(e)),
                         *Eq[-1].limits,
                         plausible=True)

    Eq << Eq.forall_x.definition.split()

    P = Eq[-1].limits[0][1]
    Eq << sets.imply.conditionset.apply(P)
    Eq << Eq[-1].apply(sets.equality.imply.equality.permutation, x)

    Eq.equality_e = Eq[-3] & Eq[-1]

    Eq << sets.imply.conditionset.apply(F(e)).reversed
Exemple #9
0
def prove(Eq): 
    n = Symbol.n(domain=Interval(2, oo, integer=True))
    S = Symbol.S(dtype=dtype.integer * n)    
    
    x = Symbol.x(**S.element_symbol().dtype.dict)
    
    i = Symbol.i(integer=True)
    j = Symbol.j(integer=True)    
    
    w = Symbol.w(integer=True, shape=(n, n, n, n), definition=LAMBDA[j:n, i:n](Swap(n, i, j)))
    
    given = ForAll[x:S](Contains(w[0, j] @ x, S))
    
    Eq << apply(given)
    
    Eq.given_i = given.subs(j, i)    
    
    Eq << given.subs(x, Eq.given_i.function.lhs)
    
    Eq << (Eq.given_i & Eq[-1]).split()[-1]
    
    Eq << Eq.given_i.subs(x, Eq[-1].function.lhs)
    
    Eq.final_statement = (Eq[-2] & Eq[-1]).split()[0]
    
    Eq << swap2.equality.apply(n, w)
    
    Eq << Eq[-1] @ x
    
    Eq << Eq[-1].forall((Eq[-1].limits[0].args[1].args[1].arg,))
    
    Eq.i_complement = Eq.final_statement.subs(Eq[-1])
    
    Eq.plausible = ForAll(Contains(w[i, j] @ x, S), (x, S), (j, Interval(1, n - 1, integer=True)), plausible=True)    
    
    Eq << Eq.plausible.bisect(i.set, wrt=j)
    
    Eq.i_complement, Eq.i_intersection = Eq[-1].split()
    
    Eq << sets.imply.equality.intersection.apply(i, Interval(1, n - 1, integer=True))
    
    Eq << Eq.i_intersection.this.limits[1].subs(Eq[-1])
    
    Eq << Eq[-1].subs(w[i, i].equality_defined())
    
    Eq << (Eq.i_complement & Eq.i_intersection)
    
    Eq << elementary.swap.transpose.apply(w).subs(j, 0)
    Eq << Eq.given_i.subs(Eq[-1].reversed)
    
    Eq << (Eq[-1] & Eq.plausible)
Exemple #10
0
def prove(Eq):    
    n = Symbol.n(integer=True, positive=True)
    Eq << apply(n)
    x = Eq[0].rhs.variable.base
    P = Eq[0].lhs
    P_quote = Eq[1].lhs
    
    i = Symbol.i(integer=True)
    
    x_quote = Symbol("x'", definition=LAMBDA[i:n + 1](Piecewise((n, Equality(i, n)), (x[i], True))))
    Eq.x_quote_definition = x_quote.this.definition
    
    Eq << Eq.x_quote_definition[:n]
    
    Eq.mapping = Eq[-1].this.rhs().function.simplify()
    
    Eq << Eq.x_quote_definition[i]
    
    Eq << Eq[-1].set.union_comprehension((i, 0, n - 1))
    
    Eq.x_quote_n_definition = Eq[-2].subs(i, n)
    
    Eq << sets.imply.conditionset.apply(P)
    
    Eq << Eq[-2].subs(Eq[-1])
    
    Eq.P2P_quote = ForAll[x[:n]:P](Contains(x_quote, P_quote), plausible=True)
    
    Eq << Eq.P2P_quote.definition.split()
    
    Eq << sets.imply.conditionset.apply(P_quote)
    
    Eq << Eq[-1].split()
    
    Eq << Eq[-2].reversed + Eq.x_quote_n_definition
    
    Eq.mapping_quote = ForAll[x[:n + 1]:P_quote](Equality(x_quote, x[:n + 1]), plausible=True)
    
    Eq << Eq.mapping_quote.this.function.bisect(Slice[-1:]).split()
    
    Eq << Eq[-1].subs(Eq.mapping)
    
    Eq << ForAll[x[:n + 1]:P_quote](Contains(x[:n], P), plausible=True)

    Eq << Eq[-1].definition
    
    Eq << sets.forall_contains.forall_contains.forall_equality.forall_equality.imply.equality.apply(Eq[-1], Eq.P2P_quote, Eq.mapping_quote, Eq.mapping)
    
    Eq << Eq[-1].reversed
Exemple #11
0
def limits_cond(limits):
    eqs = []
    from sympy.sets.contains import Contains
    limitsdict = limits_dict(limits)
    for x, cond in limitsdict.items():
        if isinstance(cond, list):
            if not cond:
                continue
            cond, baseset = cond
            cond &= Contains(x, baseset)
        elif cond.is_set:
            cond = Contains(x, cond)

        eqs.append(cond)
    return And(*eqs)
Exemple #12
0
def prove(Eq):
    n = Symbol.n(integer=True, positive=True)
    x = Symbol.x(complex=True, shape=(n,))
    A = Symbol.A(dtype=dtype.integer*n)
    B = Symbol.B(dtype=dtype.integer*n)
    
    Eq << apply(ForAll[x:A](Contains(x, B)), ForAll[x:B](Contains(x, A)))
    
    Eq << sets.forall_contains.imply.subset.apply(Eq[0])
    
    Eq << sets.forall_contains.imply.subset.apply(Eq[1])
    
    Eq <<= Eq[-1] & Eq[-2]
    
    Eq << Eq[-1].reversed
Exemple #13
0
def apply(given):
    assert given.is_ForAll and len(given.limits) == 2
    j, a, n_munis_1 = given.limits[0]
    assert a == 1
    x, S = given.limits[1]

    contains = given.function
    assert contains.is_Contains
    ref, _S = contains.args
    assert S == _S and ref.is_LAMBDA and S.is_set
    dtype = S.element_type

    assert len(ref.limits) == 1
    i, a, _n_munis_1 = ref.limits[0]
    assert _n_munis_1 == n_munis_1 and a == 0

    piecewise = ref.function
    assert piecewise.is_Piecewise and len(piecewise.args) == 3

    x0, condition0 = piecewise.args[0]
    assert condition0.is_Equality and {*condition0.args} == {i, j}

    xj, conditionj = piecewise.args[1]
    assert conditionj.is_Equality and {*conditionj.args} == {i, 0}

    xi, conditioni = piecewise.args[2]
    assert conditioni

    n = n_munis_1 + 1

    assert x[j] == xj and x[i] == xi and x[0] == x0 and dtype == x.dtype

    w = Symbol.w(definition=LAMBDA[j:n, i:n](Swap(n, i, j)))

    return ForAll(Contains(w[i, j] @ x, S), (x, S), given=given)
Exemple #14
0
def apply(*given):
    equality = given[0]
    assert equality.is_Equality

    intersection, a = equality.args
    if not intersection.is_Intersection:
        a, intersection = equality.args
        assert intersection.is_Intersection

    e_set, s = intersection.args
    if not e_set.is_FiniteSet:
        s, e_set = intersection.args
        assert e_set.is_FiniteSet

    assert len(e_set) == 1

    e, *_ = e_set.args
    if len(given) > 1:
        positive = given[1]
        x_abs = positive.is_positive_relationship()
        assert x_abs is not None
        assert x_abs.is_Abs
        assert a == x_abs.arg
    else:
        assert abs(a) > 0

    return Contains(e, s, given=given)
Exemple #15
0
 def pmf(self, x):
     x = sympify(x)
     if not (x.is_number or x.is_Symbol or is_random(x)):
         raise ValueError("'x' expected as an argument of type 'number', 'Symbol', or "
                     "'RandomSymbol' not %s" % (type(x)))
     cond = Ge(x, 1) & Le(x, self.sides) & Contains(x, S.Integers)
     return Piecewise((S.One/self.sides, cond), (S.Zero, True))
def apply(n, dx, dz):
    x = Symbol.x(shape=(n, dx), real=True)
    W_Q = Symbol("W^Q", shape=(dx, dz), real=True)
    W_K = Symbol("W^K", shape=(dx, dz), real=True)
    W_V = Symbol("W^V", shape=(dx, dz), real=True)
    
    Q = Symbol.Q(definition=x @ W_Q)
    K = Symbol.K(definition=x @ W_K)
    
    i = Symbol.i(integer=True)
    j = Symbol.j(integer=True)
    
    k = Symbol.k(integer=True, positive=True)
    w_K = Symbol("w^K", shape=(2 * k + 1, dz), real=True)
    w_V = Symbol("w^V", shape=(2 * k + 1, dz), real=True)
    
    a_K = Symbol("a^K", definition=LAMBDA[j:n, i:n](w_K[k + clip(j - i, -k, k)]))
    a_V = Symbol("a^V", definition=LAMBDA[j:n, i:n](w_V[k + clip(j - i, -k, k)]))
    
    e = Symbol.e(definition=(Q @ K.T + LAMBDA[i:n](Q[i] @ a_K[i].T)) / sqrt(dz))
    α = Symbol.α(definition=softmax(e))
    
    z = Symbol.z(shape=(n, dz), definition=α @ (x @ W_V) + LAMBDA[i:n](α[i] @ a_V[i]))
    
    return Contains(k + clip(j - i, -k, k), Interval(0, 2 * k, integer=True)), Equality(z[i], Sum[j:n](α[i, j] * (x[j] @ W_V + a_V[i, j]))), Equality(e[i, j], (x[i] @ W_Q @ (x[j] @ W_K + a_K[i, j])) / sqrt(dz))
Exemple #17
0
def image_set_definition(self, reverse=False):
    image_set = self.image_set()
    if image_set is None:
        return

    expr, variables, base_set = image_set
    from sympy.tensor.indexed import Slice
    from sympy.core.relational import Equality
    from sympy.concrete.expr_with_limits import ForAll, Exists

    if isinstance(base_set, Symbol):
        if reverse:
            return ForAll(Contains(expr, self), (variables, base_set))

        element_symbol = self.element_symbol()
        assert expr.dtype == element_symbol.dtype
        condition = Equality(expr, element_symbol)
        return ForAll(Exists(condition, (variables, base_set)), (element_symbol, self))

    else:
        if not isinstance(base_set, ConditionSet):
            return

    variable = base_set.variable
    if isinstance(variable, Symbol):
        ...
    elif isinstance(variable, Slice):
        condition = base_set.condition
        element_symbol = self.element_symbol()
        assert expr.dtype == element_symbol.dtype

        exists = Exists(condition.func(*condition.args), (variables, Equality(expr, element_symbol)))
        return ForAll(exists, (element_symbol, self))
Exemple #18
0
def apply(n, u, v):
    Q, w, x = predefined_symbols(n)
    X, index = X_definition(n, w, x)
    return ForAll[x[:n +
                    1]:Q[u]](Contains(X[v], Q[v])
                             & Equality(x[:n +
                                          1], w[n, index[u](X[v])] @ X[v]))
Exemple #19
0
def prove(Eq):
    n = Symbol.n(domain=Interval(2, oo, integer=True))
    S = Symbol.S(dtype=dtype.integer * n)

    x = Symbol.x(**S.element_symbol().dtype.dict)

    i = Symbol.i(integer=True)
    j = Symbol.j(integer=True)

    w = Symbol.w(integer=True,
                 shape=(n, n, n, n),
                 definition=LAMBDA[j:n, i:n](Swap(n, i, j)))

    k = Symbol.k(integer=True)

    given = ForAll[x:S](Contains(LAMBDA[k:n](x[(w[i, j] @ LAMBDA[k:n](k))[k]]),
                                 S))

    Eq.P_definition, Eq.w_definition, Eq.swap, Eq.axiom = apply(given)

    Eq << algebre.matrix.elementary.swap.identity.apply(x, w)

    Eq << Eq.swap.subs(Eq[-1])

    Eq << swapn.permutation.apply(Eq[-1])
Exemple #20
0
 def as_image_set(self):
     try:
         expr, variable, base_set = self.base_set.image_set()
         from sympy import sets
         condition = Contains(variable, base_set).simplify() & self.condition._subs(self.variable, expr)
         return sets.image_set(expr, variable, ConditionSet(variable, condition))
     except:
         ...
Exemple #21
0
def apply(given, t):
    assert given.is_Contains    
    
    e, interval = given.args    
    
    a, b, _ = interval.args
        
    return Contains(e + t, interval.copy(start=a + t, stop=b + t), given=given)
Exemple #22
0
 def pmf(self, x):
     n, p = self.n, self.p
     x = sympify(x)
     if not (x.is_number or x.is_Symbol or is_random(x)):
         raise ValueError("'x' expected as an argument of type 'number', 'Symbol', or "
                     "'RandomSymbol' not %s" % (type(x)))
     cond = Ge(x, 0) & Le(x, n) & Contains(x, S.Integers)
     return Piecewise((binomial(n, x) * p**x * (1 - p)**(n - x), cond), (S.Zero, True))
Exemple #23
0
def test_GammaProcess_numeric():
    t, d, x, y = symbols('t d x y', positive=True)
    X = GammaProcess("X", 1, 2)
    assert X.state_space == Interval(0, oo)
    assert X.index_set == Interval(0, oo)
    assert X.lamda == 1
    assert X.gamma == 2

    raises(ValueError, lambda: GammaProcess("X", -1, 2))
    raises(ValueError, lambda: GammaProcess("X", 0, -2))
    raises(ValueError, lambda: GammaProcess("X", -1, -2))

    # all are independent because of non-overlapping intervals
    assert P((X(t) > 4) & (X(d) > 3) & (X(x) > 2) & (X(y) > 1), Contains(t,
        Interval.Lopen(0, 1)) & Contains(d, Interval.Lopen(1, 2)) & Contains(x,
        Interval.Lopen(2, 3)) & Contains(y, Interval.Lopen(3, 4))).simplify() == \
                                                            120*exp(-10)

    # Check working with Not and Or
    assert P(
        Not((X(t) < 5) & (X(d) > 3)),
        Contains(t, Interval.Ropen(2, 4)) & Contains(d, Interval.Lopen(
            7, 8))).simplify() == -4 * exp(-3) + 472 * exp(-8) / 3 + 1
    assert P((X(t) > 2) | (X(t) < 4), Contains(t, Interval.Ropen(1, 4))).simplify() == \
                                            -643*exp(-4)/15 + 109*exp(-2)/15 + 1

    assert E(X(t)) == 2 * t  # E(X(t)) == gamma*t/l
    assert E(X(2) + x * E(X(5))) == 10 * x + 4
Exemple #24
0
def apply(*given):
    contains, subset = given
    if contains.is_Subset:
        subset, contains = given
    assert contains.is_Contains and subset.is_Subset
    x, A = contains.args
    _A, B = subset.args
    assert A == _A
    return Contains(x, B, given=given)
Exemple #25
0
def apply(given, limit):
    assert given.is_Contains

    k, a, b = limit
    e, A = given.args

    assert Interval(a, b, integer=True) in A.domain_defined(k)

    return Contains(e, INTERSECTION[k:a:b](A), given=given)
Exemple #26
0
def prove(Eq):
    x = Symbol.x(integer=True)
    given = Contains(x, {0, 1})

    Eq << apply(given)

    Eq << Eq[-1].this.lhs.as_Piecewise()

    Eq << Eq[-1].as_Or()
Exemple #27
0
def prove(Eq):
    e = Symbol.e(integer=True)
    s = Symbol.s(dtype=dtype.integer)
    contains = Contains(e, s, evaluate=False)
    
    Eq << apply(contains)
    
    assert Eq[0].plausible is None
    Eq << Eq[-1].definition
Exemple #28
0
def prove(Eq):
    n = Symbol.n(integer=True, positive=True)
    x = Symbol.x(complex=True, shape=(n,))
    A = Symbol.A(dtype=dtype.complex * n)
    B = Symbol.B(dtype=dtype.complex * n)
    Eq << apply(Contains(x, A), Subset(A, B))
    
#     Eq <<= Eq[0] & Eq[1]
    Eq <<= Eq[1] & Eq[0]
Exemple #29
0
def apply(given, var=None):
    assert given.is_Equality
    S_abs, one = given.args
    assert S_abs.is_Abs and one == 1
    S = S_abs.arg
    assert S.is_set
    if var is None:
        var = S.element_symbol()
        assert not var.is_set
    return Contains(Sum[var:S](var), S, given=given)
Exemple #30
0
def apply(*given):
    notcontains1, notcontains2 = given
    assert notcontains1.is_Contains
    assert notcontains2.is_Contains

    e, A = notcontains1.args
    _e, B = notcontains2.args
    assert e == _e

    return Contains(e, A & B, given=given)