% ---
% jupyter:
%   jupytext:
%     text_representation:
%       extension: .m
%       format_name: light
%       format_version: '1.5'
%       jupytext_version: 1.14.4
%   kernelspec:
%     display_name: Octave
%     language: octave
%     name: octave
% ---

% ### Linear dependence and matrices
%
% To determine if a set of matrices is linearly dependent/independent, enter each matrix as a column into a larger matrix.
%
% Given this set of matrices:
%
% $ \left( 
%     \left[ \begin{matrix} 2 & -1 \\ 4 & 6 \end{matrix} \right], 
%     \left[ \begin{matrix} 3 & 2 \\ 8 & 3 \end{matrix} \right],
%     \left[ \begin{matrix} -5 & -8 \\ -16 & 4 \end{matrix} \right],
%     \left[ \begin{matrix} 0 & -7 \\ -4 & 13 \end{matrix} \right]
% \right) $
%
% And some scalars $ a,b,c,d \in R $
%
% To assume linear indepedence means $ a,b,c,d = 0 $, such that:
%
% $
%     a\left[ \begin{matrix} 2 & -1 \\ 4 & 6 \end{matrix} \right] + 
%     b\left[ \begin{matrix} 3 & 2 \\ 8 & 3 \end{matrix} \right] +
%     c\left[ \begin{matrix} -5 & -8 \\ -16 & 4 \end{matrix} \right] +
%     d\left[ \begin{matrix} 0 & -7 \\ -4 & 13 \end{matrix} \right] =
%     \left[ \begin{matrix} 0 & 0 \\ 0 & 0 \end{matrix} \right]
% $
%
% Then treat this as a system of equations. Consider the top left element of each matrix, then:
%
% $ 2a + 3b + -5c + 0d = 0 $
%
% Top right:
%
% $ -1a + 2b -8c -7d = 0 $
%
% Bottom left:
%
% $ 4a + 8b -16c -4d = 0 $
%
% Bottom right:
%
% $ 6a + 3b + 4c + 13d = 0 $

# To do the same in Octave, starting with the same matrices:
M_1 = [2 -1; 4 6]
M_2 = [3 2; 8 3]
M_3 = [-5 -8; -16 4]
M_4 = [0 -7; -4 13]

# Then enter each matrix as a row
M = [];
M(:,1) = M_1'(:)
M(:,2) = M_2'(:)
M(:,3) = M_3'(:)
M(:,4) = M_4'(:)

% For completeness, we can add the indentity matrix in there, so we need to augment the matrix with a zero vector. The definition of linear dependence requires an equality with 0. It's not strictly required to find the answer but it is helpful to remember what we're trying to do.

M(:,5) = zeros(4,1)

% And now we can reduce this via row operations.

M(4,:) = M(4,:) + M(3,:) *-1;

M(4,:) = M(4,:) + M(1,:) *-1;

M(3,:) = M(3,:) + M(1,:) *-2;

M(1,:) = M(1,:) + M(2,:);

M(2,:) = M(2,:) + M(1,:);

M(4,:) = M(4,:) + M(3,:) *4;

M(2,:) = M(2,:) + M(3,:) *-3;

M(3,:) = M(3,:) * 1/2

% With two rows the same, we can disregard a row. That means that the final colum, which represents the scalar $ d $ can have any value. This will require a non-trivial solution and therefore, these matrices are not linearly independent.

StarcoderdataJupyterScriptsDedupFiltered