from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
# El siguiente comando es opcional pero hará que la solución sea vea con Latex
init_printing(use_unicode=True)
Calcular las siguientes operaciones:
$$3+4+5+6$$3+4+5+6
4*x+7*x-2*x
5*x+3*y-2*x+9*y
sqrt(2)
2**(1/2)
expresion = sqrt(3)
expresion
Si queremos obtener el valor númerico hacemos uso de la función evaluar
expresion.evalf()
Por ejemplo la variable pi se almacena por defecto como pi
pi
pi.evalf(5)
Divide los polinomios calculando el cociente y el reso de la división.
((x**3+x)/(x**2-x))
simplify((x**3+x)/(x**2-x))
# Pendiente, en maple son comandos quo e int
Calcula las raices de los polinomios: $$4x^2-3x-2$$
Cálculo de integrales indefinidas
integrate(sin(x), x)
integrate(cos(x), x)
integrate(sin(x), (x, 0, 2))
(integrate(sin(x), (x, 0, 2))).evalf()
Representación de funciones
from sympy.plotting import plot
from sympy.plotting import plot3d
%matplotlib inline
# plot3d(x*y, (x, -5, 5), (y, -5, 5))
plot(x**2, (x, -5, 5))
<sympy.plotting.plot.Plot at 0xe93aba8>
f1 = (3*x**2-2*x+1)/(4*x**2-3*x-2)
f1
plot(f1, (x,-1,2))
<sympy.plotting.plot.Plot at 0xedfe7b8>
# Un ejemplo para una funcion de dos variables
plot3d(x*y, (x, -5, 5), (y, -5, 5))
<sympy.plotting.plot.Plot at 0xf6bfd68>
Definición de matrices
Matrix(2, 3, [1, 2, 3, 4, 5, 6])
a, b, c = symbols('a b c')
Matrix(2, 3, [1, 2, 3, a, b, c])
matriz_simple = Matrix(2, 3, [1, 2, 3, a, b, c])
En Python la indexación empieza desde 0:
matriz_simple[(0,2)]
vector_automatico = zeros(1,3)
vector_automatico
# En Python los bucles se cierran por indentación (sangrado)
for i in range(3):
vector_automatico[i] = x+i+1
vector_automatico
matriz_automatica = zeros(3,5)
matriz_automatica
[nfilas, ncolumnas] = (matriz_automatica).shape
nfilas
# inacabado
for i in range(ncolumnas):
for j in range(nfilas):
vector_automatico[(i,j)] = i+j
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-104-51936fc936a0> in <module>() 2 for i in range(ncolumnas+1): 3 for j in range(nfilas+1): ----> 4 vector_automatico[(i,j)] = i+j C:\Anaconda\lib\site-packages\sympy\matrices\dense.pyc in __setitem__(self, key, value) 648 [2, 2, 4, 2]]) 649 """ --> 650 rv = self._setitem(key, value) 651 if rv is not None: 652 i, j, value = rv C:\Anaconda\lib\site-packages\sympy\matrices\matrices.pyc in _setitem(self, key, value) 268 269 is_slice = isinstance(key, slice) --> 270 i, j = key = self.key2ij(key) 271 is_mat = isinstance(value, MatrixBase) 272 if type(i) is slice or type(j) is slice: C:\Anaconda\lib\site-packages\sympy\matrices\matrices.pyc in key2ij(self, key) 1130 raise TypeError('key must be a sequence of length 2') 1131 return [a2idx(i, n) if not isinstance(i, slice) else i -> 1132 for i, n in zip(key, self.shape)] 1133 elif isinstance(key, slice): 1134 return key.indices(len(self))[:2] C:\Anaconda\lib\site-packages\sympy\matrices\matrices.pyc in a2idx(j, n) 4162 j += n 4163 if not (j >= 0 and j < n): -> 4164 raise IndexError("Index out of range: a[%s]" % (j, )) 4165 return int(j) IndexError: Index out of range: a[3]
range(ncolumnas)