def test_tensorflow_placeholders(): if not tensorflow: skip("tensorflow not installed.") expr = Max(sin(x), Abs(1 / (x + 2))) func = lambdify(x, expr, modules="tensorflow") with tensorflow.compat.v1.Session() as s: a = tensorflow.compat.v1.placeholder(dtype=tensorflow.float32) assert func(a).eval(session=s, feed_dict={a: 0}) == 0.5
def test_tensorflow_variables(): if not tensorflow: skip("tensorflow not installed.") expr = Max(sin(x), Abs(1 / (x + 2))) func = lambdify(x, expr, modules="tensorflow") a = tensorflow.Variable(0, dtype=tensorflow.float32) s = tensorflow.Session() s.run(tensorflow.initialize_all_variables()) assert func(a).eval(session=s) == 0.5
def test_tensorflow_basic_math(): if not tensorflow: skip("tensorflow not installed.") expr = Max(sin(x), Abs(1 / (x + 2))) func = lambdify(x, expr, modules="tensorflow") with tensorflow.compat.v1.Session() as s: a = tensorflow.constant(0, dtype=tensorflow.float32) assert func(a).eval(session=s) == 0.5
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_max_fraction(): assert_equal("\\max(1/2, 1/4)", Max(Rational('1/2'), Rational('1/4'))) assert_equal("\\max(6/2, 3)", Max(Rational('6/2'), 3)) assert_equal("\\max(2/4, 1/2)", Max(Rational('2/4'), Rational('1/2'))) assert_equal("\\max(-12/5, 6.4)", Max(Rational('-12/5'), Rational('6.4'))) assert_equal("\\max(1/10)", Max(Rational('1/10'))) assert_equal("\\max(1.5, \\pi/2)", Max(Rational('1.5'), pi / 2, evaluate=False)) assert_equal("\\max(-4/3, -2/1, 0/9, -3)", Max(Rational('-4/3'), Rational('-2/1'), Rational('0/9'), -3))
def test_max_expr(): assert_equal("\\max((1+6)/3, 7)", Max(Rational(1 + 6, 3), 7)) assert_equal("\\max(58*9)", Max(58 * 9)) assert_equal("\\max(1+6/3, -5)", Max(1 + Rational('6/3'), -5)) assert_equal("\\max(7*4/5, 092) * 2", Max(7 * 4 / 5, 92) * 2) assert_equal("38+\\max(13, 15-2.3)", 38 + Max(13, 15 - Rational('2.3'))) assert_equal("\\sqrt{\\max(99.9999999999999, 100)}", sqrt(Max(Rational('99.9999999999999'), 100))) assert_equal("\\max(274/(5+2), \\exp(12.4), 1.4E2)", Max(Rational(274, 5 + 2), exp(Rational('12.4')), Rational('1.4E2')))
def test_max_multiarg(): assert_equal("\\max(1,2)", Max(1, 2)) assert_equal("\\max(9,876,543)", Max(9, 876, 543)) assert_equal("\\max(x, y,z)", Max(x, y, z), symbolically=True) assert_equal("\\max(5.8,7.4, 2.2,-10)", Max(Rational('5.8'), Rational('7.4'), Rational('2.2'), -10)) assert_equal("\\max(\\pi,12E2,84,\\sqrt{5},12/5)", Max(pi, Rational('12E2'), 84, sqrt(5), Rational('12/5'))) assert_equal("\\max(823,51)", Max(823, 51)) assert_equal("\\max(72*4,23, 9)", Max(72 * 4, 23, 9))
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 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 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_Max(self, expr): from sympy import Max if len(expr.args) == 1: return self._print(expr.args[0]) return "%smax(%s, %s)" % ( self._ns, expr.args[0], self._print(Max(*expr.args[1:])), )
def _print_Max(self, expr): if "Max" in self.known_functions: return self._print_Function(expr) from sympy import Max 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(Max(*expr.args[1:])) }
def test_tensorflow_variables(): if not tensorflow: skip("tensorflow not installed.") expr = Max(sin(x), Abs(1 / (x + 2))) func = lambdify(x, expr, modules="tensorflow") with tensorflow.compat.v1.Session() as s: a = tensorflow.Variable(0, dtype=tensorflow.float32) s.run(a.initializer) assert func(a).eval(session=s, feed_dict={a: 0}) == 0.5
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_issue_6900(): from itertools import permutations t0, t1, T, t = symbols('t0, t1 T t') f = Piecewise((0, t < t0), (x, And(t0 <= t, t < t1)), (0, t >= t1)) g = f.integrate(t) assert g == Piecewise((0, t <= t0), (t * x - t0 * x, t <= Max(t0, t1)), (-t0 * x + x * Max(t0, t1), True)) for i in permutations(range(2)): reps = dict(zip((t0, t1), i)) for tt in range(-1, 3): assert (g.xreplace(reps).subs( t, tt) == f.xreplace(reps).integrate(t).subs(t, tt)) lim = Tuple(t, t0, T) g = f.integrate(lim) ans = Piecewise((-t0 * x + x * Min(T, Max(t0, t1)), T > t0), (0, True)) for i in permutations(range(3)): reps = dict(zip((t0, t1, T), i)) tru = f.xreplace(reps).integrate(lim.xreplace(reps)) assert tru == ans.xreplace(reps) assert g == ans
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_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_max_negative(): assert_equal("\\max(-9, 4)", Max(-9, 4)) assert_equal("\\max(4, -9)", Max(4, -9)) assert_equal("\\max(-7)", Max(-7)) assert_equal("\\max(-2, -2)", Max(-2, -2)) assert_equal("\\max(-324E-3, -58)", Max(Rational('-324E-3'), -58)) assert_equal("\\max(-1, 0, 1, -37, 42)", Max(-1, 0, 1, -37, 42))
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 _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 _collapse_extra(self, extra): from sympy import And, Max conds = [] planes = [] for plane, cond in extra: conds.append(cond) planes.append(plane) cond = And(*conds) plane = Max(*planes) if cond is False: raise IntegralTransformError('Laplace', None, 'No combined convergence.') return plane, cond
def _apply_fft_equations(o): """ Discrete fourier transformation of grids to images (and back) References: SKA-TEL-SDP-0000040 01D section 3.6.13 - FFT and iFFT """ if not o.pipeline in Pipelines.imaging: return # Determine number of w-stacks we need o.Nwstack = Max(1, o.DeltaW_max(o.Bmax) / o.DeltaW_stack) o.Nwstack_predict = Max(1, o.DeltaW_max(o.Bmax) / o.DeltaW_stack) # These are real-to-complex for which the prefactor in the FFT is 2.5 o.set_product(Products.FFT, T = o.Tsnap, N = o.Nmajortotal * o.Nbeam * o.Npp * o.Nf_FFT_backward * o.Nfacet**2, Rflop = 2.5 * o.Nwstack * o.Npix_linear ** 2 * log(o.Npix_linear**2, 2) / o.Tsnap, Rout = o.Mpx * o.Npix_linear**2 / o.Tsnap) o.set_product(Products.IFFT, T = o.Tsnap, N = o.Nmajortotal * o.Nbeam * o.Npp * o.Nf_FFT_predict * o.Nfacet_predict**2, Rflop = 2.5 * o.Nwstack_predict * o.Npix_linear_predict**2 * log(o.Npix_linear_predict**2, 2) / o.Tsnap, Rout = o.Mcpx * o.Npix_linear_predict * (o.Npix_linear_predict / 2 + 1) / 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 _make_partree(self, candidates, nthreads=None): """Parallelize the `candidates` Iterations attaching suitable OpenMP pragmas.""" assert candidates root = candidates[0] # Get the collapsable Iterations collapsable = self._find_collapsable(root, candidates) ncollapse = 1 + len(collapsable) # Prepare to build a ParallelTree if all(i.is_Affine for i in candidates): bundles = FindNodes(ExpressionBundle).visit(root) sops = sum(i.ops for i in bundles) if sops >= self.dynamic_work: schedule = 'dynamic' else: schedule = 'static' if nthreads is None: # pragma omp for ... schedule(..., 1) nthreads = self.nthreads body = OpenMPIteration(schedule=schedule, ncollapse=ncollapse, **root.args) else: # pragma omp parallel for ... schedule(..., 1) body = OpenMPIteration(schedule=schedule, parallel=True, ncollapse=ncollapse, nthreads=nthreads, **root.args) prefix = [] else: # pragma omp for ... schedule(..., expr) assert nthreads is None nthreads = self.nthreads_nonaffine chunk_size = Symbol(name='chunk_size') body = OpenMPIteration(ncollapse=ncollapse, chunk_size=chunk_size, **root.args) niters = prod([root.symbolic_size] + [j.symbolic_size for j in collapsable]) value = INT(Max(niters / (nthreads * self.chunk_nonaffine), 1)) prefix = [Expression(DummyEq(chunk_size, value, dtype=np.int32))] # Create a ParallelTree partree = ParallelTree(prefix, body, nthreads=nthreads) collapsed = [partree] + collapsable return root, partree, collapsed
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 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 g(v,w,i): from sympy import Lambda, Function ,symbols ,IndexedBase,Idx ,Max, Sum x = Function('x') i, n, j, dim, k =symbols('i, n, j, dim, k') v=IndexedBase('v') w=IndexedBase('w') net = Lambda((i, n, dim, k), Max(0.0, Sum(x(k)*w[n, k, i], (k, 0, dim-1)))) dim = [10**4]*10 index =[symbols('i%s'%m) for m in range(len(dim)+1)] y = [0]*len(dim) new = v[index[0]] for n in range(len(dim)): y[n] = net(index[n+1], n, dim[n], index[n]) y[n] = y[n].subs(x(index[n]), new) new = y[n] return y[-1]
def test_issue_18770(): numpy = import_module('numpy') if not numpy: skip("numpy not installed.") from sympy import lambdify, Min, Max expr1 = Min(0.1 * x + 3, x + 1, 0.5 * x + 1) func = lambdify(x, expr1, "numpy") assert (func(numpy.linspace(0, 3, 3)) == [1.0, 1.75, 2.5]).all() assert func(4) == 3 expr1 = Max(x**2, x**3) func = lambdify(x, expr1, "numpy") assert (func(numpy.linspace(-1, 2, 4)) == [1, 0, 1, 8]).all() assert func(4) == 64
def test_finite_basic(): x = Symbol('x') A = FiniteSet(1, 2, 3) B = FiniteSet(3, 4, 5) AorB = Union(A, B) AandB = A.intersect(B) assert AorB.subset(A) and AorB.subset(B) assert A.subset(AandB) assert AandB == FiniteSet(3) assert A.inf == 1 and A.sup == 3 assert AorB.inf == 1 and AorB.sup == 5 assert FiniteSet(x, 1, 5).sup == Max(x, 5) assert FiniteSet(x, 1, 5).inf == Min(x, 1) # Ensure a variety of types can exist in a FiniteSet S = FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval)
def test_expint(): from sympy import E1, expint, Max, re, lerchphi, Symbol, simplify, Si, Ci, Ei aneg = Symbol('a', negative=True) u = Symbol('u', polar=True) assert mellin_transform(E1(x), x, s) == (gamma(s) / s, (0, oo), True) assert inverse_mellin_transform(gamma(s) / s, s, x, (0, oo)).rewrite(expint).expand() == E1(x) assert mellin_transform(expint(a, x), x, s) == \ (gamma(s)/(a + s - 1), (Max(1 - re(a), 0), oo), True) # XXX IMT has hickups with complicated strips ... assert simplify(unpolarify( inverse_mellin_transform(gamma(s)/(aneg + s - 1), s, x, (1 - aneg, oo)).rewrite(expint).expand(func=True))) == \ expint(aneg, x) assert mellin_transform(Si(x), x, s) == \ (-2**s*sqrt(pi)*gamma(s/2 + S(1)/2)/( 2*s*gamma(-s/2 + 1)), (-1, 0), True) assert inverse_mellin_transform(-2**s*sqrt(pi)*gamma((s + 1)/2) /(2*s*gamma(-s/2 + 1)), s, x, (-1, 0)) \ == Si(x) assert mellin_transform(Ci(sqrt(x)), x, s) == \ (-2**(2*s - 1)*sqrt(pi)*gamma(s)/(s*gamma(-s + S(1)/2)), (0, 1), True) assert inverse_mellin_transform( -4**s * sqrt(pi) * gamma(s) / (2 * s * gamma(-s + S(1) / 2)), s, u, (0, 1)).expand() == Ci(sqrt(u)) # TODO LT of Si, Shi, Chi is a mess ... assert laplace_transform(Ci(x), x, s) == (-log(1 + s**2) / 2 / s, 0, True) assert laplace_transform(expint(a, x), x, s) == \ (lerchphi(s*polar_lift(-1), 1, a), 0, S(0) < re(a)) assert laplace_transform(expint(1, x), x, s) == (log(s + 1) / s, 0, True) assert laplace_transform(expint(2, x), x, s) == \ ((s - log(s + 1))/s**2, 0, True) assert inverse_laplace_transform(-log(1 + s**2)/2/s, s, u).expand() == \ Heaviside(u)*Ci(u) assert inverse_laplace_transform(log(s + 1)/s, s, x).rewrite(expint) == \ Heaviside(x)*E1(x) assert inverse_laplace_transform((s - log(s + 1))/s**2, s, x).rewrite(expint).expand() == \ (expint(2, x)*Heaviside(x)).rewrite(Ei).rewrite(expint).expand()