Esempio n. 1
0
def test_piecewise_integrate1c():
    y = symbols('y', real=True)
    for i, g in enumerate([
            Piecewise((1 - x, Interval(0, 1).contains(x)),
                      (1 + x, Interval(-1, 0).contains(x)), (0, True)),
            Piecewise((0, Or(x <= -1, x >= 1)), (1 - x, x > 0), (1 + x, True))
    ]):
        gy1 = g.integrate((x, y, 1))
        g1y = g.integrate((x, 1, y))
        for yy in (-2, 0, 2):
            assert g.integrate((x, yy, 1)) == gy1.subs(y, yy)
            assert g.integrate((x, 1, yy)) == g1y.subs(y, yy)
        assert piecewise_fold(gy1.rewrite(Piecewise)) == Piecewise(
            (1, y <= -1), (-y**2 / 2 - y + 1 / 2, y <= 0),
            (y**2 / 2 - y + 1 / 2, y < 1), (0, True))
        assert piecewise_fold(g1y.rewrite(Piecewise)) == Piecewise(
            (-1, y <= -1), (y**2 / 2 + y - 1 / 2, y <= 0),
            (-y**2 / 2 + y - 1 / 2, y < 1), (0, True))
        # g1y and gy1 should simplify if the condition that y < 1
        # is applied, e.g. Min(1, Max(-1, y)) --> Max(-1, y)
        assert gy1 == Piecewise(
            (-Min(1, Max(-1, y))**2 / 2 - Min(1, Max(-1, y)) +
             Min(1, Max(0, y))**2 + 1 / 2, y < 1), (0, True))
        assert g1y == Piecewise(
            (Min(1, Max(-1, y))**2 / 2 + Min(1, Max(-1, y)) -
             Min(1, Max(0, y))**2 - 1 / 2, y < 1), (0, True))
Esempio n. 2
0
def test_piecewise_integrate1c():
    for i, g in enumerate([
        Piecewise((1 - x, Interval(0, 1).contains(x)),
            (1 + x, Interval(-1, 0).contains(x)), (0, True)),
        Piecewise((0, Or(x <= -1, x >= 1)), (1 - x, x > 0),
            (1 + x, True))]):
        gy1 = g.integrate((x, y, 1))
        g1y = g.integrate((x, 1, y))
        for yy in (-2, 0, 2):
            assert g.integrate((x, yy, 1)) == gy1.subs(y, yy)
            assert g.integrate((x, 1, yy)) == g1y.subs(y, yy)
        assert piecewise_fold(gy1.rewrite(Piecewise)) == Piecewise(
            (1, y <= -1),
            (-y**2/2 - y + 1/2, y <= 0),
            (y**2/2 - y + 1/2, y < 1),
            (0, True))
        assert piecewise_fold(g1y.rewrite(Piecewise)) == Piecewise(
            (-1, y <= -1),
            (y**2/2 + y - 1/2, y <= 0),
            (-y**2/2 + y - 1/2, y < 1),
            (0, True))
        # g1y and gy1 should simplify if the condition that y < 1
        # is applied, e.g. Min(1, Max(-1, y)) --> Max(-1, y)
        assert gy1 == Piecewise(
            (-Min(1, Max(-1, y))**2/2 - Min(1, Max(-1, y)) +
                Min(1, Max(0, y))**2 + 1/2, y < 1),
            (0, True))
        assert g1y == Piecewise(
            (Min(1, Max(-1, y))**2/2 + Min(1, Max(-1, y)) -
                Min(1, Max(0, y))**2 - 1/2, y < 1),
            (0, True))
Esempio n. 3
0
def test_piecewise_integrate1cb():
    y = symbols('y', real=True)
    g = Piecewise((0, Or(x <= -1, x >= 1)), (1 - x, x > 0), (1 + x, True))
    gy1 = g.integrate((x, y, 1))
    g1y = g.integrate((x, 1, y))

    assert g.integrate((x, -2, 1)) == gy1.subs(y, -2)
    assert g.integrate((x, 1, -2)) == g1y.subs(y, -2)
    assert g.integrate((x, 0, 1)) == gy1.subs(y, 0)
    assert g.integrate((x, 1, 0)) == g1y.subs(y, 0)
    assert g.integrate((x, 2, 1)) == gy1.subs(y, 2)
    assert g.integrate((x, 1, 2)) == g1y.subs(y, 2)

    assert piecewise_fold(gy1.rewrite(Piecewise)) == \
        Piecewise(
            (1, y <= -1),
            (-y**2/2 - y + S(1)/2, y <= 0),
            (y**2/2 - y + S(1)/2, y < 1),
            (0, True))
    assert piecewise_fold(g1y.rewrite(Piecewise)) == \
        Piecewise(
            (-1, y <= -1),
            (y**2/2 + y - S(1)/2, y <= 0),
            (-y**2/2 + y - S(1)/2, y < 1),
            (0, True))

    # g1y and gy1 should simplify if the condition that y < 1
    # is applied, e.g. Min(1, Max(-1, y)) --> Max(-1, y)
    assert gy1 == Piecewise((-Min(1, Max(-1, y))**2 / 2 - Min(1, Max(-1, y)) +
                             Min(1, Max(0, y))**2 + S(1) / 2, y < 1),
                            (0, True))
    assert g1y == Piecewise((Min(1, Max(-1, y))**2 / 2 + Min(1, Max(-1, y)) -
                             Min(1, Max(0, y))**2 - S(1) / 2, y < 1),
                            (0, True))
Esempio n. 4
0
 def supply(self):
     aggregate = sp.Piecewise((0, True))
     for firm, n in self.firms():
         supply = firm.supply()
         if isinstance(supply, sp.relational.Relational):
             aggregate = sp.piecewise_fold(aggregate + firm.supply())
         else:
             aggregate = sp.piecewise_fold(aggregate + n*firm.supply())
     return aggregate
Esempio n. 5
0
def test_piecewise_fold_expand():
    p1 = Piecewise((1,Interval(0,1,False,True)),(0,True))

    p2 = piecewise_fold(expand((1-x)*p1))
    assert p2 == Piecewise((1 - x, Interval(0,1,False,True)), \
        (Piecewise((-x, Interval(0,1,False,True)), (0, True)), True))

    p2 = expand(piecewise_fold((1-x)*p1))
    assert p2 == Piecewise((1 - x, Interval(0,1,False,True)), (0, True))
Esempio n. 6
0
 def supply(self):
     aggregate = sp.Piecewise((0, True))
     for firm, n in self.firms():
         supply = firm.supply()
         if isinstance(supply, sp.relational.Relational):
             aggregate = sp.piecewise_fold(aggregate + firm.supply())
         else:
             aggregate = sp.piecewise_fold(aggregate + n * firm.supply())
     return aggregate
