```python
import numpy as np
import sympy as sy
sy.init_printing(use_unicode=False)
```


```python
x=sy.symbols('x')
type(x)
```




    sympy.core.symbol.Symbol




```python

x
```


```python
row,col=2,2
kwds=['x{}(0:{})'.format(i,col) for i in range(row) ]
symbols_row_col=np.array([sy.symbols(kwd) for kwd in kwds]).reshape(row,col)
symbols_row_col=sy.Matrix(symbols_row_col)
symbols_row_col
```


```python
x,y,z=sy.symbols('x y z')
x,y,z#x,y,zを要素とするタプルです．
```


```python
#連番記号を定義できるよ．
sy.symbols('a:3')
```


```python
sy.symbols('c2(3:6)')
```


```python
sy.symbols(':z')
```


```python
sy.symbols('x(b:d)')
```


```python
sy.symbols('(x:y)(0:2)')
```


```python
#数式表現
x=sy.symbols('x')
1/sy.sqrt(x)-sy.sqrt(1/x)
```


```python
x=sy.symbols('x',positive=True)
1/sy.sqrt(x)-sy.sqrt(1/x)
```


```python
myinteger=sy.Integer(5)
myinteger
```


```python
myinteger-4
```


```python
x=sy.Rational('1/3')
y=sy.Rational('1/2')
x+y
```


```python
x**y
```


```python
x=sy.symbols('x')
f=sy.Lambda(x,2*x)
f(4)
```


```python
x,y=sy.symbols('x,y')
g=sy.Lambda((x,y),x**2+y**2)
g
```


```python
#evaluate!
g(3,4)
```


```python
#define equation
```


```python
x=sy.symbols('x')
eq1=0.5*x+2*x**3-4/x
eq1
```


```python
eq2=x**2+2*x*(x+1)+1
eq2
```


```python
sy.simplify(eq2)
```


```python
eq2.simplify()
```


```python
eq3=x**2+2*x+1
eq3
```


```python
eq3.factor()
```


```python
x,y,z=sy.symbols('x y z')
eq4=((1+2*x+y+3*z)**2).expand()
eq4
```


```python
eq4.collect(x)
```


```python
eq4.collect([x,y])
```


```python
eq4.collect([y,x])
```

# 部分分数分解


```python
x,y=sy.symbols('x y')
eq=x*y/((x+1)*(y+1))
eq
```


```python
#部分分数分解
eq.apart(x)
```


```python
eq.apart(y)
```


```python
eq.apart(x).together(x)
```


```python
eq.apart(x).simplify()
```

# 代入



```python
x,y,z=sy.symbols('x y z')
(x+x*y).subs(x,y)
```


```python
(sy.sin(x)).subs(sy.sin,sy.cos)
```


```python
#辞書型にまとめて指定することができる．
sy.log(x+y).subs({x:sy.sqrt(y),y:z,sy.log:sy.exp})
```


```python
x,y,z=sy.symbols('x y z')
eq=x+y+z
eq.subs({x:0.1,y:0.3,z:0.5})
```


```python
eq.subs({y:x,z:x})
```


```python
x=sy.symbols('x')
eq=1+x+x**2
[eq.subs(x,xx).evalf() for xx in range(5)]
```


```python
import numpy as np
x=sy.symbols('x')
eq=1+x+x**2
eqf=sy.lambdify(x,eq,'numpy')
v=np.arange(5)
eqf(v)
```




    array([ 1,  3,  7, 13, 21])



# 微分



```python
f=sy.Function('f')(x)
f
```


```python
sy.diff(f,x)
```


```python
f.diff(x,2)
```


```python
g=sy.Function('g')(x,y)
g.diff(x)
```


```python
g.diff(x,y)
```


```python
g.diff(x,2,y,3)
```


```python
eq=sy.cos(x**2)+sy.exp(x)+x**3
eq
```


```python
eq.diff(x)
```


