WUT_Computer_Science/NotProgramming/ENUME/projectA/sections/20-task-4-code/04-qrshifts.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

210 lines
6.7 KiB
TeX

\subsection{QRShifts}
\begin{simplechar}
\begin{lstlisting}
function [eigenValues, whatIterationAreWeOn, Matrix] = QRShifts(Matrix)
eigenFromMatlab = eig(Matrix);
initialMatrix = Matrix;
[eigenValues, whatIterationAreWeOn, matrixSize, minThreshold] = initiateValues(Matrix);
[Matrix, whatIterationAreWeOn, eigenValues] = QRShiftLoop(matrixSize, Matrix, eigenValues, minThreshold, whatIterationAreWeOn);
dispResults(eigenValues, Matrix, whatIterationAreWeOn, eigenFromMatlab, initialMatrix);
end
\end{lstlisting}
\end{simplechar}
\subsubsection{initiateValues}
\begin{simplechar}
\begin{lstlisting}
function [eigenValues, whatIterationAreWeOn, matrixSize, minThreshold] = initiateValues(Matrix)
eigenValues = double.empty(1, 0);
whatIterationAreWeOn = 0;
matrixSize = size(Matrix, 1);
minThreshold = 1e-6;
end
\end{lstlisting}
\end{simplechar}
\newpage
\subsubsection{QRShiftLoop}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, whatIterationAreWeOn, eigenValues] = QRShiftLoop(matrixSize, Matrix, eigenValues, minThreshold, whatIterationAreWeOn)
while matrixSize >= 2
flag = 0;
[Matrix, matrixSize, whatIterationAreWeOn, eigenValues] = findEigenValue(Matrix, matrixSize, whatIterationAreWeOn, eigenValues, minThreshold, flag);
[matrixSize, Matrix] = deflateMatrix(Matrix, matrixSize);
end
eigenValues(size(eigenValues, 2) + 1) = Matrix(1, 1);
end
\end{lstlisting}
\end{simplechar}
\subsubsection{findEigenValue}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, matrixSize, whatIterationAreWeOn, eigenValues] = findEigenValue(Matrix, matrixSize, whatIterationAreWeOn, eigenValues, minThreshold, flag)
while flag == 0
eigenValueCorner = getEigenValueFromCorner(Matrix, matrixSize);
[Matrix, whatIterationAreWeOn] = shiftAndIterate(matrixSize, Matrix, eigenValueCorner, whatIterationAreWeOn);
[flag, eigenValues] = thresholdBreached(Matrix, matrixSize, minThreshold, eigenValues);
end
end
\end{lstlisting}
\end{simplechar}
\newpage
\subsubsection{getEigenValueFromCorner}
\begin{simplechar}
\begin{lstlisting}
function eigenValueCorner = getEigenValueFromCorner(Matrix, matrixSize)
corner = Matrix((matrixSize - 1) : matrixSize, (matrixSize - 1) : matrixSize);
% get 2 x 2 corner of the matrix
eigenValueCorner = solveCharactersticEquation(corner);
end
\end{lstlisting}
\end{simplechar}
\subsubsection{shiftAndIterate}
\begin{simplechar}
\begin{lstlisting}
function [Matrix, whatIterationAreWeOn] = shiftAndIterate(matrixSize, Matrix, eigenValueCorner, whatIterationAreWeOn)
% shift and iterate algorithm
identityMatrix = eye(matrixSize);
Matrix = Matrix - identityMatrix * eigenValueCorner;
[Q, R] = gramSchmidtAlgorithm(Matrix);
Matrix = R * Q + identityMatrix * eigenValueCorner;
whatIterationAreWeOn = whatIterationAreWeOn + 1;
end
\end{lstlisting}
\end{simplechar}
\subsubsection{deflateMatrix}
\begin{simplechar}
\begin{lstlisting}
function [matrixSize, Matrix] = deflateMatrix(Matrix, matrixSize)
matrixSize = matrixSize - 1;
Matrix = Matrix(1:matrixSize, 1:matrixSize);
end
\end{lstlisting}
\end{simplechar}
\newpage
\subsubsection{thresholdBreached}
\begin{simplechar}
\begin{lstlisting}
function [flag, eigenValues] = thresholdBreached(Matrix, matrixSize, minThreshold, eigenValues)
flag = 0;
threshold = max(abs(Matrix(matrixSize, 1:(matrixSize - 1))));
% once we zero the row (or rather get close enough to zero) exit loop
if (threshold <= minThreshold)
eigenValues(size(eigenValues, 2) + 1) = Matrix(matrixSize, matrixSize);
flag = 1;
end
end
\end{lstlisting}
\end{simplechar}
\subsubsection{solveCharactersticEquation}
\begin{simplechar}
\begin{lstlisting}
% finds the eigenvalue of a 2x2 matrix that is closer to the lower right corner
function eigen = solveCharactersticEquation(Matrix)
[eigenOne, eigenTwo] = calculateZeros(Matrix);
eigen = valueCloserToLowerRightCorner(eigenOne, eigenTwo, Matrix);
end
function eigen = valueCloserToLowerRightCorner(eigenOne, eigenTwo, Matrix)
if abs(Matrix(4) - eigenOne) < abs(Matrix(4) - eigenTwo)
eigen = eigenOne;
else
eigen = eigenTwo;
end
end
\end{lstlisting}
\end{simplechar}
\subsubsection{calculateZeros}
\begin{simplechar}
\begin{lstlisting}
function [zeroOne, zeroTwo] = calculateZeros(Matrix)
b = Matrix(1) + Matrix(4);
ac = Matrix(1) * Matrix(4) - Matrix(2) * Matrix(3);
delta = (b) ^ 2 - 4 * ac;
% get delta of quadratic equation
squareRootDelta = sqrt(delta);
% square delta
zeroOne = (b - squareRootDelta) / 2;
zeroTwo = (b + squareRootDelta) / 2;
end
\end{lstlisting}
\end{simplechar}
\subsubsection{dispResults}
\begin{simplechar}
\begin{lstlisting}
function dispResults(eigenValues, Matrix, whatIterationAreWeOn, eigenFromMatlab, initialMatrix)
disp("Initial matrix: ");
disp(initialMatrix);
disp("Final matrix: ");
disp(Matrix);
disp("Number of iterations: ");
disp(whatIterationAreWeOn);
disp("Eigen values from eig(Matrix):");
disp(eigenFromMatlab);
disp("Our eigen values: ");
disp(eigenValues);
end
\end{lstlisting}
\end{simplechar}
\newpage
\hypertarget{function4}{\subsection{task4Plot}}
\begin{simplechar}
\begin{lstlisting}
function task4Plot(maxMatrixSize)
iterationsNoShiftsVector = zeros(maxMatrixSize);
iterationsShiftsVector = zeros(maxMatrixSize);
for i = 1 : maxMatrixSize
[~, iterationsNoShifts, ~, ~, iterationsShifts, ~] = task4(matrix4(i));
iterationsNoShiftsVector(i) = iterationsNoShifts;
iterationsShiftsVector(i) = iterationsShifts;
end
nexttile
plot(iterationsNoShiftsVector, '.');
title('Number of iterations for different sizes of matrices for no shift method');
xlabel('Size of matrix A');
ylabel('Number of iterations');
nexttile
plot(iterationsShiftsVector, '.');
title('Number of iterations for different sizes of matrices for shift method');
xlabel('Size of matrix A');
ylabel('Number of iterations');
end
\end{lstlisting}
\end{simplechar}
\hypertarget{function4_matrix_generation}{\subsection{Matrix generation}}
\begin{simplechar}
\begin{lstlisting}
function A = matrix4()
A = 10 * rand(5); % rand generates 5x5 matrix filled with random numbers
% we multiply by 10 to get at lest one digit in front of the dot
A = floor(A); % we floor the matrix we got to get nice natural numbers matrix
A = A * A'; % we get symmetric matrix
disp(issymmetric(A)); % we check if matrix is symmetric
end
\end{lstlisting}
\end{simplechar}
\begin{thebibliography}{9}
\bibitem{texbook}
Piotr Tatjewski (2014) \emph{Numerical Methods}, Oficyna Wydawnicza Politechniki Warszawskiej
\end{thebibliography}