Esempio n. 7
0
def test_piecewise_fold_expand():
    p1 = Piecewise((1, Interval(0, 1, False, True)), (0, True))

    p2 = piecewise_fold(expand((1 - x) * p1))
    assert p2 == Piecewise((1 - x, Interval(0,1,False,True)), \
        (Piecewise((-x, Interval(0,1,False,True)), (0, True)), True))

    p2 = expand(piecewise_fold((1 - x) * p1))
    assert p2 == Piecewise((1 - x, Interval(0, 1, False, True)), (0, True))
Esempio n. 8
0
def test_issue_10087():
    a, b = Piecewise((x, x > 1), (2, True)), Piecewise((x, x > 3), (3, True))
    m = a*b
    f = piecewise_fold(m)
    for i in (0, 2, 4):
        assert m.subs(x, i) == f.subs(x, i)
    m = a + b
    f = piecewise_fold(m)
    for i in (0, 2, 4):
        assert m.subs(x, i) == f.subs(x, i)
Esempio n. 9
0
def test_issue_10087():
    a, b = Piecewise((x, x > 1), (2, True)), Piecewise((x, x > 3), (3, True))
    m = a * b
    f = piecewise_fold(m)
    for i in (0, 2, 4):
        assert m.subs(x, i) == f.subs(x, i)
    m = a + b
    f = piecewise_fold(m)
    for i in (0, 2, 4):
        assert m.subs(x, i) == f.subs(x, i)
Esempio n. 10
0
 def test_surplus(self):
     """Practice problem 2.2
     """
     sp.var('A x p')
     cons = Consumer(x, p, 2*A * sp.sqrt(x))
     self.assertEqual(sp.piecewise_fold(cons.surplus()),
                      sp.Piecewise((0, p < 0),
                                   (-A**2/p + 2*A*sp.sqrt(A**2/p**2), True)))
     self.assertEqual(sp.piecewise_fold(cons.surplus_at(p*2)),
                      sp.Piecewise((0, 2*p < 0),
                                   (-A**2/(2*p) + A*sp.sqrt(A**2/p**2), True)))
Esempio n. 11
0
def test_piecewise_fold():

    p = Piecewise((x, x < 1), (1, 1 <= x))

    assert piecewise_fold(x*p) == Piecewise((x**2, x < 1), (x, 1 <= x))
    assert piecewise_fold(p+p) == Piecewise((2*x, x < 1), (2, 1 <= x))

    p1 = Piecewise((0, x < 0), (x, x <= 1), (0, True))
    p2 = Piecewise((0, x < 0), (1 - x, x <=1), (0, True))

    p = 4*p1 + 2*p2
    assert integrate(piecewise_fold(p),(x,-oo,oo)) == integrate(2*x + 2, (x, 0, 1))
Esempio n. 12
0
def test_piecewise_fold():

    p = Piecewise((x, x < 1), (1, 1 <= x))

    assert piecewise_fold(x*p) == Piecewise((x**2, x < 1), (x, 1 <= x))
    assert piecewise_fold(p+p) == Piecewise((2*x, x < 1), (2, 1 <= x))

    p1 = Piecewise((0, x < 0), (x, x <= 1), (0, True))
    p2 = Piecewise((0, x < 0), (1 - x, x <=1), (0, True))

    p = 4*p1 + 2*p2
    assert integrate(piecewise_fold(p),(x,-oo,oo)) == integrate(2*x + 2, (x, 0, 1))
Esempio n. 13
0
 def test_surplus(self):
     """Practice problem 2.2
     """
     sp.var('A x p')
     cons = Consumer(x, p, 2 * A * sp.sqrt(x))
     self.assertEqual(
         sp.piecewise_fold(cons.surplus()),
         sp.Piecewise((0, p < 0),
                      (-A**2 / p + 2 * A * sp.sqrt(A**2 / p**2), True)))
     self.assertEqual(
         sp.piecewise_fold(cons.surplus_at(p * 2)),
         sp.Piecewise((0, 2 * p < 0),
                      (-A**2 / (2 * p) + A * sp.sqrt(A**2 / p**2), True)))
Esempio n. 14
0
def test_piecewise_fold():
    p = Piecewise((x, x < 1), (1, 1 <= x))

    assert piecewise_fold(x * p) == Piecewise((x**2, x < 1), (x, 1 <= x))
    assert piecewise_fold(p + p) == Piecewise((2 * x, x < 1), (2, 1 <= x))
    assert piecewise_fold(Piecewise((1, x < 0), (2, True))
                          + Piecewise((10, x < 0), (-10, True))) == \
        Piecewise((11, x < 0), (-8, True))

    p1 = Piecewise((0, x < 0), (x, x <= 1), (0, True))
    p2 = Piecewise((0, x < 0), (1 - x, x <= 1), (0, True))

    p = 4 * p1 + 2 * p2
    assert integrate(piecewise_fold(p),
                     (x, -oo, oo)) == integrate(2 * x + 2, (x, 0, 1))

    assert piecewise_fold(
        Piecewise((1, y <= 0), (-Piecewise((2, y >= 0)), True))) == Piecewise(
            (1, y <= 0), (-2, y >= 0))

    assert piecewise_fold(Piecewise(
        (x, ITE(x > 0, y < 1, y > 1)))) == Piecewise(
            (x, ((x <= 0) | (y < 1)) & ((x > 0) | (y > 1))))

    a, b = (Piecewise((2, Eq(x, 0)), (0, True)),
            Piecewise((x, Eq(-x + y, 0)), (1, Eq(-x + y, 1)), (0, True)))
    assert piecewise_fold(Mul(a, b, evaluate=False)) == piecewise_fold(
        Mul(b, a, evaluate=False))
Esempio n. 15
0
def test_piecewise_fold():
    p = Piecewise((x, x < 1), (1, 1 <= x))

    assert piecewise_fold(x*p) == Piecewise((x**2, x < 1), (x, 1 <= x))
    assert piecewise_fold(p + p) == Piecewise((2*x, x < 1), (2, 1 <= x))
    assert piecewise_fold(Piecewise((1, x < 0), (2, True))
                          + Piecewise((10, x < 0), (-10, True))) == \
        Piecewise((11, x < 0), (-8, True))

    p1 = Piecewise((0, x < 0), (x, x <= 1), (0, True))
    p2 = Piecewise((0, x < 0), (1 - x, x <= 1), (0, True))

    p = 4*p1 + 2*p2
    assert integrate(
        piecewise_fold(p), (x, -oo, oo)) == integrate(2*x + 2, (x, 0, 1))

    assert piecewise_fold(
        Piecewise((1, y <= 0), (-Piecewise((2, y >= 0)), True)
        )) == Piecewise((1, y <= 0), (-2, y >= 0))

    assert piecewise_fold(Piecewise((x, ITE(x > 0, y < 1, y > 1)))
        ) == Piecewise((x, ((x <= 0) | (y < 1)) & ((x > 0) | (y > 1))))

    a, b = (Piecewise((2, Eq(x, 0)), (0, True)),
        Piecewise((x, Eq(-x + y, 0)), (1, Eq(-x + y, 1)), (0, True)))
    assert piecewise_fold(Mul(a, b, evaluate=False)
        ) == piecewise_fold(Mul(b, a, evaluate=False))
