M.Hiroi's Home Page
http://www.geocities.jp/m_hiroi/

Python3 Programming

お気楽 SymPy プログラミング超入門

[ Home | Light | Python3 ]

●行列

>>> sy.Matrix([1, 2, 3])
⎡1⎤
⎢ ⎥
⎢2⎥
⎢ ⎥
⎣3⎦
>>> sy.Matrix([[1, 2, 3]])
[1  2  3]
>>> sy.Matrix([[1, 2], [3, 4]])
⎡1  2⎤
⎢    ⎥
⎣3  4⎦
>>> sy.Matrix(3, 3, range(1, 10))
⎡1  2  3⎤
⎢       ⎥
⎢4  5  6⎥
⎢       ⎥
⎣7  8  9⎦
>>> sy.Matrix(3, 3, lambda i, j: i * 3 + j)
⎡0  1  2⎤
⎢       ⎥
⎢3  4  5⎥
⎢       ⎥
⎣6  7  8⎦
>>> sy.zeros(2)
⎡0  0⎤
⎢    ⎥
⎣0  0⎦
>>> sy.zeros(2, 3)
⎡0  0  0⎤
⎢       ⎥
⎣0  0  0⎦
>>> sy.ones(2)
⎡1  1⎤
⎢    ⎥
⎣1  1⎦
>>> sy.ones(2, 3)
⎡1  1  1⎤
⎢       ⎥
⎣1  1  1⎦
>>> sy.eye(2)
⎡1  0⎤
⎢    ⎥
⎣0  1⎦
>>> sy.eye(2, 3)
⎡1  0  0⎤
⎢       ⎥
⎣0  1  0⎦
>>> sy.eye(3, 2)
⎡1  0⎤
⎢    ⎥
⎢0  1⎥
⎢    ⎥
⎣0  0⎦
>>> sy.diag(sy.eye(2), sy.ones(2))
⎡1  0  0  0⎤
⎢          ⎥
⎢0  1  0  0⎥
⎢          ⎥
⎢0  0  1  1⎥
⎢          ⎥
⎣0  0  1  1⎦
>>> sy.diag(sy.Matrix([[1, 2, 3]]), sy.Matrix([4, 5, 6]))
⎡1  2  3  0⎤
⎢          ⎥
⎢0  0  0  4⎥
⎢          ⎥
⎢0  0  0  5⎥
⎢          ⎥
⎣0  0  0  6⎦
>>> x = sy.Matrix(4, 4, range(16))
>>> x
⎡0   1   2   3 ⎤
⎢              ⎥
⎢4   5   6   7 ⎥
⎢              ⎥
⎢8   9   10  11⎥
⎢              ⎥
⎣12  13  14  15⎦
>>> x[0, 0]
0
>>> x[3, 3]
15
>>> x.row(0)
[0  1  2  3]
>>> x.row(3)
[12  13  14  15]
>>> x.col(1)
⎡1 ⎤
⎢  ⎥
⎢5 ⎥
⎢  ⎥
⎢9 ⎥
⎢  ⎥
⎣13⎦
>>> x.col(2)
⎡2 ⎤
⎢  ⎥
⎢6 ⎥
⎢  ⎥
⎢10⎥
⎢  ⎥
⎣14⎦
>>> x[1, :]
[4  5  6  7]
>>> x[:, 2]
⎡2 ⎤
⎢  ⎥
⎢6 ⎥
⎢  ⎥
⎢10⎥
⎢  ⎥
⎣14⎦
>>> x[2:, 2:]
⎡10  11⎤
⎢      ⎥
⎣14  15⎦
>>> x[2:, 2:] = sy.Matrix(2, 2, [100, 100, 100, 100])
>>> x
⎡0   1    2    3 ⎤
⎢                ⎥
⎢4   5    6    7 ⎥
⎢                ⎥
⎢8   9   100  100⎥
⎢                ⎥
⎣12  13  100  100⎦
>>> y = x[:2, :2]
>>> y
⎡0  1⎤
⎢    ⎥
⎣4  5⎦
>>> y[1,1] *= 10
>>> y
⎡0  1 ⎤
⎢     ⎥
⎣4  50⎦
>>> x
⎡0   1    2    3 ⎤
⎢                ⎥
⎢4   5    6    7 ⎥
⎢                ⎥
⎢8   9   100  100⎥
⎢                ⎥
⎣12  13  100  100⎦
>>> z = x.row(0)
>>> z
[0  1  2  3]
>>> z[3] *= 10
>>> z
[0  1  2  30]
>>> x
⎡0   1    2    3 ⎤
⎢                ⎥
⎢4   5    6    7 ⎥
⎢                ⎥
⎢8   9   100  100⎥
⎢                ⎥
⎣12  13  100  100⎦
>>> x = sy.Matrix([[1, 2, 3], [4, 5, 6]])
>>> y = x * 10
>>> y
⎡10  20  30⎤
⎢          ⎥
⎣40  50  60⎦
>>> y / 5
⎡2  4   6 ⎤
⎢         ⎥
⎣8  10  12⎦
>>> x + y
⎡11  22  33⎤
⎢          ⎥
⎣44  55  66⎦
>>> y - x
⎡9   18  27⎤
⎢          ⎥
⎣36  45  54⎦
>>> x * y.T
⎡140  320⎤
⎢        ⎥
⎣320  770⎦
>>> x.multiply_elementwise(y)
⎡10   40   90 ⎤
⎢             ⎥
⎣160  250  360⎦

