# Predavanje 5

## Sympy


```python
import sympy as sp
sp.init_printing()
```

### Računanje s brojevima


```python
x = sp.sqrt(8)
x
```


```python
sp.srepr(x)
```




    'Mul(Integer(2), Pow(Integer(2), Rational(1, 2)))'




```python
print(x)
```

    2*sqrt(2)



```python
x.func, x.args
```




    (sympy.core.mul.Mul, (2, sqrt(2)))




```python
x.args[1].args[0].func
```




    sympy.core.numbers.Integer




```python
sp.srepr(sp.sqrt(2) - sp.sqrt(3))
```




    'Add(Mul(Integer(-1), Pow(Integer(3), Rational(1, 2))), Pow(Integer(2), Rational(1, 2)))'




```python
- sp.sqrt(3) + sp.sqrt(2) == sp.sqrt(2) - sp.sqrt(3)
```




    True




```python
x.n(50)
```


```python
x**2
```

### Simboli


```python
x = sp.Symbol('x')
```


```python
x, y = sp.symbols('x, y')
```


```python
izraz = x + 2*y
```


```python
sp.srepr(izraz)
```




    "Add(Symbol('x'), Mul(Integer(2), Symbol('y')))"




```python
izraz.args[1].args
```


```python
izraz.subs({x: sp.E, y: y-3})
```


```python
sp.srepr(izraz.subs({x: sp.E, y: y-3}))
```




    "Add(Mul(Integer(2), Symbol('y')), Integer(-6), E)"




```python
izraz.n(50, subs={x:5, y:sp.pi})
```

### Analiza


```python
sp.Derivative(sp.sin(x+y) * sp.E**y, y, 2, x, y)
```


```python
der = sp.Derivative(sp.sin(x+y) * sp.E**y, x, y, 3)
der
```


```python
der = der.doit()
der
```


```python
intg = sp.Integral(der, y, y, y, x)
intg
```


```python
%time intg.doit()
```


```python
sp.Integral(sp.sin(x**2), (x, 0, sp.oo)).doit()
```


```python
lim = sp.Limit((sp.cos(x) - 1) / x**2, x, 0, '-')
lim
```


```python
lim.doit()
```


```python
sp.Limit(3**x / 2**x, x, sp.oo).doit()
```


```python
red = sp.Sum(x**-2, (x, 4, sp.oo))
red
```


```python
konačna_suma = sp.Sum(x**-2, (x, 4, 10))
sp.Eq(konačna_suma, konačna_suma.doit())
```


```python
čvor = sp.Add(1, 1, evaluate=False)
sp.Eq(čvor, čvor.doit(), evaluate=False)
```


```python
red.doit()
```


```python
sp.Integral(sp.sqrt(x**2 - 1)).doit()
```

### Rješavanje jednadžbi


```python
jednadžba = sp.Eq(x**3 + 8)
jednadžba
```


```python
sp.solve(jednadžba)
```


```python
sp.solve(sp.Eq(x**2 + x + 1, 0), x)
```


```python
sp.solve([sp.Eq(x**2 + y**2, 10), sp.Eq(x - y, 1)], [x, y], dict=True)
```


```python
y, t = sp.Function('y'), sp.Symbol('t')
```


```python
difj = sp.Eq(sp.Derivative(y(t), t, 2) - y(t), sp.E ** t)
difj
```


```python
sp.classify_ode(difj, y(t))
```




    ('nth_linear_constant_coeff_undetermined_coefficients',
     'nth_linear_constant_coeff_variation_of_parameters',
     'nth_linear_constant_coeff_variation_of_parameters_Integral')




```python
sp.dsolve(difj, y(t))
```

### Linearna algebra


```python
sp.Determinant(sp.Matrix([[1,2,3], [4,5,6], [1,3,4]])**-1).doit()
```


```python
M = sp.Matrix([[1, 2, 3], [2, 2, 2], [1, 2, 3]])
```


```python
M.eigenvals()
```


```python
M.eigenvects()
```

### Razne manipulacije s izrazima


