コード例 #1
0
    def _compute_normalization_constant(self, beta, n):
        """
        Helper function for computing normalization
        constant for joint probability density of eigen
        values of Gaussian ensembles.

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Selberg_integral#Mehta's_integral
        """
        n = S(n)
        prod_term = lambda j: gamma(1 + beta * S(j) / 2) / gamma(S.One + beta /
                                                                 S(2))
        j = Dummy('j', integer=True, positive=True)
        term1 = Product(prod_term(j), (j, 1, n)).doit()
        term2 = (2 / (beta * n))**(beta * n * (n - 1) / 4 + n / 2)
        term3 = (2 * pi)**(n / 2)
        return term1 * term2 * term3
コード例 #2
0
def test_free_symbols():
    for func in [Sum, Product]:
        assert func(1, (x, 1, 2)).free_symbols == set()
        assert func(0, (x, 1, y)).free_symbols == {y}
        assert func(2, (x, 1, y)).free_symbols == {y}
        assert func(x, (x, 1, 2)).free_symbols == set()
        assert func(x, (x, 1, y)).free_symbols == {y}
        assert func(x, (y, 1, y)).free_symbols == {x, y}
        assert func(x, (y, 1, 2)).free_symbols == {x}
        assert func(x, (y, 1, 1)).free_symbols == {x}
        assert func(x, (y, 1, z)).free_symbols == {x, z}
        assert func(x, (x, 1, y), (y, 1, 2)).free_symbols == set()
        assert func(x, (x, 1, y), (y, 1, z)).free_symbols == {z}
        assert func(x, (x, 1, y), (y, 1, y)).free_symbols == {y}
        assert func(x, (y, 1, y), (y, 1, z)).free_symbols == {x, z}
    assert Sum(1, (x, 1, y)).free_symbols == {y}
    # free_symbols answers whether the object *as written* has free symbols,
    # not whether the evaluated expression has free symbols
    assert Product(1, (x, 1, y)).free_symbols == {y}
コード例 #3
0
def test_MultivariateEwens():
    from sympy.stats.joint_rv_types import MultivariateEwens

    n, theta, i = symbols("n theta i", positive=True)

    # tests for integer dimensions
    theta_f = symbols("t_f", negative=True)
    a = symbols("a_1:4", positive=True, integer=True)
    ed = MultivariateEwens("E", 3, theta)
    assert density(ed)(a[0], a[1], a[2]) == Piecewise(
        (
            6 * 2**(-a[1]) * 3**(-a[2]) * theta**a[0] * theta**a[1] *
            theta**a[2] / (theta * (theta + 1) *
                           (theta + 2) * factorial(a[0]) * factorial(a[1]) *
                           factorial(a[2])),
            Eq(a[0] + 2 * a[1] + 3 * a[2], 3),
        ),
        (0, True),
    )
    assert marginal_distribution(ed, ed[1])(a[1]) == Piecewise(
        (
            6 * 2**(-a[1]) * theta**a[1] / ((theta + 1) *
                                            (theta + 2) * factorial(a[1])),
            Eq(2 * a[1] + 1, 3),
        ),
        (0, True),
    )
    raises(ValueError, lambda: MultivariateEwens("e1", 5, theta_f))

    # tests for symbolic dimensions
    eds = MultivariateEwens("E", n, theta)
    a = IndexedBase("a")
    j, k = symbols("j, k")
    den = Piecewise(
        (
            factorial(n) *
            Product(theta**a[j] * (j + 1)**(-a[j]) / factorial(a[j]),
                    (j, 0, n - 1)) / RisingFactorial(theta, n),
            Eq(n, Sum((k + 1) * a[k], (k, 0, n - 1))),
        ),
        (0, True),
    )
    assert density(eds)(a).dummy_eq(den)
コード例 #4
0
 def pdf(self, *syms):
     n, theta = self.n, self.theta
     condi = isinstance(self.n, Integer)
     if not (isinstance(syms[0], IndexedBase) or condi):
         raise ValueError("Please use IndexedBase object for syms as "
                          "the dimension is symbolic")
     term_1 = factorial(n) / rf(theta, n)
     if condi:
         term_2 = Mul.fromiter(theta**syms[j] /
                               ((j + 1)**syms[j] * factorial(syms[j]))
                               for j in range(n))
         cond = Eq(sum([(k + 1) * syms[k] for k in range(n)]), n)
         return Piecewise((term_1 * term_2, cond), (0, True))
     syms = syms[0]
     j, k = symbols('j, k', positive=True, integer=True)
     term_2 = Product(
         theta**syms[j] / ((j + 1)**syms[j] * factorial(syms[j])),
         (j, 0, n - 1))
     cond = Eq(Sum((k + 1) * syms[k], (k, 0, n - 1)), n)
     return Piecewise((term_1 * term_2, cond), (0, True))