>>> z = sy.Matrix([[1, 1], [1, 0]])
>>> z ** 2
⎡2  1⎤
⎢    ⎥
⎣1  1⎦
>>> z ** 10
⎡89  55⎤
⎢      ⎥
⎣55  34⎦
>>> z ** 40
⎡165580141  102334155⎤
⎢                    ⎥
⎣102334155  63245986 ⎦
>>> z ** 100
⎡573147844013817084101  354224848179261915075⎤
⎢                                            ⎥
⎣354224848179261915075  218922995834555169026⎦

>>> x = sy.Matrix([1, 2, 3])
>>> y = sy.Matrix([4, 5, 6])
>>> x
⎡1⎤
⎢ ⎥
⎢2⎥
⎢ ⎥
⎣3⎦
>>> y
⎡4⎤
⎢ ⎥
⎢5⎥
⎢ ⎥
⎣6⎦
>>> x.T
[1  2  3]
>>> x.T * y
[32]
>>> x.dot(y)
32
>>> sy.Matrix([[1, 2], [3, 4]]).applyfunc(sy.sqrt)
⎡1   √2⎤
⎢       ⎥
⎣√3  2 ⎦
>>> sy.Matrix([[1, 2], [3, 4]]).applyfunc(lambda x: x**2)
⎡1  4 ⎤
⎢     ⎥
⎣9  16⎦
>>> x.row_join(sy.ones(3))
⎡1  2  3  1  1  1⎤
⎢                ⎥
⎢4  5  6  1  1  1⎥
⎢                ⎥
⎣7  8  9  1  1  1⎦
>>> x.col_join(sy.ones(3))
⎡1  2  3⎤
⎢       ⎥
⎢4  5  6⎥
⎢       ⎥
⎢7  8  9⎥
⎢       ⎥
⎢1  1  1⎥
⎢       ⎥
⎢1  1  1⎥
⎢       ⎥
⎣1  1  1⎦
>>> x.row_insert(0, sy.Matrix([[10, 11, 12]]))
⎡10  11  12⎤
⎢          ⎥
⎢1   2   3 ⎥
⎢          ⎥
⎢4   5   6 ⎥
⎢          ⎥
⎣7   8   9 ⎦
>>> x.row_insert(3, sy.Matrix([[10, 11, 12]]))
⎡1   2   3 ⎤
⎢          ⎥
⎢4   5   6 ⎥
⎢          ⎥
⎢7   8   9 ⎥
⎢          ⎥
⎣10  11  12⎦
>>> x.row_insert(0, sy.Matrix([[10, 11, 12]]))
⎡10  11  12⎤
⎢          ⎥
⎢1   2   3 ⎥
⎢          ⎥
⎢4   5   6 ⎥
⎢          ⎥
⎣7   8   9 ⎦
>>> x.row_insert(3, sy.Matrix([[10, 11, 12]]))
⎡1   2   3 ⎤
⎢          ⎥
⎢4   5   6 ⎥
⎢          ⎥
⎢7   8   9 ⎥
⎢          ⎥
⎣10  11  12⎦
>>> x.col_insert(0, sy.Matrix([10, 11, 12]))
⎡10  1  2  3⎤
⎢           ⎥
⎢11  4  5  6⎥
⎢           ⎥
⎣12  7  8  9⎦
>>> x.col_insert(3, sy.Matrix([10, 11, 12]))
⎡1  2  3  10⎤
⎢           ⎥
⎢4  5  6  11⎥
⎢           ⎥
⎣7  8  9  12⎦
>>> x.row_del(0)
>>> x
⎡4  5  6⎤
⎢       ⎥
⎣7  8  9⎦
>>> x.col_del(2)
>>> x
⎡4  5⎤
⎢    ⎥
⎣7  8⎦