Esempio n. 16
0
def pw_roots(f, x):
	roots = []
	#This combines any addition or multiplication into the piecewise
	f = sym.piecewise_fold(f)
	def rec_search(expr):
		nonlocal roots
		if type(expr) == sym.Piecewise:
			sdoms = expr.as_expr_set_pairs()
			for sdom in sdoms:
				subf = sdom[0]
				limits = list(sdom[1].boundary)
				#print(65, limits)
				sf_eq0 = sym.Eq(subf, 0)
				if sf_eq0 == True: #function is zero
					numlims = len(limits)
					if numlims == 0: #(-inf, inf)
						#Arbitrarily return zero if the range is infinite
						root = 0
					elif numlims == 1:
						root = limits[0]
					else:
						root = float(limits[1]+limits[0])/2
					roots = np.append(roots, root)
				else:
					roots = np.append(roots, sym.solve(subf, x))
		for arg in expr.args:
			rec_search(arg)
	if type(f) == sym.Piecewise:
		rec_search(f)
	else:
		roots = sym.solve(f, x)
	return roots
Esempio n. 17
0
    def b_spline(self, n, k):
        """
        Calculates the B-Spline b and returns it in a symbolic representation
        :param n: n in b^n
        :param k: k in b_k
        :return: kth-b_spline of degree n
        """
        x = sympy.symbols('x')
        spline = None

        if n in self.b_splines and k in self.b_splines[n]:
            return self.b_splines[n][k]

        if n == 0:
            spline = sympy.Piecewise((1, (x < sympy.S(self.knot_list[k + 1])) & (x >= sympy.S(self.knot_list[k]))),
                                     (0, True))
        else:
            gamma_k = (x - sympy.S(self.knot_list[k])) / (sympy.S(self.knot_list[k + n]) - sympy.S(self.knot_list[k]))
            gamma_k_plus_1 = (x - sympy.S(self.knot_list[k + 1])) / (
                sympy.S(self.knot_list[k + 1 + n]) - sympy.S(self.knot_list[k + 1]))
            spline = sympy.piecewise_fold(
                gamma_k * self.b_spline(n - 1, k) + (1 - gamma_k_plus_1) * self.b_spline(
                    n - 1, k + 1))

        self.add_spline(spline, n, k)
        return spline
Esempio n. 18
0
def test_issue_14240():
    assert piecewise_fold(
        Piecewise((1, a), (2, b), (4, True)) +
        Piecewise((8, a), (16, True))) == Piecewise((9, a), (18, b),
                                                    (20, True))
    assert piecewise_fold(
        Piecewise((2, a), (3, b), (5, True)) * Piecewise(
            (7, a), (11, True))) == Piecewise((14, a), (33, b), (55, True))
    # these will hang if naive folding is used
    assert piecewise_fold(
        Add(*[Piecewise((i, a), (0, True)) for i in range(40)])) == Piecewise(
            (780, a), (0, True))
    assert piecewise_fold(
        Mul(*[Piecewise((i, a), (0, True))
              for i in range(1, 41)])) == Piecewise((factorial(40), a),
                                                    (0, True))
Esempio n. 19
0
 def _stress(self):
     z = sympy.Symbol('z')
     x = sympy.Symbol('x')
     props = self.laminate.piecewise_cr()
     strain = self.strain
     stress = sympy.piecewise_fold(props * self.strain)
     return stress
Esempio n. 20
0
def test_issue_14240():
    assert piecewise_fold(
        Piecewise((1, a), (2, b), (4, True)) +
        Piecewise((8, a), (16, True))
        ) == Piecewise((9, a), (18, b), (20, True))
    assert piecewise_fold(
        Piecewise((2, a), (3, b), (5, True)) *
        Piecewise((7, a), (11, True))
        ) == Piecewise((14, a), (33, b), (55, True))
    # these will hang if naive folding is used
    assert piecewise_fold(Add(*[
        Piecewise((i, a), (0, True)) for i in range(40)])
        ) == Piecewise((780, a), (0, True))
    assert piecewise_fold(Mul(*[
        Piecewise((i, a), (0, True)) for i in range(1, 41)])
        ) == Piecewise((factorial(40), a), (0, True))
Esempio n. 21
0
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
    p3 = piecewise_fold(p2)
    assert p2.subs(x, -pi / 2) == 0.0
    assert p2.subs(x, 1) == 0.0
    assert p2.subs(x, -pi / 4) == 1.0
    p4 = Piecewise((0, Eq(p1, 0)), (1, True))
    assert piecewise_fold(p4) == Piecewise((0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True))

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    assert piecewise_fold(r1) == Not(x < 1)

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True)))
    assert Piecewise((1, And(Not(x < 1), x < 0)), (0, True))
Esempio n. 22
0
def test_stackoverflow_43852159():
    f = lambda x: Piecewise((1, (x >= -1) & (x <= 1)), (0, True))
    Conv = lambda x: integrate(f(x - y) * f(y), (y, -oo, +oo))
    cx = Conv(x)
    assert cx.subs(x, -1.5) == cx.subs(x, 1.5)
    assert cx.subs(x, 3) == 0
    assert piecewise_fold(f(x - y) * f(y)) == Piecewise(
        (1, (y >= -1) & (y <= 1) & (x - y >= -1) & (x - y <= 1)), (0, True))
Esempio n. 23
0
def test_piecewise_fold_piecewise_in_cond_2():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (1 / p1, True))
    p3 = Piecewise(
        (0, (x >= 0) | Eq(cos(x), 0)),
        (1/cos(x), x < 0),
        (zoo, True))  # redundant b/c all x are already covered
    assert(piecewise_fold(p2) == p3)
Esempio n. 24
0
def test_piecewise_fold_piecewise_in_cond_2():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (1 / p1, True))
    p3 = Piecewise(
        (0, (x >= 0) | Eq(cos(x), 0)),
        (1/cos(x), x < 0),
        (zoo, True))  # redundant b/c all x are already covered
    assert(piecewise_fold(p2) == p3)