コード例 #5
0
def test_Wishart():
    W = Wishart('W', 5, [[1, 0], [0, 1]])
    assert W.pspace.distribution.set == MatrixSet(2, 2, S.Reals)
    X = MatrixSymbol('X', 2, 2)
    term1 = exp(Trace(Matrix([[-S(1)/2, 0], [0, -S(1)/2]])*X))
    assert density(W)(X).doit() == term1 * Determinant(X)/(24*pi)
    assert density(W)([[2, 1], [1, 2]]).doit() == exp(-2)/(8*pi)
    n = symbols('n', positive=True)
    d = symbols('d', positive=True, integer=True)
    Y = MatrixSymbol('Y', d, d)
    SM = MatrixSymbol('SM', d, d)
    W = Wishart('W', n, SM)
    k = Dummy('k')
    exprd = 2**(-d*n/2)*pi**(-d*(d - 1)/4)*exp(Trace(-(S(1)/2)*SM**(-1)*Y)
    )*Determinant(SM)**(-n/2)*Determinant(Y)**(
    -d/2 + n/2 - S(1)/2)/Product(gamma(-k/2 + n/2 + S(1)/2), (k, 1, d))
    assert density(W)(Y).dummy_eq(exprd)
    raises(ValueError, lambda: density(W)(1))
    raises(ValueError, lambda: Wishart('W', -1, [[1, 0], [0, 1]]))
    raises(ValueError, lambda: Wishart('W', -1, [[1, 0], [2, 1]]))
    raises(ValueError, lambda: Wishart('W',  2, [[1, 0], [0]]))
コード例 #6
0
ファイル: test_random_matrix.py プロジェクト: msgoff/sympy
def test_GaussianUnitaryEnsemble():
    H = RandomMatrixSymbol("H", 3, 3)
    G = GUE("U", 3)
    assert density(G)(H) == sqrt(2) * exp(
        -3 * Trace(H**2) / 2) / (4 * pi**Rational(9, 2))
    i, j = (
        Dummy("i", integer=True, positive=True),
        Dummy("j", integer=True, positive=True),
    )
    l = IndexedBase("l")
    assert joint_eigen_distribution(G).dummy_eq(
        Lambda(
            (l[1], l[2], l[3]),
            27 * sqrt(6) * exp(-3 * (l[1]**2) / 2 - 3 * (l[2]**2) / 2 - 3 *
                               (l[3]**2) / 2) *
            Product(Abs(l[i] - l[j])**2, (j, i + 1, 3),
                    (i, 1, 2)) / (16 * pi**Rational(3, 2)),
        ))
    s = Dummy("s")
    assert level_spacing_distribution(G).dummy_eq(
        Lambda(s, 32 * s**2 * exp(-4 * s**2 / pi) / pi**2))
コード例 #7
0
ファイル: test_random_matrix.py プロジェクト: msgoff/sympy
def test_GaussianOrthogonalEnsemble():
    H = RandomMatrixSymbol("H", 3, 3)
    _H = MatrixSymbol("_H", 3, 3)
    G = GOE("O", 3)
    assert density(G)(H) == exp(-3 * Trace(H**2) / 4) / Integral(
        exp(-3 * Trace(_H**2) / 4), _H)
    i, j = (
        Dummy("i", integer=True, positive=True),
        Dummy("j", integer=True, positive=True),
    )
    l = IndexedBase("l")
    assert joint_eigen_distribution(G).dummy_eq(
        Lambda(
            (l[1], l[2], l[3]),
            9 * sqrt(2) *
            exp(-3 * l[1]**2 / 2 - 3 * l[2]**2 / 2 - 3 * l[3]**2 / 2) *
            Product(Abs(l[i] - l[j]), (j, i + 1, 3), (i, 1, 2)) / (32 * pi),
        ))
    s = Dummy("s")
    assert level_spacing_distribution(G).dummy_eq(
        Lambda(s,
               s * pi * exp(-(s**2) * pi / 4) / 2))
コード例 #8
0
ファイル: test_random_matrix.py プロジェクト: msgoff/sympy
def test_GaussianSymplecticEnsemble():
    H = RandomMatrixSymbol("H", 3, 3)
    _H = MatrixSymbol("_H", 3, 3)
    G = GSE("O", 3)
    assert density(G)(H) == exp(-3 * Trace(H**2)) / Integral(
        exp(-3 * Trace(_H**2)), _H)
    i, j = (
        Dummy("i", integer=True, positive=True),
        Dummy("j", integer=True, positive=True),
    )
    l = IndexedBase("l")
    assert joint_eigen_distribution(G).dummy_eq(
        Lambda(
            (l[1], l[2], l[3]),
            162 * sqrt(3) *
            exp(-3 * l[1]**2 / 2 - 3 * l[2]**2 / 2 - 3 * l[3]**2 / 2) *
            Product(Abs(l[i] - l[j])**4, (j, i + 1, 3),
                    (i, 1, 2)) / (5 * pi**Rational(3, 2)),
        ))
    s = Dummy("s")
    assert level_spacing_distribution(G).dummy_eq(
        Lambda(s,
               S(262144) * s**4 * exp(-64 * s**2 / (9 * pi)) / (729 * pi**3)))
コード例 #9
0
ファイル: test_products.py プロジェクト: twobitlogic/sympy
def test_simple_products():
    assert product(2, (k, a, n)) == 2**(n - a + 1)
    assert product(k, (k, 1, n)) == factorial(n)
    assert product(k**3, (k, 1, n)) == factorial(n)**3

    assert product(k + 1, (k, 0, n - 1)) == factorial(n)
    assert product(k + 1, (k, a, n - 1)) == rf(1 + a, n - a)

    assert product(cos(k),
                   (k, 0, 5)) == cos(1) * cos(2) * cos(3) * cos(4) * cos(5)
    assert product(cos(k), (k, 3, 5)) == cos(3) * cos(4) * cos(5)
    assert product(cos(k), (k, 1, Rational(5, 2))) != cos(1) * cos(2)

    assert isinstance(product(k**k, (k, 1, n)), Product)

    assert Product(x**k, (k, 1, n)).variables == [k]

    raises(ValueError, lambda: Product(n))
    raises(ValueError, lambda: Product(n, k))
    raises(ValueError, lambda: Product(n, k, 1))
    raises(ValueError, lambda: Product(n, k, 1, 10))
    raises(ValueError, lambda: Product(n, (k, 1)))