●線形代数

>>> sy.Matrix([1,2,3,4,5]).norm()
√55
>>> sy.Matrix([[1,2,3,4,5]]).norm()
√55
>>> sy.var('a b c d')
(a, b, c, d)
>>> x = sy.Matrix([[a, b], [c, d]])
>>> x
⎡a  b⎤
⎢    ⎥
⎣c  d⎦
>>> x.trace()
a + d
>>> x**(-1)
⎡    d         -b    ⎤
⎢─────  ────⎥
⎢a⋅d - b⋅c  a⋅d - b⋅c⎥
⎢                    ⎥
⎢   -c          a    ⎥
⎢─────  ────⎥
⎣a⋅d - b⋅c  a⋅d - b⋅c⎦
>>> x.inv()
⎡    d         -b    ⎤
⎢─────  ────⎥
⎢a⋅d - b⋅c  a⋅d - b⋅c⎥
⎢                    ⎥
⎢   -c          a    ⎥
⎢─────  ────⎥
⎣a⋅d - b⋅c  a⋅d - b⋅c⎦
>>> x.det()
a⋅d - b⋅c
>>> sy.Matrix(3, 3, range(1, 10))
⎡1  2  3⎤
⎢       ⎥
⎢4  5  6⎥
⎢       ⎥
⎣7  8  9⎦
>>> sy.Matrix(3, 3, range(1, 10)).trace()
15
>>> sy.Matrix(3, 3, range(1, 10)).det()
0
>>> sy.Matrix(3, 3, range(1, 10)).rank()
2
>>> sy.Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 10])
⎡1  2  3 ⎤
⎢        ⎥
⎢4  5  6 ⎥
⎢        ⎥
⎣7  8  10⎦
>>> sy.Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 10]).det()
-3
>>> sy.Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 10]).rank()
3
>>> sy.Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 10]).inv()
⎡-2/3  -4/3  1 ⎤
⎢              ⎥
⎢-2/3  11/3  -2⎥
⎢              ⎥
⎣ 1     -2   1 ⎦
>>> a = sy.Matrix([[1, 1, 1], [2, 4, 6], [2, 0, 4]])
>>> a
⎡1  1  1⎤
⎢       ⎥
⎢2  4  6⎥
⎢       ⎥
⎣2  0  4⎦
>>> L, U, _ = a.LUdecomposition()
>>> L
⎡1  0   0⎤
⎢        ⎥
⎢2  1   0⎥
⎢        ⎥
⎣2  -1  1⎦
>>> U
⎡1  1  1⎤
⎢       ⎥
⎢0  2  4⎥
⎢       ⎥
⎣0  0  6⎦
>>> L * U
⎡1  1  1⎤
⎢       ⎥
⎢2  4  6⎥
⎢       ⎥
⎣2  0  4⎦

