feat: 1522 l2 notes

This commit is contained in:
Yadunand Prem 2024-02-01 08:26:59 +08:00
parent e790a1f29e
commit 113700eac3
No known key found for this signature in database
7 changed files with 386 additions and 15 deletions

View File

@ -215,9 +215,7 @@ nth(Idx, [_|Tail], Elem) :-
Idx1 is Idx - 1,
nth(Idx1, Tail, Elem).
% Taufiq
nth(0, [V | _], V) :- !.
nth(I, [_|T], V) :- nth(I1, T, V), I is I1 + 1, I > 0.
%%%%%%%%%%%%%%
@ -258,9 +256,6 @@ pos_helper(Target, [_|Tail], Acc, Index) :-
Acc1 is Acc + 1,
pos_helper(Target, Tail, Acc1, Index).
% Taufiq
pos(X, [X | _], 0).
pos(X, [_ | T], I) :- pos(X, T, I1), I is I1 + 1.
%%%%%%%%%%%%%%
@ -294,9 +289,6 @@ all_pos_helper(Target, [_|Tail], Acc, Rest) :-
Acc1 is Acc + 1,
all_pos_helper(Target, Tail, Acc1, Rest).
% Taufiq
all_pos(X, L, []) :- not(member(X, L)).
all_pos(X, L, LI) :- bagof(I, pos(X, L, I), LI).
%%%%%%%%%%%%%%

View File