コード例 #10
0
def test_indexed_idx_sum():
    i = symbols('i', cls=Idx)
    r = Indexed('r', i)
    assert Sum(r, (i, 0, 3)).doit() == sum([r.xreplace({i: j}) for j in range(4)])
    assert Product(r, (i, 0, 3)).doit() == prod([r.xreplace({i: j}) for j in range(4)])

    j = symbols('j', integer=True)
    assert Sum(r, (i, j, j+2)).doit() == sum([r.xreplace({i: j+k}) for k in range(3)])
    assert Product(r, (i, j, j+2)).doit() == prod([r.xreplace({i: j+k}) for k in range(3)])

    k = Idx('k', range=(1, 3))
    A = IndexedBase('A')
    assert Sum(A[k], k).doit() == sum([A[Idx(j, (1, 3))] for j in range(1, 4)])
    assert Product(A[k], k).doit() == prod([A[Idx(j, (1, 3))] for j in range(1, 4)])

    raises(ValueError, lambda: Sum(A[k], (k, 1, 4)))
    raises(ValueError, lambda: Sum(A[k], (k, 0, 3)))
    raises(ValueError, lambda: Sum(A[k], (k, 2, oo)))

    raises(ValueError, lambda: Product(A[k], (k, 1, 4)))
    raises(ValueError, lambda: Product(A[k], (k, 0, 3)))
    raises(ValueError, lambda: Product(A[k], (k, 2, oo)))
コード例 #11
0
def test_karr_convention():
    # Test the Karr product convention that we want to hold.
    # See his paper "Summation in Finite Terms" for a detailed
    # reasoning why we really want exactly this definition.
    # The convention is described for sums on page 309 and
    # essentially in section 1.4, definition 3. For products
    # we can find in analogy:
    #
    # \prod_{m <= i < n} f(i) 'has the obvious meaning'      for m < n
    # \prod_{m <= i < n} f(i) = 0                            for m = n
    # \prod_{m <= i < n} f(i) = 1 / \prod_{n <= i < m} f(i)  for m > n
    #
    # It is important to note that he defines all products with
    # the upper limit being *exclusive*.
    # In contrast, sympy and the usual mathematical notation has:
    #
    # prod_{i = a}^b f(i) = f(a) * f(a+1) * ... * f(b-1) * f(b)
    #
    # with the upper limit *inclusive*. So translating between
    # the two we find that:
    #
    # \prod_{m <= i < n} f(i) = \prod_{i = m}^{n-1} f(i)
    #
    # where we intentionally used two different ways to typeset the
    # products and its limits.

    i = Symbol("i", integer=True)
    k = Symbol("k", integer=True)
    j = Symbol("j", integer=True)

    # A simple example with a concrete factors and symbolic limits.

    # The normal product: m = k and n = k + j and therefore m < n:
    m = k
    n = k + j

    a = m
    b = n - 1
    S1 = Product(i**2, (i, a, b)).doit()

    # The reversed product: m = k + j and n = k and therefore m > n:
    m = k + j
    n = k

    a = m
    b = n - 1
    S2 = Product(i**2, (i, a, b)).doit()

    assert simplify(S1 * S2) == 1

    # Test the empty product: m = k and n = k and therefore m = n:
    m = k
    n = k

    a = m
    b = n - 1
    Sz = Product(i**2, (i, a, b)).doit()

    assert Sz == 1

    # Another example this time with an unspecified factor and
    # numeric limits. (We can not do both tests in the same example.)
    f = Function("f")

    # The normal product with m < n:
    m = 2
    n = 11

    a = m
    b = n - 1
    S1 = Product(f(i), (i, a, b)).doit()

    # The reversed product with m > n:
    m = 11
    n = 2

    a = m
    b = n - 1
    S2 = Product(f(i), (i, a, b)).doit()

    assert simplify(S1 * S2) == 1

    # Test the empty product with m = n:
    m = 5
    n = 5

    a = m
    b = n - 1
    Sz = Product(f(i), (i, a, b)).doit()

    assert Sz == 1
コード例 #12
0
ファイル: test_evalf.py プロジェクト: vctcn93/sympy
def test_evalf_product():
    assert Product(n, (n, 1, 10)).evalf() == 3628800.
    assert Product(1 - S.Half**2 / n**2, (n, 1, oo)).evalf(5) == 0.63662
    assert Product(n, (n, -1, 3)).evalf() == 0
コード例 #13
0
ファイル: test_products.py プロジェクト: tuhina/sympy
def test_infinite_product():
    # Issue 2638
    assert isinstance(Product(2**(1 / factorial(n)), (n, 0, oo)), Product)
コード例 #14
0
def test_KroneckerDelta_Product():
    y = Symbol('y')
    assert Product(x*KroneckerDelta(x, y), (x, 0, 1)).doit() == 0