>>> Q, R = a.QRdecomposition()
>>> Q
⎡           -2⋅√2⎤
⎢1/3   0    ───⎥
⎢             3   ⎥
⎢                 ⎥
⎢      √2    √2 ⎥
⎢2/3   ──   ──⎥
⎢      2      6   ⎥
⎢                 ⎥
⎢     -√2    √2 ⎥
⎢2/3  ──    ──⎥
⎣      2      6   ⎦
>>> R
⎡3   3     7 ⎤
⎢            ⎥
⎢0  2⋅√2 √2⎥
⎢            ⎥
⎣0   0    √2⎦
>>> Q * R
⎡1  1  1⎤
⎢       ⎥
⎢2  4  6⎥
⎢       ⎥
⎣2  0  4⎦

>>> b = sy.Matrix([[0, 2, 4], [1, 1, 1], [4, 2, 6]])
>>> b
⎡0  2  4⎤
⎢       ⎥
⎢1  1  1⎥
⎢       ⎥
⎣4  2  6⎦
>>> L, U, p = b.LUdecomposition()
>>> L
⎡1  0   0⎤
⎢        ⎥
⎢0  1   0⎥
⎢        ⎥
⎣4  -1  1⎦
>>> U
⎡1  1  1⎤
⎢       ⎥
⎢0  2  4⎥
⎢       ⎥
⎣0  0  6⎦
>>> p
[[0, 1]]
>>> L * U
⎡1  1  1⎤
⎢       ⎥
⎢0  2  4⎥
⎢       ⎥
⎣4  2  6⎦

>>> Q, R = b.QRdecomposition()
>>> Q
⎡        √34    √2   ⎤
⎢  0     ──    ──  ⎥
⎢         6       6    ⎥
⎢                      ⎥
⎢ √17   2⋅√34  -2⋅√2⎥
⎢ ──   ───  ───⎥
⎢  17     51      3    ⎥
⎢                      ⎥
⎢4⋅√17  -√34   √2   ⎥
⎢───  ───  ──  ⎥
⎣  17    102      6    ⎦
>>> R
⎡     9⋅√17  25⋅√17 ⎤
⎢√17  ─── ────⎥
⎢       17      17    ⎥
⎢                     ⎥
⎢     6⋅√34  11⋅√34 ⎥
⎢ 0   ───  ────⎥
⎢       17      17    ⎥
⎢                     ⎥
⎣ 0     0      √2    ⎦
>>> Q * R
⎡0  2  4⎤
⎢       ⎥
⎢1  1  1⎥
⎢       ⎥
⎣4  2  6⎦

>>> P, D = a.diagonalize()
>>> P
⎡-3  -1  1⎤
⎢         ⎥
⎢-2  -2  4⎥
⎢         ⎥
⎣2   1   1⎦
>>> D
⎡1  0  0⎤
⎢       ⎥
⎢0  2  0⎥
⎢       ⎥
⎣0  0  6⎦
>>> P * D * P.inv()
⎡1  1  1⎤
⎢       ⎥
⎢2  4  6⎥
⎢       ⎥
⎣2  0  4⎦
>>> a = sy.Matrix([[1, 2], [2, 1]])
>>> a
⎡1  2⎤
⎢    ⎥
⎣2  1⎦
>>> a.eigenvals()
{-1: 1, 3: 1}
>>> a.eigenvects()
⎡⎛       ⎡⎡-1⎤⎤⎞  ⎛      ⎡⎡1⎤⎤⎞⎤
⎢⎜-1, 1, ⎢⎢  ⎥⎥⎟, ⎜3, 1, ⎢⎢ ⎥⎥⎟⎥
⎣⎝       ⎣⎣1 ⎦⎦⎠  ⎝      ⎣⎣1⎦⎦⎠⎦
>>> b = sy.ones(3) + sy.diag(3,3,3)
>>> b
⎡4  1  1⎤
⎢       ⎥
⎢1  4  1⎥
⎢       ⎥
⎣1  1  4⎦
>>> b.eigenvals()
{3: 2, 6: 1}
>>> b.eigenvects()
⎡⎛      ⎡⎡-1⎤  ⎡-1⎤⎤⎞  ⎛      ⎡⎡1⎤⎤⎞⎤
⎢⎜      ⎢⎢  ⎥  ⎢  ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟⎥
⎢⎜3, 2, ⎢⎢1 ⎥, ⎢0 ⎥⎥⎟, ⎜6, 1, ⎢⎢1⎥⎥⎟⎥
⎢⎜      ⎢⎢  ⎥  ⎢  ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟⎥
⎣⎝      ⎣⎣0 ⎦  ⎣1 ⎦⎦⎠  ⎝      ⎣⎣1⎦⎦⎠⎦

