WUT_Computer_Science/NotProgramming/ENUME/projectA/sections/19-task-3-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

223 lines
7.6 KiB
TeX

\section{Task 3 Code}
\begin{simplechar}
\begin{lstlisting}
function [x_j, x_g] = iterative(Matrix, Vector)
[L, D, U, initial_x, whichIterationAreWeOnJ, whichIterationAreWeOnG, demandedToleranceJ, demandedToleranceG, flag, Rows] = initializeValues(Matrix);
[x_j, whichIterationAreWeOnJ, demandedToleranceJ] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOnJ, demandedToleranceJ, Vector, flag);
[x_g, whichIterationAreWeOnG, demandedToleranceG] = gaussSeidelLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOnG, demandedToleranceG, Vector, flag, Rows);
dispFinalResults(x_j, x_g, demandedToleranceJ, demandedToleranceG, whichIterationAreWeOnJ, whichIterationAreWeOnG, Matrix, Vector);
end
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{initializeValues}
\begin{simplechar}
\begin{lstlisting}
function [L, D, U, initial_x, whichIterationAreWeOnJ, whichIterationAreWeOnG, demandedToleranceJ, demandedToleranceG, flag, Rows] = initializeValues(Matrix)
[Rows, ~] = size(Matrix);
[L, D, U] = decomposeMatrix(Matrix);
initial_x = zeros(Rows, 1);
whichIterationAreWeOnJ = 0;
whichIterationAreWeOnG = 0;
demandedToleranceJ = 10e-10; % as per task description
demandedToleranceG = 10e-10; % as per task description
flag = 0;
end
\end{lstlisting}
\end{simplechar}
\subsection{decomposeMatrix}
\begin{simplechar}
\begin{lstlisting}
function [L, D, U] = decomposeMatrix(Matrix)
D = diag(diag(Matrix));
U = triu(Matrix, 1); % Generates upper triangular part of matrix
% where the second variable denotes on which diagonal of matrix should be
% start
L = tril(Matrix, -1); % Generates lower triangular part of matrix
% where the second variable denotes on which diagonal of matrix should be
% start
end
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{jacobiLoop}
\begin{simplechar}
\begin{lstlisting}
function [x_j, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag)
while flag ~= 1 % flag denotes whether norm(Matrix*x_g-Vector) <= demandedTolerance
[x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector);
end
end
\end{lstlisting}
\end{simplechar}
\subsection{jacobiInsideLoop}
\begin{simplechar}
\begin{lstlisting}
function [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector)
x_j = jacobiEquation(D, L, U, initial_x, Vector);
[flag, demandedTolerance] = checkError(x_j, initial_x, demandedTolerance, Matrix, Vector);
[initial_x, whichIterationAreWeOn] = endOfLoop(x_j, whichIterationAreWeOn);
end
\end{lstlisting}
\end{simplechar}
\subsection{jacobiEquation}
\begin{simplechar}
\begin{lstlisting}
function x = jacobiEquation(D, L, U, initial_x, Vector)
x = - D \ ( L + U ) * initial_x + D \ Vector; % As per formula
% We will be using D \ Vector and D \ ( ) instead of inverseD since
% this is faster according to matlab
end
\end{lstlisting}
\end{simplechar}
\subsection{gaussSeidelLoop}
\begin{simplechar}
\begin{lstlisting}
function [x_g, whichIterationAreWeOn, demandedTolerance] = gaussSeidelLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag, Rows)
while flag ~= 1 % flag denotes whether norm(Matrix*x_g-Vector) <= demandedTolerance
[x_g, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = gaussiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows);
end
end
\end{lstlisting}
\end{simplechar}
\subsection{gaussiInsideLoop}
\begin{simplechar}
\begin{lstlisting}
function [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = gaussiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows)
x_j = gaussSeidelEquation(D, L, U, initial_x, Vector, Rows);
[flag, demandedTolerance] = checkError(x_j, initial_x, demandedTolerance, Matrix, Vector);
[initial_x, whichIterationAreWeOn] = endOfLoop(x_j, whichIterationAreWeOn);
end
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{gaussSeidelEquation}
\begin{simplechar}
\begin{lstlisting}
function x_g = gaussSeidelEquation(D, L, U, initial_x, Vector, Rows)
W = U*initial_x - Vector;
x_g(1, 1) = -W(1, 1) / D(1,1);
for i = 2 : Rows
x_g(i, 1) = calculateNominator(i, L, x_g, W) / D(i, i);
end
end
\end{lstlisting}
\end{simplechar}
\subsection{checkError}
\begin{simplechar}
\begin{lstlisting}
function [flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector)
flag = 0;
currentError = norm(x - initial_x);
if currentError <= demandedTolerance
currentError = norm(Matrix*x-Vector);
if currentError <= demandedTolerance % if sequence as per textbook
flag = 1;
else
demandedTolerance = demandedTolerance * 2; % arbitrary value
end
end
end
\end{lstlisting}
\end{simplechar}
\newpage
\subsection{endOfLoop}
\begin{simplechar}
\begin{lstlisting}
function [initial_x, whichIterationAreWeOn, flag] = endOfLoop(x, whichIterationAreWeOn)
initial_x = x;
whichIterationAreWeOn = whichIterationAreWeOn + 1;
flag = 0;
end
\end{lstlisting}
\end{simplechar}
\subsection{dispFinalResults}
\begin{simplechar}
\begin{lstlisting}
function dispFinalResults(x_j, x_g, demandedToleranceJ, demandedToleranceG, whichIterationAreWeOnJ, whichIterationAreWeOnG, Matrix, Vector)
disp("Final demandedTolerance for Jacobi method");
disp(demandedToleranceJ);
disp("Final demandedTolerance for Gaussian-Seidel method:");
disp(demandedToleranceG);
disp("Final Iteration for Jacobi method: ");
disp(whichIterationAreWeOnJ);
disp("Final Iteration for Gaussian-Seidel method: ");
disp(whichIterationAreWeOnG);
disp("Error for Jacobi method:");
disp(norm(Matrix*x_j - Vector));
disp("Error for Gaussian-Seidel method:");
disp(norm(Matrix*x_g - Vector));
disp("A\b error:");
disp(norm(Matrix * (Matrix\Vector) - Vector));
disp("Answer for Jacobi method: ");
disp(x_j);
disp("Answer for Gaussian-Seidel method: ");
disp(x_g);
end
\end{lstlisting}
\end{simplechar}
\newpage
\hypertarget{function_3_plot}{\subsection{plotIterations}}
\begin{simplechar}
\begin{lstlisting}
function plotIterations()
maxMatrixSize = 500;
iterationsJ = zeros(maxMatrixSize);
iterationsG = zeros(maxMatrixSize);
for i = 1 : maxMatrixSize
[~, ~, whichIterationAreWeOnJ, whichIterationAreWeOnG] = iterative(matrixA(i), vectorA(i));
iterationsJ(i) = whichIterationAreWeOnJ;
iterationsG(i) = whichIterationAreWeOnG;
end
nexttile
plot(iterationsJ, '.');
title('Number of iterations for different sizes of matrices for Jacobi method');
xlabel('Size of matrix A');
ylabel('Number of iterations');
nexttile
plot(iterationsG, '.');
title('Number of iterations for different sizes of matrices for Gauss-Seidel method');
xlabel('Size of matrix A');
ylabel('Number of iterations');
end
\end{lstlisting}
\end{simplechar}
\newpage
\hypertarget{function_3_dominant}{Code for checking if matrix is diagonally dominant}
\begin{lstlisting}
function d = checkIfDiagonallyDominant(Matrix)
d = 1;
[Rows, ~] = size(Matrix);
for i = 1 : Rows
rowsSum = 0;
for j = 1 : Rows
rowsSum = rowsSum + abs(Matrix(i, j));
end
rowsSum = rowsSum - Matrix(i, i);
if Matrix(i, i) <= rowsSum
d = 0;
break
end
end
end
\end{lstlisting}
\end{simplechar}
\newpage