コード例 #15
0
class TestAllGood(object):
    # These latex strings should parse to the corresponding SymPy expression
    GOOD_PAIRS = [
        ("0", Rational(0)),
        ("1", Rational(1)),
        ("-3.14", Rational(-314, 100)),
        ("5-3", _Add(5, _Mul(-1, 3))),
        ("(-7.13)(1.5)", _Mul(Rational('-7.13'), Rational('1.5'))),
        ("\\left(-7.13\\right)\\left(1.5\\right)", _Mul(Rational('-7.13'), Rational('1.5'))),
        ("x", x),
        ("2x", 2 * x),
        ("x^2", x**2),
        ("x^{3 + 1}", x**_Add(3, 1)),
        ("x^{\\left\\{3 + 1\\right\\}}", x**_Add(3, 1)),
        ("-3y + 2x", _Add(_Mul(2, x), Mul(-1, 3, y, evaluate=False))),
        ("-c", -c),
        ("a \\cdot b", a * b),
        ("a / b", a / b),
        ("a \\div b", a / b),
        ("a + b", a + b),
        ("a + b - a", Add(a, b, _Mul(-1, a), evaluate=False)),
        ("a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)),
        ("a^2 + b^2 != 2c^2", Ne(a**2 + b**2, 2 * c**2)),
        ("a\\mod b", Mod(a, b)),
        ("\\sin \\theta", sin(theta)),
        ("\\sin(\\theta)", sin(theta)),
        ("\\sin\\left(\\theta\\right)", sin(theta)),
        ("\\sin^{-1} a", asin(a)),
        ("\\sin a \\cos b", _Mul(sin(a), cos(b))),
        ("\\sin \\cos \\theta", sin(cos(theta))),
        ("\\sin(\\cos \\theta)", sin(cos(theta))),
        ("\\arcsin(a)", asin(a)),
        ("\\arccos(a)", acos(a)),
        ("\\arctan(a)", atan(a)),
        ("\\sinh(a)", sinh(a)),
        ("\\cosh(a)", cosh(a)),
        ("\\tanh(a)", tanh(a)),
        ("\\sinh^{-1}(a)", asinh(a)),
        ("\\cosh^{-1}(a)", acosh(a)),
        ("\\tanh^{-1}(a)", atanh(a)),
        ("\\arcsinh(a)", asinh(a)),
        ("\\arccosh(a)", acosh(a)),
        ("\\arctanh(a)", atanh(a)),
        ("\\arsinh(a)", asinh(a)),
        ("\\arcosh(a)", acosh(a)),
        ("\\artanh(a)", atanh(a)),
        ("\\operatorname{arcsinh}(a)", asinh(a)),
        ("\\operatorname{arccosh}(a)", acosh(a)),
        ("\\operatorname{arctanh}(a)", atanh(a)),
        ("\\operatorname{arsinh}(a)", asinh(a)),
        ("\\operatorname{arcosh}(a)", acosh(a)),
        ("\\operatorname{artanh}(a)", atanh(a)),
        ("\\operatorname{gcd}(a, b)", UnevaluatedExpr(gcd(a, b))),
        ("\\operatorname{lcm}(a, b)", UnevaluatedExpr(lcm(a, b))),
        ("\\operatorname{gcd}(a,b)", UnevaluatedExpr(gcd(a, b))),
        ("\\operatorname{lcm}(a,b)", UnevaluatedExpr(lcm(a, b))),
        ("\\operatorname{floor}(a)", floor(a)),
        ("\\operatorname{ceil}(b)", ceiling(b)),
        ("\\cos^2(x)", cos(x)**2),
        ("\\cos(x)^2", cos(x)**2),
        ("\\gcd(a, b)", UnevaluatedExpr(gcd(a, b))),
        ("\\lcm(a, b)", UnevaluatedExpr(lcm(a, b))),
        ("\\gcd(a,b)", UnevaluatedExpr(gcd(a, b))),
        ("\\lcm(a,b)", UnevaluatedExpr(lcm(a, b))),
        ("\\floor(a)", floor(a)),
        ("\\ceil(b)", ceiling(b)),
        ("\\max(a, b)", Max(a, b)),
        ("\\min(a, b)", Min(a, b)),
        ("\\frac{a}{b}", a / b),
        ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))),
        ("\\frac{7}{3}", Rational(7, 3)),
        ("(\\csc x)(\\sec y)", csc(x) * sec(y)),
        ("\\lim_{x \\to 3} a", Limit(a, x, 3)),
        ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)),
        ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)),
        ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)),
        ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)),
        ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')),
        ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')),
        ("\\infty", oo),
        ("\\infty\\%", oo),
        ("\\$\\infty", oo),
        ("-\\infty", -oo),
        ("-\\infty\\%", -oo),
        ("-\\$\\infty", -oo),
        ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)),
        ("\\frac{d}{dx} x", Derivative(x, x)),
        ("\\frac{d}{dt} x", Derivative(x, t)),
        # ("f(x)", f(x)),
        # ("f(x, y)", f(x, y)),
        # ("f(x, y, z)", f(x, y, z)),
        # ("\\frac{d f(x)}{dx}", Derivative(f(x), x)),
        # ("\\frac{d\\theta(x)}{dx}", Derivative(theta(x), x)),
        ("|x|", _Abs(x)),
        ("\\left|x\\right|", _Abs(x)),
        ("||x||", _Abs(_Abs(x))),
        ("|x||y|", _Abs(x) * _Abs(y)),
        ("||x||y||", _Abs(_Abs(x) * _Abs(y))),
        ("\\lfloor x\\rfloor", floor(x)),
        ("\\lceil y\\rceil", ceiling(y)),
        ("\\pi^{|xy|}", pi**_Abs(x * y)),
        ("\\frac{\\pi}{3}", _Mul(pi, _Pow(3, -1))),
        ("\\sin{\\frac{\\pi}{2}}", sin(_Mul(pi, _Pow(2, -1)), evaluate=False)),
        ("a+bI", a + I * b),
        ("e^{I\\pi}", Integer(-1)),
        ("\\int x dx", Integral(x, x)),
        ("\\int x d\\theta", Integral(x, theta)),
        ("\\int (x^2 - y)dx", Integral(x**2 - y, x)),
        ("\\int x + a dx", Integral(_Add(x, a), x)),
        ("\\int da", Integral(1, a)),
        ("\\int_0^7 dx", Integral(1, (x, 0, 7))),
        ("\\int_a^b x dx", Integral(x, (x, a, b))),
        ("\\int^b_a x dx", Integral(x, (x, a, b))),
        ("\\int_{a}^b x dx", Integral(x, (x, a, b))),
        ("\\int^{b}_a x dx", Integral(x, (x, a, b))),
        ("\\int_{a}^{b} x dx", Integral(x, (x, a, b))),
        ("\\int_{  }^{}x dx", Integral(x, x)),
        ("\\int^{  }_{ }x dx", Integral(x, x)),
        ("\\int^{b}_{a} x dx", Integral(x, (x, a, b))),
        # ("\\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))),
        ("\\int (x+a)", Integral(_Add(x, a), x)),
        ("\\int a + b + c dx", Integral(Add(a, b, c, evaluate=False), x)),
        ("\\int \\frac{dz}{z}", Integral(Pow(z, -1), z)),
        ("\\int \\frac{3 dz}{z}", Integral(3 * Pow(z, -1), z)),
        ("\\int \\frac{1}{x} dx", Integral(Pow(x, -1), x)),
        ("\\int \\frac{1}{a} + \\frac{1}{b} dx", Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)),
        ("\\int \\frac{3 \\cdot d\\theta}{\\theta}", Integral(3 * _Pow(theta, -1), theta)),
        ("\\int \\frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)),
        ("x_0", Symbol('x_0', real=True, positive=True)),
        ("x_{1}", Symbol('x_1', real=True, positive=True)),
        ("x_a", Symbol('x_a', real=True, positive=True)),
        ("x_{b}", Symbol('x_b', real=True, positive=True)),
        ("h_\\theta", Symbol('h_{\\theta}', real=True, positive=True)),
        ("h_\\theta ", Symbol('h_{\\theta}', real=True, positive=True)),
        ("h_{\\theta}", Symbol('h_{\\theta}', real=True, positive=True)),
        # ("h_{\\theta}(x_0, x_1)", Symbol('h_{theta}', real=True)(Symbol('x_{0}', real=True), Symbol('x_{1}', real=True))),
        ("x!", _factorial(x)),
        ("100!", _factorial(100)),
        ("\\theta!", _factorial(theta)),
        ("(x + 1)!", _factorial(_Add(x, 1))),
        ("\\left(x + 1\\right)!", _factorial(_Add(x, 1))),
        ("(x!)!", _factorial(_factorial(x))),
        ("x!!!", _factorial(_factorial(_factorial(x)))),
        ("5!7!", _Mul(_factorial(5), _factorial(7))),
        ("\\sqrt{x}", sqrt(x)),
        ("\\sqrt{x + b}", sqrt(_Add(x, b))),
        ("\\sqrt[3]{\\sin x}", root(sin(x), 3)),
        ("\\sqrt[y]{\\sin x}", root(sin(x), y)),
        ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)),
        ("x < y", StrictLessThan(x, y)),
        ("x \\leq y", LessThan(x, y)),
        ("x > y", StrictGreaterThan(x, y)),
        ("x \\geq y", GreaterThan(x, y)),
        ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))),
        ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))),
        ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))),
        ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))),
        ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))),
        ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))),
        ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))),
        ("\\prod_{a = b}^c x", Product(x, (a, b, c))),
        ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))),
        ("\\prod^c_{a = b} x", Product(x, (a, b, c))),
        ("\\ln x", _log(x, E)),
        ("\\ln xy", _log(x * y, E)),
        ("\\log x", _log(x, 10)),
        ("\\log xy", _log(x * y, 10)),
        # ("\\log_2 x", _log(x, 2)),
        ("\\log_{2} x", _log(x, 2)),
        # ("\\log_a x", _log(x, a)),
        ("\\log_{a} x", _log(x, a)),
        ("\\log_{11} x", _log(x, 11)),
        ("\\log_{a^2} x", _log(x, _Pow(a, 2))),
        ("[x]", x),
        ("[a + b]", _Add(a, b)),
        ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x)),
        ("2\\overline{x}", 2 * Symbol('xbar', real=True, positive=True)),
        ("2\\overline{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)),
        ("\\frac{x}{\\overline{x}_n}", x / Symbol('xbar_n', real=True, positive=True)),
        ("\\frac{\\sin(x)}{\\overline{x}_n}", sin(x) / Symbol('xbar_n', real=True, positive=True)),
        ("2\\bar{x}", 2 * Symbol('xbar', real=True, positive=True)),
        ("2\\bar{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)),
        ("\\sin\\left(\\theta\\right) \\cdot4", sin(theta) * 4),
        ("\\ln\\left(\\theta\\right)", _log(theta, E)),
        ("\\ln\\left(x-\\theta\\right)", _log(x - theta, E)),
        ("\\ln\\left(\\left(x-\\theta\\right)\\right)", _log(x - theta, E)),
        ("\\ln\\left(\\left[x-\\theta\\right]\\right)", _log(x - theta, E)),
        ("\\ln\\left(\\left\\{x-\\theta\\right\\}\\right)", _log(x - theta, E)),
        ("\\ln\\left(\\left|x-\\theta\\right|\\right)", _log(_Abs(x - theta), E)),
        ("\\frac{1}{2}xy(x+y)", Mul(Rational(1, 2), x, y, (x + y), evaluate=False)),
        ("\\frac{1}{2}\\theta(x+y)", Mul(Rational(1, 2), theta, (x + y), evaluate=False)),
        ("1-f(x)", 1 - f * x),

        ("\\begin{matrix}1&2\\\\3&4\\end{matrix}", Matrix([[1, 2], [3, 4]])),
        ("\\begin{matrix}x&x^2\\\\\\sqrt{x}&x\\end{matrix}", Matrix([[x, x**2], [_Pow(x, S.Half), x]])),
        ("\\begin{matrix}\\sqrt{x}\\\\\\sin(\\theta)\\end{matrix}", Matrix([_Pow(x, S.Half), sin(theta)])),
        ("\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}", Matrix([[1, 2], [3, 4]])),
        ("\\begin{bmatrix}1&2\\\\3&4\\end{bmatrix}", Matrix([[1, 2], [3, 4]])),

        # scientific notation
        ("2.5\\times 10^2", Rational(250)),
        ("1,500\\times 10^{-1}", Rational(150)),

        # e notation
        ("2.5E2", Rational(250)),
        ("1,500E-1", Rational(150)),

        # multiplication without cmd
        ("2x2y", Mul(2, x, 2, y, evaluate=False)),
        ("2x2", Mul(2, x, 2, evaluate=False)),
        ("x2", x * 2),

        # lin alg processing
        ("\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(theta, Matrix([[1, 2], [3, 4]]), evaluate=False)),
        ("\\theta\\begin{matrix}1\\\\3\\end{matrix} - \\begin{matrix}-1\\\\2\\end{matrix}", MatAdd(MatMul(theta, Matrix([[1], [3]]), evaluate=False), MatMul(-1, Matrix([[-1], [2]]), evaluate=False), evaluate=False)),
        ("\\theta\\begin{matrix}1&0\\\\0&1\\end{matrix}*\\begin{matrix}3\\\\-2\\end{matrix}", MatMul(theta, Matrix([[1, 0], [0, 1]]), Matrix([3, -2]), evaluate=False)),
        ("\\frac{1}{9}\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(Rational(1, 9), theta, Matrix([[1, 2], [3, 4]]), evaluate=False)),
        ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]),
        ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix};\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]),
        ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]),
        ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix},\\begin{pmatrix}1\\\\1\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1]), Matrix([1, 1, 1])]),
        ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right\\}", Matrix([1, 2, 3])),
        ("\\left{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right}", Matrix([1, 2, 3])),
        ("{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}}", Matrix([1, 2, 3])),

        # us dollars
        ("\\$1,000.00", Rational(1000)),
        ("\\$543.21", Rational(54321, 100)),
        ("\\$0.009", Rational(9, 1000)),

        # percentages
        ("100\\%", Rational(1)),
        ("1.5\\%", Rational(15, 1000)),
        ("0.05\\%", Rational(5, 10000)),

        # empty set
        ("\\emptyset", S.EmptySet),

        # divide by zero
        ("\\frac{1}{0}", _Pow(0, -1)),
        ("1+\\frac{5}{0}", _Add(1, _Mul(5, _Pow(0, -1)))),

        # adjacent single char sub sup
        ("4^26^2", _Mul(_Pow(4, 2), _Pow(6, 2))),
        ("x_22^2", _Mul(Symbol('x_2', real=True, positive=True), _Pow(2, 2)))
    ]

    def test_good_pair(self, s, eq):
        assert_equal(s, eq)
