clc;
clear all;

%% Linear Algebra (Matrix) Operations in MATLAB

%% Matrix definitions
% MATLAB allows for both spaces and commas as seperators of individual
%entries in matrix. Rows are seperated by either line breaks or semicolons
%';'
fprintf('######### Matrix definitions #########')
A = [1 2 3
    4 5 6]            % Define 2x3 matrix [A]

B = [1 0 1 ; 0 1 0]            % Define 2x3 matrix [B]


fprintf('######### Matrix operations ########\n')
fprintf('## Addition\n')
fprintf('C = A + B\n ')
C = A+B                % Defines C equal to addition of A & B

fprintf('## Subtraction\nD=A-B\n')
D = A-B                % A-B

fprintf('\nE = B - A\n')
E = B-A                % B-A

fprintf('## Scalar (S) multiplication S=2\n')
s = 2;                 % Scalar ";" suppresses screen output
fprintf('F = s * A\n')
F = s*A                % Multiplies scalar (s) * matrix [A]

fprintf('Redifine [B]\n')
B = [1 4 7
    2 5 8
    3 6 9]            % Redefines B to a 3 x 3 matrix

%%
fprintf('## Dot product (A · B = C)\n')
fprintf('C=A*B\n')
C = A*B                % Multiplication of [A] and [B]
fprintf('*Note: B · A not possible as dimensions don''t match\n')
% Note: B*A not possible as dimensions don't match

fprintf('\n## Transpose A^T\n')
fprintf('A = \n')
disp(A)

fprintf('A^T=\n')
AT = A';               % Transposes matrix A
disp(AT);

fprintf('## Transpose Equivalency\n')
fprintf('O = A * B^T - B^T * A\n')
O = (A*B)' - B'*A'    % Transpose equivalency

fprintf('## Identity matrix [I]\n')
I = eye(3,3)           % 3x3 Identity matrix
%% Solving Linear System of Equations
fprintf('########## Solving linear systems of equations #########\n')
fprintf(' ###   [A] * {x} = {B}  ###\n')



A = [3 2 1
    2 4 3
    1 3 5]            % Redefine A to system of equation coefficients


B = [10;19;22;]        % Create right-hand-side column vector

fprintf('##### Solving directly with matrix inversion (Computationally expensive)\n')
fprintf('X = A^-1 * B\n')
tic
x = inv(A)*B           % Solves linear system by inversion ("expensive")
inverse = toc;
fprintf('Time to solve: %.6f s\n', inverse)

fprintf('##### Solving with backslash operator\n')
fprintf('X = A \\ B\n')
tic
X = A\B                % Solves linear system by factorization ("cheaper")
backslash = toc;
fprintf('Time to solve: %.6f s\n', backslash)

%% Additional Info
fprintf('########## Additional Info ##########\n')
fprintf('### Orthogonal matrix\n')
Q = [0.8 0.6 0
    -0.6 0.8 0
    0   0 1]        % Orthogonal matrix

fprintf('\nnull = Q^-1 - Q^T\n')
null = inv(Q)-Q'       % Orthogonal equivalency
% down to double precision 10^-16
fprintf('Note double precision round-off error 1e-16\n')

fprintf('\n#### Using a loop to construct an array\n')
fprintf('Preallocate with zeros matrix\n')
A = zeros(3,3)        % Pre-allocation of 3x3 matrix space

for i = 1:3            % Construction of A matrix using for loops
    for j = 1:3
        
        A(i,j) = i+2*j;
        fprintf('A(%d, %d) = %d\n', i, j, A(i,j))
        
    end
end

A

