Refactored comments

This commit is contained in:
Sergiusz Warga 2021-03-14 18:40:53 +01:00
parent 7e55d72cfe
commit 37feb7c484
8 changed files with 112 additions and 77 deletions

View File

@ -1,5 +1,5 @@
function [Q R] = Alg11(A) function [Q R] = Alg11(A)
% Algorithm 11: QR factorization via Housholder algorithm. % Algorithm 11: QR factorization via Householder algorithm.
[m, n] = size(A); [m, n] = size(A);
@ -17,5 +17,8 @@ end
R = triu(A) R = triu(A)
H = tril(A, -1)
end end

View File

@ -1,21 +1,25 @@
function A = Alg1_outer_product_gaussian_elimination(A) function A = Alg1_outer_product_gaussian_elimination(A)
% Algorithm 1: Outer Product Gaussian Elimination (Golub, Loan, 3.2.1) % Algorithm 1: Outer Product Gaussian Elimination
% Performs a gaussian eliminaion on a square matrix A. % Performs a gaussian eliminaion on a square matrix A.
[m, n] = size(A); [m, n] = size(A);
if m ~= n if m ~= n
error('Matrix is not squared!') error('Matrix is not square!')
end end
% if det(A) == 0 for k = 1:n-1
% error('Matrix is not nonsingular!') if det(A(1:k, 1:k)) < eps
% end error('Matrix is not nonsingular!')
for k = 1 : m-1
rows = k + 1 : m;
A(rows, k) = A(rows, k)/A(k, k);
A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows);
end end
end
% The following algorithm is based on the Algrotihm 3.2.1 from [2].
for k = 1 : m-1
rows = k + 1 : m;
A(rows, k) = A(rows, k)/A(k, k);
A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows);
end
end end

View File

@ -1,43 +1,47 @@
function [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A) function [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A)
% Algorithm 2: Gaussian Elimination with Complete Pivoting. % Algorithm 2: Gaussian Elimination with Complete Pivoting.
% [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A) % [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A)
% computes the complete pivoting factorization PAQ = LU.
[n, m] = size(A); [m, n] = size(A);
if n ~= m if m ~= n
error('Matrix is not squared!') error('Matrix is not square!')
end end
% if det(A) == 0
% error('Matrix is not nonsingular!')
% end
p = 1:n; % p and q are permutation vectors respectively rows and columns
q = 1:n; p = 1:m;
q = 1:m;
for k = 1 : n-1 % The following algorithm is based on the Algrotihm 3.4.2 from [2].
i = k:n;
j = k:n; for k = 1 : m-1
i = k:m;
j = k:m;
% Find the maximum entry to be the next pivot
[max_val, rows_of_max_in_col] = max(abs(A(i, j))); [max_val, rows_of_max_in_col] = max(abs(A(i, j)));
[~, max_col] = max(max_val); [~, max_col] = max(max_val);
max_row = rows_of_max_in_col(max_col); max_row = rows_of_max_in_col(max_col);
% Assign value of mu and lambda in respect to the main A matrix % Assign value of mu and lambda in respect to the main matrix A
[mi, lm] = deal(max_row+k-1, max_col+k-1); [mi, lm] = deal(max_row+k-1, max_col+k-1);
A([k mi], 1:n) = deal(A([mi k], 1:n)); % Interchange the rows and columns of matrix A...
A(1:n, [k lm]) = deal(A(1:n, [lm k])); A([k mi], 1:m) = deal(A([mi k], 1:m));
A(1:m, [k lm]) = deal(A(1:m, [lm k]));
% ...and respective permutation vectors entries.
p([k, mi]) = p([mi, k]); p([k, mi]) = p([mi, k]);
q([k, lm]) = q([lm, k]); q([k, lm]) = q([lm, k]);
% Perform Gaussian elimination with the greatest pivot % Perform Gaussian elimination with the greatest pivot
if A(k, k) ~= 0 if A(k, k) ~= 0
rows = k+1 : n; rows = k+1 : m;
A(rows, k) = A(rows, k)/A(k, k); A(rows, k) = A(rows, k)/A(k, k);
A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows); A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows);
end end
end end
I = eye(n); I = eye(m);
U = triu(A); U = triu(A);
L = tril(A, -1) + I; L = tril(A, -1) + I;
P = I(p, :); P = I(p, :);
Q = I(:, q); Q = I(:, q);
end

View File

