<jupyter_start><jupyter_text>部分分数分解<jupyter_code>x,y=sy.symbols('x y')
eq=x*y/((x+1)*(y+1))
eq
#部分分数分解
eq.apart(x)
eq.apart(y)
eq.apart(x).together(x)
eq.apart(x).simplify()<jupyter_output><empty_output><jupyter_text>代入<jupyter_code>x,y,z=sy.symbols('x y z')
(x+x*y).subs(x,y)
(sy.sin(x)).subs(sy.sin,sy.cos)
#辞書型にまとめて指定することができる．
sy.log(x+y).subs({x:sy.sqrt(y),y:z,sy.log:sy.exp})
x,y,z=sy.symbols('x y z')
eq=x+y+z
eq.subs({x:0.1,y:0.3,z:0.5})
eq.subs({y:x,z:x})
x=sy.symbols('x')
eq=1+x+x**2
[eq.subs(x,xx).evalf() for xx in range(5)]
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)<jupyter_output><empty_output><jupyter_text>微分<jupyter_code>f=sy.Function('f')(x)
f
sy.diff(f,x)
f.diff(x,2)
g=sy.Function('g')(x,y)
g.diff(x)
g.diff(x,y)
g.diff(x,2,y,3)
eq=sy.cos(x**2)+sy.exp(x)+x**3
eq
eq.diff(x)
sigmoid=1/(1+sy.exp(-x))
sigmoid
sigmoid.diff(x)<jupyter_output><empty_output><jupyter_text>solve equation<jupyter_code>import sympy as sy

x=sy.symbols('x')
sy.solve(x**2-1)
a,b=sy.symbols('a b')
sy.solve(a*x+b,x)
ans=sy.solve(x**5-x-1,x)
ans
type(ans[0])
[sy.N(i) for i in ans]<jupyter_output><empty_output><jupyter_text>連立方程式<jupyter_code>x,y=sy.symbols('x y')
eq1=x+y-7
eq2=-3*x-y+5
ans=sy.solve([eq1,eq2],[x,y])
ans
ans[x],ans[y]#辞書型でリターンするので参照が左のようにできる．<jupyter_output><empty_output><jupyter_text>matrix<jupyter_code>sy.Matrix([1,5])
sy.Matrix([[1,3]])
sy.Matrix([[1,3],[5,7]])
x,y=sy.symbols('x y')
M=sy.Matrix([[1,x],[y,2]])
M
N=M.subs({x:3})# deep copy maybe...
M,N
A=sy.Matrix([[1,3],[5,7]])
v=sy.Matrix(sy.symbols('x y'))
v
a=A*v
a
B=sy.Matrix([sy.symbols('a b'),sy.symbols('c d')])
B

w=sy.Matrix(sy.symbols('x y'))
b=B*w
b
b.T#transpose
C=B.inv()
C<jupyter_output><empty_output><jupyter_text>draw fraction graph<jupyter_code>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
frac, x = convert_input('1/(x+1) + 1/(x-1) + x+1')
frac
den=sy.denom(frac)
poles=sy.solve(den,x)
poles
domain=sy.Interval(-sy.oo,sy.oo)-sy.FiniteSet.fromiter(poles)
domain
deriv=sy.cancel(sy.diff(frac,x))
deriv
extrema=sy.solve(deriv,x)
extrema
extrema_values=[frac.subs(x,x0) for x0 in extrema]
extrema_values<jupyter_output><empty_output><jupyter_text>calc asymptotes<jupyter_code>m=sy.limit(frac/x,x,sy.oo)
p=sy.limit(frac-m*x,x,sy.oo)
m,p
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
   
[find_asymptotes(e,x) for e  in (frac, x**2,x**2/(x**2+1))]
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)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21741815935762818, 'fraction_numerical': 0.021000617665225447, 'mean_word_length': 5.747916666666667, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '508939', 'license': 'MIT', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1678, 'n_words': 207, 'path': 'sympyExercise/sympy-exercise.ipynb', 'repo_name': 'terasakisatoshi/pythonCodes', 'size': '3238'}