Fundamentos de Matrizes | Matrix Fundamentals:

  • Uma forma organizada de representar os dados numéricos.
  • O tamanho ou a dimensão da matriz (nro linhas) X (nro colunas), por exemplo $2x3$
  • O elemento que ocupa a i-ésima linha e a j-ésima coluna é denotado por $a_{ij}$

Examplo de uma Matriz $2x3$ | Example of a $2x3$ Matrix

$$A_{2x3} = \begin{pmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{pmatrix}$$

Exemplo numérico de uma Matriz $2x3$ | Numeric example of a $2x3$ Matrix

$$A_{2x3} = \begin{pmatrix} -1 & 42 & 10 \\ 12 & 0 & 9 \end{pmatrix}$$

Alguns exemplos em Python3 | Some examples in Python3


In [1]:
import numpy as np # for array, dot and so on

Matrix creation


In [53]:
B = np.arange(9).reshape(3, 3)
print(B)


[[0 1 2]
 [3 4 5]
 [6 7 8]]

In [54]:
A = np.array([
    [-1, 42, 10],
    [12, 0, 9]
])
print(A)


[[-1 42 10]
 [12  0  9]]

In [55]:
# inspecting the matrices
print(A.shape) # 2 x 3
print(B.shape) # 3 x 3


(2, 3)
(3, 3)

In [56]:
# We have 2 dimensions `X1` and `X2`
print(A.ndim)
print(B.ndim)


2
2

In [57]:
Zeros = np.zeros((2, 3))
print(Zeros)


[[ 0.  0.  0.]
 [ 0.  0.  0.]]

In [58]:
Ones = np.ones((3, 3))
print(Ones)


[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]

In [59]:
Empty = np.empty((4, 4))
print(Empty)


[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

Vector creation


In [9]:
print(np.arange(5, 30, 7))


[ 5 12 19 26]

In [10]:
print(np.arange(10, 13, .3))


[ 10.   10.3  10.6  10.9  11.2  11.5  11.8  12.1  12.4  12.7]

In [11]:
print(np.linspace(0, 2, 13))


[ 0.          0.16666667  0.33333333  0.5         0.66666667  0.83333333
  1.          1.16666667  1.33333333  1.5         1.66666667  1.83333333
  2.        ]

np.arange bahevior to large numbers


In [12]:
print(np.arange(10000))


[   0    1    2 ..., 9997 9998 9999]

In [13]:
print(np.arange(10000).reshape(100,100))


[[   0    1    2 ...,   97   98   99]
 [ 100  101  102 ...,  197  198  199]
 [ 200  201  202 ...,  297  298  299]
 ..., 
 [9700 9701 9702 ..., 9797 9798 9799]
 [9800 9801 9802 ..., 9897 9898 9899]
 [9900 9901 9902 ..., 9997 9998 9999]]

Basic Operations

$$A_{mxn} \pm B_{mxn} \mapsto C_{mxn}$$$$u_{1xn} \pm v_{1xn} \mapsto w_{1xn} \quad (u_n \pm v_n \mapsto w_n)$$

In [61]:
A = np.array([10, 20, 30, 40, 50, -1])
B = np.linspace(0, 1, A.size)

print("{} + {} -> {}".format(A, B, A + B))
print("{} - {} -> {}".format(A, B, A - B))


[10 20 30 40 50 -1] + [ 0.   0.2  0.4  0.6  0.8  1. ] -> [ 10.   20.2  30.4  40.6  50.8   0. ]
[10 20 30 40 50 -1] - [ 0.   0.2  0.4  0.6  0.8  1. ] -> [ 10.   19.8  29.6  39.4  49.2  -2. ]
$$f:M_{mxn} \to M_{mxn}$$$$a_{ij} \mapsto a_{ij}^2$$

In [62]:
print("{} ** 2 -> {}".format(A, A ** 2))


[10 20 30 40 50 -1] ** 2 -> [ 100  400  900 1600 2500    1]
$$f:M_{mxn} \to M_{mxn}$$$$a_{ij} \mapsto 2\sin(a_{ij})$$

In [64]:
print("2 * sin({}) -> {}".format(A, 2 * np.sin(A)))


2 * sin([10 20 30 40 50 -1]) -> [-1.08804222  1.8258905  -1.97606325  1.49022632 -0.52474971 -1.68294197]
$$f:M_{mxn} \to M_{mxn}$$$$ \forall \quad i, j: \quad i < m, j < n \qquad a_{ij} = \left\{ \begin{array}{ll} \text{True} & \quad se \quad a_{ij} > 30 \\ \text{False} & \quad \text{c.c} \end{array} \right. $$

In [67]:
print(A > 30)


[False False False  True  True False]

Usando um vetor de Bools como Indexador


In [68]:
print(A[A > 30])


[40 50]
$$A_{mxn} * B_{mxn} \mapsto C_{mxn}$$$$c_{ij} = a_{ij} * b_{ij}$$$$\forall \quad i, j: \quad i < m, j < n$$

In [19]:
print("{} * {} -> {}".format(A, B, A * B))


[10 20 30 40 50 -1] * [ 0.   0.2  0.4  0.6  0.8  1. ] -> [  0.   4.  12.  24.  40.  -1.]


In [20]:
print("{}.{} -> {}".format(A, B, A.dot(B)))


[10 20 30 40 50 -1].[ 0.   0.2  0.4  0.6  0.8  1. ] -> 79.0

In [21]:
print("{}.{} -> {}".format(A, B, np.dot(A, B)))


[10 20 30 40 50 -1].[ 0.   0.2  0.4  0.6  0.8  1. ] -> 79.0

In [22]:
print(np.ones(10) * 12)


[ 12.  12.  12.  12.  12.  12.  12.  12.  12.  12.]

In [23]:
M = np.linspace(-1, 1, 16).reshape(4, 4)
print(M)


[[-1.         -0.86666667 -0.73333333 -0.6       ]
 [-0.46666667 -0.33333333 -0.2        -0.06666667]
 [ 0.06666667  0.2         0.33333333  0.46666667]
 [ 0.6         0.73333333  0.86666667  1.        ]]

In [24]:
print("sum(A) -> {}".format(M.sum()))


sum(A) -> 0.0

In [25]:
print("max(A) -> {} | min(A) -> {}" .format(M.max(), M.min()))


max(A) -> 1.0 | min(A) -> -1.0

In [26]:
N = np.arange(16).reshape(4, 4)

print(N)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

In [27]:
print(N.sum(axis=0)) # sum by column


[24 28 32 36]

In [28]:
print(N.sum(axis=1)) #sum by row


[ 6 22 38 54]

In [29]:
print(N.min(axis=1))


[ 0  4  8 12]

In [30]:
print(N.cumsum(axis=0))


[[ 0  1  2  3]
 [ 4  6  8 10]
 [12 15 18 21]
 [24 28 32 36]]

In [31]:
print(N)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

In [32]:
for column in range(N.shape[1]):
    print(N[:,column])


[ 0  4  8 12]
[ 1  5  9 13]
[ 2  6 10 14]
[ 3  7 11 15]

In [33]:
print(N.T)


[[ 0  4  8 12]
 [ 1  5  9 13]
 [ 2  6 10 14]
 [ 3  7 11 15]]

In [34]:
print(N)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

In [35]:
print(N.transpose())


[[ 0  4  8 12]
 [ 1  5  9 13]
 [ 2  6 10 14]
 [ 3  7 11 15]]

In [36]:
print(N)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

In [37]:
I = np.eye(2)
print(I)


[[ 1.  0.]
 [ 0.  1.]]

In [38]:
I2 = I * 2
I2_inv = np.linalg.inv(I2)
print(I2_inv)


[[ 0.5  0. ]
 [ 0.   0.5]]

In [39]:
print(np.dot(I2, I2_inv))


[[ 1.  0.]
 [ 0.  1.]]

In [40]:
dir(np.linalg)


Out[40]:
['LinAlgError',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '_numpy_tester',
 '_umath_linalg',
 'absolute_import',
 'bench',
 'cholesky',
 'cond',
 'det',
 'division',
 'eig',
 'eigh',
 'eigvals',
 'eigvalsh',
 'info',
 'inv',
 'lapack_lite',
 'linalg',
 'lstsq',
 'matrix_power',
 'matrix_rank',
 'multi_dot',
 'norm',
 'pinv',
 'print_function',
 'qr',
 'slogdet',
 'solve',
 'svd',
 'tensorinv',
 'tensorsolve',
 'test']

In [41]:
print(np.trace(I2))


4.0

In [42]:
Prod = np.dot(I2, I2)
print(Prod)


[[ 4.  0.]
 [ 0.  4.]]

In [43]:
print(np.linalg.eig(Prod))


(array([ 4.,  4.]), array([[ 1.,  0.],
       [ 0.,  1.]]))

$$Ax = y$$


In [44]:
A = np.linspace(1, 4, 4).reshape(2, 2)
print(A)


[[ 1.  2.]
 [ 3.  4.]]

In [45]:
y = np.array([5., 7.])

In [46]:
x = np.linalg.solve(A, y)
print(x)


[-3.  4.]

In [47]:
print(np.dot(A, x.T))


[ 5.  7.]

In [48]:
x = np.arange(0, 10, 2)
y = np.arange(5)

print(np.vstack([x, y]))


[[0 2 4 6 8]
 [0 1 2 3 4]]

In [49]:
print(np.hstack([x, y]))


[0 2 4 6 8 0 1 2 3 4]

In [50]:
print(np.hsplit(x, [2]))


[array([0, 2]), array([4, 6, 8])]

In [51]:
print(np.hsplit(x, [2, 4]))


[array([0, 2]), array([4, 6]), array([8])]

In [52]:
print(np.vsplit(np.eye(3), range(1, 3)))


[array([[ 1.,  0.,  0.]]), array([[ 0.,  1.,  0.]]), array([[ 0.,  0.,  1.]])]

In [ ]:


In [ ]: