本文整理汇总了Python中sympy.piecewise_fold函数的典型用法代码示例。如果您正苦于以下问题:Python piecewise_fold函数的具体用法?Python piecewise_fold怎么用?Python piecewise_fold使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了piecewise_fold函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_piecewise_integrate1c
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))
示例2: supply
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
示例3: test_piecewise_fold_expand
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))
示例4: test_issue_10087
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)
示例5: test_surplus
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)))
示例6: test_piecewise_fold
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))
示例7: test_piecewise_fold
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))
示例8: b_spline
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
示例9: test_issue_14240
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))
示例10: _stress
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
示例11: test_piecewise_fold_piecewise_in_cond_2
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)
示例12: test_piecewise_fold_piecewise_in_cond
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))
示例13: test_piecewise_integrate1ca
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))
示例14: test_stackoverflow_43852159
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))
示例15: test_piecewise_fold
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))