```python
print(sp.latex(intg))
```

    \iiiint \left(- 2 \left(\sin{\left (x + y \right )} + \cos{\left (x + y \right )}\right) e^{y}\right)\, dy\, dy\, dy\, dx



```python
[sp.cos(x).rewrite(funkcija) for funkcija in (sp.sin, sp.tan, sp.csc, sp.exp, sp.acos)]
```


```python
with sp.assuming(sp.Q.real(x), sp.Q.nonzero(x)):
    print(sp.ask(sp.Q.positive(x ** 2)))
```

    True



```python
with sp.assuming(sp.Q.integer(x)):
    print(sp.ask(sp.Q.prime(4 * x)))
```

    False



```python
(x**2 + 2*x + 1).equals((x+1)**2)
```




    True




```python
x + 1 / 3
```


```python
x + sp.S(1) / 3
```


```python
izraz = 0
for ponavljanje in range(5):
    izraz = x ** izraz
izraz
```


```python
izraz = x
for ponavljanje in range(3):
    izraz = izraz.subs(x, x**x)
izraz
```


```python
izraz = sp.sin(2*x) + sp.cos(2*x)
```


```python
izraz.expand(trig=True)
```


```python
def parcijalna_zamjena(izraz, podizraz, transformacija):
    return izraz.subs(podizraz, transformacija(podizraz))
```


```python
parcijalna_zamjena(izraz, sp.sin(2*x), sp.expand_trig)
```


```python
x, y = sp.symbols('x, y')
polinom = x**4 - 4*x**3 + 4*x**2 - 2*x + 3
zamjene = {x**i : y**i for i in range(5) if not i % 2}
zamjene
```


```python
polinom.subs(zamjene)
```


```python
sp.S('x^2+y^3-1/3+pi+alpha')
```


```python
from sympy.printing.dot import dotprint
```


