Exemple #1
0
def test_Intersection_as_relational():
    x = Symbol('x')
    assert (Intersection(Interval(0, 1), FiniteSet(2),
                         evaluate=False).as_relational(x) == And(
                             And(Le(0, x), Le(x, 1)), Eq(x, 2)))
Exemple #2
0
def test_reduce_poly_inequalities_real_relational():
    global_assumptions.add(Q.real(x))
    global_assumptions.add(Q.real(y))

    assert reduce_poly_inequalities([[Eq(x**2, 0)]], x,
                                    relational=True) == Eq(x, 0)
    assert reduce_poly_inequalities([[Le(x**2, 0)]], x,
                                    relational=True) == Eq(x, 0)
    assert reduce_poly_inequalities([[Lt(x**2, 0)]], x,
                                    relational=True) == False
    assert reduce_poly_inequalities([[Ge(x**2, 0)]], x,
                                    relational=True) == True
    assert reduce_poly_inequalities([[Gt(x**2, 0)]], x,
                                    relational=True) == Or(Lt(x, 0), Lt(0, x))
    assert reduce_poly_inequalities([[Ne(x**2, 0)]], x,
                                    relational=True) == Or(Lt(x, 0), Lt(0, x))

    assert reduce_poly_inequalities([[Eq(x**2, 1)]], x, relational=True) == Or(
        Eq(x, -1), Eq(x, 1))
    assert reduce_poly_inequalities([[Le(x**2, 1)]], x,
                                    relational=True) == And(
                                        Le(-1, x), Le(x, 1))
    assert reduce_poly_inequalities([[Lt(x**2, 1)]], x,
                                    relational=True) == And(
                                        Lt(-1, x), Lt(x, 1))
    assert reduce_poly_inequalities([[Ge(x**2, 1)]], x, relational=True) == Or(
        Le(x, -1), Le(1, x))
    assert reduce_poly_inequalities([[Gt(x**2, 1)]], x, relational=True) == Or(
        Lt(x, -1), Lt(1, x))
    assert reduce_poly_inequalities([[Ne(x**2, 1)]], x, relational=True) == Or(
        Lt(x, -1), And(Lt(-1, x), Lt(x, 1)), Lt(1, x))

    assert reduce_poly_inequalities([[Eq(x**2, 1.0)]], x,
                                    relational=True).evalf() == Or(
                                        Eq(x, -1.0), Eq(x, 1.0)).evalf()
    assert reduce_poly_inequalities([[Le(x**2, 1.0)]], x,
                                    relational=True) == And(
                                        Le(-1.0, x), Le(x, 1.0))
    assert reduce_poly_inequalities([[Lt(x**2, 1.0)]], x,
                                    relational=True) == And(
                                        Lt(-1.0, x), Lt(x, 1.0))
    assert reduce_poly_inequalities([[Ge(x**2, 1.0)]], x,
                                    relational=True) == Or(
                                        Le(x, -1.0), Le(1.0, x))
    assert reduce_poly_inequalities([[Gt(x**2, 1.0)]], x,
                                    relational=True) == Or(
                                        Lt(x, -1.0), Lt(1.0, x))
    assert reduce_poly_inequalities([[Ne(x**2, 1.0)]], x,
                                    relational=True) == Or(
                                        Lt(x, -1.0),
                                        And(Lt(-1.0, x), Lt(x, 1.0)),
                                        Lt(1.0, x))

    global_assumptions.remove(Q.real(x))
    global_assumptions.remove(Q.real(y))
Exemple #3
0
def test_Union_as_relational():
    x = Symbol('x')
    assert (Interval(0, 1) + FiniteSet(2)).as_relational(x) == \
        Or(And(Le(0, x), Le(x, 1)), Eq(x, 2))
    assert (Interval(0, 1, True, True) + FiniteSet(1)).as_relational(x) == \
        And(Lt(0, x), Le(x, 1))
Exemple #4
0
def test_reduce_inequalities_multivariate():
    assert reduce_inequalities([Ge(x**2, 1), Ge(y**2, 1)]) == \
        And(And(Or(Le(re(x), -1), Le(1, re(x))), Eq(im(x), 0)),
            And(Or(Le(re(y), -1), Le(1, re(y))), Eq(im(y), 0)))