コード例 #16
0
def test_issue_13546():
    n = Symbol('n')
    k = Symbol('k')
    p = Product(n + 1 / 2**k, (k, 0, n-1)).doit()
    assert p.subs(n, 2).doit() == Rational(15, 2)
コード例 #17
0
def test_rewrite_Sum():
    assert Product(1 - S.Half**2/k**2, (k, 1, oo)).rewrite(Sum) == \
        exp(Sum(log(1 - 1/(4*k**2)), (k, 1, oo)))
コード例 #18
0
def test_reverse_order():
    x, y, a, b, c, d= symbols('x, y, a, b, c, d', integer = True)

    assert Product(x, (x, 0, 3)).reverse_order(0) == Product(1/x, (x, 4, -1))
    assert Product(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(0, 1) == \
           Product(x*y, (x, 6, 0), (y, 7, -1))
    assert Product(x, (x, 1, 2)).reverse_order(0) == Product(1/x, (x, 3, 0))
    assert Product(x, (x, 1, 3)).reverse_order(0) == Product(1/x, (x, 4, 0))
    assert Product(x, (x, 1, a)).reverse_order(0) == Product(1/x, (x, a + 1, 0))
    assert Product(x, (x, a, 5)).reverse_order(0) == Product(1/x, (x, 6, a - 1))
    assert Product(x, (x, a + 1, a + 5)).reverse_order(0) == \
           Product(1/x, (x, a + 6, a))
    assert Product(x, (x, a + 1, a + 2)).reverse_order(0) == \
           Product(1/x, (x, a + 3, a))
    assert Product(x, (x, a + 1, a + 1)).reverse_order(0) == \
           Product(1/x, (x, a + 2, a))
    assert Product(x, (x, a, b)).reverse_order(0) == Product(1/x, (x, b + 1, a - 1))
    assert Product(x, (x, a, b)).reverse_order(x) == Product(1/x, (x, b + 1, a - 1))
    assert Product(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1) == \
           Product(x*y, (x, b + 1, a - 1), (y, 6, 1))
    assert Product(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x) == \
           Product(x*y, (x, b + 1, a - 1), (y, 6, 1))
コード例 #19
0
def test_issue_9983():
    n = Symbol('n', integer=True, positive=True)
    p = Product(1 + 1/n**Rational(2, 3), (n, 1, oo))
    assert p.is_convergent() is S.false
    assert product(1 + 1/n**Rational(2, 3), (n, 1, oo)) == p.doit()
コード例 #20
0
ファイル: test_products.py プロジェクト: yang603/sympy
def test_simplify():
    y, t, b, c = symbols('y, t, b, c', integer=True)

    assert simplify(Product(x*y, (x, n, m), (y, a, k)) * \
        Product(y, (x, n, m), (y, a, k))) == \
            Product(x*y**2, (x, n, m), (y, a, k))
    assert simplify(3 * y* Product(x, (x, n, m)) * Product(x, (x, m + 1, a))) \
        == 3 * y * Product(x, (x, n, a))
    assert simplify(Product(x, (x, k + 1, a)) * Product(x, (x, n, k))) == \
        Product(x, (x, n, a))
    assert simplify(Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k))) == \
        Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k))
    assert simplify(Product(x, (t, a, b)) * Product(y, (t, a, b)) * \
        Product(x, (t, b+1, c))) == Product(x*y, (t, a, b)) * \
            Product(x, (t, b+1, c))
    assert simplify(Product(x, (t, a, b)) * Product(x, (t, b+1, c)) * \
        Product(y, (t, a, b))) == Product(x*y, (t, a, b)) * \
            Product(x, (t, b+1, c))