>>> c = sy.diag(1,2,3,4,5)
>>> c
⎡1  0  0  0  0⎤
⎢             ⎥
⎢0  2  0  0  0⎥
⎢             ⎥
⎢0  0  3  0  0⎥
⎢             ⎥
⎢0  0  0  4  0⎥
⎢             ⎥
⎣0  0  0  0  5⎦
>>> c.eigenvals()
{1: 1, 2: 1, 3: 1, 4: 1, 5: 1}
>>> c.eigenvects()
⎡⎛      ⎡⎡1⎤⎤⎞  ⎛      ⎡⎡0⎤⎤⎞  ⎛      ⎡⎡0⎤⎤⎞  ⎛      ⎡⎡0⎤⎤⎞  ⎛      ⎡⎡0⎤⎤⎞⎤
⎢⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟⎥
⎢⎜      ⎢⎢0⎥⎥⎟  ⎜      ⎢⎢1⎥⎥⎟  ⎜      ⎢⎢0⎥⎥⎟  ⎜      ⎢⎢0⎥⎥⎟  ⎜      ⎢⎢0⎥⎥⎟⎥
⎢⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟⎥
⎢⎜1, 1, ⎢⎢0⎥⎥⎟, ⎜2, 1, ⎢⎢0⎥⎥⎟, ⎜3, 1, ⎢⎢1⎥⎥⎟, ⎜4, 1, ⎢⎢0⎥⎥⎟, ⎜5, 1, ⎢⎢0⎥⎥⎟⎥
⎢⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟⎥
⎢⎜      ⎢⎢0⎥⎥⎟  ⎜      ⎢⎢0⎥⎥⎟  ⎜      ⎢⎢0⎥⎥⎟  ⎜      ⎢⎢1⎥⎥⎟  ⎜      ⎢⎢0⎥⎥⎟⎥
⎢⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟⎥
⎣⎝      ⎣⎣0⎦⎦⎠  ⎝      ⎣⎣0⎦⎦⎠  ⎝      ⎣⎣0⎦⎦⎠  ⎝      ⎣⎣0⎦⎦⎠  ⎝      ⎣⎣1⎦⎦⎠⎦

●素数と約数

>>> sy.gcd(12345678, 123456789)
9
>>> sy.gcd(1234321, 12345654321)
121

>>> sy.lcm(5, 7)
35
>>> sy.lcm(14, 35)
70

>>> sy.divisors(24)
[1, 2, 3, 4, 6, 8, 12, 24]
>>> sy.divisors(12345678)
[1, 2, 3, 6, 9, 18, 47, 94, 141, 282, 423, 846, 14593, 29186, 43779, 87558, 131337, 262674, 685871, 1371742,
 2057613, 4115226, 6172839, 12345678]
>>> sy.divisors(123456789)
[1, 3, 9, 3607, 3803, 10821, 11409, 32463, 34227, 13717421, 41152263, 123456789]
>>> sy.divisors(1111111111)
[1, 11, 41, 271, 451, 2981, 9091, 11111, 100001, 122221, 372731, 2463661, 4100041, 27100271, 101010101, 1111111111]

