paizaでsympy


概要

sympyが、魔法なので、paizaで、やってみた。

練習問題

# coding: utf-8
# Your code here!

import sympy as sym

x = sym.symbols('x')
print ("expand - 展開")
print (sym.expand((x + 1) ** 2))
print ("factor - 因数分解")
print (sym.factor(x ** 4 - 3 * x ** 2 + 1))
print (sym.factor(x ** 3 - x ** 2 - 3 * x + 3))
print ("simplify - 簡約")
print (sym.simplify((x ** 3 + x ** 2 - x - 1) / (x ** 2 + 2 * x + 1)))
print (sym.simplify(x ** 2 / (2 * x ** (4 / 3)) ** 3))
print (sym.simplify((2 * x ** (4 / 3)) ** 2 / x ** (2 / 3)))
print (sym.simplify(1 / x + (x * sym.sin(x) - 1) / x))
print ("limit - 極限")
print (sym.limit(x, x, sym.oo))
print (sym.limit(x, x, sym.O))
print ("diff - 微分")
print (sym.diff(sym.cos(x), x))
print (sym.diff(x ** 3 + x ** 2 - x - 1, x))
print (sym.diff(sym.cos(x)))
print (sym.diff(sym.exp(x)))
print (sym.diff(sym.log(x)))
print ("integrate - 積分")
print (sym.integrate(sym.cos(x), x))
print (sym.integrate(x ** 3 + x ** 2 - x - 1, x))
print (sym.integrate(sym.cos(x)))
print (sym.integrate(sym.exp(x)))
print (sym.integrate(sym.log(x)))
print ("Matrix - 行列")
print (sym.Matrix([[1, 2, 3], [-2, 0, 4]]))
print ("solve - 式を解く")
print (sym.solve(x ** 2 - 1, x))
print (sym.solve(x ** 2 - 3 * x + 2))
print (sym.solve(x ** 2 + x + 1))
print (sym.solve(x ** 3 + 2 * x ** 2 + 4 * x + 8, x))

結果

expand - 展開
x**2 + 2*x + 1
factor - 因数分解
(x**2 - x - 1)*(x**2 + x - 1)
(x - 1)*(x**2 - 3)
simplify - 簡約
x - 1
x**(-2.0)/8
4*x**2.0
sin(x)
limit - 極限
oo
<class 'sympy.series.order.Order'>
diff - 微分
-sin(x)
3*x**2 + 2*x - 1
-sin(x)
exp(x)
1/x
integrate - 積分
sin(x)
x**4/4 + x**3/3 - x**2/2 - x
sin(x)
exp(x)
x*log(x) - x
Matrix - 行列
Matrix([[1, 2, 3], [-2, 0, 4]])
solve - 式を解く
[-1, 1]
[1, 2]
[-1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]
[-2, -2*I, 2*I]

成果物

以上。