# SYMPY


```python
# las funciones que usaremos durante la práctica
from sympy import Symbol, symbols, factor, expand, simplify, solve, pprint, plot, Derivative, Integral, sin
```


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


```python
x + x + 1
```




    2*x + 1




```python
type(2*x)
```




    sympy.core.mul.Mul




```python
type(2*x+1)
```




    sympy.core.add.Add



## Atajo para describir incógnitas de una vez


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




    x




```python
y = Symbol('y')
y
```




    y




```python
z = Symbol('z')
z
```




    z




```python
# equivalente a
x, y, z = symbols('x,y,z')
x, y, z
```




    (x, y, z)



## Simplificaciones básicas


```python
x*y+x*y
```




    2*x*y




```python
x * (x+x)
```




    2*x**2




```python
x * y / y
```




    x



## Expandir y factorizar


```python
factor(x**2 - y**2)
```




    (x - y)*(x + y)




```python
factor(x**3 + 3*x**2*y + 3*x*y**2 + y**3)
```




    (x + y)**3




```python
expand((x - y)*(x + y))
```




    x**2 - y**2




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




    2*x**3 + 3*x**2 - 2*x - 3



## Evaluar


```python
# evaluar en un punto f(1, 2)
f = x*x + x*y + x*y + y*y
f.subs({x: 1, y: 2})
```




    9




```python
# cambio de variable x -> 1-y
f.subs({x: 1-y})
```




    y**2 + 2*y*(-y + 1) + (-y + 1)**2




```python
simplify(_)
```




    1




```python
# cambio de variable x -> 2-y**2
f.subs({x: 2-y**2})
```




    y**2 + 2*y*(-y**2 + 2) + (-y**2 + 2)**2




```python
simplify(_)
```




    y**2 - 2*y*(y**2 - 2) + (y**2 - 2)**2




```python
expand(_)
```




    y**4 - 2*y**3 - 3*y**2 + 4*y + 4



## Despejando


```python
a, b, c = symbols("a,b,c")
```


```python
f = x**2 + 5*x + 4      # f(x) = 0
solve(f, dict=True)     # despeja (resuelve) x
```




    [{x: -4}, {x: -1}]




```python
f = a*x**2 + b*x + c    # f(x, a, b, c) = 0
solve(f, x, dict=True)  # despeja x
```




    [{x: (-b + sqrt(-4*a*c + b**2))/(2*a)}, {x: -(b + sqrt(-4*a*c + b**2))/(2*a)}]




```python
pprint(_)
```

    ⎡⎧           _____________⎫  ⎧    ⎛       _____________⎞ ⎫⎤
    ⎢⎪          ╱           2 ⎪  ⎪    ⎜      ╱           2 ⎟ ⎪⎥
    ⎢⎨   -b + ╲╱  -4⋅a⋅c + b  ⎬  ⎨   -⎝b + ╲╱  -4⋅a⋅c + b  ⎠ ⎬⎥
    ⎢⎪x: ─────────────────────⎪, ⎪x: ────────────────────────⎪⎥
    ⎣⎩            2⋅a         ⎭  ⎩             2⋅a           ⎭⎦



```python
pprint(solve(f, a, dict=True))
```

    ⎡⎧   -(b⋅x + c) ⎫⎤
    ⎢⎪a: ───────────⎪⎥
    ⎢⎨         2    ⎬⎥
    ⎢⎪        x     ⎪⎥
    ⎣⎩              ⎭⎦


## Representando


```python
plot(2*x**2 + 3*x)
```


```python
plot(2*x**2 + 3*x, (x, -5, 5))
```

## Derivadas e integrales


```python
Derivative(2*x**2+3*x+1, x).doit()
```




    4*x + 3




```python
Derivative(2*sin(x)*x**2, x).doit()
```




    2*x**2*cos(x) + 4*x*sin(x)




```python
Integral(4*x + 3, x).doit()
```




    2*x**2 + 3*x




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




    x/2 - sin(x)*cos(x)/2



cbe000692500545d6f36576b6cca9d65eb8dc92d
ipynb
Jupyter Notebook
matematicas_ipynb/Sympy.ipynb
amd77/presentaciones
06d1df8e5236c22d5f84d8cab887b5215d1456d5
MIT
matematicas_ipynb/Sympy.ipynb
amd77/presentaciones
06d1df8e5236c22d5f84d8cab887b5215d1456d5
MIT
matematicas_ipynb/Sympy.ipynb
amd77/presentaciones
06d1df8e5236c22d5f84d8cab887b5215d1456d5
MIT
Qwen/Qwen-72B
1. YES
2. YES