Exemple #5
0
def test_reduce_inequalities_assume():
    assert reduce_inequalities([Le(x**2, 1),
                                Q.real(x)]) == And(Le(-1, x), Le(x, 1))
    assert reduce_inequalities([Le(x**2, 1)],
                               Q.real(x)) == And(Le(-1, x), Le(x, 1))
Exemple #6
0
def test_reduce_poly_inequalities_complex_relational():
    cond = Eq(im(x), 0)

    assert reduce_poly_inequalities([[Eq(x**2, 0)]], x,
                                    relational=True) == And(
                                        Eq(re(x), 0), cond)
    assert reduce_poly_inequalities([[Le(x**2, 0)]], x,
                                    relational=True) == And(
                                        Eq(re(x), 0), cond)
    assert reduce_poly_inequalities([[Lt(x**2, 0)]], x,
                                    relational=True) == False
    assert reduce_poly_inequalities([[Ge(x**2, 0)]], x,
                                    relational=True) == cond
    assert reduce_poly_inequalities([[Gt(x**2, 0)]], x,
                                    relational=True) == And(
                                        Or(Lt(re(x), 0), Lt(0, re(x))), cond)
    assert reduce_poly_inequalities([[Ne(x**2, 0)]], x,
                                    relational=True) == And(
                                        Or(Lt(re(x), 0), Lt(0, re(x))), cond)

    assert reduce_poly_inequalities([[Eq(x**2, 1)]], x,
                                    relational=True) == And(
                                        Or(Eq(re(x), -1), Eq(re(x), 1)), cond)
    assert reduce_poly_inequalities([[Le(x**2, 1)]], x,
                                    relational=True) == And(
                                        And(Le(-1, re(x)), Le(re(x), 1)), cond)
    assert reduce_poly_inequalities([[Lt(x**2, 1)]], x,
                                    relational=True) == And(
                                        And(Lt(-1, re(x)), Lt(re(x), 1)), cond)
    assert reduce_poly_inequalities([[Ge(x**2, 1)]], x,
                                    relational=True) == And(
                                        Or(Le(re(x), -1), Le(1, re(x))), cond)
    assert reduce_poly_inequalities([[Gt(x**2, 1)]], x,
                                    relational=True) == And(
                                        Or(Lt(re(x), -1), Lt(1, re(x))), cond)
    assert reduce_poly_inequalities([[Ne(x**2, 1)]], x,
                                    relational=True) == And(
                                        Or(Lt(re(x), -1),
                                           And(Lt(-1, re(x)), Lt(re(x), 1)),
                                           Lt(1, re(x))), cond)

    assert reduce_poly_inequalities([[Eq(x**2, 1.0)]], x,
                                    relational=True).evalf() == And(
                                        Or(Eq(re(x), -1.0), Eq(re(x), 1.0)),
                                        cond)
    assert reduce_poly_inequalities([[Le(x**2, 1.0)]], x,
                                    relational=True) == And(
                                        And(Le(-1.0, re(x)), Le(re(x), 1.0)),
                                        cond)
    assert reduce_poly_inequalities([[Lt(x**2, 1.0)]], x,
                                    relational=True) == And(
                                        And(Lt(-1.0, re(x)), Lt(re(x), 1.0)),
                                        cond)
    assert reduce_poly_inequalities([[Ge(x**2, 1.0)]], x,
                                    relational=True) == And(
                                        Or(Le(re(x), -1.0), Le(1.0, re(x))),
                                        cond)
    assert reduce_poly_inequalities([[Gt(x**2, 1.0)]], x,
                                    relational=True) == And(
                                        Or(Lt(re(x), -1.0), Lt(1.0, re(x))),
                                        cond)
    assert reduce_poly_inequalities(
        [[Ne(x**2, 1.0)]], x, relational=True) == And(
            Or(Lt(re(x), -1.0), And(Lt(-1.0, re(x)), Lt(re(x), 1.0)),
               Lt(1.0, re(x))), cond)