@ -1,18 +1,29 @@
function b = Alg3_forward_substitution(L, b) function b = Alg3_forward_substitution(L, b)
% Algorithm 3: Forward Substitution (Golub, Loan, Alg. 3.1.1) % Algorithm 3: Forward Substitution
% b = Alg3_forward_substitution(L, b) overwrites b with the solution to
% Lx = b.
[m, n] = size(L); [m, n] = size(L);
if m ~= n
error('Matrix is not squar!') if m ~= n
end error('Matrix is not square!')
end
if length(b) ~= m
error('Vector b has wrong length!')
end
if det(L) < eps
error("Matrix is not nonsingular!")
end
% The following algorithm is based on the Algrotihm 3.1.1 from [2].
% b(m, :) so that matrices are also accepted
b(1, :) = b(1, :)/L(1,1);
for i = 2:m
b(i, :) = (b(i, :) - L(i, 1:i-1)*b(1:i-1, :))/L(i, i);
end
if length(b) ~= m
error('Vector b has wrong length!')
end
b(1, :) = b(1, :)/L(1,1);
for i = 2:m
b(i, :) = (b(i, :) - L(i, 1:i-1)*b(1:i-1, :))/L(i, i);
end
end end

View File

@ -1,11 +1,12 @@
function b = Alg4_back_substitution(U,b) function b = Alg4_back_substitution(U,b)
% Argorithm 4: Back Substitution (Golub, Loan, Alg. 3.1.2) % Argorithm 4: Back Substitution
% Returns vetor b with solution to he Ux = b. % b = Alg4_back_substitution(U,b) returns vetor b with solution to the
% Ux = b.
[m, n] = size(U); [m, n] = size(U);
if U ~= triu(U) if U ~= triu(U)
error('Matrix is not upper triangular!') error('Matrix U is not upper triangular!')
end end
if m ~= n if m ~= n
@ -16,12 +17,14 @@ if length(b) ~= m
error('Vector b has wrong length!') error('Vector b has wrong length!')
end end
% if det(U) < 0.001 if det(U) < eps
% error('Matrix is not nonsingular!') error('Matrix is not nonsingular!')
% end end
% The following algorithm is based on the Algrotihm 3.1.2 from [2].
% b(m, :) so that matrices are also accepted % b(m, :) so that matrices are also accepted
b(m, :) = b(m, :)/U(m, m); b(m, :) = b(m, :)/U(m, m);
for i = m-1:-1:1 for i = m-1:-1:1
b(i, :) = (b(i, :) - U(i, i+1 : m)*b(i+1 : m, :))/U(i, i); b(i, :) = (b(i, :) - U(i, i+1 : m)*b(i+1 : m, :))/U(i, i);

View File

@ -1,6 +1,7 @@
function A = Alg5_gauss_jordan_elimination(A) function A = Alg5_gauss_jordan_elimination(A)
% Algorithm 5: Gauss-Jordan Elimination % Algorithm 5: Gauss-Jordan Elimination
% Argument A is an augmented matrix % A = Alg5_gauss_jordan_elimination(A) performs Gauss-Jordan elimination
% on an augmented matrix A.
[m, n] = size(A); [m, n] = size(A);
@ -16,4 +17,5 @@ for k = 1 : m
end end
end end
end end
end end

View File

@ -2,38 +2,38 @@ function A = Alg6_RREF(A)
% Algorithm 6: Reduced Row Echelon Form (RREF) % Algorithm 6: Reduced Row Echelon Form (RREF)
% A = Alg6_RREF(A) returns RREF of matrix A. % A = Alg6_RREF(A) returns RREF of matrix A.
[M, N] = size(A); [m, n] = size(A);
n = 0; j = 0;
for m = 1 : M for k = 1 : m
n = n + 1; j = j + 1;
if n > N if j > n
break break
end end
% We want the left-most coefficient to be 1 (pivot) % We want the left-most coefficient to be 1 (pivot)
row = A(m, :); row = A(k, :);
if row(m) == 0 if row(k) == 0
n = n + 1; j = j + 1;
end end
row = row/row(n); row = row/row(j);
A(m, :) = row; A(k, :) = row;
for i = 1 : M for i = 1 : m
if i ~= m if i ~= k
A(i, :) = A(i, :)-(A(i, n))*row; A(i, :) = A(i, :)-(A(i, j))*row;
end end
end end
for i = m + 1 : M for i = k + 1 : m
A(i:end, m+1:end); % Partial matrix (in which we are looking for non-zero pivots) A(i:end, k+1:end); % Partial matrix (in which we are looking for non-zero pivots)
A(i:end, m+1); % Left-most column A(i:end, k+1); % Left-most column
if ~any(A(i:end, m+1)) % If the left-most column has only zeros check the next one if ~any(A(i:end, k+1)) % If the left-most column has only zeros check the next one
m = m + 1; k = k + 1;
end end
A(i:end, m+1:end); A(i:end, k+1:end);
if A(i, m+1) == 0 if A(i, k+1) == 0
non_zero_row = find(A(i:end,m+1), 1); non_zero_row = find(A(i:end,k+1), 1);
if isempty(non_zero_row) if isempty(non_zero_row)
continue continue
end end

View File

@ -0,0 +1,8 @@
# Direct Methods for Solving Linear Systems
## Bibliography
References in the code comments are represented by `[number]`:
- 1
- 2 Golub, van Loan, Matrix Computations, 3rd edition