Esempio n. 25
0
def median(X, evaluate=True, **kwargs):
    r"""
    Calculuates the median of the probability distribution.
    Mathematically, median of Probability distribution is defined as all those
    values of `m` for which the following condition is satisfied

    .. math::
        P(X\geq m)\geq 1/2 \hspace{5} \text{and} \hspace{5} P(X\leq m)\geq 1/2

    Parameters
    ==========

    X: The random expression whose median is to be calculated.

    Returns
    =======

    The FiniteSet or an Interval which contains the median of the
    random expression.

    Examples
    ========

    >>> from sympy.stats import Normal, Die, median
    >>> N = Normal('N', 3, 1)
    >>> median(N)
    FiniteSet(3)
    >>> D = Die('D')
    >>> median(D)
    FiniteSet(3, 4)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Median#Probability_distributions

    """
    from sympy.stats.crv import ContinuousPSpace
    from sympy.stats.drv import DiscretePSpace
    from sympy.stats.frv import FinitePSpace

    if isinstance(pspace(X), FinitePSpace):
        cdf = pspace(X).compute_cdf(X)
        result = []
        for key, value in cdf.items():
            if value>= Rational(1, 2) and (1 - value) + \
            pspace(X).probability(Eq(X, key)) >= Rational(1, 2):
                result.append(key)
        return FiniteSet(*result)
    if isinstance(pspace(X), ContinuousPSpace) or isinstance(
            pspace(X), DiscretePSpace):
        cdf = pspace(X).compute_cdf(X)
        x = Dummy('x')
        result = solveset(piecewise_fold(cdf(x) - Rational(1, 2)), x,
                          pspace(X).set)
        return result
    raise NotImplementedError("The median of %s is not implemeted." %
                              str(pspace(X)))
Esempio n. 26
0
def test_piecewise_integrate1ca():
    y = symbols('y', real=True)
    g = Piecewise(
        (1 - x, Interval(0, 1).contains(x)),
        (1 + x, Interval(-1, 0).contains(x)),
        (0, True)
        )
    gy1 = g.integrate((x, y, 1))
    g1y = g.integrate((x, 1, y))

    assert g.integrate((x, -2, 1)) == gy1.subs(y, -2)
    assert g.integrate((x, 1, -2)) == g1y.subs(y, -2)
    assert g.integrate((x, 0, 1)) == gy1.subs(y, 0)
    assert g.integrate((x, 1, 0)) == g1y.subs(y, 0)
    # XXX Make test pass without simplify
    assert g.integrate((x, 2, 1)) == gy1.subs(y, 2).simplify()
    assert g.integrate((x, 1, 2)) == g1y.subs(y, 2).simplify()

    assert piecewise_fold(gy1.rewrite(Piecewise)) == \
        Piecewise(
            (1, y <= -1),
            (-y**2/2 - y + S(1)/2, y <= 0),
            (y**2/2 - y + S(1)/2, y < 1),
            (0, True))
    assert piecewise_fold(g1y.rewrite(Piecewise)) == \
        Piecewise(
            (-1, y <= -1),
            (y**2/2 + y - S(1)/2, y <= 0),
            (-y**2/2 + y - S(1)/2, y < 1),
            (0, True))

    # g1y and gy1 should simplify if the condition that y < 1
    # is applied, e.g. Min(1, Max(-1, y)) --> Max(-1, y)
    # XXX Make test pass without simplify
    assert gy1.simplify() == Piecewise(
        (
            -Min(1, Max(-1, y))**2/2 - Min(1, Max(-1, y)) +
            Min(1, Max(0, y))**2 + S(1)/2, y < 1),
        (0, True)
        )
    assert g1y.simplify() == Piecewise(
        (
            Min(1, Max(-1, y))**2/2 + Min(1, Max(-1, y)) -
            Min(1, Max(0, y))**2 - S(1)/2, y < 1),
        (0, True))
Esempio n. 27
0
def test_piecewise_integrate1ca():
    y = symbols('y', real=True)
    g = Piecewise(
        (1 - x, Interval(0, 1).contains(x)),
        (1 + x, Interval(-1, 0).contains(x)),
        (0, True)
        )
    gy1 = g.integrate((x, y, 1))
    g1y = g.integrate((x, 1, y))

    assert g.integrate((x, -2, 1)) == gy1.subs(y, -2)
    assert g.integrate((x, 1, -2)) == g1y.subs(y, -2)
    assert g.integrate((x, 0, 1)) == gy1.subs(y, 0)
    assert g.integrate((x, 1, 0)) == g1y.subs(y, 0)
    # XXX Make test pass without simplify
    assert g.integrate((x, 2, 1)) == gy1.subs(y, 2).simplify()
    assert g.integrate((x, 1, 2)) == g1y.subs(y, 2).simplify()

    assert piecewise_fold(gy1.rewrite(Piecewise)) == \
        Piecewise(
            (1, y <= -1),
            (-y**2/2 - y + S(1)/2, y <= 0),
            (y**2/2 - y + S(1)/2, y < 1),
            (0, True))
    assert piecewise_fold(g1y.rewrite(Piecewise)) == \
        Piecewise(
            (-1, y <= -1),
            (y**2/2 + y - S(1)/2, y <= 0),
            (-y**2/2 + y - S(1)/2, y < 1),
            (0, True))

    # g1y and gy1 should simplify if the condition that y < 1
    # is applied, e.g. Min(1, Max(-1, y)) --> Max(-1, y)
    # XXX Make test pass without simplify
    assert gy1.simplify() == Piecewise(
        (
            -Min(1, Max(-1, y))**2/2 - Min(1, Max(-1, y)) +
            Min(1, Max(0, y))**2 + S(1)/2, y < 1),
        (0, True)
        )
    assert g1y.simplify() == Piecewise(
        (
            Min(1, Max(-1, y))**2/2 + Min(1, Max(-1, y)) -
            Min(1, Max(0, y))**2 - S(1)/2, y < 1),
        (0, True))
Esempio n. 28
0
def test_stackoverflow_43852159():
    f = lambda x: Piecewise((1 , (x >= -1) & (x <= 1)) , (0, True))
    Conv = lambda x: integrate(f(x - y)*f(y), (y, -oo, +oo))
    cx = Conv(x)
    assert cx.subs(x, -1.5) == cx.subs(x, 1.5)
    assert cx.subs(x, 3) == 0
    assert piecewise_fold(f(x - y)*f(y)) == Piecewise(
        (1, (y >= -1) & (y <= 1) & (x - y >= -1) & (x - y <= 1)),
        (0, True))
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
    p3 = piecewise_fold(p2)
    assert (p2.subs(x, -pi / 2) == 0.0)
    assert (p2.subs(x, 1) == 0.0)
    assert (p2.subs(x, -pi / 4) == 1.0)
    p4 = Piecewise((0, Eq(p1, 0)), (1, True))
    assert (piecewise_fold(p4) == Piecewise(
        (0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True)))

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    assert (piecewise_fold(r1) == Not(x < 1))

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True)))
    assert (Piecewise((1, And(Not(x < 1), x < 0)), (0, True)))
Esempio n. 30
0
def test_piecewise_fold():
    p = Piecewise((x, x < 1), (1, 1 <= x))

    assert piecewise_fold(x*p) == Piecewise((x**2, x < 1), (x, 1 <= x))
    assert piecewise_fold(p + p) == Piecewise((2*x, x < 1), (2, 1 <= x))
    assert piecewise_fold(Piecewise((1, x < 0), (2, True))
                          + Piecewise((10, x < 0), (-10, True))) == \
        Piecewise((11, x < 0), (-8, True))

    p1 = Piecewise((0, x < 0), (x, x <= 1), (0, True))
    p2 = Piecewise((0, x < 0), (1 - x, x <= 1), (0, True))

    p = 4*p1 + 2*p2
    assert integrate(
        piecewise_fold(p), (x, -oo, oo)) == integrate(2*x + 2, (x, 0, 1))

    assert piecewise_fold(
        Piecewise((1, y <= 0), (-Piecewise((2, y >= 0)), True)
        )) == Piecewise((1, y <= 0), (-2, y >= 0))