@ -538,9 +538,8 @@ def average_increase_in_cases(n_cases_increase, n_adj_entries_avg=7):
'''
sd_win = np.lib.stride_tricks.sliding_window_view(n_cases_increase, 2 * n_adj_entries_avg + 1, axis=1)
avg = np.mean(sd_win, axis=2)
avg = np.floor(np.mean(sd_win, axis=2))
res = np.pad(avg, ((0, 0), (n_adj_entries_avg, n_adj_entries_avg)), 'constant', constant_values=(np.nan, np.nan))
print(res)
return res
@ -552,7 +551,7 @@ def test_26():
assert(np.array_equal(actual, expected, equal_nan=True))
test_26()
# test_26()
# Task 2.7
def is_peak(n_cases_increase_avg, n_adj_entries_peak=7):
@ -605,9 +604,23 @@ def is_peak(n_cases_increase_avg, n_adj_entries_peak=7):
Hint: to determine `n_adj_entries_avg` from `n_cases_increase_avg`,
`np.count_nonzero` and `np.isnan` may be helpful.
'''
# get the number of paddings to add (count nans and / 2)
paddings = int(np.count_nonzero(np.isnan(n_cases_increase_avg[0])) / 2)
nanless = n_cases_increase_avg[:, paddings: -paddings]
# find peaks (get the sliding window)
sd_win = np.lib.stride_tricks.sliding_window_view(nanless, 2 * n_adj_entries_peak + 1, axis=1)
mids = sd_win[:, :, n_adj_entries_peak]
left_vals = np.max(sd_win[:, :, :n_adj_entries_peak], axis=2)
right_vals = np.max(sd_win[:, :, n_adj_entries_peak+1:], axis=2)
peaks = (mids > left_vals) & (mids >= right_vals)
# TODO: add your solution here and remove `raise NotImplementedError`
raise NotImplementedError
means = np.nanmean(nanless, axis=1)[:, np.newaxis] * 0.1
significant = mids > means
result = np.logical_and(peaks, significant)
res = np.pad(result, ((0, 0), (paddings+n_adj_entries_peak, paddings+ n_adj_entries_peak)), 'constant', constant_values=(False, False))
return res
def test_27():
@ -627,7 +640,7 @@ def test_27():
[False, False, False, False, False, False, False, False, False]])
assert np.all(actual2 == expected2)
#test_27()
test_27()
def visualise_increase(n_cases_increase, n_cases_increase_avg=None):
'''

View File

@ -166,9 +166,12 @@ To solve a linear system we perform operations:
\item add a constant multiple of an equation to another
\end{itemize}
Likewise, for a augmented matrix, the operations are on the \textbf{rows} of the augmented matrix
\begin{itemize}
\item Multiply row by nonzero constant
\item Interchange 2 rows
\item add a constant multiple of a row to another row
\end{itemize}
To note: all these operations are revertible

View File

@ -0,0 +1,352 @@
\subsection{Recap}
Given the linear equation $a_1x_1 + a_2x_2 + ... + a_nx_n = b$
\begin{enumerate}
\item $a_1 = a_2 = ... = a_n = b = 0$ zero equation
Solution: $x_1 = t_1, x_2 = t_2, ... = x_n = t_n$
\item $a_1 = a_2 = ... = a_n = 0 \neq b$ inconsistent
No Solution
\item Not all $a_1 ... a_n$ are zero.
Set $n-1$ of $x_i$ as params, solve for last variable
\end{enumerate}
\subsection{Elementary Row Operations Example}
\begin{center}
\begin{minipage}{.3\linewidth}
\systeme{x+y+3z = 0, 2x-2y+2z=4, 3x+9y=3}
\end{minipage}%
\begin{minipage}{.3\linewidth}
\begin{equation*}
\begin{amatrix}{3}
1 & 1 & 3 & 0 \\
2 & 2 & 2 & 4 \\
3 & 9 & 0 & 3
\end{amatrix}
\end{equation*}
\end{minipage}
\end{center}
\subsection{Row Equivalent Matrices}
2 Augmented Matrices are row equivalent if one can be obtained from the other by a series of elementary row operations
Given a augmented matrix $A$, how to find a row equivalent augmented matrix B of which is of a \textbf{simple} form?
\subsection{Row Echelon Form}
\begin{defn}[Row Echelon Form (Simple)]
Augmented Matrix is in row-echelon form if
\begin{itemize}
\item Zero rows are grouped together at the bottom
\item For any 2 successive nonzero rows, The first nonzero number in the lower row appears to the right of the first nonzero number on the higher row
$\begin{amatrix}{4}
0 & 0 & 1 & 2 & 3 \\
0 & 0 & 0 & 1 & 2 \\
\end{amatrix}$
\item Leading entry if a nonzero row is a \textbf{pivot point}
\item Column of augmented matrix is called
\begin{itemize}
\item \textbf{Pivot Column} if it contains a pivot point
\item \textbf{Non Pivot Column} if it contains no pivot point
\end{itemize}
\item Pivot Column contains exactly 1 pivot point
\# of pivots = \# of leading entries = \# of nonzero rows
\end{itemize}
\end{defn}
Examples of row echlon form:
\begin{equation*}
\begin{amatrix}{2}
3 & 2 & 1 \\
\end{amatrix}
\begin{amatrix}{2}
1 & -1 & 0 \\
0 & 1 & 0 \\
\end{amatrix}
\begin{amatrix}{2}
2 & 1 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{amatrix}
\begin{amatrix}{3}
1 & 2 & 3 & 4 \\
0 & 1 & 1 & 2 \\
0 & 0 & 2 & 3 \\
\end{amatrix}
\begin{amatrix}{4}
0 & 1 & 2 & 8 & 1 \\
0 & 0 & 0 & 0 & 3 \\
0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 \\
\end{amatrix}
\end{equation*}
Examples of NON row echlon form:
\begin{equation*}
\begin{amatrix}{2}
0 & \textbf{1} & 0 \\
\textbf{1} & 0 & 0 \\
\end{amatrix}
\begin{amatrix}{2}
0 & 0 & \textbf{1} \\
\textbf{1} & -1 & 0 \\
0 & 0 & 1 \\
\end{amatrix}
\begin{amatrix}{3}
\textbf{1} & 0 & 2 & 1 \\
0 & \textbf{1} & 0 & 2 \\
0 & \textbf{1} & 1 & 3 \\
\end{amatrix}
\begin{amatrix}{4}
\textbf{0} & \textbf{0} & \textbf{0} & \textbf{0} & \textbf{0} \\
1 & 0 & 2 & 0 & 1 \\
0 & 0 & 0 & 1 & 3 \\
0 & 0 & 0 & 0 & 0 \\
\end{amatrix}
\end{equation*}
\subsection{Reduced Row-Echelon Form}
\begin{defn}[Reduced Row-Echelon Form]
Suppose an augmented matrix is in row-echelon form. It is in \textbf{reduced row-echelon form} if
\begin{itemize}
\item Leading entry of every nonzero row is 1
\subitem Every pivot point is one
\item In each pivot column, except the pivot point, all other entries are 0.
\end{itemize}
\end{defn}
Examples of reduced row-echelon form:
\begin{equation*}
\begin{amatrix}{2}
1 & 2 & 3 \\
\end{amatrix}
\begin{amatrix}{2}
0 & 0 & 0 \\
0 & 0 & 0 \\
\end{amatrix}
\begin{amatrix}{2}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{amatrix}
\begin{amatrix}{3}
1 & 0 & 0 & 1 \\
0 & 1 & 0 & 2 \\
0 & 0 & 1 & 3 \\
\end{amatrix}
\begin{amatrix}{4}
0 & 1 & 2 & 0 & 1 \\
0 & 0 & 0 & 1 & 3 \\
0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 \\
\end{amatrix}
\end{equation*}
Examples of row-echelon form but not reduced: (pivot point is not 1 / all other elements \textbf{in pivot column} must be zero)
\begin{equation*}
\begin{amatrix}{2}
\textbf{3} & 2 & 1 \\
\end{amatrix}
\begin{amatrix}{2}
1 & \textbf{-1} & 0 \\
0 & 1 & 0 \\
\end{amatrix}
\begin{amatrix}{2}
\textbf{2} & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1 \\
\end{amatrix}
\begin{amatrix}{3}
\textbf{-1} & 2 & 3 & 4 \\
0 & 1 & 1 & 2 \\
0 & 0 & 2 & 3 \\
\end{amatrix}
\begin{amatrix}{4}
0 & 1 & 2 & \textbf{8} & 1 \\
0 & 0 & 0 & \textbf{4} & 3 \\
0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 \\
\end{amatrix}
\end{equation*}
To note: 2nd matrix has -1 in the pivot column, but 5th matrix has 2 in a non-pivot column so its fine
\subsection{Solving Linear System}
If Augmented Matrix is in reduced row-echelon form, then solving it is easy
\begin{equation*}
\begin{amatrix}{3}
1 & 0 & 0 & 1 \\
0 & 1 & 0 & 2 \\
0 & 0 & 1 & 3 \\
\end{amatrix}
\text{then } x_1 = 1, x_2 = 2, x_3 = 3
\end{equation*}
\begin{note}
\begin{itemize}
\item If any equations in the system is inconsistent, the whole system is inconsistent
\end{itemize}
\end{note}
\subsubsection{Examples}
Augmented Matrix: $\begin{amatrix}{4}
1 & -1 & 0 & 3 & -2 \\
0 & 0 & 1 & 2 & 5 \\
0 & 0 & 0 & 0 & 0 \\
\end{amatrix}$
\begin{itemize}
\item The zero row can be ignored.
\systeme{x_1 - x_2 + 3x_4 = -2, x_3 + 2x_4 = 5}
\item Degree of freedom(\# cols): 4, number of restrictions (\# pivot cols): 2, arbitrary vars(\# non pivot cols): 4-2 = 2. Set this to the non-pivot cols
\end{itemize}
\begin{enumerate}
\item Let $x_4 = t$ and sub into 2nd eqn
\subitem $x_3 + 2t = 5 \Rightarrow x_3 = 5-2t$
\item sub $x_4 = t$ into 1st eqn
\subitem $x_1 - x_2 + 3t = -2$
\subitem Let $x_2 = s$. Then $x_1 = -2 + s - 3t$
\item Infinitely many sols with ($s$ and $t$ as arbitrary params)
\subitem $x_1 = -2 + s - 3t, x_2 = s, x_3 = 5-2t, x_4 = t$
\end{enumerate}
Augmented Matrix: $\begin{amatrix}{5}
0 & 2 & 2 & 1 & -2 & 2 \\
0 & 0 & 1 & 1 & 1 & 3 \\
0 & 0 & 0 & 0 & 2 & 4 \\
\end{amatrix}$
\begin{itemize}
\item \systeme{0x_1 + 2x_2 + 2x_3 + 1x_4 -2x_5 = 2,x_3 + x_4 +x_5 = 3,2x_5 = 4}
\item Degree of freedom: 5, number of restrictions: 3, arbitrary vars: 5-3 = 2
\end{itemize}
\begin{enumerate}
\item by 3rd eqn, $2x_5 = 4 \Rightarrow x_5 = 2$
\item sub $x_5 = 2$ into 2nd eqn
\subitem $x_3 + x_4 + 2 = 3 \Rightarrow x_3 + x_4 = 1$
\subitem let $x_4 = t$. Then $x_3 = 1-t$
\item sub $x_5 = 2, x_3 = 1-t, x_4 = t$ into 1st eqn
\subitem $2x_2 + 2(1-t) + t - 2(2) = 2 \Rightarrow 2x_2 -t = 4 \Rightarrow x_2 = \frac{t}{2} + 2$
\item system has inf many solns: $x_1 = s, x_2 = \frac{t}{2} + 2, x_3 = 1-t, x_4 = t, x_5 = 2$ where $s$ and $t$ are arbitrary
\end{enumerate}
\subsubsection{Algorithm}
Given the augmented matrix is in row-echelon form.
\begin{enumerate}
\item Set variables corresponding to non-pivot columns to be arbitrary parameters
\item Solve variables corresponding to pivot columns by back substitution (from last eqn to first)
\end{enumerate}
\subsection{Gaussian Eliminiation}
\begin{defn}[Gaussian Elimination]\ \\
\begin{enumerate}
\item Find the left most column which is not entirely zero
\item If top entry of such column is 0, replace with nonzero number by swapping rows
\item For each row below top row, add multiple of top row so that leading entry becomes 0
\item Cover top row and repeat to remaining matrix
\end{enumerate}
\end{defn}
\begin{note}[Algorithm with Example]\ \\
$\begin{amatrix}{6}
0 & 0 & 0 & 2 & 4 & 2 & 8 \\
0 & 1 & 2 & 4 & 5 & 3 &-9 \\
0 &-2 &-4 &-5 &-4 & 3 & 6 \\
\end{amatrix}$
\begin{enumerate}
\item Find the left most column which is not all zero (2nd column)
\item Check top entry of the selection. If its zero, replace it by a nonzero number by interchanging the top row with another row below
\subitem $\begin{amatrix}{6}
0 & 1 & 2 & 4 & 5 & 3 &-9 \\
0 & 0 & 0 & 2 & 4 & 2 & 8 \\
0 &-2 &-4 &-5 &-4 & 3 & 6 \\
\end{amatrix}$
\item For each row below the top row, adda suitable multiple of top row so that leading entry becomes 0.
\subitem $2R_1 + R_3$ will ensure that the -2 turns to 0
\subitem $\begin{amatrix}{6}
0 & 1 & 2 & 4 & 5 & 3 &-9 \\
0 & 0 & 0 & 2 & 4 & 2 & 8 \\
0 & 0 & 0 & 3 & 6 & 9 &-12\\
\end{amatrix}$
\item Cover top row and repeat procedure to the remaining matrix
\subitem $\begin{amatrix}{6}
0 & 1 & 2 & 4 & 5 & 3 &-9 \\
\hline
0 & 0 & 0 & 2 & 4 & 2 & 8 \\
0 & 0 & 0 & 3 & 6 & 9 &-12\\
\end{amatrix}$
\subitem Look at $C_4$. $R_3 \times -1.5R_2$ will set $R_3C_4$ to zero.
\subitem $\begin{amatrix}{6}
0 & 1 & 2 & 4 & 5 & 3 &-9 \\
\hline
0 & 0 & 0 & 2 & 4 & 2 & 8 \\
0 & 0 & 0 & 0 & 0 & 6 & -24\\
\end{amatrix}$
\subitem This is now in row echelon form.
\end{enumerate}
Only use $R_i \Leftrightarrow R_j or R_i + CR_j$ in this method.
\end{note}
\subsection{Gauss-Jordan Elimination}
\begin{defn}[Gauss Joran Elimination]\ \\
\begin{enumerate}
\item[1-4.] Use Gaussian Eliminiation to get row-echelon form
\setcounter{enumi}{4}
\item For each nonzero row, multiply a suitable constant so pivot point becomes 1
\item Begin with last nonzero row and work backwords
\subitem Add suitable multiple of each row to the rows above to introduce 0 above pivot point
\end{enumerate}
\begin{itemize}
\item Every matrix has a unique reduced row-echelon form.
\item Every nonzero matrix has infinitely many row-echelon ofrm
\end{itemize}
\end{defn}
\begin{note}[Gauss Jordan Elimination Example] Suppose an augmented matrix is in row-echelon form.
$\begin{amatrix}{5}
1 & 2 & 4 & 5 & 3 & -9 \\
0 & 0 & 2 & 4 & 2 & 8 \\
0 & 0 & 0 & 0 & 6 & -24 \\
\end{amatrix}$
\begin{enumerate}
\item All pivot points must be 1
\subitem multiply $R_2$ by $\frac{1}{2}$ and $R_3$ by $\frac{1}{6}$
\subitem $\begin{amatrix}{5}
1 & 2 & 4 & 5 & 3 & -9 \\
0 & 0 & 1 & 2 & 1 & 4 \\
0 & 0 & 0 & 0 & 1 & -4 \\
\end{amatrix}$
\item In each pivot col, all entries other than pivot point must be 0. Work backwards
\subitem $R_1 + -3R_1$, $R_2 + -R_1$
\subitem $\begin{amatrix}{5}
1 & 2 & 4 & 5 & 0 & 3 \\
0 & 0 & 1 & 2 & 0 & 8 \\
0 & 0 & 0 & 0 & 1 & -4 \\
\end{amatrix}$
\subitem $R_1 + -4R_2$
\subitem $\begin{amatrix}{5}
1 & 2 & 0 & -3 & 0 & -29 \\
0 & 0 & 1 & 2 & 0 & 8 \\
0 & 0 & 0 & 0 & 1 & -4 \\
\end{amatrix}$
\end{enumerate}
\end{note}

Binary file not shown.

View File

@ -69,6 +69,10 @@
This is a note
\end{note}
\begin{defn}[Some Term]
This is a definition
\end{defn}
% Maybe I need to add one more part: Examples.
% Set style and colour later.

View File

@ -9,7 +9,10 @@
\usepackage[english]{babel}
\usepackage{framed}
\usepackage[dvipsnames]{xcolor}
\usepackage{soul}
\usepackage{tcolorbox}
\usepackage{systeme}
\usepackage{multicol}
\colorlet{LightGray}{White!90!Periwinkle}
\colorlet{LightOrange}{Orange!15}
@ -34,6 +37,10 @@
\declaretheorem[style=notesty,numbered=no]{note}
\tcolorboxenvironment{note}{colback=LightRed}
\declaretheoremstyle[name=Definition,]{defnsty}
\declaretheorem[style=defnsty,numbered=no]{defn}
\tcolorboxenvironment{defn}{colback=LightGray}
\setstretch{1.2}
\geometry{
textheight=9in,