WUT_Computer_Science/NotProgramming/ENUME/projectA/sections/18-task-2-code.tex
Krzysztof kuhy Rudnicki ffa5dad02a Split 19 over-cap LaTeX reports, proving each PDF is unchanged
The 250-line cap applies to prose too, and the reports were the worst
offenders -- ENUME's projectA.tex alone was 1,876 lines. Each is now a
preamble plus one \input{} per section, which is how a long LaTeX document is
normally organised; not one byte of prose was rewritten.

"The PDF is unchanged" is checked by a script rather than by eye, because
hashing the PDF cannot work: pdflatex stamps a /CreationDate and an /ID into
every build, so two runs of the SAME source already differ. tex_fingerprint.sh
compares what a reader sees instead -- page count plus a hash of the extracted
text -- and it is stable across rebuilds of untouched sources, which is the
property that makes it usable as a gate.

It earned its keep immediately. Both ECOTE reports quote an example LaTeX
document inside an lstlisting block, so \end{document} appears in the middle
of the file; splitting on the FIRST one stranded everything after it. Same
page count, different text, caught automatically. EOPSY lab3 exposed a second
bug: a \section{} title that runs onto the next line.

Result: 19 .tex over the cap -> 1. The survivor,
ELAC/.../03-building-nfa-...tex, is 418 lines of consecutive tikz figures
with no subsection to split on and needs a hand-placed boundary.

Verified: 29 splits across two passes (section, then subsection for the
pieces still over), every one reporting `unchanged` from
scripts/tex_fingerprint.sh --check.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172dj34FDpao6wsix8ZuCzE
2026-08-28 19:47:12 +02:00

218 lines
6.8 KiB
TeX