```python
from sympy.abc import a, b, c, x
print(dotprint(sp.solve(a * x**2 + b * x + c, x)[~0]))
```

    digraph{
    
    # Graph style
    "ordering"="out"
    "rankdir"="TD"
    
    #########
    # Nodes #
    #########
    
    "Mul(Rational(-1, 2), Pow(Symbol(a), NegativeOne()), Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())))_()" ["color"="black", "label"="Mul", "shape"="ellipse"];
    "Rational(-1, 2)_(0,)" ["color"="black", "label"="-1/2", "shape"="ellipse"];
    "Pow(Symbol(a), NegativeOne())_(1,)" ["color"="black", "label"="Pow", "shape"="ellipse"];
    "Symbol(a)_(1, 0)" ["color"="black", "label"="a", "shape"="ellipse"];
    "NegativeOne()_(1, 1)" ["color"="black", "label"="-1", "shape"="ellipse"];
    "Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half()))_(2,)" ["color"="black", "label"="Add", "shape"="ellipse"];
    "Symbol(b)_(2, 0)" ["color"="black", "label"="b", "shape"="ellipse"];
    "Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())_(2, 1)" ["color"="black", "label"="Pow", "shape"="ellipse"];
    "Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c)))_(2, 1, 0)" ["color"="black", "label"="Add", "shape"="ellipse"];
    "Pow(Symbol(b), Integer(2))_(2, 1, 0, 0)" ["color"="black", "label"="Pow", "shape"="ellipse"];
    "Symbol(b)_(2, 1, 0, 0, 0)" ["color"="black", "label"="b", "shape"="ellipse"];
    "Integer(2)_(2, 1, 0, 0, 1)" ["color"="black", "label"="2", "shape"="ellipse"];
    "Mul(Integer(-4), Symbol(a), Symbol(c))_(2, 1, 0, 1)" ["color"="black", "label"="Mul", "shape"="ellipse"];
    "Integer(-4)_(2, 1, 0, 1, 0)" ["color"="black", "label"="-4", "shape"="ellipse"];
    "Symbol(a)_(2, 1, 0, 1, 1)" ["color"="black", "label"="a", "shape"="ellipse"];
    "Symbol(c)_(2, 1, 0, 1, 2)" ["color"="black", "label"="c", "shape"="ellipse"];
    "Half()_(2, 1, 1)" ["color"="black", "label"="1/2", "shape"="ellipse"];
    
    #########
    # Edges #
    #########
    
    "Mul(Rational(-1, 2), Pow(Symbol(a), NegativeOne()), Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())))_()" -> "Rational(-1, 2)_(0,)";
    "Mul(Rational(-1, 2), Pow(Symbol(a), NegativeOne()), Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())))_()" -> "Pow(Symbol(a), NegativeOne())_(1,)";
    "Mul(Rational(-1, 2), Pow(Symbol(a), NegativeOne()), Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())))_()" -> "Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half()))_(2,)";
    "Pow(Symbol(a), NegativeOne())_(1,)" -> "Symbol(a)_(1, 0)";
    "Pow(Symbol(a), NegativeOne())_(1,)" -> "NegativeOne()_(1, 1)";
    "Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half()))_(2,)" -> "Symbol(b)_(2, 0)";
    "Add(Symbol(b), Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half()))_(2,)" -> "Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())_(2, 1)";
    "Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())_(2, 1)" -> "Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c)))_(2, 1, 0)";
    "Pow(Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c))), Half())_(2, 1)" -> "Half()_(2, 1, 1)";
    "Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c)))_(2, 1, 0)" -> "Pow(Symbol(b), Integer(2))_(2, 1, 0, 0)";
    "Add(Pow(Symbol(b), Integer(2)), Mul(Integer(-4), Symbol(a), Symbol(c)))_(2, 1, 0)" -> "Mul(Integer(-4), Symbol(a), Symbol(c))_(2, 1, 0, 1)";
    "Pow(Symbol(b), Integer(2))_(2, 1, 0, 0)" -> "Symbol(b)_(2, 1, 0, 0, 0)";
    "Pow(Symbol(b), Integer(2))_(2, 1, 0, 0)" -> "Integer(2)_(2, 1, 0, 0, 1)";
    "Mul(Integer(-4), Symbol(a), Symbol(c))_(2, 1, 0, 1)" -> "Integer(-4)_(2, 1, 0, 1, 0)";
    "Mul(Integer(-4), Symbol(a), Symbol(c))_(2, 1, 0, 1)" -> "Symbol(a)_(2, 1, 0, 1, 1)";
    "Mul(Integer(-4), Symbol(a), Symbol(c))_(2, 1, 0, 1)" -> "Symbol(c)_(2, 1, 0, 1, 2)";
    }



```python
from IPython.display import SVG
```


```python
SVG('izraz.svg')
```




    

    




```python
(sp.gamma(x + 2) / sp.gamma(x)).simplify()
```


```python
(x ** 2 + 2 * x + 1).simplify(), (x ** 2 + 2 * x + 1).factor()
```


```python
((x + 1) * (x - 2) - (x + 3) * (x - 1)).expand()
```


```python
from sympy.abc import z
(x**2*z + 4*x*y*z + 4*y**2*z).factor()
```


```python
(sp.cos(x)**2 + sp.sin(x)**2 - 2*sp.sin(x)*sp.cos(x)).factor()
```


```python
izraz = x*y + x - 3 + 2*x**2 - z*x**2 + x**3
```


```python
izraz.coeff(x, 2)
```


```python
izraz = 1 / x + (3*x/2 - 2) / (x - 4)
izraz
```


```python
izraz.together()
```


```python
izraz.expand()
```


```python
izraz.cancel()
```


```python
izraz.apart()
```


```python
izraz.together().expand().together()
```


```python
izraz.simplify()
```


```python
izraz = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)
```


```python
izraz, izraz.cancel(), izraz.factor()
```


```python
((4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)).apart()
```


```python
(sp.acos(sp.cos(7)) - (7 - 2*sp.pi)).n()
```


```python
sp.trigsimp(sp.acos(sp.cos(7)))
```


```python
sp.acos(sp.cos(7)).simplify()  # Krivo!
```


```python
sp.trigsimp(sp.sinh(x)**4 - sp.cosh(x)**4)
```


