summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShantanu Choudhary2009-11-18 22:30:43 +0530
committerShantanu Choudhary2009-11-18 22:30:43 +0530
commit3fae3ead8125022796b0db97efd38972c7158e78 (patch)
treea137018e32dc4944fa0381de264b3ba404457e2f
parent0a34ac02288b5df1d0f35411fc8cc5f50e193f8d (diff)
downloadworkshops-3fae3ead8125022796b0db97efd38972c7158e78.tar.gz
workshops-3fae3ead8125022796b0db97efd38972c7158e78.tar.bz2
workshops-3fae3ead8125022796b0db97efd38972c7158e78.zip
Modified Matrix operation part of cheat sheet 4.
-rwxr-xr-xday1/cheatsheet4.tex81
1 files changed, 59 insertions, 22 deletions
diff --git a/day1/cheatsheet4.tex b/day1/cheatsheet4.tex
index a7a6a6e..3f67f2d 100755
--- a/day1/cheatsheet4.tex
+++ b/day1/cheatsheet4.tex
@@ -24,25 +24,42 @@
\large{FOSSEE}
\end{center}
\section{Matrices}
-Inputting a Matrix
+\subsection{Basics}
+Matrix Creation\\
+\typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\
+It creates C matrix of shape 3x3\\
+Shape is dimenions of given array.
\begin{lstlisting}
-In []: C = array([[1,1,2],
- [2,4,1],
- [-1,3,7]])
-In []: B = ones_like(C)
-In []: A = ones((3,2))
-In []: I = identity(3)
+In []: C.shape
+Out[]: (3, 3)
+In []: shape([[1,2],[4,5],[3,0]])
+Out[]: (3, 2)
\end{lstlisting}
-Accessing Elements
+\typ{In []: B = ones_like(C)} \\
+B would be array of ones with the same shape and type as C.\\
+\typ{In []: A = ones((3,2))} \\
+A would be new array of given shape(arguments), filled with ones.\\
+\typ{In []: I = identity(3)}\\
+I would be identity matrix of shape 3x3
+
+\subsection{Accessing Elements}
\begin{lstlisting}
+In []: C
+Out[]:
+array([[ 1, 1, 2],
+ [ 2, 4, 1],
+ [-1, 3, 7]])
In []: C[1,2]
Out[]: 1
-
+\end{lstlisting}
+Two indexes seperated by ',' specifies row, column. So \kwrd{C[1,2]} gets third element of second row(indices starts from 0).
+\newpage
+\begin{lstlisting}
In []: C[1]
Out[]: array([2, 4, 1])
\end{lstlisting}
-
-Changing elements
+Single index implies complete row.
+\subsection{Changing elements}
\begin{lstlisting}
In []: C[1,1] = -2
In []: C
@@ -59,19 +76,27 @@ array([[ 1, 1, 2],
[-1, 3, 7]])
\end{lstlisting}
-Slicing
+\subsection{Slicing}
+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.
\begin{lstlisting}
In []: C[:,1]
Out[]: array([1, 0, 3])
-
+\end{lstlisting}
+First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column).
+\begin{lstlisting}
In []: C[1,:]
Out[]: array([0, 0, 0])
-
+\end{lstlisting}
+Here we get second row(1), all columns(':') of C matrix.
+\newpage
+\begin{lstlisting}
In []: C[0:2,:]
Out[]:
array([[1, 1, 2],
[0, 0, 0]])
-
+\end{lstlisting}
+Result is sub-matrix with first and second row(endpoint is excluded), and all columns from C.
+\begin{lstlisting}
In []: C[1:3,:]
Out[]:
array([[ 0, 0, 0],
@@ -81,7 +106,9 @@ In []: C[:2,:]
Out[]:
array([[1, 1, 2],
[0, 0, 0]])
-
+\end{lstlisting}
+\typ{':2'} => start from first row, till and excluding third row.
+\begin{lstlisting}
In []: C[1:,:]
Out[]:
array([[ 0, 0, 0],
@@ -92,32 +119,42 @@ Out[]:
array([[ 0, 0],
[-1, 3]])
\end{lstlisting}
-
-Striding
+\typ{'1:'} => Start from second row, till last row\\
+\typ{':2'} => Start from first column, till and excluding third column.
+\subsection{Striding}
+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. \\
+Following method will return Matrix with alternate rows.
\begin{lstlisting}
In []: C[::2,:]
Out[]:
array([[ 1, 1, 2],
[-1, 3, 7]])
-
+\end{lstlisting}
+\typ{C[startR:stopR:stepR,startC:stopC:stepC]} => Syntax of mentioning starting index, ending index, and step to jump.\\
+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.
+\begin{lstlisting}
In []: C[:,::2]
Out[]:
xarray([[ 1, 2],
[ 0, 0],
[-1, 7]])
-
+\end{lstlisting}
+Same as above, just that here we get matrix with each alternate column and all rows.
+\begin{lstlisting}
In []: C[::2,::2]
Out[]:
array([[ 1, 2],
[-1, 7]])
\end{lstlisting}
-Matrix Operations
+\Section{Matrix Operations}
+For a Matrix A and B of equal shapes.
\begin{lstlisting}
In []: A.T # Transpose
In []: sum(A) # Sum of all elements
In []: A+B # Addition
-In []: A*B # Product
+In []: A*B # Element wise product
+In []: dot(A,b) #Matrix multiplication
In []: inv(A) # Inverse
In []: det(A) # Determinant
\end{lstlisting}