import numpy as np
m = np.array([[1,2,3], [-2,0,1]])
print(m)
[[ 1 2 3] [-2 0 1]]
b = np.array([[1], [2], [3]])
print(b)
[[1] [2] [3]]
a = np.transpose(b) # cambiamos filas por columnas
print(a)
[[1 2 3]]
np.transpose(m)
array([[ 1, -2], [ 2, 0], [ 3, 1]])
print(np.transpose(a))
[[1] [2] [3]]
Es necesario usar dobles corchetes.
print(np.transpose([[1, 2, 3]]))
[[1] [2] [3]]
Nuestro objetivo es calcular $x$.
$$A= \begin{pmatrix} 1 & -3 & 2\\ 5 & 6 & -1\\ 4 & -1 & 3 \end{pmatrix}$$.
$$b= \begin{pmatrix} -3 \\ 13 \\ 8 \end{pmatrix}$$Solución: $$x=A^{-1}b= \begin{pmatrix} -2 \\ 5 \\ 7 \end{pmatrix}$$
A = np.array([[1,-3,2], [5,6,-1], [4,-1,3]])
print(A)
array([[ 1, -3, 2], [ 5, 6, -1], [ 4, -1, 3]])
b = np.array([[-3], [13], [8]])
print(b)
[[-3] [13] [ 8]]
x = np.linalg.solve(A, b)
print(x)
[[-2.] [ 5.] [ 7.]]
La función allclose nos permite comprobar que los valores, aunque no sean exactamente iguales, si son muy cercanos.
np.allclose(np.dot(A,x),b)
True
np.dot(A, x)
array([[-3.], [13.], [ 8.]])
b
array([[-3], [13], [ 8]])
np.dot(A,x) - b
array([[-3.55271368e-15], [ 0.00000000e+00], [ 3.55271368e-15]])
En la práctica equivale a transponer el vector.
c = np.array([1,2,3])
print(c)
[1 2 3]
c.shape = (3, 1)
c
array([[1], [2], [3]])
c.shape = (1, 3)
c
array([[1, 2, 3]])