Esempio n. 31
0
def test_piecewise_integrate1cb():
    y = symbols('y', real=True)
    g = Piecewise(
        (0, Or(x <= -1, x >= 1)),
        (1 - x, x > 0),
        (1 + x, True)
        )
    gy1 = g.integrate((x, y, 1))
    g1y = g.integrate((x, 1, y))

    assert g.integrate((x, -2, 1)) == gy1.subs(y, -2)
    assert g.integrate((x, 1, -2)) == g1y.subs(y, -2)
    assert g.integrate((x, 0, 1)) == gy1.subs(y, 0)
    assert g.integrate((x, 1, 0)) == g1y.subs(y, 0)
    assert g.integrate((x, 2, 1)) == gy1.subs(y, 2)
    assert g.integrate((x, 1, 2)) == g1y.subs(y, 2)

    assert piecewise_fold(gy1.rewrite(Piecewise)) == \
        Piecewise(
            (1, y <= -1),
            (-y**2/2 - y + S(1)/2, y <= 0),
            (y**2/2 - y + S(1)/2, y < 1),
            (0, True))
    assert piecewise_fold(g1y.rewrite(Piecewise)) == \
        Piecewise(
            (-1, y <= -1),
            (y**2/2 + y - S(1)/2, y <= 0),
            (-y**2/2 + y - S(1)/2, y < 1),
            (0, True))

    # g1y and gy1 should simplify if the condition that y < 1
    # is applied, e.g. Min(1, Max(-1, y)) --> Max(-1, y)
    assert gy1 == Piecewise(
        (
            -Min(1, Max(-1, y))**2/2 - Min(1, Max(-1, y)) +
            Min(1, Max(0, y))**2 + S(1)/2, y < 1),
        (0, True)
        )
    assert g1y == Piecewise(
        (
            Min(1, Max(-1, y))**2/2 + Min(1, Max(-1, y)) -
            Min(1, Max(0, y))**2 - S(1)/2, y < 1),
        (0, True))
Esempio n. 32
0
def test_piecewise_fold():

    p = Piecewise((x, x < 1), (1, 1 <= x))

    assert piecewise_fold(x*p) == Piecewise((x**2, x < 1), (x, 1 <= x))
    assert piecewise_fold(p + p) == Piecewise((2*x, x < 1), (2, 1 <= x))
    assert piecewise_fold(Piecewise((1, x < 0), (2, True))
                          + Piecewise((10, x < 0), (-10, True))) == \
        Piecewise((11, x < 0), (-8, True))

    p1 = Piecewise((0, x < 0), (x, x <= 1), (0, True))
    p2 = Piecewise((0, x < 0), (1 - x, x <= 1), (0, True))

    p = 4*p1 + 2*p2
    assert integrate(
        piecewise_fold(p), (x, -oo, oo)) == integrate(2*x + 2, (x, 0, 1))

    assert piecewise_fold(
        Piecewise((1, y <= 0), (-Piecewise((2, y >= 0)), True)
        )) == Piecewise((1, y <= 0), (-2, y >= 0))
Esempio n. 33
0
def _build_line_series(*args, **kwargs):
    """Loop over the provided arguments. If a piecewise function is found,
    decompose it in such a way that each argument gets its own series.
    """
    series = []
    for arg in args:
        expr, r, label = arg
        if expr.has(Piecewise):
            expr = piecewise_fold(expr)
            series += _process_piecewise(expr, r, label, **kwargs)
        else:
            series.append(LineOver1DRangeSeries(*arg, **kwargs))
    return series
Esempio n. 34
0
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
    assert p2.subs(x, -pi / 2) == 0
    assert p2.subs(x, 1) == 0
    assert p2.subs(x, -pi / 4) == 1
    p4 = Piecewise((0, Eq(p1, 0)), (1, True))
    ans = piecewise_fold(p4)
    for i in range(-1, 1):
        assert ans.subs(x, i) == p4.subs(x, i)

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    ans = piecewise_fold(r1)
    for i in range(2):
        assert ans.subs(x, i) == r1.subs(x, i)

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = Piecewise((1, p5 < p6), (0, True))
    ans = piecewise_fold(p7)
    for i in range(-1, 2):
        assert ans.subs(x, i) == p7.subs(x, i)
def fold_piecewises(expr):
    """Performs a piecewise_fold on the sympy expression, using a work-around to prevent errors with complex nesting.
    :param: expr the expression to piecewise_fold.
    :return: (equivalent to) piecewise_fold(expr)
    """
    # Since piecewise_fold hangs with some complicated nestings, due to simplification we use the following workaround:
    # First extract common terms, perform piecewise_fold, re-insert the common terms
    # see: https://github.com/sympy/sympy/issues/20850
    common_terms, expr = cse(expr)
    expr = piecewise_fold(expr[0])
    for term, ex in reversed(common_terms):
        expr = expr.xreplace({term: ex})
    return expr
Esempio n. 36
0
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
    p3 = piecewise_fold(p2)
    assert(p2.subs(x, -pi/2) == 0.0)
    assert(p2.subs(x, 1) == 0.0)
    assert(p2.subs(x, -pi/4) == 1.0)
    p4 = Piecewise((0, Eq(p1, 0)), (1,True))
    ans = piecewise_fold(p4)
    for i in range(-1, 1):
        assert ans.subs(x, i) == p4.subs(x, i)

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    ans = piecewise_fold(r1)
    for i in range(2):
        assert ans.subs(x, i) == r1.subs(x, i)

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = Piecewise((1, p5 < p6), (0, True))
    ans = piecewise_fold(p7)
    for i in range(-1, 2):
        assert ans.subs(x, i) == p7.subs(x, i)
Esempio n. 37
0
def piecewise_matrix(p):

    p_f = piecewise_fold(p)

    if p_f.is_Piecewise:
        nbr = p_f.args[0][0].shape[0]
        nbc = p_f.args[0][0].shape[1]

        return Matrix(
            nbr, nbc,
            lambda i, j: Piecewise(*[(e[i, j], c) for e, c in p_f.args]))

    else:
        return p
