Revisão de Álgebra Linear


In [3]:
import numpy as np
from numpy.random import randn

Matrizes

$$ A = \begin{bmatrix} 123, & 343, & 100\\ 33, & 0, & -50 \end{bmatrix} $$

In [4]:
A = np.array([[123, 343, 100],
              [ 33,   0, -50]])
print (A )
print (A.shape )
print (A.shape[0] )
print (A.shape[1] )


[[123 343 100]
 [ 33   0 -50]]
(2, 3)
2
3

In [5]:
B = np.array([[5, 3, 2, 1, 4],
              [0, 2, 1, 3, 8]])
print (B )
print (B.shape )
print (B.shape[0] )
print (B.shape[1] )


[[5 3 2 1 4]
 [0 2 1 3 8]]
(2, 5)
2
5
$$ A = \begin{bmatrix} 123, & 343, & 100\\ 33, & 0, & -50 \end{bmatrix} = \begin{bmatrix} a_{0,0}, & a_{0,1}, & a_{0,2}\\ a_{1,0}, & a_{1,1}, & a_{1,2} \end{bmatrix} $$$$ a_{i,j} $$

é elemento da i-ésima linha e j-ésima coluna

Em NumPy, para matriz de duas dimensões, a primeira dimensão é o número de linhas shape[0] e a segunda dimensão é o número de colunas, shape[1].

O primeiro índice i de A[i,j], é o índice das linhas e o segundo índice j, é o índice das colunas.


In [6]:
print ('A=\n', A )
for i in range(A.shape[0]):
    for j in range(A.shape[1]):
        print ('A[%d,%d] = %d' % (i,j, A[i,j]) )


A=
 [[123 343 100]
 [ 33   0 -50]]
A[0,0] = 123
A[0,1] = 343
A[0,2] = 100
A[1,0] = 33
A[1,1] = 0
A[1,2] = -50

Matriz vetor

Um vetor coluna é uma matriz de duas dimensões, porém com apenas uma coluna, tem o shape (n,1), isto é, tem n linhas e 1 coluna.


In [7]:
B = np.array([[3],
              [5]])
print ('B=\n', B )
print ('B.shape:', B.shape )


B=
 [[3]
 [5]]
B.shape: (2, 1)

Adição de matrizes

$$ C = A + B $$$$ c_{i,j} = a_{i,j} + b_{i,j} $$

para todo os elementos de $A$, $B$ e $C$.

É importante que as dimensões destas três matrizes sejam iguais.


In [8]:
A = (10*randn(2,3)).astype(int)
B = randn(2,3)
C = A + B
print ('A=\n',A )
print ('B=\n',B )
print ('C=\n',C )


A=
 [[ 2 11 13]
 [-6 -7  0]]
B=
 [[ 1.36385048  1.06926793 -0.02541034]
 [ 0.26885721  0.55095132  1.27389218]]
C=
 [[  3.36385048  12.06926793  12.97458966]
 [ -5.73114279  -6.44904868   1.27389218]]

Multiplicação de matrizes

Multiplicação matriz e escalar

$$ \beta A = \begin{bmatrix} \beta a_{0,0} & \beta a_{0,1} & \ldots & a_{0,m-1}\\ \beta a_{1,0} & \beta a_{1,1} & \ldots & a_{1,m-1} \\ \vdots & \vdots & \vdots & vdots \\ \beta a_{n-1,0} & \beta a_{n1,1} & \ldots & a_{n-1,m-1} \end{bmatrix} $$

In [9]:
print ('A=\n', A )
print()
print ('4 * A=\n', 4 * A )


A=
 [[ 2 11 13]
 [-6 -7  0]]

4 * A=
 [[  8  44  52]
 [-24 -28   0]]

Multiplicação de matrizes

$$ C_{(3,4)} = A_{(3,2)} B_{(2,4)} = \begin{bmatrix} a_{0,0} & a_{0,1}\\ a_{1,0} & a_{1,1}\\ a_{2,0} & a_{2,1}\\ \end{bmatrix} \begin{bmatrix} b_{0,0} & b_{0,1} & b_{0,2} & b_{0,3} \\ b_{1,0} & b_{1,1} & b_{1,2} & b_{1,3} \\ \end{bmatrix} $$

$$ C_{(3,4)} = \begin{bmatrix} a_{0,0} b_{0,0} + a_{0,1} b_{1,0} & a_{0,0} b_{0,1} + a_{0,1} b_{1,1} & a_{0,0} b_{0,2} + a_{0,1} b_{1,2} & a_{0,0} b_{0,3} + a_{0,1} b_{1,3} \\ a_{1,0} b_{0,0} + a_{1,1} b_{1,0} & a_{1,0} b_{0,1} + a_{1,1} b_{1,1} & a_{1,0} b_{0,2} + a_{1,1} b_{1,2} & a_{1,0} b_{0,3} + a_{1,1} b_{1,3} \\ a_{2,0} b_{0,0} + a_{2,1} b_{1,0} & a_{2,0} b_{0,1} + a_{2,1} b_{1,1} & a_{2,0} b_{0,2} + a_{2,1} b_{1,2} & a_{2,0} b_{0,3} + a_{2,1} b_{1,3} \\ \end{bmatrix} $$