26 \section{Matrices} |
26 \section{Matrices} |
27 \subsection{Basics} |
27 \subsection{Basics} |
28 Matrix Creation\\ |
28 Matrix Creation\\ |
29 \typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\ |
29 \typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\ |
30 It creates C matrix of shape 3x3\\ |
30 It creates C matrix of shape 3x3\\ |
31 Shape is dimenions of given array. |
31 Shape is dimensions of given array. |
32 \begin{lstlisting} |
32 \begin{lstlisting} |
33 In []: C.shape |
33 In []: C.shape |
34 Out[]: (3, 3) |
34 Out[]: (3, 3) |
35 In []: shape([[1,2],[4,5],[3,0]]) |
35 In []: shape([[1,2],[4,5],[3,0]]) |
36 Out[]: (3, 2) |
36 Out[]: (3, 2) |
37 \end{lstlisting} |
37 \end{lstlisting} |
38 \typ{In []: B = ones_like(C)} \\ |
38 \typ{In []: B = ones_like(C)} \\ |
39 B would be array of ones with the same shape and type as C.\\ |
39 B would be array of ones with the same shape and type as C.\\ |
40 \typ{In []: A = ones((3,2))} \\ |
40 \typ{In []: A = ones((3,2))} \\ |
41 A would be new array of given shape(arguments), filled with ones.\\ |
41 A would be new matrix of given shape(arguments), filled with ones.\\ |
42 \typ{In []: I = identity(3)}\\ |
42 \typ{In []: I = identity(3)}\\ |
43 I would be identity matrix of shape 3x3 |
43 I would be identity matrix of shape 3x3 |
44 |
44 |
45 \subsection{Accessing Elements} |
45 \subsection{Accessing Elements} |
46 \begin{lstlisting} |
46 \begin{lstlisting} |
75 [ 0, 0, 0], |
75 [ 0, 0, 0], |
76 [-1, 3, 7]]) |
76 [-1, 3, 7]]) |
77 \end{lstlisting} |
77 \end{lstlisting} |
78 |
78 |
79 \subsection{Slicing} |
79 \subsection{Slicing} |
80 Accessing rows with Matricies is straightforward. But If one wants to access particular Column, or want a sub-matrix, Slicing is the way to go. |
80 Accessing rows with Matrices is straightforward. But If one wants to access particular Column, or want a sub-matrix, Slicing is the way to go. |
81 \begin{lstlisting} |
81 \begin{lstlisting} |
82 In []: C[:,1] |
82 In []: C[:,1] |
83 Out[]: array([1, 0, 3]) |
83 Out[]: array([1, 0, 3]) |
84 \end{lstlisting} |
84 \end{lstlisting} |
85 First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column). |
85 First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column). |