\section{Task 2 Code}
\subsection{Main function}
\begin{simplechar}
\begin{lstlisting}
function x = indicatedMethod(Matrix, Vector) % Name of the method as in the textbook
% x stands for obtained result
[~,Columns] = size(Matrix); % We need to know how big the matrix is in next steps
% notice the '~', since We assume We use square matrix, We do not need
% to have another variable for number of rows since it is the same as
% number of columns
checkIfMatrixIsSquareMatrix(Matrix);
[Matrix, Vector] = gaussianEliminationWithPartialPivoting(Columns, Matrix, Vector);
% Change matrix to upper triangular matrix
[Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector);
% Get the solution
x = iterativeResidualCorrection(Matrix, x, Vector); % Improve on the solution
end % end function
\end{lstlisting}
\end{simplechar}
\subsection{checkIfMatrixIsSquareMatrix}
\begin{simplechar}
\begin{lstlisting}
function checkIfMatrixIsSquareMatrix(Matrix)
[Rows,Columns] = size(Matrix);
if Rows ~= Columns
error ('Matrix is not square matrix!');
end % end if
end % end function
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{gaussianEliminationWithPartialPivoting}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, Vector] = gaussianEliminationWithPartialPivoting(Columns, Matrix, Vector)
for j = 1 : Columns
centralElement = max(Matrix(j:Columns,j));
% We stay in the same row (j) but We change columns, as in the
% textbook
[Matrix, Vector] = partialPivoting(Matrix, Vector, j, centralElement, Columns);
% ensures that a_kk != 0 and reduces errors
[Matrix, Vector] = gaussianElimination(j, Columns, Matrix, Vector);
% change matrix into upper triangular matrix
end % end for
end % end function
\end{lstlisting}
\end{simplechar}
\subsection{partialPivoting}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, Vector] = partialPivoting(Matrix, Vector, j, centralElement, Columns)
for k = j : Columns
partialPivotingSwapOneRow(Matrix, Vector, j, k, centralElement);
end % end for
end % end function
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{partialPivotingSwapOneRow}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, Vector] = partialPivotingSwapOneRow(Matrix, Vector, j, k, centralElement)
if Matrix(k,j) == centralElement
swapRowMatrix(Matrix, j, k); % swap jth row with kth row
swapValueVector(Vector, j, k); % swap jth value with kth value
end % end if
end % end function
\end{lstlisting}
\end{simplechar}
\subsection{swapRowMatrix}
\begin{simplechar}
\begin{lstlisting}
function Matrix = swapRowMatrix(Matrix, j, k)
temp = Matrix(j , :); % ' : ' denote "all elements in jth row"
Matrix(j , :) = Matrix(k, :);
Matrix(k, :) = temp; % temp equal to previous value of jth row
end
\end{lstlisting}
\end{simplechar}
\subsection{swapValueVector}
\begin{simplechar}
\begin{lstlisting}
function Vector = swapValueVector(Vector, j, k)
temp = Vector(j);
Vector(j) = Vector(k);
Vector(k) = temp; % temp equal to previous value of k element of vector
end % end function
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{gaussianElimination}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, Vector] = gaussianElimination(j, Columns, Matrix, Vector)
for i = j + 1 : Columns
rowMultiplier = Matrix(i,j) / Matrix(j,j);
[Matrix, Vector] = substractRows(Matrix, Vector, i, rowMultiplier, j, Columns);
end % end for
end % end function
\end{lstlisting}
\end{simplechar}
\subsection{substractRows}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, Vector] = substractRows(Matrix, Vector, i, rowMultiplier, j, Columns)
Vector(i) = Vector(i) - rowMultiplier * Vector(j);
for curentColumn = 1 : Columns
Matrix(i,curentColumn) = Matrix(i,curentColumn) - rowMultiplier * Matrix(j, curentColumn);
end % end for
end % end function
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{backSubstitutionPhase}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector)
for k = Columns : -1 : 1
% Start at final column and move by -1 each iteration until we reach 1
equation = 0;
for j = k+1 : Columns
equation = equation + Matrix(k,j) * x(j, 1);
% even though x is a vector we still need to put '1' to ensure
% that number of columns in the first matrix matches number of
% rows in second matrix
end % end for
x(k, 1) = (Vector(k,1) - equation) / Matrix(k,k);
% even though x is a vector we still need to put '1' to ensure
% that we do not exceed array bounds
end % end for
end % end function
\end{lstlisting}
\end{simplechar}
\subsection{iterativeResidualCorrection}
\begin{simplechar}
\begin{lstlisting}
function x = iterativeResidualCorrection(Matrix, x, Vector)
residuum = Matrix*x - Vector; % as in the book
newResiduum = residuum;
x = improveSolution(x, newResiduum, residuum, Matrix, Vector);
end % end function
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{improveSolution}
\begin{simplechar}
\begin{lstlisting}
function x = iterativeResidualCorrection(A, x, b)
r = A*x - b;
for i = 1 : 100
[~, ~, deltaX] = solveSystem(A, r);
newX = x - deltaX;
r = A*newX - b;
x = newX;
end
end % end function
\end{lstlisting}
\end{simplechar}
\newpage
\hypertarget{function2_plot}{\subsection{plotErrorsGaussian}}
\begin{simplechar}
\begin{lstlisting}
function plotErrorsGaussian(maxMatrixSize)
errorsA = zeros(maxMatrixSize);
errorsB = zeros(maxMatrixSize);
errorsAR = zeros(maxMatrixSize);
errorsBR = zeros(maxMatrixSize);
for i = 1 : maxMatrixSize
[~, errorBeforeResidualCorrection, errorAfterResidualCorrection] = indicatedMethod(matrixA(i), vectorA(i));
errorsA(i) = errorBeforeResidualCorrection;
errorsAR(i) = errorAfterResidualCorrection;
[~, errorBeforeResidualCorrection, errorAfterResidualCorrection] = indicatedMethod(matrixB(i), vectorB(i));
errorsB(i) = errorBeforeResidualCorrection;
errorsBR(i) = errorAfterResidualCorrection;
end
nexttile
plot(errorsA, '.');
title('Errors before residual correction for task 2a:');
xlabel('Size of matrix A');
ylabel('Errors');
nexttile
plot(errorsAR, '.');
title('Errors after residual correction for task 2a:');
xlabel('Size of matrix A');
ylabel('Errors');
nexttile
plot(errorsB, '.');
title('Errors before residual correction for task 2b:');
xlabel('Size of matrix A');
ylabel('Errors');
nexttile
plot(errorsBR, '.');
title('Errors after residual correction for task 2b:');
xlabel('Size of matrix A');
ylabel('Errors');
end
\end{lstlisting}
\end{simplechar}