Esempio n. 38
0
def test_issue_10258():
    assert Piecewise((0, x < 1), (1, True)).is_zero is None
    assert Piecewise((-1, x < 1), (1, True)).is_zero is False
    a = Symbol('a', zero=True)
    assert Piecewise((0, x < 1), (a, True)).is_zero
    assert Piecewise((1, x < 1), (a, x < 3)).is_zero is None
    a = Symbol('a')
    assert Piecewise((0, x < 1), (a, True)).is_zero is None
    assert Piecewise((0, x < 1), (1, True)).is_nonzero is None
    assert Piecewise((1, x < 1), (2, True)).is_nonzero
    assert Piecewise((0, x < 1), (oo, True)).is_finite is None
    assert Piecewise((0, x < 1), (1, True)).is_finite
    b = Basic()
    assert Piecewise((b, x < 1)).is_finite is None

    # 10258
    c = Piecewise((1, x < 0), (2, True)) < 3
    assert c != True
    assert piecewise_fold(c) == True
Esempio n. 39
0
def test_issue_10258():
    assert Piecewise((0, x < 1), (1, True)).is_zero is None
    assert Piecewise((-1, x < 1), (1, True)).is_zero is False
    a = Symbol('a', zero=True)
    assert Piecewise((0, x < 1), (a, True)).is_zero
    assert Piecewise((1, x < 1), (a, x < 3)).is_zero is None
    a = Symbol('a')
    assert Piecewise((0, x < 1), (a, True)).is_zero is None
    assert Piecewise((0, x < 1), (1, True)).is_nonzero is None
    assert Piecewise((1, x < 1), (2, True)).is_nonzero
    assert Piecewise((0, x < 1), (oo, True)).is_finite is None
    assert Piecewise((0, x < 1), (1, True)).is_finite
    b = Basic()
    assert Piecewise((b, x < 1)).is_finite is None

    # 10258
    c = Piecewise((1, x < 0), (2, True)) < 3
    assert c != True
    assert piecewise_fold(c) == True
Esempio n. 40
0
def test_piecewise_complex():
    p1 = Piecewise((2, x < 0), (1, 0 <= x))
    p2 = Piecewise((2*I, x < 0), (I, 0 <= x))
    p3 = Piecewise((I*x, x > 1), (1 + I, True))
    p4 = Piecewise((-I*conjugate(x), x > 1), (1 - I, True))

    assert conjugate(p1) == p1
    assert conjugate(p2) == piecewise_fold(-p2)
    assert conjugate(p3) == p4

    assert p1.is_imaginary is False
    assert p1.is_real is True
    assert p2.is_imaginary is True
    assert p2.is_real is False
    assert p3.is_imaginary is None
    assert p3.is_real is None

    assert p1.as_real_imag() == (p1, 0)
    assert p2.as_real_imag() == (0, -I*p2)
def test_piecewise_complex():
    p1 = Piecewise((2, x < 0), (1, 0 <= x))
    p2 = Piecewise((2 * I, x < 0), (I, 0 <= x))
    p3 = Piecewise((I * x, x > 1), (1 + I, True))
    p4 = Piecewise((-I * conjugate(x), x > 1), (1 - I, True))

    assert conjugate(p1) == p1
    assert conjugate(p2) == piecewise_fold(-p2)
    assert conjugate(p3) == p4

    assert p1.is_imaginary is False
    assert p1.is_real is True
    assert p2.is_imaginary is True
    assert p2.is_real is False
    assert p3.is_imaginary is None
    assert p3.is_real is None

    assert p1.as_real_imag() == (p1, 0)
    assert p2.as_real_imag() == (0, -I * p2)
Esempio n. 42
0
def _simpsol(soleq):
    lhs = soleq.lhs
    sol = soleq.rhs
    sol = powsimp(sol)
    gens = list(sol.atoms(exp))
    p = Poly(sol, *gens, expand=False)
    gens = [factor_terms(g) for g in gens]
    if not gens:
        gens = p.gens
    syms = [Symbol('C1'), Symbol('C2')]
    terms = []
    for coeff, monom in zip(p.coeffs(), p.monoms()):
        coeff = piecewise_fold(coeff)
        if type(coeff) is Piecewise:
            coeff = Piecewise(*((ratsimp(coef).collect(syms), cond)
                                for coef, cond in coeff.args))
        else:
            coeff = ratsimp(coeff).collect(syms)
        monom = Mul(*(g**i for g, i in zip(gens, monom)))
        terms.append(coeff * monom)
    return Eq(lhs, Add(*terms))
Esempio n. 43
0
    def equilibrium(self, rational=True):
        """
        >>> sp.var('x p', positive=True)
        (x, p)
        >>> consumer = Consumer(x, p, 20*sp.sqrt(x), W=100)
        >>> cons_aggregate = ConsumerAggregate((consumer, 100))
        >>> cons_aggregate.demand()
        Piecewise((0, p < 0), (10000/p**2, 0 < p), (0, True))
        >>> firm = Firm(x, p, 1/2. * x**2, SFC=0, FC=0)
        >>> firm_aggregate = ProducerAggregate((firm, 10))
        >>> firm_aggregate.supply()
        Piecewise((0, p < 0), (10*p, And(0 <= p, p >= 0)))
        >>> mkt = Market(x, p,
        ...              cons_aggregate.demand(),
        ...              firm_aggregate.supply())
        >>> mkt.equilibrium()
        (10, 100)
        >>> mkt = Market(x, p,
        ...              demand=1000-p,
        ...              supply=sp.Eq(p, 100))
        >>> mkt.equilibrium()
        (100, 900)
        """
        demand = self.demand
        if not rational:
            demand = self.deluded_demand
        eq = sp.solve((et.implicit(self.q, demand),
                       et.implicit(self.q, self.supply)),
                      self.p, self.q, dict=True)
        if eq:
            eq = eq[-1]
            return eq[self.p], eq[self.q] #self.supply.subs(self.p, peq)

        peq = sp.solve(sp.piecewise_fold(demand - self.supply), self.p)
        if peq:
            peq = peq[-1]
            return peq, self.supply.subs(self.p, peq)
        return None, None
Esempio n. 44
0
            x, 16)),  # continuidad en theta en x=16     
        sp.Eq(M2.subs(x, 16), M3.subs(
            x, 16)),  # continuidad en M     en x=16     
        sp.Eq(M3.subs(x, 19), 0),  # momento flector en x=19 es 0     
        sp.Eq(V3.subs(x, 19), 15)  # fuerza cortante en x=19 es 15    
    ],
    [C1_1, C1_2, C1_3, C1_4, C2_1, C2_2, C2_3, C2_4, C3_1, C3_2, C3_3, C3_4])

# %% Se fusionan las fórmulas y se reemplaza el valor de las constantes
V = (V1 * rect(0, 10) + V2 * rect(10, 16) + V3 * rect(16, 19)).subs(sol)
M = (M1 * rect(0, 10) + M2 * rect(10, 16) + M3 * rect(16, 19)).subs(sol)
t = (t1 * rect(0, 10) + t2 * rect(10, 16) + t3 * rect(16, 19)).subs(sol)
v = (v1 * rect(0, 10) + v2 * rect(10, 16) + v3 * rect(16, 19)).subs(sol)

