def test_deltaproduct_mul_add_x_kd_add_y_kd(): assert dp((x + KD(i, k))*(y + KD(i, j)), (j, 1, 3)) == \ KD(i, 1)*(KD(i, k) + x)*((KD(i, k) + x)*y)**2 + \ KD(i, 2)*(KD(i, k) + x)*y*(KD(i, k) + x)**2*y + \ KD(i, 3)*((KD(i, k) + x)*y)**2*(KD(i, k) + x) + \ ((KD(i, k) + x)*y)**3 assert dp((x + KD(i, k))*(y + KD(i, j)), (j, 1, 1)) == \ (x + KD(i, k))*(y + KD(i, 1)) assert dp((x + KD(i, k))*(y + KD(i, j)), (j, 2, 2)) == \ (x + KD(i, k))*(y + KD(i, 2)) assert dp((x + KD(i, k))*(y + KD(i, j)), (j, 3, 3)) == \ (x + KD(i, k))*(y + KD(i, 3)) assert dp((x + KD(i, k))*(y + KD(i, j)), (j, 1, k)) == \ ((x + KD(i, k))*y)**k + Piecewise( (((x + KD(i, k))*y)**(i - 1)*(x + KD(i, k)) * ((x + KD(i, k))*y)**(-i + k), And(Integer(1) <= i, i <= k)), (0, True) ) assert dp((x + KD(i, k))*(y + KD(i, j)), (j, k, 3)) == \ ((x + KD(i, k))*y)**(4 - k) + Piecewise( (((x + KD(i, k))*y)**(i - k)*(x + KD(i, k)) * ((x + KD(i, k))*y)**(-i + 3), And(k <= i, i <= 3)), (0, True) ) assert dp((x + KD(i, k))*(y + KD(i, j)), (j, k, l)) == \ ((x + KD(i, k))*y)**(-k + l + 1) + Piecewise( (((x + KD(i, k))*y)**(i - k)*(x + KD(i, k)) * ((x + KD(i, k))*y)**(-i + l), And(k <= i, i <= l)), (0, True) )
def test_deltaproduct_mul_add_x_y_add_y_kd(): assert dp((x + y)*(y + KD(i, j)), (j, 1, 3)) == ((x + y)*y)**3 + \ (x + y)*((x + y)*y)**2*KD(i, 1) + \ (x + y)*y*(x + y)**2*y*KD(i, 2) + \ ((x + y)*y)**2*(x + y)*KD(i, 3) assert dp((x + y) * (y + KD(i, j)), (j, 1, 1)) == (x + y) * (y + KD(i, 1)) assert dp((x + y) * (y + KD(i, j)), (j, 2, 2)) == (x + y) * (y + KD(i, 2)) assert dp((x + y) * (y + KD(i, j)), (j, 3, 3)) == (x + y) * (y + KD(i, 3)) assert dp((x + y)*(y + KD(i, j)), (j, 1, k)) == \ ((x + y)*y)**k + Piecewise( (((x + y)*y)**(i - 1)*(x + y)*((x + y)*y)**(k - i), And(Integer(1) <= i, i <= k)), (0, True) ) assert dp((x + y)*(y + KD(i, j)), (j, k, 3)) == \ ((x + y)*y)**(-k + 4) + Piecewise( (((x + y)*y)**(i - k)*(x + y)*((x + y)*y)**(3 - i), And(k <= i, i <= 3)), (0, True) ) assert dp((x + y)*(y + KD(i, j)), (j, k, l)) == \ ((x + y)*y)**(-k + l + 1) + Piecewise( (((x + y)*y)**(i - k)*(x + y)*((x + y)*y)**(l - i), And(k <= i, i <= l)), (0, True) )
def test_deltasummation_mul_x_kd(): assert ds(x*KD(i, j), (j, 1, 3)) == \ Piecewise((x, And(Integer(1) <= i, i <= 3)), (0, True)) assert ds(x * KD(i, j), (j, 1, 1)) == Piecewise((x, Eq(i, 1)), (0, True)) assert ds(x * KD(i, j), (j, 2, 2)) == Piecewise((x, Eq(i, 2)), (0, True)) assert ds(x * KD(i, j), (j, 3, 3)) == Piecewise((x, Eq(i, 3)), (0, True)) assert ds(x*KD(i, j), (j, 1, k)) == \ Piecewise((x, And(Integer(1) <= i, i <= k)), (0, True)) assert ds(x*KD(i, j), (j, k, 3)) == \ Piecewise((x, And(k <= i, i <= 3)), (0, True)) assert ds(x*KD(i, j), (j, k, l)) == \ Piecewise((x, And(k <= i, i <= l)), (0, True))
def test_deltasummation_basic_symbolic(): assert ds(KD(i, j), (j, 1, 3)) == \ Piecewise((1, And(Integer(1) <= i, i <= 3)), (0, True)) assert ds(KD(i, j), (j, 1, 1)) == Piecewise((1, Eq(i, 1)), (0, True)) assert ds(KD(i, j), (j, 2, 2)) == Piecewise((1, Eq(i, 2)), (0, True)) assert ds(KD(i, j), (j, 3, 3)) == Piecewise((1, Eq(i, 3)), (0, True)) assert ds(KD(i, j), (j, 1, k)) == \ Piecewise((1, And(Integer(1) <= i, i <= k)), (0, True)) assert ds(KD(i, j), (j, k, 3)) == \ Piecewise((1, And(k <= i, i <= 3)), (0, True)) assert ds(KD(i, j), (j, k, l)) == \ Piecewise((1, And(k <= i, i <= l)), (0, True))
def reduce_piecewise_inequalities(exprs, gen): """ Reduce a system of inequalities with nested piecewise functions. Examples ======== >>> from diofant import Abs, Symbol >>> from diofant.solvers.inequalities import reduce_piecewise_inequalities >>> x = Symbol('x', real=True) >>> reduce_piecewise_inequalities([(Abs(3*x - 5) - 7, '<'), ... (Abs(x + 25) - 13, '>')], x) And(-2/3 < x, Or(-12 < x, x < -38), x < 4) >>> reduce_piecewise_inequalities([(Abs(x - 4) + Abs(3*x - 5) - 7, '<')], x) And(1/2 < x, x < 4) See Also ======== reduce_piecewise_inequality """ return And( *[reduce_piecewise_inequality(expr, rel, gen) for expr, rel in exprs])
def test_deltasummation_mul_x_add_y_kd(): assert ds(x*(y + KD(i, j)), (j, 1, 3)) == \ Piecewise((3*x*y + x, And(Integer(1) <= i, i <= 3)), (3*x*y, True)) assert ds(x*(y + KD(i, j)), (j, 1, 1)) == \ Piecewise((x*y + x, Eq(i, 1)), (x*y, True)) assert ds(x*(y + KD(i, j)), (j, 2, 2)) == \ Piecewise((x*y + x, Eq(i, 2)), (x*y, True)) assert ds(x*(y + KD(i, j)), (j, 3, 3)) == \ Piecewise((x*y + x, Eq(i, 3)), (x*y, True)) assert ds(x*(y + KD(i, j)), (j, 1, k)) == \ Piecewise((k*x*y + x, And(Integer(1) <= i, i <= k)), (k*x*y, True)) assert ds(x*(y + KD(i, j)), (j, k, 3)) == \ Piecewise(((4 - k)*x*y + x, And(k <= i, i <= 3)), ((4 - k)*x*y, True)) assert ds(x * (y + KD(i, j)), (j, k, l)) == Piecewise( ((l - k + 1) * x * y + x, And(k <= i, i <= l)), ((l - k + 1) * x * y, True))
def _reduce_inequalities(inequalities, symbols): # helper for reduce_inequalities poly_part, pw_part = {}, {} other = [] for inequality in inequalities: if inequality is S.true: continue elif inequality is S.false: return S.false expr, rel = inequality.lhs, inequality.rel_op # rhs is 0 # check for gens using atoms which is more strict than free_symbols to # guard against EX domain which won't be handled by # reduce_rational_inequalities gens = expr.atoms(Dummy, Symbol) if len(gens) == 1: gen = gens.pop() else: common = expr.free_symbols & symbols if len(common) == 1: gen = common.pop() other.append( solve_univariate_inequality(Relational(expr, 0, rel), gen)) continue else: raise NotImplementedError( filldedent(''' inequality has more than one symbol of interest''')) if expr.is_polynomial(gen): poly_part.setdefault(gen, []).append((expr, rel)) else: components = expr.find(lambda u: u.has(gen) and ( u.is_Function or u.is_Pow and not u.exp.is_Integer)) if components and all( isinstance(i, Abs) or isinstance(i, Piecewise) for i in components): pw_part.setdefault(gen, []).append((expr, rel)) else: other.append( solve_univariate_inequality(Relational(expr, 0, rel), gen)) poly_reduced = [] pw_reduced = [] for gen, exprs in poly_part.items(): poly_reduced.append(reduce_rational_inequalities([exprs], gen)) for gen, exprs in pw_part.items(): pw_reduced.append(reduce_piecewise_inequalities(exprs, gen)) return And(*(poly_reduced + pw_reduced + other))
def test_global_dict(): global_dict = { 'Symbol': Symbol } inputs = { 'Q & S': And(Symbol('Q'), Symbol('S')) } for text, result in inputs.items(): assert parse_expr(text, global_dict=global_dict) == result
def test_deltasummation_mul_add_x_kd_add_y_kd(): assert ds((x + KD(i, k)) * (y + KD(i, j)), (j, 1, 3)) == piecewise_fold( Piecewise((KD(i, k) + x, And(Integer(1) <= i, i <= 3)), (0, True)) + 3 * (KD(i, k) + x) * y) assert ds((x + KD(i, k)) * (y + KD(i, j)), (j, 1, 1)) == piecewise_fold( Piecewise((KD(i, k) + x, Eq(i, 1)), (0, True)) + (KD(i, k) + x) * y) assert ds((x + KD(i, k)) * (y + KD(i, j)), (j, 2, 2)) == piecewise_fold( Piecewise((KD(i, k) + x, Eq(i, 2)), (0, True)) + (KD(i, k) + x) * y) assert ds((x + KD(i, k)) * (y + KD(i, j)), (j, 3, 3)) == piecewise_fold( Piecewise((KD(i, k) + x, Eq(i, 3)), (0, True)) + (KD(i, k) + x) * y) assert ds((x + KD(i, k)) * (y + KD(i, j)), (j, 1, k)) == piecewise_fold( Piecewise((KD(i, k) + x, And(Integer(1) <= i, i <= k)), (0, True)) + k * (KD(i, k) + x) * y) assert ds((x + KD(i, k)) * (y + KD(i, j)), (j, k, 3)) == piecewise_fold( Piecewise((KD(i, k) + x, And(k <= i, i <= 3)), (0, True)) + (4 - k) * (KD(i, k) + x) * y) assert ds((x + KD(i, k)) * (y + KD(i, j)), (j, k, l)) == piecewise_fold( Piecewise((KD(i, k) + x, And(k <= i, i <= l)), (0, True)) + (l - k + 1) * (KD(i, k) + x) * y)
def test_deltaproduct_mul_x_add_y_twokd(): assert dp(x*(y + 2*KD(i, j)), (j, 1, 3)) == (x*y)**3 + \ 2*x*(x*y)**2*KD(i, 1) + 2*x*y*x*x*y*KD(i, 2) + 2*(x*y)**2*x*KD(i, 3) assert dp(x * (y + 2 * KD(i, j)), (j, 1, 1)) == x * (y + 2 * KD(i, 1)) assert dp(x * (y + 2 * KD(i, j)), (j, 2, 2)) == x * (y + 2 * KD(i, 2)) assert dp(x * (y + 2 * KD(i, j)), (j, 3, 3)) == x * (y + 2 * KD(i, 3)) assert dp(x*(y + 2*KD(i, j)), (j, 1, k)) == \ (x*y)**k + Piecewise( (2*(x*y)**(i - 1)*x*(x*y)**(k - i), And(Integer(1) <= i, i <= k)), (0, True) ) assert dp(x*(y + 2*KD(i, j)), (j, k, 3)) == \ (x*y)**(-k + 4) + Piecewise( (2*(x*y)**(i - k)*x*(x*y)**(3 - i), And(k <= i, i <= 3)), (0, True) ) assert dp(x*(y + 2*KD(i, j)), (j, k, l)) == \ (x*y)**(-k + l + 1) + Piecewise( (2*(x*y)**(i - k)*x*(x*y)**(l - i), And(k <= i, i <= l)), (0, True) )
def _bottom_up_scan(expr): exprs = [] if expr.is_Add or expr.is_Mul: op = expr.func for arg in expr.args: _exprs = _bottom_up_scan(arg) if not exprs: exprs = _exprs else: args = [] for expr, conds in exprs: for _expr, _conds in _exprs: args.append((op(expr, _expr), conds + _conds)) exprs = args elif expr.is_Pow: n = expr.exp if not n.is_Integer: raise NotImplementedError("only integer powers are supported") _exprs = _bottom_up_scan(expr.base) for expr, conds in _exprs: exprs.append((expr**n, conds)) elif isinstance(expr, Abs): _exprs = _bottom_up_scan(expr.args[0]) for expr, conds in _exprs: exprs.append((expr, conds + [Ge(expr, 0)])) exprs.append((-expr, conds + [Lt(expr, 0)])) elif isinstance(expr, Piecewise): for a in expr.args: _exprs = _bottom_up_scan(a.expr) for ex, conds in _exprs: if a.cond is not S.true: exprs.append((ex, conds + [a.cond])) else: oconds = [ c[1] for c in expr.args if c[1] is not S.true ] exprs.append( (ex, conds + [And(*[~c for c in oconds])])) else: exprs = [(expr, [])] return exprs
def test_deltasummation_basic_numerical(): n = symbols('n', integer=True, nonzero=True) assert ds(KD(n, 0), (n, 1, 3)) == 0 # return unevaluated, until it gets implemented assert ds(KD(i**2, j**2), (j, -oo, oo)) == \ Sum(KD(i**2, j**2), (j, -oo, oo)) assert Piecewise((KD(i, k), And(Integer(1) <= i, i <= 3)), (0, True)) == \ ds(KD(i, j)*KD(j, k), (j, 1, 3)) == \ ds(KD(j, k)*KD(i, j), (j, 1, 3)) assert ds(KD(i, k), (k, -oo, oo)) == 1 assert ds(KD(i, k), (k, 0, oo)) == Piecewise((1, Integer(0) <= i), (0, True)) assert ds(KD(i, k), (k, 1, 3)) == \ Piecewise((1, And(Integer(1) <= i, i <= 3)), (0, True)) assert ds(k * KD(i, j) * KD(j, k), (k, -oo, oo)) == j * KD(i, j) assert ds(j * KD(i, j), (j, -oo, oo)) == i assert ds(i * KD(i, j), (i, -oo, oo)) == j assert ds(x, (i, 1, 3)) == 3 * x assert ds((i + j) * KD(i, j), (j, -oo, oo)) == 2 * i
def test_deltasummation_mul_add_x_y_add_kd_kd(): assert ds((x + y) * (KD(i, k) + KD(j, k)), (k, 1, 3)) == piecewise_fold( Piecewise((x + y, And(Integer(1) <= i, i <= 3)), (0, True)) + Piecewise((x + y, And(Integer(1) <= j, j <= 3)), (0, True))) assert ds((x + y) * (KD(i, k) + KD(j, k)), (k, 1, 1)) == piecewise_fold( Piecewise((x + y, Eq(i, 1)), (0, True)) + Piecewise((x + y, Eq(j, 1)), (0, True))) assert ds((x + y) * (KD(i, k) + KD(j, k)), (k, 2, 2)) == piecewise_fold( Piecewise((x + y, Eq(i, 2)), (0, True)) + Piecewise((x + y, Eq(j, 2)), (0, True))) assert ds((x + y) * (KD(i, k) + KD(j, k)), (k, 3, 3)) == piecewise_fold( Piecewise((x + y, Eq(i, 3)), (0, True)) + Piecewise((x + y, Eq(j, 3)), (0, True))) assert ds((x + y) * (KD(i, k) + KD(j, k)), (k, 1, l)) == piecewise_fold( Piecewise((x + y, And(Integer(1) <= i, i <= l)), (0, True)) + Piecewise((x + y, And(Integer(1) <= j, j <= l)), (0, True))) assert ds((x + y) * (KD(i, k) + KD(j, k)), (k, l, 3)) == piecewise_fold( Piecewise((x + y, And(l <= i, i <= 3)), (0, True)) + Piecewise((x + y, And(l <= j, j <= 3)), (0, True))) assert ds((x + y) * (KD(i, k) + KD(j, k)), (k, l, m)) == piecewise_fold( Piecewise((x + y, And(l <= i, i <= m)), (0, True)) + Piecewise((x + y, And(l <= j, j <= m)), (0, True)))