Exemple #7
0
def test_reduce_poly_inequalities_real_interval():
    global_assumptions.add(Q.real(x))
    global_assumptions.add(Q.real(y))

    assert reduce_poly_inequalities([[Eq(x**2, 0)]], x,
                                    relational=False) == FiniteSet(0)
    assert reduce_poly_inequalities([[Le(x**2, 0)]], x,
                                    relational=False) == FiniteSet(0)
    assert reduce_poly_inequalities([[Lt(x**2, 0)]], x,
                                    relational=False) == S.EmptySet
    assert reduce_poly_inequalities([[Ge(x**2, 0)]], x,
                                    relational=False) == Interval(-oo, oo)
    assert reduce_poly_inequalities(
        [[Gt(x**2, 0)]], x, relational=False) == FiniteSet(0).complement
    assert reduce_poly_inequalities(
        [[Ne(x**2, 0)]], x, relational=False) == FiniteSet(0).complement

    assert reduce_poly_inequalities([[Eq(x**2, 1)]], x,
                                    relational=False) == FiniteSet(-1, 1)
    assert reduce_poly_inequalities([[Le(x**2, 1)]], x,
                                    relational=False) == Interval(-1, 1)
    assert reduce_poly_inequalities([[Lt(x**2, 1)]], x,
                                    relational=False) == Interval(
                                        -1, 1, True, True)
    assert reduce_poly_inequalities([[Ge(x**2, 1)]], x,
                                    relational=False) == Union(
                                        Interval(-oo, -1), Interval(1, oo))
    assert reduce_poly_inequalities([[Gt(x**2, 1)]], x,
                                    relational=False) == Interval(-1,
                                                                  1).complement
    assert reduce_poly_inequalities(
        [[Ne(x**2, 1)]], x, relational=False) == FiniteSet(-1, 1).complement
    assert reduce_poly_inequalities([[Eq(x**2, 1.0)]],
                                    x, relational=False) == FiniteSet(
                                        -1.0, 1.0).evalf()
    assert reduce_poly_inequalities([[Le(x**2, 1.0)]], x,
                                    relational=False) == Interval(-1.0, 1.0)
    assert reduce_poly_inequalities([[Lt(x**2, 1.0)]], x,
                                    relational=False) == Interval(
                                        -1.0, 1.0, True, True)
    assert reduce_poly_inequalities([[Ge(x**2, 1.0)]], x,
                                    relational=False) == Union(
                                        Interval(-inf, -1.0),
                                        Interval(1.0, inf))
    assert reduce_poly_inequalities([[Gt(x**2, 1.0)]], x,
                                    relational=False) == Union(
                                        Interval(-inf, -1.0, right_open=True),
                                        Interval(1.0, inf, left_open=True))
    assert reduce_poly_inequalities([[Ne(x**2, 1.0)]],
                                    x, relational=False) == FiniteSet(
                                        -1.0, 1.0).complement

    s = sqrt(2)

    assert reduce_poly_inequalities(
        [[Lt(x**2 - 1, 0), Gt(x**2 - 1, 0)]], x,
        relational=False) == S.EmptySet
    assert reduce_poly_inequalities(
        [[Le(x**2 - 1, 0), Ge(x**2 - 1, 0)]], x,
        relational=False) == FiniteSet(-1, 1)
    assert reduce_poly_inequalities(
        [[Le(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, False, False),
                                   Interval(1, s, False, False))
    assert reduce_poly_inequalities(
        [[Le(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, False, True),
                                   Interval(1, s, True, False))
    assert reduce_poly_inequalities(
        [[Lt(x**2 - 2, 0), Ge(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, True, False),
                                   Interval(1, s, False, True))
    assert reduce_poly_inequalities(
        [[Lt(x**2 - 2, 0), Gt(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, True, True),
                                   Interval(1, s, True, True))
    assert reduce_poly_inequalities(
        [[Lt(x**2 - 2, 0), Ne(x**2 - 1, 0)]], x,
        relational=False) == Union(Interval(-s, -1, True, True),
                                   Interval(-1, 1, True, True),
                                   Interval(1, s, True, True))

    global_assumptions.remove(Q.real(x))
    global_assumptions.remove(Q.real(y))
Exemple #8
0
    def callback(self, clusters, prefix, cache=None):
        async_degree = self.options['buf-async-degree']

        # Locate all Function accesses within the provided `clusters`
        accessmap = AccessMapper(clusters)

        # Create the buffers
        buffers = BufferBatch()
        for f, accessv in accessmap.items():
            # Has a buffer already been produced for `f`?
            if f in cache:
                continue

            # Is `f` really a buffering candidate?
            dims = self.callback0(f)
            if dims is None:
                continue
            if not all(
                    any([i.dim in d._defines for i in prefix]) for d in dims):
                continue

            b = cache[f] = buffers.make(f, dims, accessv, async_degree,
                                        self.sregistry)

        if not buffers:
            return clusters

        try:
            pd = prefix[-2].dim
        except IndexError:
            pd = None

        # Create Eqs to initialize buffers. Note: a buffer needs to be initialized
        # only if the buffered Function is read in at least one place or in the case
        # of non-uniform SubDimensions, to avoid uninitialized values to be copied-back
        # into the buffered Function
        processed = []
        for b in buffers:
            if b.is_read or not b.has_uniform_subdims:
                dims = b.function.dimensions
                lhs = b.indexed[[b.initmap.get(d, Map(d, d)).b for d in dims]]
                rhs = b.function[[b.initmap.get(d, Map(d, d)).f for d in dims]]

                expr = lower_exprs(Eq(lhs, rhs))
                ispace = b.writeto
                dspace = derive_dspace(expr, ispace)
                guards = {
                    pd: Le(d.root.symbolic_min, d.root.symbolic_max)
                    for d in b.contraction_mapper
                }
                properties = {d: {PARALLEL} for d in ispace.itdimensions}

                processed.append(
                    Cluster(expr, ispace, dspace, guards, properties))

        # Substitution rules to replace buffered Functions with buffers
        subs = {}
        for b in buffers:
            for a in b.accessv.accesses:
                subs[a] = b.indexed[[
                    b.index_mapper_flat.get(i, i) for i in a.indices
                ]]

        for c in clusters:
            # If a buffer is read but never written, then we need to add
            # an Eq to step through the next slot
            # E.g., `ub[0, x] = u[time+2, x]`
            for b in buffers:
                if not b.is_readonly:
                    continue
                try:
                    c.exprs.index(b.firstread)
                except ValueError:
                    continue

                dims = b.function.dimensions
                lhs = b.indexed[[b.lastmap.get(d, Map(d, d)).b for d in dims]]
                rhs = b.function[[b.lastmap.get(d, Map(d, d)).f for d in dims]]

                expr = lower_exprs(uxreplace(Eq(lhs, rhs), b.subdims_mapper))
                ispace = b.written
                dspace = derive_dspace(expr, ispace)

                processed.append(
                    c.rebuild(exprs=expr, ispace=ispace, dspace=dspace))

            # Substitute buffered Functions with the newly created buffers
            exprs = [uxreplace(e, subs) for e in c.exprs]
            ispace = c.ispace
            for b in buffers:
                ispace = ispace.augment(b.sub_iterators)
            processed.append(c.rebuild(exprs=exprs, ispace=ispace))

            # Also append the copy-back if `e` is the last-write of some buffers
            # E.g., `u[time + 1, x] = ub[sb1, x]`
            for b in buffers:
                if b.is_readonly:
                    continue
                try:
                    c.exprs.index(b.lastwrite)
                except ValueError:
                    continue

                dims = b.function.dimensions
                lhs = b.function[[b.lastmap.get(d, Map(d, d)).f for d in dims]]
                rhs = b.indexed[[b.lastmap.get(d, Map(d, d)).b for d in dims]]

                expr = lower_exprs(uxreplace(Eq(lhs, rhs), b.subdims_mapper))
                ispace = b.written
                dspace = derive_dspace(expr, ispace)

                processed.append(
                    c.rebuild(exprs=expr, ispace=ispace, dspace=dspace))

        return processed