```python
sp.expand_trig(sp.cos(1 - x))
```


```python
sp.trigsimp(_)
```


```python
from sympy.abc import a, b, c, x, y, z
```


```python
sp.powsimp(x**a * x**b)
```


```python
sp.powsimp(2**a * 3**a)
```


```python
ar, br, cr, xr, yr, zr = sp.symbols('a b c x y z', real=True)
```


```python
ap, bp, cp, xp, yp, zp = sp.symbols('a b c x y z', nonnegative=True)
```


```python
sp.powsimp(xp ** ar * yp ** ar)
```


```python
sp.powsimp(x ** a * y ** a, force=True)
```


```python
sp.powsimp(x ** 2 * y ** 2)
```


```python
2*(x+y), (x*y)**2
```


```python
sp.expand_power_exp(x ** (a + b))
```


```python
sp.expand_power_base((xp * yp) ** ar)
```


```python
sp.powdenest((xp**ar)**br)
```


```python
bi = sp.Symbol('b', integer=True)
```


```python
sp.powdenest((x**a)**bi)
```


```python
from sympy.abc import x, y
xp, yp = sp.symbols('x y', positive=True)
sp.expand_log(sp.log(xp * yp))
```


```python
sp.exp(sp.log(x))
```


```python
sp.log(sp.exp(x))
```


```python
sp.expand_log(sp.log(xp ** ar))
```


```python
sp.logcombine(2 * sp.log(xp))
```


```python
sp.Eq(sp.factorial(x), sp.gamma(x + 1))
```


```python
_.simplify()
```


```python
sp.Integral(t**z * sp.E**-t, (t, 0, sp.oo)).doit().rewrite(sp.factorial)
```


```python
from sympy.abc import n, k
sp.binomial(n, k)
```


```python
sp.binomial(n, k).rewrite(sp.factorial)
```


```python
sp.combsimp(sp.binomial(n, n-2))
```


```python
sp.combsimp(sp.factorial(x) * sp.factorial(-x))
```


```python
sp.series(sp.tan(x), x, sp.pi / 2, 9)
```


```python
sp.solveset(sp.cos(x), x)
```


```python
sp.solveset(x**2 - x + 1, x)
```


```python
sp.solveset(sp.Eq(x - y, x + y + 2), x)
```


```python
sp.solveset(sp.sin(x) <= 0, x, sp.S.Reals)  # Krivo!
```


```python
sp.srepr(_)
```




    'Union(FiniteSet(Integer(0)), Interval(pi, Mul(Integer(2), pi), S.false, S.false))'




```python
M = sp.Matrix([[2, 3], [4, 5], [7, 1]])
```


```python
M.col(0)
```


```python
sp.Matrix([1, 9])
```


```python
M * _
```


```python
(M.T * M) ** n
```


```python
sp.eye(5)
```


```python
sp.zeros(3, 7)
```


```python
(sp.ones(3, 3) + sp.eye(3)) ** -1
```


```python
n = sp.Symbol('n', positive=True)
sp.simplify(((M * M.T) ** n).subs({n: 4}).expand())
```


```python
sp.Matrix([[1, 0, 1, 3], [2, 3, 4, 7], [-1, -3, -3, -4]]).rref()
```


```python

```

99bc959ccb937593fcfa713f62c7f2034a532b18
ipynb
Jupyter Notebook
Predavanje 5.ipynb
pevlaic/MS
83c271d68a0cac63d145a98d0dd0cb5413a938fa
Unlicense
2018-03-26T18:58:42.000Z
2020-04-25T15:46:08.000Z
Predavanje 5.ipynb
pevlaic/MS
83c271d68a0cac63d145a98d0dd0cb5413a938fa
Unlicense
Predavanje 5.ipynb
pevlaic/MS
83c271d68a0cac63d145a98d0dd0cb5413a938fa
Unlicense
2018-03-18T17:19:28.000Z
2020-10-28T22:13:21.000Z
Qwen/Qwen-72B
1. YES
2. YES