def test_functions_X1(): g = WildFunction('g') p = Wild('p') q = Wild('q') f = cos(5 * x) assert f.match(p * g(q * x)) == {p: 1, g: cos, q: 5}
def test_functions(): g = WildFunction('g') p = Wild('p') q = Wild('q') f = cos(5 * x) notf = x assert f.match(p * cos(q * x)) == {p: 1, q: 5} assert f.match(p * g) == {p: 1, g: cos(5 * x)} assert notf.match(g) is None F = WildFunction('F', nargs=2) assert F.nargs == FiniteSet(2) f = Function('f') assert f(x).match(F) is None F = WildFunction('F', nargs=(1, 2)) assert F.nargs == FiniteSet(1, 2) # issue sympy/sympy#2711 f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == { 0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1, (): 3, (0, ): 1, ((), ()): 1, ((0, ), ()): 1 } assert f.find(a + b) == {0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1} assert f.find(a**2) == {x: 1, meijerg(((), ()), ((0, ), ()), x): 1}
def test_Derivative_bug1(): f = Function("f") x = abc.x a = Wild("a", exclude=[f(x)]) b = Wild("b", exclude=[f(x)]) eq = f(x).diff(x) assert eq.match(a * Derivative(f(x), x) + b) == {a: 1, b: 0}
def _eval_expand_func(self, **hints): arg = self.args[0] symbs = arg.free_symbols if len(symbs) == 1: z = symbs.pop() c = Wild("c", exclude=[z]) d = Wild("d", exclude=[z]) m = Wild("m", exclude=[z]) n = Wild("n", exclude=[z]) M = arg.match(c * (d * z**n)**m) if M is not None: m = M[m] # The transformation is in principle # given by 03.08.16.0001.01 but note # that there is an error in this formule. # http://functions.wolfram.com/Bessel-TypeFunctions/AiryBiPrime/16/01/01/0001/ if (3 * m).is_integer: c = M[c] d = M[d] n = M[n] pf = (d**m * z**(n * m)) / (d * z**n)**m newarg = c * d**m * z**(n * m) return S.Half * (sqrt(3) * (pf - S.One) * airyaiprime(newarg) + (pf + S.One) * airybiprime(newarg))
def test_functions_X1(): from diofant.core.function import WildFunction x = Symbol('x') g = WildFunction('g') p = Wild('p') q = Wild('q') f = cos(5 * x) assert f.match(p * g(q * x)) == {p: 1, g: cos, q: 5}
def test_derivative_bug1(): f = Function("f") a = Wild("a", exclude=[f, x]) b = Wild("b", exclude=[f]) pattern = a * Derivative(f(x), x, x) + b expr = Derivative(f(x), x) + x**2 d1 = {b: x**2} d2 = pattern.xreplace(d1).matches(expr, d1) assert d2 is None
def test_derivative_bug1(): f = Function('f') a = Wild('a', exclude=[f, x]) b = Wild('b', exclude=[f]) pattern = a * Derivative(f(x), x, x) + b expr = Derivative(f(x), x) + x**2 d1 = {b: x**2} d2 = expr.match(pattern.xreplace(d1)) assert d2 is None
def test_sympyissue_8694(): theta1, theta2, rho = symbols('theta1, theta2, rho') S1, C1 = sin(theta1), cos(theta1) X1 = Wild('X1', exclude=[rho, theta1, theta2]) Y1 = Wild('Y1', exclude=[rho, theta1, theta2]) Z1 = Wild('Z1', exclude=[rho, theta1, theta2]) eq = -Y + (-X + Z) * cos(theta1) + (X + Y) * sin(theta1) assert eq.match(X1 * C1 + Y1 * S1 + Z1) == {X1: Z - X, Y1: X + Y, Z1: -Y} eq = -Y + Z * cos(theta1) + (X + Y) * sin(theta1) assert eq.match(X1 * C1 + Y1 * S1 + Z1) == {X1: Z, Y1: X + Y, Z1: -Y}
def test_functions(): g = WildFunction('g') p = Wild('p') q = Wild('q') f = cos(5 * x) notf = x assert f.match(p * cos(q * x)) == {p: 1, q: 5} assert f.match(p * g) == {p: 1, g: cos(5 * x)} assert notf.match(g) is None
def test_sympyissue_2711(): f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == {(S.Zero, ), ((), ()), ((S.Zero, ), ()), x, S.Zero, (), meijerg(((), ()), ((S.Zero, ), ()), x)} assert f.find(a + b) == \ {meijerg(((), ()), ((S.Zero,), ()), x), x, S.Zero} assert f.find(a**2) == {meijerg(((), ()), ((S.Zero, ), ()), x), x}
def test_gh_issue_2711(): x = Symbol('x') f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == {(S.Zero, ), ((), ()), ((S.Zero, ), ()), x, S.Zero, (), meijerg(((), ()), ((S.Zero, ), ()), x)} assert f.find(a + b) == \ {meijerg(((), ()), ((S.Zero,), ()), x), x, S.Zero} assert f.find(a**2) == {meijerg(((), ()), ((S.Zero, ), ()), x), x}
def test_match_polynomial(): a = Wild('a', exclude=[x]) b = Wild('b', exclude=[x]) c = Wild('c', exclude=[x]) d = Wild('d', exclude=[x]) eq = 4 * x**3 + 3 * x**2 + 2 * x + 1 pattern = a * x**3 + b * x**2 + c * x + d assert eq.match(pattern) == {a: 4, b: 3, c: 2, d: 1} assert (eq - 3 * x**2).match(pattern) == {a: 4, b: 0, c: 2, d: 1} assert (x + sqrt(2) + 3).match(a + b*x + c*x**2) == \ {b: 1, a: sqrt(2) + 3, c: 0}
def test_functions(): from diofant.core.function import WildFunction x = Symbol('x') g = WildFunction('g') p = Wild('p') q = Wild('q') f = cos(5 * x) notf = x assert f.match(p * cos(q * x)) == {p: 1, q: 5} assert f.match(p * g) == {p: 1, g: cos(5 * x)} assert notf.match(g) is None
def test_sympyissue_3883(): f = (-gamma * (x - mu)**2 - log(gamma) + log(2 * pi)) / 2 a, b, c = symbols('a b c', cls=Wild, exclude=(gamma, )) assert f.match(a * log(gamma) + b * gamma + c) == \ {a: -Rational(1, 2), b: -(mu - x)**2/2, c: log(2*pi)/2} assert f.expand().collect(gamma).match(a * log(gamma) + b * gamma + c) == \ {a: -Rational(1, 2), b: (-(x - mu)**2/2).expand(), c: (log(2*pi)/2).expand()} g1 = Wild('g1', exclude=[gamma]) g2 = Wild('g2', exclude=[gamma]) g3 = Wild('g3', exclude=[gamma]) assert f.expand().match(g1 * log(gamma) + g2 * gamma + g3) == \ {g3: log(2)/2 + log(pi)/2, g1: -Rational(1, 2), g2: -mu**2/2 + mu*x - x**2/2}
def test_issue_4559(): x = Symbol('x') e = Symbol('e') w = Wild('w', exclude=[x]) y = Wild('y') # this is as it should be assert (3 / x).match(w / y) == {w: 3, y: x} assert (3 * x).match(w * y) == {w: 3, y: x} assert (x / 3).match(y / w) == {w: 3, y: x} assert (3 * x).match(y / w) == {w: Rational(1, 3), y: x} # these could be allowed to fail assert (x / 3).match(w / y) == {w: Rational(1, 3), y: 1 / x} assert (3 * x).match(w / y) == {w: 3, y: 1 / x} assert (3 / x).match(w * y) == {w: 3, y: 1 / x} # Note that solve will give # multiple roots but match only gives one: # # >>> solve(x**r-y**2,y) # [-x**(r/2), x**(r/2)] r = Symbol('r', rational=True) assert (x**r).match(y**2) == {y: x**(r / 2)} assert (x**e).match(y**2) == {y: sqrt(x**e)} # since (x**i = y) -> x = y**(1/i) where i is an integer # the following should also be valid as long as y is not # zero when i is negative. a = Wild('a') e = Integer(0) assert e.match(a) == {a: e} assert e.match(1 / a) is None assert e.match(a**.3) is None e = Integer(3) assert e.match(1 / a) == {a: 1 / e} assert e.match(1 / a**2) == {a: 1 / sqrt(e)} e = pi assert e.match(1 / a) == {a: 1 / e} assert e.match(1 / a**2) == {a: 1 / sqrt(e)} assert (-e).match(sqrt(a)) is None assert (-e).match(a**2) == {a: I * sqrt(pi)}
def test_derivative2(): f = Function('f') a = Wild('a', exclude=[f, x]) b = Wild('b', exclude=[f]) e = Derivative(f(x), x) assert e.match(Derivative(f(x), x)) == {} assert e.match(Derivative(f(x), x, x)) is None e = Derivative(f(x), x, x) assert e.match(Derivative(f(x), x)) is None assert e.match(Derivative(f(x), x, x)) == {} e = Derivative(f(x), x) + x**2 assert e.match(a * Derivative(f(x), x) + b) == {a: 1, b: x**2} assert e.match(a * Derivative(f(x), x, x) + b) is None e = Derivative(f(x), x, x) + x**2 assert e.match(a * Derivative(f(x), x) + b) is None assert e.match(a * Derivative(f(x), x, x) + b) == {a: 1, b: x**2}
def test_mul_noncommutative(): A, B = symbols('A B', commutative=False) u, v = symbols('u v', cls=Wild) w = Wild('w', commutative=False) assert (u * v).matches(x) in ({v: x, u: 1}, {u: x, v: 1}) assert (u * v).matches(x * y) in ({v: y, u: x}, {u: y, v: x}) assert (u * v).matches(A) is None assert (u * v).matches(A * B) is None assert (u * v).matches(x * A) is None assert (u * v).matches(x * y * A) is None assert (u * v).matches(x * A * B) is None assert (u * v).matches(x * y * A * B) is None assert (v * w).matches(x) is None assert (v * w).matches(x * y) is None assert (v * w).matches(A) == {w: A, v: 1} assert (v * w).matches(A * B) == {w: A * B, v: 1} assert (v * w).matches(x * A) == {w: A, v: x} assert (v * w).matches(x * y * A) == {w: A, v: x * y} assert (v * w).matches(x * A * B) == {w: A * B, v: x} assert (v * w).matches(x * y * A * B) == {w: A * B, v: x * y} assert (v * w).matches(-x) is None assert (v * w).matches(-x * y) is None assert (v * w).matches(-A) == {w: A, v: -1} assert (v * w).matches(-A * B) == {w: A * B, v: -1} assert (v * w).matches(-x * A) == {w: A, v: -x} assert (v * w).matches(-x * y * A) == {w: A, v: -x * y} assert (v * w).matches(-x * A * B) == {w: A * B, v: -x} assert (v * w).matches(-x * y * A * B) == {w: A * B, v: -x * y}
def test_sympyissue_2711(): f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == { 0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1, (): 3, (0, ): 1, ((), ()): 1, ((0, ), ()): 1 } assert f.find(a + b) == {0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1} assert f.find(a**2) == {x: 1, meijerg(((), ()), ((0, ), ()), x): 1}
def test_collect_Wild(): """Collect with respect to functions with Wild argument""" f = Function('f') w1 = Wild('.1') w2 = Wild('.2') assert collect(f(x) + a * f(x), f(w1)) == (1 + a) * f(x) assert collect(f(x, y) + a * f(x, y), f(w1)) == f(x, y) + a * f(x, y) assert collect(f(x, y) + a * f(x, y), f(w1, w2)) == (1 + a) * f(x, y) assert collect(f(x, y) + a * f(x, y), f(w1, w1)) == f(x, y) + a * f(x, y) assert collect(f(x, x) + a * f(x, x), f(w1, w1)) == (1 + a) * f(x, x) assert collect(a * (x + 1)**y + (x + 1)**y, w1**y) == (1 + a) * (x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, w1**b) == \ a*(x + 1)**y + (x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, (x + 1)**w2) == \ (1 + a)*(x + 1)**y assert collect(a * (x + 1)**y + (x + 1)**y, w1**w2) == (1 + a) * (x + 1)**y
def test_add(): p = Wild('p') e = a + b assert e.match(p + b) == {p: a} assert e.match(p + a) == {p: b} e = 1 + b assert e.match(p + b) == {p: 1} e = a + b + c assert e.match(a + p + c) == {p: b} assert e.match(b + p + c) == {p: a} e = a + b + c + x assert e.match(a + p + x + c) == {p: b} assert e.match(b + p + c + x) == {p: a} assert e.match(b) is None assert e.match(b + p) == {p: a + c + x} assert e.match(a + p + c) == {p: b + x} assert e.match(b + p + c) == {p: a + x} e = 4 * x + 5 assert e.match(4 * x + p) == {p: 5} assert e.match(3 * x + p) == {p: x + 5} assert e.match(p * x + 5) == {p: 4}
def test_core_symbol(): # make the Symbol a unique name that doesn't class with any other # testing variable in this file since after this test the symbol # having the same name will be cached as noncommutative for c in (Dummy, Dummy('x', commutative=False), Symbol, Symbol('_sympyissue_6229', commutative=False), Wild, Wild('x')): check(c)
def test_roots_preprocessed(): E, F, J, L = symbols("E,F,J,L") f = -21601054687500000000*E**8*J**8/L**16 + \ 508232812500000000*F*x*E**7*J**7/L**14 - \ 4269543750000000*E**6*F**2*J**6*x**2/L**12 + \ 16194716250000*E**5*F**3*J**5*x**3/L**10 - \ 27633173750*E**4*F**4*J**4*x**4/L**8 + \ 14840215*E**3*F**5*J**3*x**5/L**6 + \ 54794*E**2*F**6*J**2*x**6/(5*L**4) - \ 1153*E*J*F**7*x**7/(80*L**2) + \ 633*F**8*x**8/160000 assert roots(f, x) == {} R1 = roots(f.evalf(strict=False), x, multiple=True) R2 = [ -1304.88375606366, 97.1168816800648, 186.946430171876, 245.526792947065, 503.441004174773, 791.549343830097, 1273.16678129348, 1850.10650616851 ] w = Wild('w') p = w * E * J / (F * L**2) assert len(R1) == len(R2) for r1, r2 in zip(R1, R2): match = r1.match(p) assert match is not None and abs(match[w] - r2) < 1e-10
def test_sympyissue_3773(): z, phi, r = symbols('z phi r') c, A, B, N = symbols('c A B N', cls=Wild) l = Wild('l', exclude=(0, )) eq = z * sin(2 * phi) * r**7 matcher = c * sin(phi * N)**l * r**A * log(r)**B assert eq.match(matcher) == {c: z, l: 1, N: 2, A: 7, B: 0} assert (-eq).match(matcher) == {c: -z, l: 1, N: 2, A: 7, B: 0} assert (x * eq).match(matcher) == {c: x * z, l: 1, N: 2, A: 7, B: 0} assert (-7 * x * eq).match(matcher) == { c: -7 * x * z, l: 1, N: 2, A: 7, B: 0 } matcher = c * sin(phi * N)**l * r**A assert eq.match(matcher) == {c: z, l: 1, N: 2, A: 7} assert (-eq).match(matcher) == {c: -z, l: 1, N: 2, A: 7} assert (x * eq).match(matcher) == {c: x * z, l: 1, N: 2, A: 7} assert (-7 * x * eq).match(matcher) == {c: -7 * x * z, l: 1, N: 2, A: 7}
def test_derivative2(): f = Function("f") x = Symbol("x") a = Wild("a", exclude=[f, x]) b = Wild("b", exclude=[f]) e = Derivative(f(x), x) assert e.match(Derivative(f(x), x)) == {} assert e.match(Derivative(f(x), x, x)) is None e = Derivative(f(x), x, x) assert e.match(Derivative(f(x), x)) is None assert e.match(Derivative(f(x), x, x)) == {} e = Derivative(f(x), x) + x**2 assert e.match(a * Derivative(f(x), x) + b) == {a: 1, b: x**2} assert e.match(a * Derivative(f(x), x, x) + b) is None e = Derivative(f(x), x, x) + x**2 assert e.match(a * Derivative(f(x), x) + b) is None assert e.match(a * Derivative(f(x), x, x) + b) == {a: 1, b: x**2}
def test_sympyissue_4883(): a = Wild('a') e = [i**2 for i in (x - 2, 2 - x)] p = [i**2 for i in (x - a, a - x)] for eq in e: for pat in p: assert eq.match(pat) == {a: 2}
def test_functions(): g = WildFunction('g') p = Wild('p') q = Wild('q') f = cos(5 * x) notf = x assert f.match(p * cos(q * x)) == {p: 1, q: 5} assert f.match(p * g) == {p: 1, g: cos(5 * x)} assert notf.match(g) is None F = WildFunction('F', nargs=2) assert F.nargs == FiniteSet(2) f = Function('f') assert f(x).match(F) is None F = WildFunction('F', nargs=(1, 2)) assert F.nargs == FiniteSet(1, 2)
def test_wild_str(): # Check expressions containing Wild not causing infinite recursion w = Wild('x') assert str(w + 1) == 'x_ + 1' assert str(exp(2**w) + 5) == 'E**(2**x_) + 5' assert str(3 * w + 1) == '3*x_ + 1' assert str(1 / w + 1) == '1 + 1/x_' assert str(w**2 + 1) == 'x_**2 + 1' assert str(1 / (1 - w)) == '1/(-x_ + 1)'
def test_ineq_avoid_wild_symbol_flip(): p = Wild('p') assert Gt(x, p) == Gt(x, p, evaluate=False) assert (x < p) == Lt(x, p, evaluate=False) # issue sympy/sympy#7951 # Previously failed as 'p > x': e = Lt(x, y).subs({y: p}) assert e == Lt(x, p, evaluate=False) # Previously failed as 'p <= x': e = Ge(x, p).doit() assert e == Ge(x, p, evaluate=False)
def _eval_imageset(self, f): from diofant import Wild expr = f.expr if len(f.variables) > 1: return n = f.variables[0] a = Wild('a') b = Wild('b') match = expr.match(a * n + b) if match[a].is_negative: expr = -expr match = expr.match(a * n + b) if match[a] is S.One and match[b].is_integer: expr = expr - match[b] return ImageSet(Lambda(n, expr), S.Integers)
def test_exclude(): p = Wild('p', exclude=[1, x]) q = Wild('q') r = Wild('r', exclude=[sin, y]) assert sin(x).match(r) is None assert cos(y).match(r) is None e = 3 * x**2 + y * x + a assert e.match(p * x**2 + q * x + r) == {p: 3, q: y, r: a} e = x + 1 assert e.match(x + p) is None assert e.match(p + 1) is None assert e.match(x + 1 + p) == {p: 0} e = cos(x) + 5 * sin(y) assert e.match(r) is None assert e.match(cos(y) + r) is None assert e.match(r + p * sin(q)) == {r: cos(x), p: 5, q: y}