```python
sigmoid=1/(1+sy.exp(-x))
sigmoid
```


```python
sigmoid.diff(x)
```

# solve equation



```python
import sympy as sy

```


```python
x=sy.symbols('x')
sy.solve(x**2-1)
```


```python
a,b=sy.symbols('a b')
sy.solve(a*x+b,x)
```


```python
ans=sy.solve(x**5-x-1,x)
ans
```


```python
type(ans[0])
```




    sympy.polys.rootoftools.ComplexRootOf




```python
[sy.N(i) for i in ans]
```

#　連立方程式



```python
x,y=sy.symbols('x y')
eq1=x+y-7
eq2=-3*x-y+5
ans=sy.solve([eq1,eq2],[x,y])
ans
```


```python
ans[x],ans[y]#辞書型でリターンするので参照が左のようにできる．
```

# matrix


```python
sy.Matrix([1,5])
```


```python
sy.Matrix([[1,3]])
```


```python
sy.Matrix([[1,3],[5,7]])
```


```python
x,y=sy.symbols('x y')
M=sy.Matrix([[1,x],[y,2]])
M
```


```python
N=M.subs({x:3})# deep copy maybe...
M,N
```


```python
A=sy.Matrix([[1,3],[5,7]])
v=sy.Matrix(sy.symbols('x y'))
v
```


```python
a=A*v
a
```


```python
B=sy.Matrix([sy.symbols('a b'),sy.symbols('c d')])
B

```


```python
w=sy.Matrix(sy.symbols('x y'))
b=B*w
b
```


```python
b.T#transpose
```


```python
C=B.inv()
C
```

# draw fraction graph


```python
def convert_input(string):
    expr=sy.sympify(string)
    #find the symbol that expr uses with sym, = expr.free_symbols
    sym, = expr.free_symbols
    x=sy.Symbol(sym.name, real=True)
    expr=expr.subs(sym,x)
    frac=sy.cancel(sy.together(expr))
    return frac, x
```


```python
frac, x = convert_input('1/(x+1) + 1/(x-1) + x+1')
```


```python
frac
```


```python
den=sy.denom(frac)
poles=sy.solve(den,x)
poles
```


```python
domain=sy.Interval(-sy.oo,sy.oo)-sy.FiniteSet.fromiter(poles)
domain
```


```python
deriv=sy.cancel(sy.diff(frac,x))
deriv
```


```python
extrema=sy.solve(deriv,x)
extrema
```


```python
extrema_values=[frac.subs(x,x0) for x0 in extrema]
extrema_values
```

# calc asymptotes


```python
m=sy.limit(frac/x,x,sy.oo)
p=sy.limit(frac-m*x,x,sy.oo)
m,p
```


```python
def find_asymptotes(expr,x):
    """
    return m,p such that y=x*m+p is an asymptote to the curve y=expr.
    If there is no asymptote, return None
    """
    m=sy.limit(frac/x,x,sy.oo)
    try:
        if not m.is_bounded:
            return None
        else:
            p=sy.limit(frac-m*x,x,sy.oo)
        return m,p
    except:
        pass
   
```


```python
[find_asymptotes(e,x) for e  in (frac, x**2,x**2/(x**2+1))]
```




    [None, None, None]




```python
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline 

def plot_curve(expr,x,xmin,xmax,ymin,ymax):
    func=sy.lambdify([x],expr)
    xs=np.linspace(xmin,xmax,200)
    plt.plot(xs,func(xs))
    plt.ylim(ymin,ymax)
    plt.xlim(xmin,xmax)

plot_curve(frac,x,-5,5,-10,10)
```


```python

```

53ab7eaaf1e4d9d7002d565fe0b9e45b9cbe3e03
ipynb
Jupyter Notebook
sympyExercise/sympy-exercise.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
sympyExercise/sympy-exercise.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
sympyExercise/sympy-exercise.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
Qwen/Qwen-72B
1. YES
2. YES