コード例 #21
0
def test_Product_is_convergent():
    assert Product(1/n**2, (n, 1, oo)).is_convergent() is S.false
    assert Product(exp(1/n**2), (n, 1, oo)).is_convergent() is S.true
    assert Product(1/n, (n, 1, oo)).is_convergent() is S.false
    assert Product(1 + 1/n, (n, 1, oo)).is_convergent() is S.false
    assert Product(1 + 1/n**2, (n, 1, oo)).is_convergent() is S.true
コード例 #22
0
def test_distribution_over_equality():
    assert Product(Eq(x * 2, f(x)),
                   (x, 1, 3)).doit() == Eq(48,
                                           f(1) * f(2) * f(3))
    assert Sum(Eq(f(x), x**2), (x, 0, y)) == \
        Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
コード例 #23
0
def test_simple_products():
    assert Product(S.NaN, (x, 1, 3)) is S.NaN
    assert product(S.NaN, (x, 1, 3)) is S.NaN
    assert Product(x, (n, a, a)).doit() == x
    assert Product(x, (x, a, a)).doit() == a
    assert Product(x, (y, 1, a)).doit() == x**a

    lo, hi = 1, 2
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == 2
    assert s2.doit() == 1

    lo, hi = x, x + 1
    s1 = Product(n, (n, lo, hi))
    s2 = Product(n, (n, hi, lo))
    s3 = 1 / Product(n, (n, hi + 1, lo - 1))
    assert s1 != s2
    # This IS correct according to Karr product convention
    assert s1.doit() == x * (x + 1)
    assert s2.doit() == 1
    assert s3.doit() == x * (x + 1)

    assert Product(Integral(2*x, (x, 1, y)) + 2*x, (x, 1, 2)).doit() == \
        (y**2 + 1)*(y**2 + 3)
    assert product(2, (n, a, b)) == 2**(b - a + 1)
    assert product(n, (n, 1, b)) == factorial(b)
    assert product(n**3, (n, 1, b)) == factorial(b)**3
    assert product(3**(2 + n), (n, a, b)) \
        == 3**(2*(1 - a + b) + b/2 + (b**2)/2 + a/2 - (a**2)/2)
    assert product(cos(n), (n, 3, 5)) == cos(3) * cos(4) * cos(5)
    assert product(cos(n), (n, x, x + 2)) == cos(x) * cos(x + 1) * cos(x + 2)
    assert isinstance(product(cos(n), (n, x, x + S.Half)), Product)
    # If Product managed to evaluate this one, it most likely got it wrong!
    assert isinstance(Product(n**n, (n, 1, b)), Product)
