1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
\documentclass[12pt]{article}
\title{Matrices and Least Square Fit}
\author{FOSSEE}
\usepackage{listings}
\lstset{language=Python,
basicstyle=\ttfamily,
commentstyle=\itshape\bfseries,
showstringspaces=false,
}
\newcommand{\typ}[1]{\lstinline{#1}}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}
\begin{document}
\date{}
\vspace{-1in}
\begin{center}
\LARGE{Matrices and Least Square Fit}\\
\large{FOSSEE}
\end{center}
\section{Matrices}
\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 dimensions of given array.
\begin{lstlisting}
In []: C.shape
Out[]: (3, 3)
In []: shape([[1,2],[4,5],[3,0]])
Out[]: (3, 2)
\end{lstlisting}
\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 matrix 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 \typ{','} specifies [row, column]. So \typ{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}
Single index implies complete row.
\subsection{Changing elements}
\begin{lstlisting}
In []: C[1,1] = -2
In []: C
Out[]:
array([[ 1, 1, 2],
[ 2, -2, 1],
[-1, 3, 7]])
In []: C[1] = [0,0,0]
In []: C
Out[]:
array([[ 1, 1, 2],
[ 0, 0, 0],
[-1, 3, 7]])
\end{lstlisting}
\subsection{Slicing}
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.
\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],
[-1, 3, 7]])
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],
[-1, 3, 7]])
In []: C[1:,:2]
Out[]:
array([[ 0, 0],
[-1, 3]])
\end{lstlisting}
\typ{'1:'} => Start from second row, till last row\\
\typ{':2'} => Start from first column, till and excluding third column.
\newpage
\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}
\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 # Element wise product
In []: dot(A,b) #Matrix multiplication
In []: inv(A) # Inverse
In []: det(A) # Determinant
\end{lstlisting}
Eigen Values and Eigen Vectors
\begin{lstlisting}
In []: eig(A) #Eigen Values and Vectors
In []: eigvals(A) #Eigen Values
\end{lstlisting}
%% Norm
%% \begin{lstlisting}
%% In []: norm(A)
%% \end{lstlisting}
%% Single Value Decomposition
%% \begin{lstlisting}
%% In []: svd(A)
%% \end{lstlisting}
\section{Least Square Fit Line}
\begin{lstlisting}
L = []
T = []
for line in open('pendulum.txt'):
point = line.split()
L.append(float(point[0]))
T.append(float(point[1]))
Tsq = []
for time in T:
Tsq.append(time*time)
plot(L, Tsq, '.')
\end{lstlisting}
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:\\
$L=m*Tsq+c$ (something similar to $y=m*x+c$)\\
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. \\
For finding Least Square Fit line for this particular data we have to do following steps:\\
\typ{In []: A = array([L, ones\_like(L)])}\\
A is 2x(Length of array L) array.
\begin{lstlisting}
In []: A = A.T #now A.shape = (Length of array L)x2
In []: result = lstsq(A,TSq)
In []: coef = result[0]
In []: Tline = coef[0]*L + coef[1]
\end{lstlisting}
\typ{coef[0]} is array with all $m$ values, and \typ{coef[1]} contains $c$.\\
To get the final plot.\\
\typ{In []: plot(L, Tline)}
\end{document}
|