day1/cheatsheet4.tex
changeset 315 141f3903d4e8
parent 295 39d7c4e14585
child 316 6108f2007151
equal deleted inserted replaced
314:c9f05808e1c4 315:141f3903d4e8
    22 \begin{center}
    22 \begin{center}
    23 \LARGE{Matrices and Least Square Fit}\\
    23 \LARGE{Matrices and Least Square Fit}\\
    24 \large{FOSSEE}
    24 \large{FOSSEE}
    25 \end{center}
    25 \end{center}
    26 \section{Matrices}
    26 \section{Matrices}
    27 Inputting a Matrix
    27 \subsection{Basics}
       
    28 Matrix Creation\\
       
    29 \typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\
       
    30 It creates C matrix of shape 3x3\\
       
    31 Shape is dimenions of given array.
    28 \begin{lstlisting}
    32 \begin{lstlisting}
    29 In []: C = array([[1,1,2],
    33 In []: C.shape 
    30                   [2,4,1],
    34 Out[]: (3, 3)
    31                   [-1,3,7]])
    35 In []: shape([[1,2],[4,5],[3,0]])
    32 In []: B = ones_like(C)
    36 Out[]: (3, 2)
    33 In []: A = ones((3,2))
       
    34 In []: I = identity(3)
       
    35 \end{lstlisting}
    37 \end{lstlisting}
    36 Accessing Elements
    38 \typ{In []: B = ones_like(C)} \\
       
    39 B would be array of ones with the same shape and type as C.\\
       
    40 \typ{In []: A = ones((3,2))} \\
       
    41 A would be new array of given shape(arguments), filled with ones.\\ 
       
    42 \typ{In []: I = identity(3)}\\
       
    43 I would be identity matrix of shape 3x3
       
    44 
       
    45 \subsection{Accessing Elements}
    37 \begin{lstlisting}
    46 \begin{lstlisting}
       
    47 In []: C
       
    48 Out[]: 
       
    49 array([[ 1,  1,  2],
       
    50        [ 2,  4,  1],
       
    51        [-1,  3,  7]])
    38 In []: C[1,2]
    52 In []: C[1,2]
    39 Out[]: 1
    53 Out[]: 1
    40 
    54 \end{lstlisting}
       
    55 Two indexes seperated by ',' specifies row, column. So \kwrd{C[1,2]} gets third element of second row(indices starts from 0).
       
    56 \newpage
       
    57 \begin{lstlisting}
    41 In []: C[1]
    58 In []: C[1]
    42 Out[]: array([2, 4, 1])
    59 Out[]: array([2, 4, 1])
    43 \end{lstlisting}
    60 \end{lstlisting}
    44 
    61 Single index implies complete row.
    45 Changing elements
    62 \subsection{Changing elements}
    46 \begin{lstlisting}
    63 \begin{lstlisting}
    47 In []: C[1,1] = -2
    64 In []: C[1,1] = -2
    48 In []: C
    65 In []: C
    49 Out[]: 
    66 Out[]: 
    50 array([[ 1,  1,  2],
    67 array([[ 1,  1,  2],
    57 array([[ 1,  1,  2],
    74 array([[ 1,  1,  2],
    58        [ 0,  0,  0],
    75        [ 0,  0,  0],
    59        [-1,  3,  7]])
    76        [-1,  3,  7]])
    60 \end{lstlisting}
    77 \end{lstlisting}
    61 
    78 
    62 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.
    63 \begin{lstlisting}
    81 \begin{lstlisting}
    64 In []: C[:,1]
    82 In []: C[:,1]
    65 Out[]: array([1, 0, 3])
    83 Out[]: array([1, 0, 3])
    66 
    84 \end{lstlisting}
       
    85 First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column).
       
    86 \begin{lstlisting}
    67 In []: C[1,:]
    87 In []: C[1,:]
    68 Out[]: array([0, 0, 0])
    88 Out[]: array([0, 0, 0])
    69 
    89 \end{lstlisting}
       
    90 Here we get second row(1), all columns(':') of C matrix.
       
    91 \newpage
       
    92 \begin{lstlisting}
    70 In []: C[0:2,:]
    93 In []: C[0:2,:]
    71 Out[]: 
    94 Out[]: 
    72 array([[1, 1, 2],
    95 array([[1, 1, 2],
    73        [0, 0, 0]])
    96        [0, 0, 0]])
    74 
    97 \end{lstlisting}
       
    98 Result is sub-matrix with first and second row(endpoint is excluded), and all columns from C.
       
    99 \begin{lstlisting}
    75 In []: C[1:3,:]
   100 In []: C[1:3,:]
    76 Out[]: 
   101 Out[]: 
    77 array([[ 0,  0,  0],
   102 array([[ 0,  0,  0],
    78        [-1,  3,  7]])
   103        [-1,  3,  7]])
    79 
   104 
    80 In []: C[:2,:]
   105 In []: C[:2,:]
    81 Out[]: 
   106 Out[]: 
    82 array([[1, 1, 2],
   107 array([[1, 1, 2],
    83        [0, 0, 0]])
   108        [0, 0, 0]])
    84 
   109 \end{lstlisting}
       
   110 \typ{':2'} => start from first row, till and excluding third row.
       
   111 \begin{lstlisting}
    85 In []: C[1:,:]
   112 In []: C[1:,:]
    86 Out[]: 
   113 Out[]: 
    87 array([[ 0,  0,  0],
   114 array([[ 0,  0,  0],
    88        [-1,  3,  7]])
   115        [-1,  3,  7]])
    89 
   116 
    90 In []: C[1:,:2]
   117 In []: C[1:,:2]
    91 Out[]: 
   118 Out[]: 
    92 array([[ 0,  0],
   119 array([[ 0,  0],
    93        [-1,  3]])
   120        [-1,  3]])
    94 \end{lstlisting}
   121 \end{lstlisting}
    95 
   122 \typ{'1:'} => Start from second row, till last row\\
    96 Striding
   123 \typ{':2'} => Start from first column, till and excluding third column.
       
   124 \subsection{Striding}
       
   125 Often apart from submatrix, one needs to get some mechanism to jump a step. For example, how can we have all alternate rows of a Matrix. \\
       
   126 Following method will return Matrix with alternate rows.
    97 \begin{lstlisting}
   127 \begin{lstlisting}
    98 In []: C[::2,:]
   128 In []: C[::2,:]
    99 Out[]: 
   129 Out[]: 
   100 array([[ 1,  1,  2],
   130 array([[ 1,  1,  2],
   101        [-1,  3,  7]])
   131        [-1,  3,  7]])
   102 
   132 \end{lstlisting}
       
   133 \typ{C[startR:stopR:stepR,startC:stopC:stepC]} => Syntax of mentioning starting index, ending index, and step to jump.\\
       
   134 In above mentioned case, \typ{'::2'} means, start from first row, till last row(both are blank), with step of 2, that is, skipping alternate row. After first row, C[startR], next row would be C[startR+stepR] and so on.
       
   135 \begin{lstlisting}
   103 In []: C[:,::2]
   136 In []: C[:,::2]
   104 Out[]: 
   137 Out[]: 
   105 xarray([[ 1,  2],
   138 xarray([[ 1,  2],
   106        [ 0,  0],
   139        [ 0,  0],
   107        [-1,  7]])
   140        [-1,  7]])
   108 
   141 \end{lstlisting}
       
   142 Same as above, just that here we get matrix with each alternate column and all rows.
       
   143 \begin{lstlisting}
   109 In []: C[::2,::2]
   144 In []: C[::2,::2]
   110 Out[]: 
   145 Out[]: 
   111 array([[ 1,  2],
   146 array([[ 1,  2],
   112        [-1,  7]])
   147        [-1,  7]])
   113 \end{lstlisting}
   148 \end{lstlisting}
   114 
   149 
   115 Matrix Operations
   150 \Section{Matrix Operations}
       
   151 For a Matrix A and B of equal shapes.
   116 \begin{lstlisting}
   152 \begin{lstlisting}
   117 In []: A.T # Transpose
   153 In []: A.T # Transpose
   118 In []: sum(A) # Sum of all elements
   154 In []: sum(A) # Sum of all elements
   119 In []: A+B # Addition
   155 In []: A+B # Addition
   120 In []: A*B # Product
   156 In []: A*B # Element wise product
       
   157 In []: dot(A,b) #Matrix multiplication
   121 In []: inv(A) # Inverse
   158 In []: inv(A) # Inverse
   122 In []: det(A) # Determinant
   159 In []: det(A) # Determinant
   123 \end{lstlisting}
   160 \end{lstlisting}
   124 
   161 
   125 Eigen Values and Eigen Vectors
   162 Eigen Values and Eigen Vectors