コード例 #24
0
              ("\\sqrt[y]{\\sin x}", root(sin(x), y)),
              ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)),
              ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)),
              ("x > y", StrictGreaterThan(x, y)),
              ("x \\geq y", GreaterThan(x, y)), ("\\mathit{x}", Symbol('x')),
              ("\\mathit{test}", Symbol('test')),
              ("\\mathit{TEST}", Symbol('TEST')),
              ("\\mathit{HELLO world}", Symbol('HELLO world')),
              ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))),
              ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))),
              ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))),
              ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))),
              ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))),
              ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}",
               Sum(_Pow(_factorial(n), -1), (n, 0, oo))),
              ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))),
              ("\\prod_{a = b}^c x", Product(x, (a, b, c))),
              ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))),
              ("\\prod^c_{a = b} x", Product(x, (a, b, c))),
              ("\\ln x", _log(x, E)), ("\\ln xy", _log(x * y, E)),
              ("\\log x", _log(x, 10)), ("\\log xy", _log(x * y, 10)),
              ("\\log_{2} x", _log(x, 2)), ("\\log_{a} x", _log(x, a)),
              ("\\log_{11} x", _log(x, 11)),
              ("\\log_{a^2} x", _log(x, _Pow(a, 2))), ("[x]", x),
              ("[a + b]", _Add(a, b)),
              ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x))]