>>> sy.divisor_count(24)
8
>>> sy.divisor_count(12345678)
24
>>> sy.divisor_count(123456789)
12
>>> sy.divisor_count(1111111111)
16

>>> sy.factorint(24)
{2: 3, 3: 1}
>>> sy.factorint(12345678)
{2: 1, 3: 2, 47: 1, 14593: 1}
>>> sy.factorint(123456789)
{3: 2, 3607: 1, 3803: 1}
>>> sy.factorint(1111111111)
{11: 1, 41: 1, 271: 1, 9091: 1}

>>> sy.primefactors(24)
[2, 3]
>>> sy.primefactors(12345678)
[2, 3, 47, 14593]
>>> sy.primefactors(123456789)
[3, 3607, 3803]
>>> sy.primefactors(1111111111)
[11, 41, 271, 9091]

>>> for i in range(1, 11): print(sy.prime(i))
...
2
3
5
7
11
13
17
19
23
29
>>> sy.primepi(29)
10
>>> sy.primepi(100)
25
>>> sy.nextprime(100)
101
>>> sy.nextprime(100, ith=2)
103
>>> sy.prevprime(100)
97
>>> list(sy.primerange(100, 200))
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
>>> list(sy.primerange(100, 199))
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197]

>>> sy.primorial(3)
30
>>> sy.primorial(6)
30030

>>> sy.isprime(199)
True
>>> sy.isprime(1991)
False
>>> sy.isprime(19991)
True
>>> sy.multiplicity(2, 256)
8
>>> sy.multiplicity(2, 257)
0
>>> sy.multiplicity(3, 27)
3
>>> sy.multiplicity(3, 28)
0

●数列と級数

>>> sy.var('m n x y z')
(m, n, x, y, z)
>>> a = sy.sequence(x, (x, 1, 10))
>>> a
[1, 2, 3, 4, …]
>>> list(a)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a.formula
x
>>> sy.summation(a.formula, (x, 1, 1000))
500500
>>> sy.summation(a.formula, (x, 1, n))
 2
n    n
─ + ─
2    2

>>> b = sy.sequence(2*x+1, (x, 0, 10))
>>> list(b)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
>>> sy.summation(b.formula, (x, 1, 1000))
1002000
>>> sy.summation(b.formula, (x, 0, n - 1))
 2
n

>>> a + b
[4, 7, 10, 13, …]
>>> a - b
[-2, -3, -4, -5, …]
>>> a * b
[3, 10, 21, 36, …]
>>> list(a + b)
[4, 7, 10, 13, 16, 19, 22, 25, 28, 31]
>>> list(a - b)
[-2, -3, -4, -5, -6, -7, -8, -9, -10, -11]
>>> list(a * b)
[3, 10, 21, 36, 55, 78, 105, 136, 171, 210]

>>> b[2]
5
>>> b[3]
7
>>> b.coeff(2)
5
>>> b.coeff(3)
7
>>> b.coeff(2.5)
6.00000000000000

>>> c = sy.sequence(x*x, (x, 1, 10))
>>> c
[1, 4, 9, 16, …]
>>> list(c)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> sy.summation(c.formula, (x, 1, 100))
338350
>>> sy.summation(c.formula, (x, 1, n))
 3    2
n    n    n
─ + ─ + ─
3    2    6
>>> sy.factor(sy.summation(c.formula, (x, 1, n)))
n⋅(n + 1)⋅(2⋅n + 1)
──────────
         6

>>> d = sy.sequence(x*(x+1), (x, 1, 10))
>>> d
[2, 6, 12, 20, …]
>>> list(d)
[2, 6, 12, 20, 30, 42, 56, 72, 90, 110]
>>> sy.factor(sy.summation(d.formula, (x, 1, n)))
n⋅(n + 1)⋅(n + 2)
─────────
        3

