Operaciones Matemáticas Simbólicas con Python
Mariano Rivera
Mayo 2017
sympy es una módulo de python (en versión beta) para procesamiento simbólico
from sympy import *
init_printing( )
a = Rational( 1 , 2 )
a
1 2 \frac{1}{2} 2 1
Definición de símbolos (variables, vectores, matrices, funciones, etc)
x = Symbol( 'x' )
y = Symbol( 'y' )
expand( ( x+ y) ** 2 )
x 2 + 2 x y + y 2 x^{2} + 2 x y + y^{2} x 2 + 2 x y + y 2
Lista de símbolos
Elementos con subíndice
symbols( 'x5:10' )
( x 5 , x 6 , x 7 , x 8 , x 9 ) \left ( x_{5}, \quad x_{6}, \quad x_{7}, \quad x_{8}, \quad x_{9}\right ) ( x 5 , x 6 , x 7 , x 8 , x 9 )
Dos formas de símbolos con multiple indexación (p.ej., arreglos multidimensionales)
x1 = symbols( 'x_1:4\,1:3' )
x2 = symbols( 'x(1:4\,1:3)' )
x1
( x 1 , 1 , x 1 , 2 , x 2 , 1 , x 2 , 2 , x 3 , 1 , x 3 , 2 ) \left ( x_{1,1}, \quad x_{1,2}, \quad x_{2,1}, \quad x_{2,2}, \quad x_{3,1}, \quad x_{3,2}\right ) ( x 1 , 1 , x 1 , 2 , x 2 , 1 , x 2 , 2 , x 3 , 1 , x 3 , 2 )
x2
( x ( 1 , 1 ) , x ( 1 , 2 ) , x ( 2 , 1 ) , x ( 2 , 2 ) , x ( 3 , 1 ) , x ( 3 , 2 ) ) \left ( x(1,1), \quad x(1,2), \quad x(2,1), \quad x(2,2), \quad x(3,1), \quad x(3,2)\right ) ( x ( 1 , 1 ) , x ( 1 , 2 ) , x ( 2 , 1 ) , x ( 2 , 2 ) , x ( 3 , 1 ) , x ( 3 , 2 ) )
Matrix( x1) . reshape( 3 , 2 )
[ x 1 , 1 x 1 , 2 x 2 , 1 x 2 , 2 x 3 , 1 x 3 , 2 ] \left[\begin{matrix}x_{1,1} & x_{1,2}\\x_{2,1} & x_{2,2}\\x_{3,1} & x_{3,2}\end{matrix}\right] ⎣ ⎡ x 1 , 1 x 2 , 1 x 3 , 1 x 1 , 2 x 2 , 2 x 3 , 2 ⎦ ⎤
Matrix( x2) . reshape( 3 , 2 )
[ x ( 1 , 1 ) x ( 1 , 2 ) x ( 2 , 1 ) x ( 2 , 2 ) x ( 3 , 1 ) x ( 3 , 2 ) ] \left[\begin{matrix}x(1,1) & x(1,2)\\x(2,1) & x(2,2)\\x(3,1) & x(3,2)\end{matrix}\right] ⎣ ⎡ x ( 1 , 1 ) x ( 2 , 1 ) x ( 3 , 1 ) x ( 1 , 2 ) x ( 2 , 2 ) x ( 3 , 2 ) ⎦ ⎤
Números complejos
expand( x+ y, complex = True )
ℜ ( x ) + ℜ ( y ) + i ℑ x + i ℑ y \Re{\left(x\right)} + \Re{\left(y\right)} + i \Im{x} + i \Im{y} ℜ ( x ) + ℜ ( y ) + i ℑ x + i ℑ y
Expansión usaindo identidades trigonométricas
expand( cos( x+ y) , trig= True )
− sin ( x ) sin ( y ) + cos ( x ) cos ( y ) - \sin{\left (x \right )} \sin{\left (y \right )} + \cos{\left (x \right )} \cos{\left (y \right )} − sin ( x ) sin ( y ) + cos ( x ) cos ( y )
Simplificación de expresiones algebraicas
simplify( ( x+ x* y) / x** 2 )
1 x ( y + 1 ) \frac{1}{x} \left(y + 1\right) x 1 ( y + 1 )
Factorización de expresiones algebraicas
factor( x** 2 + 2 * x* y+ y** 2 )
( x + y ) 2 \left(x + y\right)^{2} ( x + y ) 2
Del help(factor) vemos los sigi¡uientes ejemplos
from sympy import factor, sqrt
from sympy. abc import x, y
factor( 2 * x** 5 + 2 * x** 4 * y + 4 * x** 3 + 4 * x** 2 * y + 2 * x + 2 * y)
2 ( x + y ) ( x 2 + 1 ) 2 2 \left(x + y\right) \left(x^{2} + 1\right)^{2} 2 ( x + y ) ( x 2 + 1 ) 2
factor( ( x** 2 + 4 * x + 4 ) ** 10000000 * ( x** 2 + 1 ) )
( x + 2 ) 2 0 0 0 0 0 0 0 ( x 2 + 1 ) \left(x + 2\right)^{20000000} \left(x^{2} + 1\right) ( x + 2 ) 2 0 0 0 0 0 0 0 ( x 2 + 1 )
factor( x** 2 + 1 )
x 2 + 1 x^{2} + 1 x 2 + 1
Definiendo el dominio en el cual se usará la factorización, si asumimos que estamos inteseados solo en el modulo 2 2 2 de lo resulta de avaluar x 2 + 1 x^2+1 x 2 + 1 , notamos que
( x + 1 ) 2 % 2 = ( x 2 + 2 x + 1 ) % 2 = ( x 2 + 1 ) % 2
(x+1)^2 \% 2 = (x^2+2x+1) \% 2 = (x^2+1) \%2
( x + 1 ) 2 % 2 = ( x 2 + 2 x + 1 ) % 2 = ( x 2 + 1 ) % 2
con sympy esta factorización se obtiene:
factor( x** 2 + 1 , modulus= 2 )
( x + 1 ) 2 \left(x + 1\right)^{2} ( x + 1 ) 2
Con gaussian=True se factoriza sobre las rotaciones Gaussianas
factor( x** 2 + 1 , gaussian= True )
( x − i ) ( x + i ) \left(x - i\right) \left(x + i\right) ( x − i ) ( x + i )
o equivalentemente, agregando una extension=[I] para factorizar usando raices complejas
factor( x** 2 + 1 , extension= [ I] )
( x − i ) ( x + i ) \left(x - i\right) \left(x + i\right) ( x − i ) ( x + i )
extendiendo usando 2 \sqrt{2} 2
factor( x** 2 - 2 , extension= sqrt( 2 ) )
( x − 2 ) ( x + 2 ) \left(x - \sqrt{2}\right) \left(x + \sqrt{2}\right) ( x − 2 ) ( x + 2 )
Límites
limit( sin( x) / x, x, 0 )
1 1 1
Diferenciación
z = diff( 1 / ( 1 + exp( - x) ) , x)
z
e − x ( 1 + e − x ) 2 \frac{e^{- x}}{\left(1 + e^{- x}\right)^{2}} ( 1 + e − x ) 2 e − x
Impresión de código python
print_python( z)
x = Symbol('x')
e = exp(-x)/(1 + exp(-x))**2
Expansión en series
series( cos( x) , x, 8 )
cos ( 8 ) − ( x − 8 ) sin ( 8 ) − 1 2 ( x − 8 ) 2 cos ( 8 ) + 1 6 ( x − 8 ) 3 sin ( 8 ) + 1 2 4 ( x − 8 ) 4 cos ( 8 ) − 1 1 2 0 ( x − 8 ) 5 sin ( 8 ) + O ( ( x − 8 ) 6 ; x → 8 ) \cos{\left (8 \right )} - \left(x - 8\right) \sin{\left (8 \right )} - \frac{1}{2} \left(x - 8\right)^{2} \cos{\left (8 \right )} + \frac{1}{6} \left(x - 8\right)^{3} \sin{\left (8 \right )} + \frac{1}{24} \left(x - 8\right)^{4} \cos{\left (8 \right )} - \frac{1}{120} \left(x - 8\right)^{5} \sin{\left (8 \right )} + \mathcal{O}\left(\left(x - 8\right)^{6}; x\rightarrow 8\right) cos ( 8 ) − ( x − 8 ) sin ( 8 ) − 2 1 ( x − 8 ) 2 cos ( 8 ) + 6 1 ( x − 8 ) 3 sin ( 8 ) + 2 4 1 ( x − 8 ) 4 cos ( 8 ) − 1 2 0 1 ( x − 8 ) 5 sin ( 8 ) + O ( ( x − 8 ) 6 ; x → 8 )
series( exp( x) , x, 5 )
e 5 + ( x − 5 ) e 5 + e 5 2 ( x − 5 ) 2 + e 5 6 ( x − 5 ) 3 + e 5 2 4 ( x − 5 ) 4 + e 5 1 2 0 ( x − 5 ) 5 + O ( ( x − 5 ) 6 ; x → 5 ) e^{5} + \left(x - 5\right) e^{5} + \frac{e^{5}}{2} \left(x - 5\right)^{2} + \frac{e^{5}}{6} \left(x - 5\right)^{3} + \frac{e^{5}}{24} \left(x - 5\right)^{4} + \frac{e^{5}}{120} \left(x - 5\right)^{5} + \mathcal{O}\left(\left(x - 5\right)^{6}; x\rightarrow 5\right) e 5 + ( x − 5 ) e 5 + 2 e 5 ( x − 5 ) 2 + 6 e 5 ( x − 5 ) 3 + 2 4 e 5 ( x − 5 ) 4 + 1 2 0 e 5 ( x − 5 ) 5 + O ( ( x − 5 ) 6 ; x → 5 )
Integración indefinida
integrate( 2 * x + sinh( x) , x)
x 2 + cosh ( x ) x^{2} + \cosh{\left (x \right )} x 2 + cosh ( x )
Integración definida
integrate( sin( x) ** 2 , ( x, 0 , pi/ 2 ) )
π 4 \frac{\pi}{4} 4 π
Solución de ecuaciones
solve( x** 4 - 1 , x)
[ − 1 , 1 , − i , i ] \left [ -1, \quad 1, \quad - i, \quad i\right ] [ − 1 , 1 , − i , i ]
solve( [ x + 5 * y - 2 , - 3 * x + 6 * y - 15 ] , [ x, y] )
{ x : − 3 , y : 1 } \left \{ x : -3, \quad y : 1\right \} { x : − 3 , y : 1 }
Algebra Lineal
from sympy import Matrix, latex
A = Matrix( [ [ 1 , 0 ] , [ 0 , 1 ] ] )
A
[ 1 0 0 1 ] \left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right] [ 1 0 0 1 ]
Impresión de código látex
latex( A)
'\\left[\\begin{matrix}1 & 0\\\\0 & 1\\end{matrix}\\right]'
Matrices
x = Symbol( 'x' )
y = Symbol( 'y' )
A = Matrix( [ [ 1 , x] , [ y, 1 ] ] )
A. T
[ 1 y x 1 ] \left[\begin{matrix}1 & y\\x & 1\end{matrix}\right] [ 1 x y 1 ]
A** 2
[ x y + 1 2 x 2 y x y + 1 ] \left[\begin{matrix}x y + 1 & 2 x\\2 y & x y + 1\end{matrix}\right] [ x y + 1 2 y 2 x x y + 1 ]
Cálculo de eigenvalores
A = Matrix( 2 , 2 , symbols( 'a_{{1:3}{1:3}}' ) )
A
[ a 1 1 a 1 2 a 2 1 a 2 2 ] \left[\begin{matrix}a_{{1}{1}} & a_{{1}{2}}\\a_{{2}{1}} & a_{{2}{2}}\end{matrix}\right] [ a 1 1 a 2 1 a 1 2 a 2 2 ]
l1 = Symbol( 'l_1' )
d= det( ( A- l1* Matrix( [ [ 1 , 0 ] , [ 0 , 1 ] ] ) ) )
solve( d, l1)
[ a 1 1 2 + a 2 2 2 − 1 2 a 1 1 2 − 2 a 1 1 a 2 2 + 4 a 1 2 a 2 1 + a 2 2 2 , a 1 1 2 + a 2 2 2 + 1 2 a 1 1 2 − 2 a 1 1 a 2 2 + 4 a 1 2 a 2 1 + a 2 2 2 ] \left [ \frac{a_{{1}{1}}}{2} + \frac{a_{{2}{2}}}{2} - \frac{1}{2} \sqrt{a_{{1}{1}}^{2} - 2 a_{{1}{1}} a_{{2}{2}} + 4 a_{{1}{2}} a_{{2}{1}} + a_{{2}{2}}^{2}}, \quad \frac{a_{{1}{1}}}{2} + \frac{a_{{2}{2}}}{2} + \frac{1}{2} \sqrt{a_{{1}{1}}^{2} - 2 a_{{1}{1}} a_{{2}{2}} + 4 a_{{1}{2}} a_{{2}{1}} + a_{{2}{2}}^{2}}\right ] [ 2 a 1 1 + 2 a 2 2 − 2 1 a 1 1 2 − 2 a 1 1 a 2 2 + 4 a 1 2 a 2 1 + a 2 2 2 , 2 a 1 1 + 2 a 2 2 + 2 1 a 1 1 2 − 2 a 1 1 a 2 2 + 4 a 1 2 a 2 1 + a 2 2 2 ]
Producto vectorial (dot) con Python 3.4+
En python 3.4+ se introdujo el operador @ para mejorar la legibilidad en operaciones que impliquen el calculo del producto vectorial
antes: np.lingalg.dot(x,x)
ahora x@x
import numpy as np
xn= np. arange( 1 , 3 )
print ( 'xn=' , xn)
print ( r'<xn,xn> =' , xn@xn)
xn= [1 2]
<xn,xn> = 5
Usando este operador con operaciones simbólicas (aqui es importante cuidar que las dimensiones de las “Matrices” sean válidas)
x = Symbol( 'x' )
y = Symbol( 'y' )
v = Matrix( [ x, y] )
v
[ x y ] \left[\begin{matrix}x\\y\end{matrix}\right] [ x y ]
v. T@v
[ x 2 + y 2 ] \left[\begin{matrix}x^{2} + y^{2}\end{matrix}\right] [ x 2 + y 2 ]
v@v. T
[ x 2 x y x y y 2 ] \left[\begin{matrix}x^{2} & x y\\x y & y^{2}\end{matrix}\right] [ x 2 x y x y y 2 ]
from sympy import Matrix, latex, symbols
X = Matrix( symbols( 'x(1:5\,1:5)' ) ) . reshape( 4 , 4 )
Y = X. T* X
Y. trace( )
x ( 1 , 1 ) 2 + x ( 1 , 2 ) 2 + x ( 1 , 3 ) 2 + x ( 1 , 4 ) 2 + x ( 2 , 1 ) 2 + x ( 2 , 2 ) 2 + x ( 2 , 3 ) 2 + x ( 2 , 4 ) 2 + x ( 3 , 1 ) 2 + x ( 3 , 2 ) 2 + x ( 3 , 3 ) 2 + x ( 3 , 4 ) 2 + x ( 4 , 1 ) 2 + x ( 4 , 2 ) 2 + x ( 4 , 3 ) 2 + x ( 4 , 4 ) 2 x(1,1)^{2} + x(1,2)^{2} + x(1,3)^{2} + x(1,4)^{2} + x(2,1)^{2} + x(2,2)^{2} + x(2,3)^{2} + x(2,4)^{2} + x(3,1)^{2} + x(3,2)^{2} + x(3,3)^{2} + x(3,4)^{2} + x(4,1)^{2} + x(4,2)^{2} + x(4,3)^{2} + x(4,4)^{2} x ( 1 , 1 ) 2 + x ( 1 , 2 ) 2 + x ( 1 , 3 ) 2 + x ( 1 , 4 ) 2 + x ( 2 , 1 ) 2 + x ( 2 , 2 ) 2 + x ( 2 , 3 ) 2 + x ( 2 , 4 ) 2 + x ( 3 , 1 ) 2 + x ( 3 , 2 ) 2 + x ( 3 , 3 ) 2 + x ( 3 , 4 ) 2 + x ( 4 , 1 ) 2 + x ( 4 , 2 ) 2 + x ( 4 , 3 ) 2 + x ( 4 , 4 ) 2
Cálculo de Gradiente y Hessiano de una función
from sympy import *
init_printing( )
x = Symbol( 'x' )
y = Symbol( 'y' )
z = Symbol( 'z' )
sbl = Matrix( symbols( 'x,y,z' ) )
f = x** 2 + 2 * y** 2 + 3 * z** 2 - 6 * x** 2 * z + 4 * y* x + 5 * y* z** 2 - 3 * x* y* z
f = x** 2 + 2 * y** 2 + 3 * z** 2
f
x 2 + 2 y 2 + 3 z 2 x^{2} + 2 y^{2} + 3 z^{2} x 2 + 2 y 2 + 3 z 2
Gradiente de la función f f f
∇ f ( x ) = [ ∂ f ( x ) ∂ x 1 ∂ f ( x ) ∂ x 2 ∂ f ( x ) ∂ x 3 ⋮ ∂ f ( x ) ∂ x n ]
\nabla f(x) =
\begin{bmatrix}
\frac{\partial f(x) }{ \partial x_1} \\
\frac{\partial f(x) }{ \partial x_2} \\
\frac{\partial f(x) }{ \partial x_3} \\
\vdots \\
\frac{\partial f(x) }{ \partial x_n} \\
\end{bmatrix}
∇ f ( x ) = ⎣ ⎢ ⎢ ⎢ ⎢ ⎢ ⎡ ∂ x 1 ∂ f ( x ) ∂ x 2 ∂ f ( x ) ∂ x 3 ∂ f ( x ) ⋮ ∂ x n ∂ f ( x ) ⎦ ⎥ ⎥ ⎥ ⎥ ⎥ ⎤
Matriz Jacobiana o Jacobiano de la función f f f :
J f = [ ∂ f ( x ) ∂ x 1 , ∂ f ( x ) ∂ x 2 , ∂ f ( x ) ∂ x 3 , … , ∂ f ( x ) ∂ x n ] J_f =
\begin{bmatrix}
\frac{\partial f(x) }{ \partial x_1}, & \frac{\partial f(x) }{ \partial x_2}, & \frac{\partial f(x) }{ \partial x_3}, & \ldots, & \frac{\partial f(x) }{ \partial x_n}
\end{bmatrix}
J f = [ ∂ x 1 ∂ f ( x ) , ∂ x 2 ∂ f ( x ) , ∂ x 3 ∂ f ( x ) , … , ∂ x n ∂ f ( x ) ]
= [ ∂ f 1 ( x ) ∂ x 1 , ∂ f 1 ( x ) ∂ x 2 , ∂ f 1 ( x ) ∂ x 3 , … , ∂ f 1 ( x ) ∂ x n ∂ f 2 ( x ) ∂ x 1 , ∂ f 2 ( x ) ∂ x 2 , ∂ f 2 ( x ) ∂ x 3 , … , ∂ f 2 ( x ) ∂ x n ∂ f 3 ( x ) ∂ x 1 , ∂ f 3 ( x ) ∂ x 2 , ∂ f 3 ( x ) ∂ x 3 , … , ∂ f 3 ( x ) ∂ x n ⋮ , ⋮ ⋮ ⋱ ⋮ ∂ f m ( x ) ∂ x 1 , ∂ f m ( x ) ∂ x 2 , ∂ f m ( x ) ∂ x 3 , … , ∂ f m ( x ) ∂ x n ] =
\begin{bmatrix}
\frac{\partial f_1(x) }{ \partial x_1} &, \frac{\partial f_1(x) }{ \partial x_2}, & \frac{\partial f_1(x) }{ \partial x_3}, &\ldots, &\frac{\partial f_1(x) }{ \partial x_n} \\
\frac{\partial f_2(x) }{ \partial x_1}, &\frac{\partial f_2(x) }{ \partial x_2}, & \frac{\partial f_2(x) }{ \partial x_3},& \ldots, &\frac{\partial f_2(x) }{ \partial x_n} \\
\frac{\partial f_3(x) }{ \partial x_1}, &\frac{\partial f_3(x) }{ \partial x_2}, & \frac{\partial f_3(x) }{ \partial x_3}, &\ldots, & \frac{\partial f_3(x) }{ \partial x_n} \\
\vdots, & \vdots& \vdots & \ddots & \vdots
\\
\frac{\partial f_m(x) }{ \partial x_1}, &\frac{\partial f_m(x) }{ \partial x_2}, & \frac{\partial f_m(x) }{ \partial x_3}, &\ldots, & \frac{\partial f_m(x) }{ \partial x_n}
\end{bmatrix}
= ⎣ ⎢ ⎢ ⎢ ⎢ ⎢ ⎡ ∂ x 1 ∂ f 1 ( x ) ∂ x 1 ∂ f 2 ( x ) , ∂ x 1 ∂ f 3 ( x ) , ⋮ , ∂ x 1 ∂ f m ( x ) , , ∂ x 2 ∂ f 1 ( x ) , ∂ x 2 ∂ f 2 ( x ) , ∂ x 2 ∂ f 3 ( x ) , ⋮ ∂ x 2 ∂ f m ( x ) , ∂ x 3 ∂ f 1 ( x ) , ∂ x 3 ∂ f 2 ( x ) , ∂ x 3 ∂ f 3 ( x ) , ⋮ ∂ x 3 ∂ f m ( x ) , … , … , … , ⋱ … , ∂ x n ∂ f 1 ( x ) ∂ x n ∂ f 2 ( x ) ∂ x n ∂ f 3 ( x ) ⋮ ∂ x n ∂ f m ( x ) ⎦ ⎥ ⎥ ⎥ ⎥ ⎥ ⎤
Podemos ver intuitivamente el Jacobiano como un operador que “expande” cada renglón de la función vectorial f f f en sus derivadas parciales.
Luego si f ≡ f 1 f \equiv f_1 f ≡ f 1 :
∇ f = J f ⊤ \nabla f = J_f^\top
∇ f = J f ⊤
f = Matrix( [ f] )
f
[ x 2 + 2 y 2 + 3 z 2 ] \left[\begin{matrix}x^{2} + 2 y^{2} + 3 z^{2}\end{matrix}\right] [ x 2 + 2 y 2 + 3 z 2 ]
A vectores les podemos calcular el Jacobiano (no a escalares)
G= f. jacobian( sbl) . T
G
[ 2 x 4 y 6 z ] \left[\begin{matrix}2 x\\4 y\\6 z\end{matrix}\right] ⎣ ⎡ 2 x 4 y 6 z ⎦ ⎤
Gradiente de f f f
∇ f ( x ) = [ ∂ f ( x ) ∂ x 1 ∂ f ( x ) ∂ x 2 ∂ f ( x ) ∂ x 3 ⋮ ∂ f ( x ) ∂ x n ]
\nabla f(x) =
\begin{bmatrix}
\frac{\partial f(x) }{ \partial x_1} \\
\frac{\partial f(x) }{ \partial x_2} \\
\frac{\partial f(x) }{ \partial x_3} \\
\vdots \\
\frac{\partial f(x) }{ \partial x_n} \\
\end{bmatrix}
∇ f ( x ) = ⎣ ⎢ ⎢ ⎢ ⎢ ⎢ ⎡ ∂ x 1 ∂ f ( x ) ∂ x 2 ∂ f ( x ) ∂ x 3 ∂ f ( x ) ⋮ ∂ x n ∂ f ( x ) ⎦ ⎥ ⎥ ⎥ ⎥ ⎥ ⎤
Matrix Hessiana o Hessiano de f f f :
H f = d e f [ ∂ 2 f ( x ) ∂ x 1 2 , ∂ 2 f ( x ) ∂ x 1 x 2 , ∂ 2 f ( x ) ∂ x 1 x 3 , … , ∂ 2 f ( x ) ∂ x 1 x n ∂ 2 f ( x ) ∂ x 2 x 1 , ∂ 2 f ( x ) ∂ x 2 2 , ∂ 2 f ( x ) ∂ x 2 x 3 , … , ∂ 2 f ( x ) ∂ x 2 x n ∂ 2 f ( x ) ∂ x 3 x 1 , ∂ 2 f ( x ) ∂ x 3 x 2 , ∂ 2 f ( x ) ∂ x 3 3 , … , ∂ 2 f ( x ) ∂ x 3 x n ⋮ , ⋮ ⋮ ⋱ ⋮ ∂ 2 f ( x ) ∂ x n x 1 , ∂ 2 f ( x ) ∂ x n x 2 , ∂ 2 f ( x ) ∂ x n x 3 , … , ∂ 2 f ( x ) ∂ x n 2 ]
H_f \overset{def}{=}
\begin{bmatrix}
\frac{\partial^2 f(x) }{ \partial x_1^2} &, \frac{\partial^2 f(x) }{\partial x_1 x_2}, & \frac{\partial^2 f (x) }{\partial x_1 x_3}, &\ldots, &\frac{\partial^2 f(x) }{ \partial x_1 x_n} \\
\frac{\partial^2 f(x) }{ \partial x_2 x_1} &, \frac{\partial^2 f(x) }{\partial x_2^2}, & \frac{\partial^2 f (x) }{\partial x_2 x_3}, &\ldots, &\frac{\partial^2 f(x) }{ \partial x_2 x_n} \\
\frac{\partial^2 f(x) }{ \partial x_3 x_1} &, \frac{\partial^2 f(x) }{\partial x_3 x_2},& \frac{\partial^2 f (x) }{\partial x_3^3}, &\ldots, &\frac{\partial^2 f(x) }{ \partial x_3 x_n} \\
\vdots, & \vdots& \vdots & \ddots & \vdots
\\
\frac{\partial^2 f(x) }{ \partial x_n x_1} &, \frac{\partial^2 f(x) }{\partial x_n x_2},& \frac{\partial^2 f (x) }{\partial x_n x_3}, &\ldots, &\frac{\partial^2 f(x) }{ \partial x_n^2}
\end{bmatrix}
H f = d e f ⎣ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎡ ∂ x 1 2 ∂ 2 f ( x ) ∂ x 2 x 1 ∂ 2 f ( x ) ∂ x 3 x 1 ∂ 2 f ( x ) ⋮ , ∂ x n x 1 ∂ 2 f ( x ) , ∂ x 1 x 2 ∂ 2 f ( x ) , , ∂ x 2 2 ∂ 2 f ( x ) , , ∂ x 3 x 2 ∂ 2 f ( x ) , ⋮ , ∂ x n x 2 ∂ 2 f ( x ) , ∂ x 1 x 3 ∂ 2 f ( x ) , ∂ x 2 x 3 ∂ 2 f ( x ) , ∂ x 3 3 ∂ 2 f ( x ) , ⋮ ∂ x n x 3 ∂ 2 f ( x ) , … , … , … , ⋱ … , ∂ x 1 x n ∂ 2 f ( x ) ∂ x 2 x n ∂ 2 f ( x ) ∂ x 3 x n ∂ 2 f ( x ) ⋮ ∂ x n 2 ∂ 2 f ( x ) ⎦ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎤
El Hessiano es una matriz de segundas derivadas
Es simétrico si
∂ 2 ∂ x i x j f = ∂ 2 ∂ x j x i f ,
\frac{\partial^2 }{\partial x_i x_j} f = \frac{\partial^2 }{\partial x_j x_i} f,
∂ x i x j ∂ 2 f = ∂ x j x i ∂ 2 f ,
esto es f f f es 2 veces continuamente diferenciable f ∈ C 2 f \in \mathcal{C}^2 f ∈ C 2 .
Note que el Hessiano, se puede ver como aplicar calcular el Jacobiano al Gradiente:
H f = J { ∇ f } = J { J f ⊤ }
H_f = J \{ \nabla f\} = J\{ J_f^\top\}
H f = J { ∇ f } = J { J f ⊤ }
Sobre la Notación:
J { ⋅ } J\{ \cdot \} J { ⋅ } se refiere al operador
J f J_f J f se refiere a la Matrix
H = G. jacobian( sbl)
H
[ 2 0 0 0 4 0 0 0 6 ] \left[\begin{matrix}2 & 0 & 0\\0 & 4 & 0\\0 & 0 & 6\end{matrix}\right] ⎣ ⎡ 2 0 0 0 4 0 0 0 6 ⎦ ⎤
det( H)
4 8 48 4 8
Ecuaciones diferenciales
x = Symbol( "x" )
f = Function( "f" )
eq = Eq( f( x) . diff( x) , f( x) )
eq
d d x f ( x ) = f ( x ) \frac{d}{d x} f{\left (x \right )} = f{\left (x \right )} d x d f ( x ) = f ( x )
latex( eq)
'\\frac{d}{d x} f{\\left (x \\right )} = f{\\left (x \\right )}'
dsolve( eq, f( x) )
f ( x ) = C 1 e x f{\left (x \right )} = C_{1} e^{x} f ( x ) = C 1 e x
eq = Eq( x** 2 * f( x) . diff( x) , - 3 * x* f( x) + sin( x) / x)
eq
x 2 d d x f ( x ) = − 3 x f ( x ) + 1 x sin ( x ) x^{2} \frac{d}{d x} f{\left (x \right )} = - 3 x f{\left (x \right )} + \frac{1}{x} \sin{\left (x \right )} x 2 d x d f ( x ) = − 3 x f ( x ) + x 1 sin ( x )
dsolve( eq, f( x) )
f ( x ) = 1 x 3 ( C 1 − cos ( x ) ) f{\left (x \right )} = \frac{1}{x^{3}} \left(C_{1} - \cos{\left (x \right )}\right) f ( x ) = x 3 1 ( C 1 − cos ( x ) )