def test_parseable():
    from sympy.parsing.latex import parse_latex
    for latex_str, sympy_expr in GOOD_PAIRS:
コード例 #25
0
def test_exp_summation():
    w = symbols("w")
    m, n, i, j = symbols("m n i j")
    expr = exp(Sum(w * i, (i, 0, n), (j, 0, m)))
    assert expr.expand() == Product(exp(w * i), (i, 0, n), (j, 0, m))
コード例 #26
0
ファイル: numbers.py プロジェクト: xivarri/sympy
 def _eval_rewrite_as_Product(self, n):
     from sympy import Product
     if not (n.is_integer and n.is_nonnegative):
         return self
     k = Dummy('k', integer=True, positive=True)
     return Product((n + k) / k, (k, 2, n))
コード例 #27
0
def test_Product_doit():
    assert Product(n * Integral(a**2), (n, 1, 3)).doit() == 2 * a**9 / 9
    assert Product(n*Integral(a**2), (n, 1, 3)).doit(deep=False) == \
        6*Integral(a**2)**3
    assert product(n * Integral(a**2), (n, 1, 3)) == 6 * Integral(a**2)**3
コード例 #28
0
def test_simplify_prod():
    y, t, b, c = symbols('y, t, b, c', integer = True)

    _simplify = lambda e: simplify(e, doit=False)
    assert _simplify(Product(x*y, (x, n, m), (y, a, k)) * \
        Product(y, (x, n, m), (y, a, k))) == \
            Product(x*y**2, (x, n, m), (y, a, k))
    assert _simplify(3 * y* Product(x, (x, n, m)) * Product(x, (x, m + 1, a))) \
        == 3 * y * Product(x, (x, n, a))
    assert _simplify(Product(x, (x, k + 1, a)) * Product(x, (x, n, k))) == \
        Product(x, (x, n, a))
    assert _simplify(Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k))) == \
        Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k))
    assert _simplify(Product(x, (t, a, b)) * Product(y, (t, a, b)) * \
        Product(x, (t, b+1, c))) == Product(x*y, (t, a, b)) * \
            Product(x, (t, b+1, c))
    assert _simplify(Product(x, (t, a, b)) * Product(x, (t, b+1, c)) * \
        Product(y, (t, a, b))) == Product(x*y, (t, a, b)) * \
            Product(x, (t, b+1, c))
コード例 #29
0
ファイル: factorials.py プロジェクト: zenquiorra/sympy
 def _eval_rewrite_as_Product(self, n, **kwargs):
     from sympy import Product
     if n.is_nonnegative and n.is_integer:
         i = Dummy('i', integer=True)
         return Product(i, (i, 1, n))
コード例 #30
0
def test_reorder():
    b, y, c, d, z = symbols('b, y, c, d, z', integer = True)

    assert Product(x*y, (x, a, b), (y, c, d)).reorder((0, 1)) == \
        Product(x*y, (y, c, d), (x, a, b))
    assert Product(x, (x, a, b), (x, c, d)).reorder((0, 1)) == \
        Product(x, (x, c, d), (x, a, b))
    assert Product(x*y + z, (x, a, b), (z, m, n), (y, c, d)).reorder(\
        (2, 0), (0, 1)) == Product(x*y + z, (z, m, n), (y, c, d), (x, a, b))
    assert Product(x*y*z, (x, a, b), (y, c, d), (z, m, n)).reorder(\
        (0, 1), (1, 2), (0, 2)) == \
        Product(x*y*z, (x, a, b), (z, m, n), (y, c, d))
    assert Product(x*y*z, (x, a, b), (y, c, d), (z, m, n)).reorder(\
        (x, y), (y, z), (x, z)) == \
        Product(x*y*z, (x, a, b), (z, m, n), (y, c, d))
    assert Product(x*y, (x, a, b), (y, c, d)).reorder((x, 1)) == \
        Product(x*y, (y, c, d), (x, a, b))
    assert Product(x*y, (x, a, b), (y, c, d)).reorder((y, x)) == \
        Product(x*y, (y, c, d), (x, a, b))