1 \documentclass[12pt]{article} |
1 \documentclass[12pt]{article} |
2 \title{Matrices and Solution of Equations} |
2 \title{Matrices and Least Square Fit} |
3 \author{FOSSEE} |
3 \author{FOSSEE} |
|
4 \usepackage{listings} |
|
5 \lstset{language=Python, |
|
6 basicstyle=\ttfamily, |
|
7 commentstyle=\itshape\bfseries, |
|
8 showstringspaces=false, |
|
9 } |
|
10 \newcommand{\typ}[1]{\lstinline{#1}} |
|
11 \usepackage[english]{babel} |
|
12 \usepackage[latin1]{inputenc} |
|
13 \usepackage{times} |
|
14 \usepackage[T1]{fontenc} |
|
15 \usepackage{ae,aecompl} |
|
16 \usepackage{mathpazo,courier,euler} |
|
17 \usepackage[scaled=.95]{helvet} |
|
18 |
4 \begin{document} |
19 \begin{document} |
5 \date{} |
20 \date{} |
6 \vspace{-1in} |
21 \vspace{-1in} |
7 \begin{center} |
22 \begin{center} |
8 \LARGE{Matrices and Solution of Equations}\\ |
23 \LARGE{Matrices and Least Square Fit}\\ |
9 \large{FOSSEE} |
24 \large{FOSSEE} |
10 \end{center} |
25 \end{center} |
11 \section{Matrices} |
26 \section{Matrices} |
12 Inputting a Matrix |
27 Inputting a Matrix |
13 \begin{verbatim} |
28 \begin{lstlisting} |
14 In [1]: A = matrix([[1, 2, 3],[4, 5, 6]]) |
29 In []: C = array([[1,1,2], |
15 \end{verbatim} |
30 [2,4,1], |
|
31 [-1,3,7]]) |
|
32 In []: B = ones_like(C) |
|
33 In []: A = ones((3,2)) |
|
34 In []: I = identity(3) |
|
35 \end{lstlisting} |
|
36 Accessing Elements |
|
37 \begin{lstlisting} |
|
38 In []: C[1,2] |
|
39 Out[]: 1 |
|
40 |
|
41 In []: C[1] |
|
42 Out[]: array([2, 4, 1]) |
|
43 \end{lstlisting} |
|
44 |
|
45 Changing elements |
|
46 \begin{lstlisting} |
|
47 In []: C[1,1] = -2 |
|
48 In []: C |
|
49 Out[]: |
|
50 array([[ 1, 1, 2], |
|
51 [ 2, -2, 1], |
|
52 [-1, 3, 7]]) |
|
53 |
|
54 In []: C[1] = [0,0,0] |
|
55 In []: C |
|
56 Out[]: |
|
57 array([[ 1, 1, 2], |
|
58 [ 0, 0, 0], |
|
59 [-1, 3, 7]]) |
|
60 \end{lstlisting} |
|
61 |
|
62 Slicing |
|
63 \begin{lstlisting} |
|
64 In []: C[:,1] |
|
65 Out[]: array([1, 0, 3]) |
|
66 |
|
67 In []: C[1,:] |
|
68 Out[]: array([0, 0, 0]) |
|
69 |
|
70 In []: C[0:2,:] |
|
71 Out[]: |
|
72 array([[1, 1, 2], |
|
73 [0, 0, 0]]) |
|
74 |
|
75 In []: C[1:3,:] |
|
76 Out[]: |
|
77 array([[ 0, 0, 0], |
|
78 [-1, 3, 7]]) |
|
79 |
|
80 In []: C[:2,:] |
|
81 Out[]: |
|
82 array([[1, 1, 2], |
|
83 [0, 0, 0]]) |
|
84 |
|
85 In []: C[1:,:] |
|
86 Out[]: |
|
87 array([[ 0, 0, 0], |
|
88 [-1, 3, 7]]) |
|
89 |
|
90 In []: C[1:,:2] |
|
91 Out[]: |
|
92 array([[ 0, 0], |
|
93 [-1, 3]]) |
|
94 \end{lstlisting} |
|
95 |
|
96 Striding |
|
97 \begin{lstlisting} |
|
98 In []: C[::2,:] |
|
99 Out[]: |
|
100 array([[ 1, 1, 2], |
|
101 [-1, 3, 7]]) |
|
102 |
|
103 In []: C[:,::2] |
|
104 Out[]: |
|
105 xarray([[ 1, 2], |
|
106 [ 0, 0], |
|
107 [-1, 7]]) |
|
108 |
|
109 In []: C[::2,::2] |
|
110 Out[]: |
|
111 array([[ 1, 2], |
|
112 [-1, 7]]) |
|
113 \end{lstlisting} |
|
114 |
16 Matrix Operations |
115 Matrix Operations |
17 \begin{verbatim} |
116 \begin{lstlisting} |
18 In [1]: A.T # Transpose |
117 In []: A.T # Transpose |
19 In [2]: sum(A) # Sum of all elements |
118 In []: sum(A) # Sum of all elements |
20 In [3]: A+B # Addition |
119 In []: A+B # Addition |
21 In [1]: A*B # Product |
120 In []: A*B # Product |
22 In [1]: inv(A) # Inverse |
121 In []: inv(A) # Inverse |
23 In [1]: det(A) # Determinant |
122 In []: det(A) # Determinant |
24 \end{verbatim} |
123 \end{lstlisting} |
25 |
124 |
26 Eigen Values and Eigen Vectors |
125 Eigen Values and Eigen Vectors |
27 \begin{verbatim} |
126 \begin{lstlisting} |
28 In [1]: eig(A) #Eigen Values and Vectors |
127 In []: eig(A) #Eigen Values and Vectors |
29 In [2]: eigvals(A) #Eigen Values |
128 In []: eigvals(A) #Eigen Values |
30 \end{verbatim} |
129 \end{lstlisting} |
31 Norm |
130 %% Norm |
32 \begin{verbatim} |
131 %% \begin{lstlisting} |
33 In [1]: norm(A) |
132 %% In []: norm(A) |
34 \end{verbatim} |
133 %% \end{lstlisting} |
35 Single Value Decomposition |
134 %% Single Value Decomposition |
36 \begin{verbatim} |
135 %% \begin{lstlisting} |
37 In [1]: svd(A) |
136 %% In []: svd(A) |
38 \end{verbatim} |
137 %% \end{lstlisting} |
39 Solving a set of equations |
138 Least Square Fit Line |
40 \begin{verbatim} |
139 \begin{lstlisting} |
41 In [1]: A = matrix([...]) # Input Equation Coefficient Matrix |
140 In []: A = array([L, ones_like(L)]) |
42 In [2]: b = matrix([...]) # Equation Target Values |
141 In []: A = A.T |
43 In [3]: x = solve(A, b) |
142 In []: result = lstsq(A,TSq) |
44 In [4]: Ax = A*x |
143 In []: coef = result[0] |
45 \end{verbatim} |
144 In []: Tline = coef[0]*L + coef[1] |
|
145 In []: plot(L, Tline) |
|
146 \end{lstlisting} |
|
147 |
46 \end{document} |
148 \end{document} |
47 |
|