day1/cheatsheet4.tex
changeset 316 6108f2007151
parent 315 141f3903d4e8
child 328 4075482a9770
equal deleted inserted replaced
315:141f3903d4e8 316:6108f2007151
    50        [ 2,  4,  1],
    50        [ 2,  4,  1],
    51        [-1,  3,  7]])
    51        [-1,  3,  7]])
    52 In []: C[1,2]
    52 In []: C[1,2]
    53 Out[]: 1
    53 Out[]: 1
    54 \end{lstlisting}
    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).
    55 Two indexes seperated by \typ{','} specifies [row, column]. So \kwrd{C[1,2]} gets third element of second row(indices starts from 0).
    56 \newpage
    56 \newpage
    57 \begin{lstlisting}
    57 \begin{lstlisting}
    58 In []: C[1]
    58 In []: C[1]
    59 Out[]: array([2, 4, 1])
    59 Out[]: array([2, 4, 1])
    60 \end{lstlisting}
    60 \end{lstlisting}
   119 array([[ 0,  0],
   119 array([[ 0,  0],
   120        [-1,  3]])
   120        [-1,  3]])
   121 \end{lstlisting}
   121 \end{lstlisting}
   122 \typ{'1:'} => Start from second row, till last row\\
   122 \typ{'1:'} => Start from second row, till last row\\
   123 \typ{':2'} => Start from first column, till and excluding third column.
   123 \typ{':2'} => Start from first column, till and excluding third column.
       
   124 \newpage
   124 \subsection{Striding}
   125 \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 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.
   127 Following method will return Matrix with alternate rows.
   127 \begin{lstlisting}
   128 \begin{lstlisting}
   128 In []: C[::2,:]
   129 In []: C[::2,:]
   144 In []: C[::2,::2]
   145 In []: C[::2,::2]
   145 Out[]: 
   146 Out[]: 
   146 array([[ 1,  2],
   147 array([[ 1,  2],
   147        [-1,  7]])
   148        [-1,  7]])
   148 \end{lstlisting}
   149 \end{lstlisting}
   149 
   150 \section{Matrix Operations}
   150 \Section{Matrix Operations}
       
   151 For a Matrix A and B of equal shapes.
   151 For a Matrix A and B of equal shapes.
   152 \begin{lstlisting}
   152 \begin{lstlisting}
   153 In []: A.T # Transpose
   153 In []: A.T # Transpose
   154 In []: sum(A) # Sum of all elements
   154 In []: sum(A) # Sum of all elements
   155 In []: A+B # Addition
   155 In []: A+B # Addition
   156 In []: A*B # Element wise product
   156 In []: A*B # Element wise product
   157 In []: dot(A,b) #Matrix multiplication
   157 In []: dot(A,b) #Matrix multiplication
   158 In []: inv(A) # Inverse
   158 In []: inv(A) # Inverse
   159 In []: det(A) # Determinant
   159 In []: det(A) # Determinant
   160 \end{lstlisting}
   160 \end{lstlisting}
   161 
       
   162 Eigen Values and Eigen Vectors
   161 Eigen Values and Eigen Vectors
   163 \begin{lstlisting}
   162 \begin{lstlisting}
   164 In []: eig(A) #Eigen Values and Vectors
   163 In []: eig(A) #Eigen Values and Vectors
   165 In []: eigvals(A) #Eigen Values 
   164 In []: eigvals(A) #Eigen Values 
   166 \end{lstlisting}
   165 \end{lstlisting}
   170 %% \end{lstlisting}
   169 %% \end{lstlisting}
   171 %% Single Value Decomposition
   170 %% Single Value Decomposition
   172 %% \begin{lstlisting}
   171 %% \begin{lstlisting}
   173 %% In []: svd(A)
   172 %% In []: svd(A)
   174 %% \end{lstlisting}
   173 %% \end{lstlisting}
   175 Least Square Fit Line
   174 \section{Least Square Fit Line}
   176 \begin{lstlisting}
   175 \begin{lstlisting}
   177 In []: A = array([L, ones_like(L)])
   176 L = []
   178 In []: A = A.T
   177 T = []
       
   178 for line in open('pendulum.txt'):
       
   179     point = line.split()
       
   180     L.append(float(point[0]))
       
   181     T.append(float(point[1]))
       
   182 Tsq = []
       
   183 for time in T:
       
   184     Tsq.append(time*time)
       
   185 plot(L, Tsq, '.')
       
   186 \end{lstlisting}
       
   187 This is exact curve we get from L Vs Tsq from data.This relation among L and Tsq is not of straight line. For getting Least Square Fit line, we have to solve the relations:\\
       
   188 $L=m*Tsq+c$ (something similar to $y=m*x+c$)\\
       
   189 For present scenario, we have L and corresponding Tsq values. For finding m and c at given points we use \typ{lstlq} function provided by pylab. It returns the least-squares solution to an equation. \\
       
   190 For finding Least Square Fit line for this particular data we have to do following steps:\\
       
   191 \typ{In []: A = array([L, ones\_like(L)])}\\
       
   192 A is 2x(Length of array L) array.
       
   193 \begin{lstlisting}
       
   194 In []: A = A.T #now A.shape = (Length of array L)x2
   179 In []: result = lstsq(A,TSq)
   195 In []: result = lstsq(A,TSq)
   180 In []: coef = result[0]
   196 In []: coef = result[0]
   181 In []: Tline = coef[0]*L + coef[1]
   197 In []: Tline = coef[0]*L + coef[1]
   182 In []: plot(L, Tline)
   198 \end{lstlisting}
   183 \end{lstlisting}
   199 \typ{coef[0]} is array with all $m$ values, and \typ{coef[1]} contains $c$.\\
       
   200 To get the final plot.\\
       
   201 \typ{In []: plot(L, Tline)}
   184 
   202 
   185 \end{document}
   203 \end{document}