>>> e = sy.sequence(1/(x*(x+1)), (x, 1, 10))
>>> e
[1/2, 1/6, 1/12, 1/20, …]
>>> list(e)
[1/2, 1/6, 1/12, 1/20, 1/30, 1/42, 1/56, 1/72, 1/90, 1/110]
>>> sy.factor(sy.summation(e.formula, (x, 1, n)))
  n
───
n + 1

>>> f = sy.sequence(2**x, (x, 0, 10))
>>> f
[1, 2, 4, 8, …]
>>> list(f)
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
>>> sy.summation(f.formula, (x, 0, n))
 n + 1
2      - 1

>>> sy.summation(y**x, (x, 0, n))
⎧   n + 1      for y = 1
⎪
⎪   n + 1
⎨- y      + 1
⎪──────  otherwise
⎪   -y + 1
⎩

>>> sy.sequence(x + y, (x, 1, 10))
[y + 1, y + 2, y + 3, y + 4, …]
>>> sy.sequence(sy.sequence(x + y, (x, 1, 10)), (y, 11, 20))
[12, 14, 16, 18, …]
>>> list(sy.sequence(sy.sequence(x + y, (x, 1, 10)), (y, 11, 20)))
[12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
>>> list(sy.sequence(sy.sequence(x * y, (x, 1, 10)), (y, 11, 20)))
[11, 24, 39, 56, 75, 96, 119, 144, 171, 200]
>>> sy.summation(x * y ** n, (n, 0, sy.oo))
  ⎛⎧   1                 ⎞
  ⎜⎪ ─── for │y│ < 1⎟
  ⎜⎪ -y + 1              ⎟
  ⎜⎪                     ⎟
  ⎜⎪  ∞                 ⎟
  ⎜⎪ ___                 ⎟
x⋅⎜⎨ ╲                   ⎟
  ⎜⎪  ╲    n             ⎟
  ⎜⎪  ╱   y    otherwise ⎟
  ⎜⎪ ╱                   ⎟
  ⎜⎪ ‾‾‾                 ⎟
  ⎜⎪n = 0                ⎟
  ⎝⎩                     ⎠
>>> sy.summation(1 / n, (n, 1, sy.oo))
∞
>>> sy.summation((-1)**(n+1) / n, (n, 1, sy.oo))
log(2)
>>> sy.var('m n x y z')
(m, n, x, y, z)
>>> sy.init_printing()
>>> sy.factor(sy.summation((m - 2)*(x - 1) + 1, (x, 1, n)))
n⋅(m⋅n - m - 2⋅n + 4)
───────────
          2
>>> sy.factor(sy.summation((m - 2)*(x - 1) + 1, (x, 1, n))).subs(m, 3)
n⋅(n + 1)
─────
    2
>>> sy.factor(sy.summation((m - 2)*(x - 1) + 1, (x, 1, n))).subs(m, 4)
 2
n
>>> sy.factor(sy.summation((m - 2)*(x - 1) + 1, (x, 1, n))).subs(m, 5)
n⋅(3⋅n - 1)
──────
     2
>>> sy.factor(sy.summation(x*(x + 1)/2, (x, 1, n)))
n⋅(n + 1)⋅(n + 2)
─────────
        6
>>> sy.summation(x*(x + 1)/2, (x, 1, n)).subs(n, 1000000)
166667166667000000
>>> sy.factor(sy.summation(x**2, (x, 1, n)))
n⋅(n + 1)⋅(2⋅n + 1)
──────────
         6
>>> sy.summation(x**2, (x, 1, n)).subs(n, 1000000)
333333833333500000
>>> sy.factor(sy.summation(x*(3*x - 1)/2, (x, 1, n)))
 2
n ⋅(n + 1)
─────
    2
>>> sy.summation(x*(3*x - 1)/2, (x, 1, n)).subs(n, 1000000)
500000500000000000

Copyright (C) 2018 Makoto Hiroi
All rights reserved.

[ Home | Light | Python3 ]