# %% Se simplifica lo calculado por sympy
V = sp.piecewise_fold(V.rewrite(sp.Piecewise))  #.nsimplify()
M = sp.piecewise_fold(M.rewrite(sp.Piecewise))  #.nsimplify()
t = sp.piecewise_fold(t.rewrite(sp.Piecewise))
v = sp.piecewise_fold(v.rewrite(sp.Piecewise))

# %% Se imprimen los resultados
print("\n\nV(x) = ")
sp.pprint(V)
print("\n\nM(x) = ")
sp.pprint(M)
print("\n\nt(x) = ")
sp.pprint(t)
print("\n\nv(x) = ")
sp.pprint(v)

# %% Se grafican los resultados
Esempio n. 45
0
 def inv(f): return piecewise_fold(meijerint_inversion(f, s, t))
 assert inv(1/(s**2 + 1)) == sin(t)*Heaviside(t)
Esempio n. 46
0
def test_piecewise_integrate1():
    x, y = symbols('x y', real=True, finite=True)

    f = Piecewise(((x - 2)**2, x >= 0), (1, True))
    assert integrate(f, (x, -2, 2)) == Rational(14, 3)

    g = Piecewise(((x - 5)**5, x >= 4), (f, True))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == Rational(43, 6)

    assert g == Piecewise(((x - 5)**5, x >= 4), (f, x < 4))

    g = Piecewise(((x - 5)**5, 2 <= x), (f, x < 2))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == -Rational(701, 6)

    assert g == Piecewise(((x - 5)**5, 2 <= x), (f, True))

    g = Piecewise(((x - 5)**5, 2 <= x), (2 * f, True))
    assert integrate(g, (x, -2, 2)) == 2 * Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == -Rational(673, 6)

    g = Piecewise((1, x > 0), (0, Eq(x, 0)), (-1, x < 0))
    assert integrate(g, (x, -1, 1)) == 0

    g = Piecewise((1, x - y < 0), (0, True))
    assert integrate(g, (y, -oo, 0)) == -Min(0, x)
    assert g.subs(x, -3).integrate((y, -oo, 0)) == 3
    assert integrate(g, (y, 0, -oo)) == Min(0, x)
    assert integrate(g, (y, 0, oo)) == -Max(0, x) + oo
    assert integrate(g, (y, -oo, 42)) == -Min(42, x) + 42
    assert integrate(g, (y, -oo, oo)) == -x + oo

    g = Piecewise((0, x < 0), (x, x <= 1), (1, True))
    assert integrate(g, (x, -5, 1)) == Rational(1, 2)
    assert integrate(g, (x, -5, y)).subs(y, 1) == Rational(1, 2)
    assert integrate(g, (x, y, 1)).subs(y, -5) == Rational(1, 2)
    assert integrate(g, (x, 1, -5)) == -Rational(1, 2)
    assert integrate(g, (x, 1, y)).subs(y, -5) == -Rational(1, 2)
    assert integrate(g, (x, y, -5)).subs(y, 1) == -Rational(1, 2)
    assert integrate(g, (x, -5, y)) == Piecewise(
        (y - Min(0, y)**2 / 2 + Min(1, y)**2 / 2 - Min(1, y), y > -5),
        (0, True))
    assert integrate(g, (x, y, 1)) == Piecewise(
        (-Min(1, Max(0, y))**2 / 2 + 1 / 2, y < 1), (-y + 1, True))

    for j, g in enumerate([
            Piecewise((1 - x, Interval(0, 1).contains(x)),
                      (1 + x, Interval(-1, 0).contains(x)), (0, True)),
            Piecewise((0, Or(x <= -1, x >= 1)), (1 - x, x > 0), (1 + x, True))
    ]):
        assert integrate(g, (x, -5, 1)) == 1
        assert integrate(g, (x, 1, -5)) == -1
        assert integrate(g, (x, 1, y)).subs(y, -5) == -1
        assert integrate(g, (x, y, -5)).subs(y, 1) == -1
        i = integrate(g, (x, -5, y))
        assert i.subs(y, 1) == 1
        assert piecewise_fold(i.rewrite(Piecewise)) == Piecewise(
            (1, y >= 1), (-y**2 / 2 + y + 1 / 2, y >= 0),
            (y**2 / 2 + y + 1 / 2, y >= -1), (0, True))
        assert i == Piecewise((-Min(-1, y)**2 / 2 - Min(-1, y) + Min(0, y)**2 -
                               Min(1, y)**2 / 2 + Min(1, y), y > -5),
                              (0, True))
        i = integrate(g, (x, y, 1))
        assert i.subs(y, -5) == 1
        assert piecewise_fold(i.rewrite(Piecewise)) == Piecewise(
            (1, y <= -1), (-y**2 / 2 - y + 1 / 2, y <= 0),
            (y**2 / 2 - y + 1 / 2, y < 1), (0, True))
        # the solutions are different because
        # Min(1, Max(-1, y), Max(0, y)) is not
        # simplifying to Min(1, Max(-1, y))
        assert i == [
            Piecewise(
                (-Min(1, Max(-1, y), Max(0, y))**2 / 2 -
                 Min(1, Max(-1, y), Max(0, y)) + Min(1, Max(0, y))**2 + 1 / 2,
                 y < 1), (0, True)),
            Piecewise((-Min(1, Max(-1, y))**2 / 2 - Min(1, Max(-1, y)) +
                       Min(1, Max(0, y))**2 + 1 / 2, y < 1), (0, True))
        ][j]
Esempio n. 47
0
def test_piecewise_fold_expand():
    p1 = Piecewise((1, Interval(0, 1, False, True).contains(x)), (0, True))

    p2 = piecewise_fold(expand((1 - x) * p1))
    assert p2 == Piecewise((1 - x, (x >= 0) & (x < 1)), (0, True))
    assert p2 == expand(piecewise_fold((1 - x) * p1))
Esempio n. 48
0
def _simplify(expr, doit):
    from sympy import powdenest, piecewise_fold
    if doit:
        return simplify(powdenest(piecewise_fold(expr), polar=True))
    return expr
Esempio n. 49
0
def test_piecewise_fold_expand():
    p1 = Piecewise((1, Interval(0, 1, False, True).contains(x)), (0, True))

    p2 = piecewise_fold(expand((1 - x)*p1))
    assert p2 == Piecewise((1 - x, (x >= 0) & (x < 1)), (0, True))
    assert p2 == expand(piecewise_fold((1 - x)*p1))
