def _print_Min(self, expr): if "Min" in self.known_functions: return self._print_Function(expr) from sympy import Min if len(expr.args) == 1: return self._print(expr.args[0]) return "%sfmin(%s, %s)" % (self._ns, expr.args[0], self._print(Min(*expr.args[1:])))
def test_issue_10137(): a = Symbol('a', real=True, finite=True) b = Symbol('b', real=True, finite=True) x = Symbol('x', real=True, finite=True) y = Symbol('y', real=True, finite=True) p0 = Piecewise((0, Or(x < a, x > b)), (1, True)) p1 = Piecewise((0, Or(a > x, b < x)), (1, True)) assert integrate(p0, (x, y, oo)) == integrate(p1, (x, y, oo)) p3 = Piecewise((1, And(0 < x, x < a)), (0, True)) p4 = Piecewise((1, And(a > x, x > 0)), (0, True)) ip3 = integrate(p3, x) assert ip3 == Piecewise((0, x <= 0), (x, x <= Max(0, a)), (Max(0, a), True)) ip4 = integrate(p4, x) assert ip4 == ip3 assert p3.integrate((x, 2, 4)) == Min(4, Max(2, a)) - 2 assert p4.integrate((x, 2, 4)) == Min(4, Max(2, a)) - 2
def _print_Min(self, expr, **kwargs): from sympy import Min if len(expr.args) == 1: return self._print(expr.args[0], **kwargs) return 'minimum({0}, {1})'.format( self._print(expr.args[0], **kwargs), self._print(Min(*expr.args[1:]), **kwargs))
def test_tensorflow_multi_min(): if not tensorflow: skip("tensorflow not installed.") expr = Min(x, -x, x**2) func = lambdify(x, expr, modules="tensorflow") with tensorflow.compat.v1.Session() as s: assert func(-2).eval(session=s) == -2
def integrate(self, expr, rvs=None, **kwargs): from sympy import Max, Min result = SingleContinuousPSpace.integrate(self, expr, rvs, **kwargs) result = result.subs({ Max(self.left, self.right): self.right, Min(self.left, self.right): self.left }) return result
def _print_Min(self, expr): if "Min" in self.known_functions: return self._print_Function(expr) from sympy import Min if len(expr.args) == 1: return self._print(expr.args[0]) return "((%(a)s < %(b)s) ? %(a)s : %(b)s)" % { 'a': expr.args[0], 'b': self._print(Min(*expr.args[1:]))}
def test_tensorflow_multi_min(): if not tensorflow: skip("tensorflow not installed.") expr = Min(x, -x, x**2) func = lambdify(x, expr, modules="tensorflow") a = tensorflow.placeholder(dtype=tensorflow.float32) s = tensorflow.Session() assert func(a).eval(session=s, feed_dict={a: -2}) == -2
def test_supinf(): x = Symbol('x', real=True) y = Symbol('y', real=True) assert (Interval(0, 1) + FiniteSet(2)).sup == 2 assert (Interval(0, 1) + FiniteSet(2)).inf == 0 assert (Interval(0, 1) + FiniteSet(x)).sup == Max(1, x) assert (Interval(0, 1) + FiniteSet(x)).inf == Min(0, x) assert FiniteSet(5, 1, x).sup == Max(5, x) assert FiniteSet(5, 1, x).inf == Min(1, x) assert FiniteSet(5, 1, x, y).sup == Max(5, x, y) assert FiniteSet(5, 1, x, y).inf == Min(1, x, y) assert FiniteSet(5, 1, x, y, S.Infinity, S.NegativeInfinity).sup == \ S.Infinity assert FiniteSet(5, 1, x, y, S.Infinity, S.NegativeInfinity).inf == \ S.NegativeInfinity assert FiniteSet('Ham', 'Eggs').sup == Max('Ham', 'Eggs')
def initialize_damp(damp, padsizes, spacing, fs=False): """ Initialise damping field with an absorbing boundary layer. Includes basic constant Q setup (not interfaced yet) and assumes that the peak frequency is 1/(10 * spacing). Parameters ---------- damp : Function The damping field for absorbing boundary condition. nbl : int Number of points in the damping layer. spacing : Grid spacing coefficient. mask : bool, optional whether the dampening is a mask or layer. mask => 1 inside the domain and decreases in the layer not mask => 0 inside the domain and increase in the layer """ lqmin = np.log(.1) lqmax = np.log(100) w0 = 1 / (10 * np.max(spacing)) z = damp.dimensions[-1] eqs = [Eq(damp, 1)] for (nbl, nbr), d in zip(padsizes, damp.dimensions): if not fs or d is not z: nbl = max(5, nbl - 10) if d is z else nbl # left dim_l = SubDimension.left(name='abc_%s_l' % d.name, parent=d, thickness=nbl) pos = Abs(dim_l - d.symbolic_min) / float(nbl) eqs.append( Eq(damp.subs({d: dim_l}), Min(damp.subs({d: dim_l}), pos))) # right dim_r = SubDimension.right(name='abc_%s_r' % d.name, parent=d, thickness=nbr) pos = Abs(d.symbolic_max - dim_r) / float(nbr) eqs.append(Eq(damp.subs({d: dim_r}), Min(damp.subs({d: dim_r}), pos))) eqs.append(Eq(damp, w0 / exp(lqmin + damp * (lqmax - lqmin)))) Operator(eqs, name='initdamp')()
def _set_pow(x, exponent): # noqa:F811 """ Powers in interval arithmetic https://en.wikipedia.org/wiki/Interval_arithmetic """ s1 = x.start ** exponent s2 = x.end ** exponent if ((s2 > s1) if exponent > 0 else (x.end > -x.start)) == True: left_open = x.left_open right_open = x.right_open # TODO: handle unevaluated condition. sleft = s2 else: # TODO: `s2 > s1` could be unevaluated. left_open = x.right_open right_open = x.left_open sleft = s1 if x.start.is_positive: return Interval(Min(s1, s2), Max(s1, s2), left_open, right_open) elif x.end.is_negative: return Interval(Min(s1, s2), Max(s1, s2), left_open, right_open) # Case where x.start < 0 and x.end > 0: if exponent.is_odd: if exponent.is_negative: if x.start.is_zero: return Interval(s2, oo, x.right_open) if x.end.is_zero: return Interval(-oo, s1, True, x.left_open) return Union( Interval(-oo, s1, True, x.left_open), Interval(s2, oo, x.right_open) ) else: return Interval(s1, s2, x.left_open, x.right_open) elif exponent.is_even: if exponent.is_negative: if x.start.is_zero: return Interval(s2, oo, x.right_open) if x.end.is_zero: return Interval(s1, oo, x.left_open) return Interval(0, oo) else: return Interval(S.Zero, sleft, S.Zero not in x, left_open)
def test_min_multiarg(): assert_equal("\\min(1,2)", Min(1, 2)) assert_equal("\\min(9,876,543)", Min(9, 876, 543)) assert_equal("\\min(x, y,z)", Min(x, y, z), symbolically=True) assert_equal("\\min(5.8,7.4, 2.2,-10)", Min(Rational('5.8'), Rational('7.4'), Rational('2.2'), -10)) assert_equal("\\min(\\pi,12E2,84,\\sqrt{5},12/5)", Min(pi, Rational('12E2'), 84, sqrt(5), Rational('12/5'))) assert_equal("\\min(823,51)", Min(823, 51)) assert_equal("\\min(72*4,23, 9)", Min(72 * 4, 23, 9))
def test_min_expr(): assert_equal("\\min((1+6)/3, 7)", Min(Rational(1 + 6, 3), 7)) assert_equal("\\min(58*9)", Min(58 * 9)) assert_equal("\\min(1+6/3, -5)", Min(1 + Rational('6/3'), -5)) assert_equal("\\min(7*4/5, 092) * 2", Min(7 * 4 / 5, 92) * 2) assert_equal("38+\\min(13, 15-2.3)", 38 + Min(13, 15 - Rational('2.3'))) assert_equal("\\sqrt{\\min(99.9999999999999, 100)}", sqrt(Min(Rational('99.9999999999999'), 100))) assert_equal("\\min(274/(5+2), \\exp(12.4), 1.4E2)", Min(Rational(274, 5 + 2), exp(Rational('12.4')), Rational('1.4E2')))
def test_min_fraction(): assert_equal("\\min(1/2, 1/4)", Min(Rational('1/2'), Rational('1/4'))) assert_equal("\\min(6/2, 3)", Min(Rational('6/2'), 3)) assert_equal("\\min(2/4, 1/2)", Min(Rational('2/4'), Rational('1/2'))) assert_equal("\\min(-12/5, 6.4)", Min(Rational('-12/5'), Rational('6.4'))) assert_equal("\\min(1/10)", Min(Rational('1/10'))) assert_equal("\\min(1.5, \\pi/2)", Min(Rational('1.5'), pi / 2, evaluate=False)) assert_equal("\\min(-4/3, -2/1, 0/9, -3)", Min(Rational('-4/3'), Rational('-2/1'), Rational('0/9'), -3))
def _laplace_transform(f, t, s, simplify=True): """ The backend function for laplace transforms. """ from sympy import (re, Max, exp, pi, Abs, Min, periodic_argument as arg, cos, Wild, symbols) F = integrate(exp(-s * t) * f, (t, 0, oo)) if not F.has(Integral): return _simplify(F, simplify), -oo, True if not F.is_Piecewise: raise IntegralTransformError('Laplace', f, 'could not compute integral') F, cond = F.args[0] if F.has(Integral): raise IntegralTransformError('Laplace', f, 'integral in unexpected form') a = -oo aux = True conds = conjuncts(to_cnf(cond)) u = Dummy('u', real=True) p, q, w1, w2, w3 = symbols('p q w1 w2 w3', cls=Wild, exclude=[s]) for c in conds: a_ = oo aux_ = [] for d in disjuncts(c): m = d.match(abs(arg((s + w3)**p * q, w1)) < w2) if m: if m[q] > 0 and m[w2] / m[p] == pi / 2: d = re(s + m[w3]) > 0 m = d.match(0 < cos(abs(arg(s, q))) * abs(s) - p) if m: d = re(s) > m[p] d_ = d.replace(re, lambda x: x.expand().as_real_imag()[0]).subs( re(s), t) if not d.is_Relational or (d.rel_op != '<' and d.rel_op != '<=') \ or d_.has(s) or not d_.has(t): aux_ += [d] continue soln = _solve_inequality(d_, t) if not soln.is_Relational or \ (soln.rel_op != '<' and soln.rel_op != '<='): aux_ += [d] continue if soln.lhs == t: raise IntegralTransformError('Laplace', f, 'convergence not in half-plane?') else: a_ = Min(soln.lhs, a_) if a_ != oo: a = Max(a_, a) else: aux = And(aux, Or(*aux_)) return _simplify(F, simplify), a, aux
def _apply_flop_equations(o): """Calculate overall flop rate""" # Sum up products o.Rflop = sum(o.get_products('Rflop').values()) # Calculate interfacet IO rate for faceting: TCC-SDP-151123-1-1 rev 1.1 o.Rinterfacet = \ 2 * o.Nmajortotal * Min(3.0, 2.0 + 18.0 * o.r_facet_base) * \ (o.Nfacet * o.Npix_linear)**2 * o.Nf_out * 4 / o.Tobs
def test_scalar_funcs(): assert SetExpr(Interval(0, 1)).set == Interval(0, 1) a, b = Symbol('a', real=True), Symbol('b', real=True) a, b = 1, 2 # TODO: add support for more functions in the future: for f in [exp, log]: input_se = f(SetExpr(Interval(a, b))) output = input_se.set expected = Interval(Min(f(a), f(b)), Max(f(a), f(b))) assert output == expected
def _print_Min(self, expr): from sympy import Min if len(expr.args) == 1: return self._print(expr.args[0]) return "%smin(%s, %s)" % ( self._ns, expr.args[0], self._print(Min(*expr.args[1:])), )
def process_conds(conds): """ Turn ``conds`` into a strip and auxiliary conditions. """ a = -oo aux = True conds = conjuncts(to_cnf(conds)) u = Dummy('u', real=True) p, q, w1, w2, w3, w4, w5 = symbols('p q w1 w2 w3 w4 w5', cls=Wild, exclude=[s]) for c in conds: a_ = oo aux_ = [] for d in disjuncts(c): m = d.match(abs(arg((s + w3)**p * q, w1)) < w2) if not m: m = d.match(abs(arg((s + w3)**p * q, w1)) <= w2) if not m: m = d.match(abs(arg((polar_lift(s + w3))**p * q, w1)) < w2) if not m: m = d.match( abs(arg((polar_lift(s + w3))**p * q, w1)) <= w2) if m: if m[q] > 0 and m[w2] / m[p] == pi / 2: d = re(s + m[w3]) > 0 m = d.match( 0 < cos(abs(arg(s**w1 * w5, q)) * w2) * abs(s**w3)**w4 - p) if not m: m = d.match( 0 < cos(abs(arg(polar_lift(s)**w1 * w5, q)) * w2) * abs(s**w3)**w4 - p) if m and all(m[wild] > 0 for wild in [w1, w2, w3, w4, w5]): d = re(s) > m[p] d_ = d.replace(re, lambda x: x.expand().as_real_imag()[0]).subs( re(s), t) if not d.is_Relational or (d.rel_op != '<' and d.rel_op != '<=') \ or d_.has(s) or not d_.has(t): aux_ += [d] continue soln = _solve_inequality(d_, t) if not soln.is_Relational or \ (soln.rel_op != '<' and soln.rel_op != '<='): aux_ += [d] continue if soln.lhs == t: raise IntegralTransformError( 'Laplace', f, 'convergence not in half-plane?') else: a_ = Min(soln.lhs, a_) if a_ != oo: a = Max(a_, a) else: aux = And(aux, Or(*aux_)) return a, aux
def test_mpi_fullmode_objects(): grid = Grid(shape=(4, 4, 4)) x, y, _ = grid.dimensions # Message f = Function(name='f', grid=grid) obj = MPIMsgEnriched('msg', f, [Halo(x, LEFT)]) pkl_obj = pickle.dumps(obj) new_obj = pickle.loads(pkl_obj) assert obj.name == new_obj.name assert obj.function.name == new_obj.function.name assert all( obj.function.dimensions[i].name == new_obj.function.dimensions[i].name for i in range(grid.dim)) assert new_obj.function.dimensions[0] is new_obj.halos[0].dim # Region x_m, x_M = x.symbolic_min, x.symbolic_max y_m, y_M = y.symbolic_min, y.symbolic_max obj = MPIRegion('reg', 1, [y, x], [(((x, OWNED, LEFT), ), { x: (x_m, Min(x_M, x_m)) }), (((y, OWNED, LEFT), ), { y: (y_m, Min(y_M, y_m)) })]) pkl_obj = pickle.dumps(obj) new_obj = pickle.loads(pkl_obj) assert obj.prefix == new_obj.prefix assert obj.key == new_obj.key assert obj.name == new_obj.name assert len(new_obj.arguments) == 2 assert all(d0.name == d1.name for d0, d1 in zip(obj.arguments, new_obj.arguments)) assert all(new_obj.arguments[i] is new_obj.owned[i][0][0][0] # `x` and `y` for i in range(2)) assert new_obj.owned[0][0][0][1] is new_obj.owned[1][0][0][1] # `OWNED` assert new_obj.owned[0][0][0][2] is new_obj.owned[1][0][0][2] # `LEFT` for n, i in enumerate(new_obj.owned): d, v = list(i[1].items())[0] assert d is new_obj.arguments[n] assert v[0] is d.symbolic_min assert v[1] == Min(d.symbolic_max, d.symbolic_min)
def update_with_box(vp, alpha, dm, vmin=2.0, vmax=3.5): """ Apply gradient update in-place to vp with box constraint Notes: ------ For more advanced algorithm, one will need to gather the non-distributed velocity array to apply constrains and such. """ update = vp + alpha * dm update_eq = Eq(vp, Max(Min(update, vmax), vmin)) Operator(update_eq)()
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))
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))
def test_min_negative(): assert_equal("\\min(-9, 4)", Min(-9, 4)) assert_equal("\\min(4, -9)", Min(4, -9)) assert_equal("\\min(-7)", Min(-7)) assert_equal("\\min(-2, -2)", Min(-2, -2)) assert_equal("\\min(-324E-3, -58)", Min(Rational('-324E-3'), -58)) assert_equal("\\min(-1, 0, 1, -37, 42)", Min(-1, 0, 1, -37, 42))
def _apply_image_equations(o): """ Calculate image parameters, such as resolution and size References: * SKA-TEL-SDP-0000040 01D section D - The Resolution and Extent of Images and uv Planes * SKA-TEL-SDP-0000040 01D section H.2 - Faceting """ # max subband wavelength to set image FoV o.wl_sb_max = o.wl *sqrt(o.Qsubband) # min subband wavelength to set pixel size o.wl_sb_min = o.wl_sb_max / o.Qsubband # Facet overlap is only needed if Nfacet > 1 o.r_facet = sign(o.Nfacet - 1)*o.r_facet_base # Facet Field-of-view (linear) at max sub-band wavelength o.Theta_fov = 7.66 * o.wl_sb_max * o.Qfov * (1+o.r_facet) \ / (pi * o.Ds * o.Nfacet) # Total linear field of view of map (all facets) o.Theta_fov_total = 7.66 * o.wl_sb_max * o.Qfov / (pi * o.Ds) # Synthesized beam at fiducial wavelength. Called Theta_PSF in PDR05. o.Theta_beam = 3 * o.wl_sb_min / (2. * o.Bmax) # Pixel size at fiducial wavelength. o.Theta_pix = o.Theta_beam / (2. * o.Qpix) # Number of pixels on side of facet in subband. o.Npix_linear = (o.Theta_fov / o.Theta_pix) o.Npix_linear_fov_total = (o.Theta_fov_total / o.Theta_pix) # grid width in wavelengths o.Lambda_grid = 1 / o.Theta_pix o.Lambda_bl = 2 * o.Bmax / o.wl_sb_min # Predict fov and number of pixels depends on whether we facet if o.scale_predict_by_facet: o.Theta_fov_predict = o.Theta_fov o.Nfacet_predict = o.Nfacet o.Npix_linear_predict = o.Npix_linear else: o.Theta_fov_predict = o.Theta_fov_total o.Nfacet_predict = 1 o.Npix_linear_predict = o.Npix_linear_fov_total # expansion of sine solves eps = arcsinc(1/amp_f_max). o.epsilon_f_approx = sqrt(6 * (1 - (1. / o.amp_f_max))) #Correlator dump rate set by smearing limit at field of view needed #for ICAL pipeline (Assuming this is always most challenging) o.Tdump_no_smear=o.epsilon_f_approx * o.wl \ / (o.Omega_E * o.Bmax * 7.66 * o.wl_sb_max * o.Qfov_ICAL / (pi * o.Ds)) o.Tint_used = Max(o.Tint_min, Min(o.Tdump_no_smear, o.Tsnap))
def test_piecewise_integrate1b(): 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)) gy1 = g.integrate((x, y, 1)) g1y = g.integrate((x, 1, y)) for yy in (-1, S.Half, 2): assert g.integrate((x, yy, 1)) == gy1.subs(y, yy) assert g.integrate((x, 1, yy)) == g1y.subs(y, yy) assert gy1 == Piecewise((-Min(1, Max(0, y))**2 / 2 + 1 / 2, y < 1), (-y + 1, True)) assert g1y == Piecewise((Min(1, Max(0, y))**2 / 2 - 1 / 2, y < 1), (y - 1, True))
def _collapse_extra(self, extra): from sympy import And, Max, Min a = [] b = [] cond = [] for (sa, sb), c in extra: a += [sa] b += [sb] cond += [c] res = (Max(*a), Min(*b)), And(*cond) if (res[0][0] >= res[0][1]) is True or res[1] is False: raise IntegralTransformError('Mellin', None, 'no combined convergence.') return res
def test_issue_8919(): c = symbols('c:5') x = symbols("x") f1 = Piecewise((c[1], x < 1), (c[2], True)) f2 = Piecewise((c[3], x < S(1) / 3), (c[4], True)) assert integrate( f1 * f2, (x, 0, 2)) == c[1] * c[3] / 3 + 2 * c[1] * c[4] / 3 + c[2] * c[4] f1 = Piecewise((0, x < 1), (2, True)) f2 = Piecewise((3, x < 2), (0, True)) assert integrate(f1 * f2, (x, 0, 3)) == 6 y = symbols("y", positive=True) a, b, c, x, z = symbols("a,b,c,x,z", real=True) I = Integral(Piecewise((0, (x >= y) | (x < 0) | (b > c)), (a, True)), (x, 0, z)) ans = I.doit() assert ans == Piecewise((0, b > c), (a * Min(y, z) - a * Min(0, z), True)) for cond in (True, False): for yy in range(1, 3): for zz in range(-yy, 0, yy): reps = [(b > c, cond), (y, yy), (z, zz)] assert ans.subs(reps) == I.subs(reps).doit()
def is_lower(self): """Check if matrix is a lower triangular matrix. True can be returned even if the matrix is not square. Examples ======== >>> from sympy import Matrix >>> m = Matrix(2, 2, [1, 0, 0, 1]) >>> m Matrix([ [1, 0], [0, 1]]) >>> m.is_lower True >>> m = Matrix(4, 3, [0, 0, 0, 2, 0, 0, 1, 4 , 0, 6, 6, 5]) >>> m Matrix([ [0, 0, 0], [2, 0, 0], [1, 4, 0], [6, 6, 5]]) >>> m.is_lower True >>> from sympy.abc import x, y >>> m = Matrix(2, 2, [x**2 + y, y**2 + x, 0, x + y]) >>> m Matrix([ [x**2 + y, x + y**2], [ 0, x + y]]) >>> m.is_lower False See Also ======== is_upper is_diagonal is_lower_hessenberg """ from sympy import Range, Min i = self.generate_var(domain=Range(0, Min(self.rows, self.cols - 1))) j = i.generate_var(free_symbols=self.free_symbols, domain=Range(i + 1, self.cols)) assert i < j return self[i, j] == 0
def process_conds(cond): """ Turn ``cond`` into a strip (a, b), and auxiliary conditions. """ a = -oo b = oo aux = True conds = conjuncts(to_cnf(cond)) t = Dummy('t', real=True) for c in conds: a_ = oo b_ = -oo aux_ = [] for d in disjuncts(c): d_ = d.replace(re, lambda x: x.as_real_imag()[0]).subs(re(s), t) if not d.is_Relational or (d.rel_op != '<' and d.rel_op != '<=') \ or d_.has(s) or not d_.has(t): aux_ += [d] continue soln = _solve_inequality(d_, t) if not soln.is_Relational or \ (soln.rel_op != '<' and soln.rel_op != '<='): aux_ += [d] continue if soln.lhs == t: b_ = Max(soln.rhs, b_) else: a_ = Min(soln.lhs, a_) if a_ != oo and a_ != b: a = Max(a_, a) elif b_ != -oo and b_ != a: b = Min(b_, b) else: aux = And(aux, Or(*aux_)) return a, b, aux
def is_upper(self): """Check if matrix is an upper triangular matrix. True can be returned even if the matrix is not square. Examples ======== >>> from sympy import Matrix >>> m = Matrix(2, 2, [1, 0, 0, 1]) >>> m Matrix([ [1, 0], [0, 1]]) >>> m.is_upper True >>> m = Matrix(4, 3, [5, 1, 9, 0, 4 , 6, 0, 0, 5, 0, 0, 0]) >>> m Matrix([ [5, 1, 9], [0, 4, 6], [0, 0, 5], [0, 0, 0]]) >>> m.is_upper True >>> m = Matrix(2, 3, [4, 2, 5, 6, 1, 1]) >>> m Matrix([ [4, 2, 5], [6, 1, 1]]) >>> m.is_upper False See Also ======== is_lower is_diagonal is_upper_hessenberg """ from sympy import Range, Min j = self.generate_var(domain=Range(0, Min(self.cols, self.rows - 1))) i = j.generate_var(free_symbols=self.free_symbols, domain=Range(j + 1, self.rows)) assert i > j return self[i, j] == 0