Esempio n. 50
0
def test_piecewise_integrate1():
    x, y = symbols('x y', real=True, finite=True)

    f = Piecewise(((x - 2)**2, x >= 0), (1, True))
    assert integrate(f, (x, -2, 2)) == Rational(14, 3)

    g = Piecewise(((x - 5)**5, x >= 4), (f, True))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == Rational(43, 6)

    assert g == Piecewise(((x - 5)**5, x >= 4), (f, x < 4))

    g = Piecewise(((x - 5)**5, 2 <= x), (f, x < 2))
    assert integrate(g, (x, -2, 2)) == Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == -Rational(701, 6)

    assert g == Piecewise(((x - 5)**5, 2 <= x), (f, True))

    g = Piecewise(((x - 5)**5, 2 <= x), (2*f, True))
    assert integrate(g, (x, -2, 2)) == 2 * Rational(14, 3)
    assert integrate(g, (x, -2, 5)) == -Rational(673, 6)

    g = Piecewise((1, x > 0), (0, Eq(x, 0)), (-1, x < 0))
    assert integrate(g, (x, -1, 1)) == 0

    g = Piecewise((1, x - y < 0), (0, True))
    assert integrate(g, (y, -oo, 0)) == -Min(0, x)
    assert g.subs(x, -3).integrate((y, -oo, 0)) == 3
    assert integrate(g, (y, 0, -oo)) == Min(0, x)
    assert integrate(g, (y, 0, oo)) == -Max(0, x) + oo
    assert integrate(g, (y, -oo, 42)) == -Min(42, x) + 42
    assert integrate(g, (y, -oo, oo)) == -x + oo

    g = Piecewise((0, x < 0), (x, x <= 1), (1, True))
    assert integrate(g, (x, -5, 1)) == Rational(1, 2)
    assert integrate(g, (x, -5, y)).subs(y, 1) == Rational(1, 2)
    assert integrate(g, (x, y, 1)).subs(y, -5) == Rational(1, 2)
    assert integrate(g, (x, 1, -5)) == -Rational(1, 2)
    assert integrate(g, (x, 1, y)).subs(y, -5) == -Rational(1, 2)
    assert integrate(g, (x, y, -5)).subs(y, 1) == -Rational(1, 2)
    assert integrate(g, (x, -5, y)) == Piecewise(
        (y - Min(0, y)**2/2 + Min(1, y)**2/2 - Min(1, y), y > -5),
        (0, True))
    assert integrate(g, (x, y, 1)) == Piecewise(
        (-Min(1, Max(0, y))**2/2 + 1/2, y < 1),
        (-y + 1, True))

    for j, g in enumerate([
        Piecewise((1 - x, Interval(0, 1).contains(x)),
            (1 + x, Interval(-1, 0).contains(x)), (0, True)),
        Piecewise((0, Or(x <= -1, x >= 1)), (1 - x, x > 0),
            (1 + x, True))]):
        assert integrate(g, (x, -5, 1)) == 1
        assert integrate(g, (x, 1, -5)) == -1
        assert integrate(g, (x, 1, y)).subs(y, -5) == -1
        assert integrate(g, (x, y, -5)).subs(y, 1) == -1
        i = integrate(g, (x, -5, y))
        assert i.subs(y, 1) == 1
        assert piecewise_fold(i.rewrite(Piecewise)
            ) == Piecewise(
            (1, y >= 1),
            (-y**2/2 + y + 1/2, y >= 0),
            (y**2/2 + y + 1/2, y >= -1),
            (0, True))
        assert i == Piecewise(
            (-Min(-1, y)**2/2 - Min(-1, y) +
                Min(0, y)**2 - Min(1, y)**2/2 + Min(1, y), y > -5),
            (0, True))
        i = integrate(g, (x, y, 1))
        assert i.subs(y, -5) == 1
        assert piecewise_fold(i.rewrite(Piecewise)
            )== Piecewise(
            (1, y <= -1),
            (-y**2/2 - y + 1/2, y <= 0),
            (y**2/2 - y + 1/2, y < 1),
            (0, True))
        # the solutions are different because
        # Min(1, Max(-1, y), Max(0, y)) is not
        # simplifying to Min(1, Max(-1, y))
        assert i == [
            Piecewise(
                (
                -Min(1, Max(-1, y), Max(0, y))**2/2
                -Min(1, Max(-1, y), Max(0, y))
                +Min(1, Max(0, y))**2
                +1/2,
                    y < 1),
                (0, True)),
            Piecewise(
                (
                -Min(1, Max(-1, y))**2/2
                -Min(1, Max(-1, y))
                +Min(1, Max(0, y))**2
                +1/2,
                    y < 1),
                (0, True))][j]
Esempio n. 51
0
def test_piecewise_fold_piecewise_in_cond_2():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (1 / p1, True))
    p3 = Piecewise((0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))),
        (1 / cos(x), True))
    assert(piecewise_fold(p2) == p3)
Esempio n. 52
0
    def _eval_as_leading_term(self, x, logx=None, cdir=0):
        from sympy import expand_mul, Order, Piecewise, piecewise_fold, log

        old = self

        if old.has(Piecewise):
            old = piecewise_fold(old)

        # This expansion is the last part of expand_log. expand_log also calls
        # expand_mul with factor=True, which would be more expensive
        if any(isinstance(a, log) for a in self.args):
            logflags = dict(deep=True,
                            log=True,
                            mul=False,
                            power_exp=False,
                            power_base=False,
                            multinomial=False,
                            basic=False,
                            force=False,
                            factor=False)
            old = old.expand(**logflags)
        expr = expand_mul(old)

        if not expr.is_Add:
            return expr.as_leading_term(x, logx=logx, cdir=cdir)

        infinite = [t for t in expr.args if t.is_infinite]

        leading_terms = [
            t.as_leading_term(x, logx=logx, cdir=cdir) for t in expr.args
        ]

        min, new_expr = Order(0), 0

        try:
            for term in leading_terms:
                order = Order(term, x)
                if not min or order not in min:
                    min = order
                    new_expr = term
                elif min in order:
                    new_expr += term

        except TypeError:
            return expr

        is_zero = new_expr.is_zero
        if is_zero is None:
            new_expr = new_expr.trigsimp().cancel()
            is_zero = new_expr.is_zero
        if is_zero is True:
            # simple leading term analysis gave us cancelled terms but we have to send
            # back a term, so compute the leading term (via series)
            n0 = min.getn()
            res = Order(1)
            incr = S.One
            while res.is_Order:
                res = old._eval_nseries(
                    x, n=n0 + incr, logx=None,
                    cdir=cdir).cancel().powsimp().trigsimp()
                incr *= 2
            return res.as_leading_term(x, logx=logx, cdir=cdir)

        elif new_expr is S.NaN:
            return old.func._from_args(infinite)

        else:
            return new_expr
def test_piecewise_fold_piecewise_in_cond_2():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (1 / p1, True))
    p3 = Piecewise((0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))),
                   (1 / cos(x), True))
    assert (piecewise_fold(p2) == p3)
Esempio n. 54
0
 def inv(f):
     return piecewise_fold(meijerint_inversion(f, s, t))
Esempio n. 55
0
 def inv(f):
     return piecewise_fold(meijerint_inversion(f, s, t))
Esempio n. 56
0
 def demand(self, rational=True):
     aggregate = sp.Piecewise((0, True))
     for consumer, n in self.consumers():
         aggregate = sp.piecewise_fold(aggregate +
                                       n*consumer.demand(rational))
     return aggregate