% https://www.mathworks.com/help/matlab/linear-algebra.html
% Matrix Operations
clear % clear all variables
help det

% Parte 1 - Casos básicos

M = [5]
test_result_det(1,1) = (det(M) == 5)

M = [1 2; 3 4]
test_result_det(2,1) = (det(M) == -2)

M = eye(3)
test_result_det(3,1) = (det(M) == 1)

M = [2,0,0;0,3,0;0,0,4]
test_result_det(4,1) = (det(M) == 24)

% Parte 2 - Matrizes triangulares

M = [2 3 1; 0 4 5; 0 0 6]
test_result_det(5,1) = (det(M) == 48)

M = [2 0 0; 3 4 0; 1 5 6]
test_result_det(6,1) = (det(M) == 48)

% Parte 3 - Matrizes singulares

M = [1 2 3; 1 2 3; 4 5 6]
test_result_det(7,1) = (det(M) == 0)

M = [1 2 3; 4 5 6; 7 8 9]
test_result_det(8,1) = (det(M) == 0)

% Parte 4 - Determinante negativo e genérico

M = [0 1; 1 0]
test_result_det(9,1) = (det(M) == -1)

M = [3 2 1; 1 1 1; 0 2 4]
test_result_det(10,1) = (det(M) == 0)

% Parte 5 - Números complexos

M = [2+i 3; 1 4]
expected = (2+i)*4 - 3*1
test_result_det(11,1) = (det(M) == expected)

M = [i 1; 1 i]
expected = i*i - 1*1   % (-1 - 1) = -2
test_result_det(12,1) = (det(M) == -2)

% Parte 6 - Matrizes maiores

M = [1 2 3 4; 2 3 4 5; 1 1 1 1; 0 1 0 1]
test_result_det(13,1) = (det(M) == 0)

% Parte 7 - Valores decimais

M = [1.5 2.5; 3.5 4.5]
expected = 1.5*4.5 - 2.5*3.5
test_result_det(14,1) = (abs(det(M) - expected) < 1e-12)

% Parte 8 - Matriz vazia (0x0)
M = zeros(0,0)
test_result_det(15,1) = (det(M) == 1)

% Parte 9 - Matriz não quadrada (erro esperado)
M = [1 2 3; 4 5 6]
det(M) % error
test_result_det(16,1) = true

% Parte 10 - Matrizes 4x4 com estrutura simples

M = [1 2 3 4
     5 6 7 8
     2 6 4 8
     3 1 1 2]
test_result_det(17,1) = (det(M) == 72)

M = [4 3 2 1
     0 1 2 3
     5 6 7 8
     1 0 1 0]
test_result_det(18,1) = (det(M) == 0)

% Parte 11 - Matriz 5x5 diagonal com elementos diferentes

M = [
    2 0 0 0 0
    0 3 0 0 0
    0 0 5 0 0
    0 0 0 7 0
    0 0 0 0 11
]
test_result_det(19,1) = (det(M) == 2*3*5*7*11)

% Parte 12 - Matriz 5x5 triangular superior

M = [1 2 3 4 5
     0 2 3 4 5
     0 0 3 4 5
     0 0 0 4 5
     0 0 0 0 5]
test_result_det(20,1) = (det(M) == 1*2*3*4*5)

% Parte 13 - Matriz simétrica com determinante negativo

M = [2 1 0
     1 2 1
     0 1 2]
test_result_det(21,1) = (det(M) == 4)

M = [1 2 3
     2 1 3
     3 3 1]
test_result_det(22,1) = (det(M) == 15)

% Parte 14 - Matrizes com frações e sinais

M = [1/2 1/3
     1/4 1/5]
expected = (1/2)*(1/5) - (1/3)*(1/4)
test_result_det(23,1) = (abs(det(M) - expected) < 1e-12)

M = [-1 2
     3 -4]
expected = (-1)*(-4) - (2*3)
test_result_det(24,1) = (det(M) == expected)

% Parte 15 - Matrizes complexas 3x3

M = [1+i  2  3
     0    4+i  5
     0    0    6+i]
expected = (1+i)*(4+i)*(6+i)
test_result_det(25,1) = (det(M) == expected)

M = [2  i  0
     0  2  i
     i  0  2]
test_result_det(26,1) = (det(M) == 8 - i)

% Parte 16 - Matrizes com dependência linear

M = [1 2 3
     2 4 6
     3 6 9]
test_result_det(27,1) = (det(M) == 0)

M = [1 2 3 4
     2 4 6 8
     3 6 9 12
     4 8 12 16]
test_result_det(28,1) = (det(M) == 0)

% Parte 17 - Matriz identidade com pequeno ruído

M = eye(3)
M(1,3) = 0.000001
M(3,1) = -0.000002
expected = 1 + M(1,3)*M(3,1)  % próximo de 1
test_result_det(29,1) = (abs(det(M) - expected) < 1e-10)

% Parte 18 - Matriz 4x4 blocada (bloco 2x2 repetido)

B = [1 2; 3 4]
M = [B zeros(2)
     zeros(2) B]
test_result_det(30,1) = (det(M) == (det(B)^2))

complete_test_result_det = all(test_result_det)

complete_test_result_det

help inv
% # `inv` extended test set

% 1. Identity matrix
M = eye(3)
R = inv(M)
test_result_inv(1,1) = all(all(M == R))

% 2. Permutation matrix
M = [0 0 1; 0 1 0; 1 0 0]
R = inv(M)
test_result_inv(2,1) = all(all(M*R == eye(3)))

% 3. Diagonal matrix
M = [2,0,0;0,3,0;0,0,4]
R = inv(M)
test_result_inv(3,1) = all(all(M*R == eye(3)))

% 4. Upper triangular matrix
M = [1 2 3; 0 4 5; 0 0 6]
R = inv(M)
test_result_inv(4,1) = all(all(M*R == eye(3)))

% 5. Lower triangular matrix
M = [1 0 0; 2 3 0; 4 5 6]
R = inv(M)
test_result_inv(5,1) = all(all(M*R == eye(3)))

% 6. Full 3x3 matrix
M = [1 2 3; 0 1 4; 5 6 0]
R = inv(M)
test_result_inv(6,1) = all(all(M*R == eye(3)))

% 7. Singular matrix (determinant zero)
M = [1 2 3; 2 4 6; 3 6 9]
R = inv(M)
INF3x3 = [Inf,Inf,Inf;Inf,Inf,Inf;Inf,Inf,Inf]
test_result_inv(7,1) = all(all(R == INF3x3))

% 8. Complex matrix (non-singular)
M = [1+1i 2 3; 0 1+2i 4; 5 6 1-1i]
R = inv(M)
test_result_inv(8,1) = all(all(M*R == eye(3)))

% 9. Random 4x4 matrix
M = [4 3 2 1; 0 1 2 3; 1 0 1 2; 2 3 0 1]
R = inv(M)
test_result_inv(9,1) = all(all(M*R == eye(4)))

% 10. 2x2 matrix
M = [2 3; 1 4]
R = inv(M)
test_result_inv(10,1) = all(all(M*R == eye(2)))

% 11. 5x5 upper triangular matrix
M = [1 2 3 4 5; 0 6 7 8 9; 0 0 2 3 4; 0 0 0 5 6; 0 0 0 0 7]
R = inv(M)
test_result_inv(11,1) = all(all(M*R == eye(5)))

% 12. 5x5 lower triangular matrix
M = [2 0 0 0 0; 3 3 0 0 0; 4 5 4 0 0; 6 7 8 5 0; 9 10 11 12 6]
R = inv(M)
test_result_inv(12,1) = all(all(M*R == eye(5)))

% 13. 5x5 dense real matrix
M = [5 4 3 2 1; 0 1 3 4 5; 6 7 8 9 1; 1 0 1 0 1; 2 3 4 5 6]
R = inv(M)
INF5x5 = [Inf,Inf,Inf,Inf,Inf;Inf,Inf,Inf,Inf,Inf;Inf,Inf,Inf,Inf,Inf;Inf,Inf,Inf,Inf,Inf;Inf,Inf,Inf,Inf,Inf]
test_result_inv(13,1) = all(all(R == INF5x5))

% 14. 3x3 complex Hermitian matrix
M = [2+0i 1-1i 0; 1+1i 3+0i 2; 0 2 4+0i]
R = inv(M)
test_result_inv(14,1) = all(all(M*R == eye(3)))

% 15. Nearly singular matrix (small determinant)
M = [1 1 1; 1.0000001 1 1; 1 1.0000001 1]
R = inv(M)
test_result_inv(15,1) = all(all(abs(M*R - eye(3)) < 1e-16))

% 16. Block-diagonal matrix
M = [2,1,0,0;1,3,0,0;0,0,4,0;0,0,0,5]
R = inv(M)
test_result_inv(16,1) = all(all(M*R == eye(4)))

% 17. Complex 4x4 random-like matrix
M = [1+2i 2 3 4; 0 1+3i 2 1; 3 0 2-1i 1; 2 1 0 1+1i]
R = inv(M)
test_result_inv(17,1) = all(all(M*R == eye(4)))

% 18. Integer 6x6 invertible matrix
M = [1 2 3 4 5 6; 0 1 2 3 4 5; 1 0 1 2 3 4; 2 1 0 1 2 3; 3 2 1 0 1 2; 4 3 2 1 0 1]
R = inv(M)
test_result_inv(18,1) = all(all(M*R == eye(6)))

% 19. Singular 4x4 (repeated rows)
M = [1 2 3 4; 2 3 4 5; 1 2 3 4; 2 3 4 5]
R = inv(M)
INF4x4 = [Inf,Inf,Inf,Inf;Inf,Inf,Inf,Inf;Inf,Inf,Inf,Inf;Inf,Inf,Inf,Inf]
test_result_inv(19,1) = all(all(R == INF4x4))

% 20. Complex diagonal matrix
M = [1+1i, 0, 0, 0; 0, 2-2i, 0, 0; 0, 0, 3+0i, 0; 0, 0, 0, 4-1i]
R = inv(M)
test_result_inv(20,1) = all(all(M*R == eye(4)))

% 21. Negative-valued real matrix
M = [-1 -2 -3; -4 -5 -6; -7 -8 -10]
R = inv(M)
test_result_inv(21,1) = all(all(abs(M*R - eye(3)) < 1e-12))

% 22. Pure imaginary diagonal matrix
M = [1i,0,0;0,2i,0;0,0,3i]
R = inv(M)
test_result_inv(22,1) = all(all(M*R == eye(3)))

% 23. Symmetric matrix
M = [2 -1 0; -1 2 -1; 0 -1 2]
R = inv(M)
test_result_inv(23,1) = all(all(M*R == eye(3)))

% 24. Anti-symmetric matrix (should be invertible only if odd dimension)
M = [0 -1 2; 1 0 -3; -2 3 0]
R = inv(M)
INF3x3 = [Inf,Inf,Inf;Inf,Inf,Inf;Inf,Inf,Inf]
test_result_inv(24,1) = all(all(R == INF3x3))

% 25. Scaled matrix with large/small magnitudes
M = [1e-6 2 3; 4 1e6 6; 7 8 9]
R = inv(M)
test_result_inv(25,1) = all(all(M*R == eye(3)))

% Final aggregated result
complete_test_result_inv = all(test_result_inv)

complete_test_result_inv

help lu

% 1. LU decomposition of a 3x3 matrix

M = [2 1 1; 4 -6 0; -2 7 2]
[L, U, P] = lu(M)
RL = [1    0   0
     0.5   1   0
    -0.5   1   1]
RU = [4  -6   0
     0   4   1
     0   0   1]
RP = [0 1 0
     1 0 0
     0 0 1]
test_result_lu(1,1) = all(all(RL == L & RU == U & RP == P & P*L*U == M))

% 2. Identity matrix
M = eye(3)
[L, U, P] = lu(M)
test_result_lu(2,1) = all(all(L == eye(3) & U == eye(3) & P == eye(3) & P*L*U == M))

% 3. Upper triangular matrix
M = [3 2 1; 0 5 4; 0 0 2]
[L, U, P] = lu(M)
test_result_lu(3,1) = all(all(L == eye(3) & U == M & P == eye(3) & P*L*U == M))

% 4. Lower triangular matrix
M = [1 0 0; 3 2 0; 5 -1 4]
[L, U, P] = lu(M)
test_result_lu(4,1) = all(all(P*L*U == M))

% 5. Singular matrix (determinant equal to zero)
M = [1 2 3; 2 4 6; 1 1 1]
[L, U, P] = lu(M)
test_result_lu(5,1) = all(all(P*M == L*U))

% 6. Matrix with permutation (singular)
M = [0 2 1; 1 1 1; 2 1 0]
[L, U, P] = lu(M)
test_result_lu(6,1) = all(all(P*M == L*U))

% 7. Symmetric matrix
M = [4 2 2; 2 10 4; 2 4 6]
[L, U, P] = lu(M)
test_result_lu(7,1) = all(all(P*L*U == M))

% 8. Negative and fractional elements
M = [-2 3 -1; 1.5 -4 2; 0 5 -3]
[L, U, P] = lu(M)
test_result_lu(8,1) = all(all(P*L*U == M))

% 9. Diagonal matrix
M = [2 0 0
     0 5 0
     0 0 7]
[L, U, P] = lu(M)
test_result_lu(9,1) = all(all(L == eye(3) & U == M & P == eye(3) & P*L*U == M))

% 10. Matrix with zeros inside
M = [1 0 2; 0 3 0; 4 0 5]
[L, U, P] = lu(M)
L*U
test_result_lu(10,1) = all(all(P*L*U == M))
test_result_lu(11,1) = all(all(P*M == L*U))
test_result_lu(12,1) = all(all((L*U) == inv(P)*M))

% Result of complete test
complete_test_result_lu = all(test_result_lu)

% Show test result
complete_test_result_lu

% Test Linear algebra functions and operations.
A = [1,2,0;1,1,0;-1,4,0]
B = [1,2,3;1,1,-1;2,2,2]
C = [1,2,3;1,1,-1;1,1,1]
% Trace
trace(A) % 2
trace(B) % 4
trace(C) % 3
% Transpose
A.'
A'
B.'
B'
% Matrix multiplication
D = A*B
E = A*C
D == E % Even though B is different from C, the product A*B and A*C is the same
% Determinant
A = [1:3;4:6;7:9]
B = [2, 1, -3; 3, 2, 4; 2, 5, -2]
C = [-7,6,3,1; -9,1,6,4; -8,-4,3,6; -5,5,9,2]
D = [1,2,1;0,3,2;0,0,1]
E = [2,1,3,0;5,1,3,2;4,2,5,1;1,2,3,7]
F = [2,1,-3;-1,3,2;3,1,-3]
det(A) % 0
det(B) % -67
det(C) % -444
det(D) % 3
det(E) % 18
det(F) % 11
% Inverse
inv(A)
inv(B)
inv(C)
inv(D)
inv(E)
inv(F)
% Gauss function
A=[2,1,-3;-1,3,2;3,1,-3]
X=[-1,12,0]
gauss(A,X)
X=[-1;12;0]
gauss(A,X)
A=[1,1;2,1]
X=[10,16]
gauss(A,X)
A=[1,1,1;2,1,2;1,2,3]
X=[6,10,14]
gauss(A,X)
% LU decomposition
F = [1,2,8; 4,4,6; 1,8,9]
[L,U,P] = lu(F)
P*F
L*U
P*F==L*U
G = [7,12,4; 21,33,6; 17,8,33]
[L,U,P] = lu(G)
P*G==L*U
H = [1,2,8; 4,9,6; 2,5,3]
[L,U,P] = lu(H)
P*H==L*U
K = [2,2,8; 4,4,6; 1,8,9]
[L,U,P] = lu(K)
P*K==L*U

help transpose % Transpose vector or matrix
help ctranspose % Complex conjugate transpose
help pagetranspose % Page-wise transpose (Since R2020b)
help pagectranspose % Page-wise complex conjugate transpose (Since R2020b)
help mtimes % Matrix multiplication
% Test matrix multiplication.
% Configure for Block Multiplication Test
configure('blockThreshold', 1e2) % (≈ 10×10)
configure('blockSize', 8)
getconfig() % Show configuration

% 1. Scalar × Scalar (function syntax)
A = 5
B = 3
R = 15
test_result_mtimes(1,1) = all(R == mtimes(A, B))

% 2. Scalar × Matrix (operator syntax)
A = 5
B = [1 2; 3 4]
R = [5 10; 15 20]
test_result_mtimes(2,1) = all(all(R == A * B))

% 3. Matrix × Scalar (function syntax)
A = [1 2; 3 4]
B = 5
R = [5 10; 15 20]
test_result_mtimes(3,1) = all(all(R == mtimes(A, B)))

% 4. Zero matrix multiplication
A = zeros(3,3)
B = zeros(3,3)
R = zeros(3,3)
test_result_mtimes(4,1) = all(all(R == A * B))

% 5. Identity matrix multiplication (operator syntax)
I = eye(3)
A = [2 4 6; 1 3 5; 7 8 9]
R1 = A * I
R2 = I * A
test_result_mtimes(5,1) = all(all(R1 == A & R2 == A))

% 6. Rectangular compatible matrices (operator syntax)
A = [1 2 3; 4 5 6]
B = [7 8; 9 10; 11 12]
R = [58 64; 139 154]
test_result_mtimes(6,1) = all(all(R == A * B))

% 7. Vector row × vector column (function syntax)
A = [1 2 3]
B = [4; 5; 6]
R = 32
test_result_mtimes(7,1) = all(all(R == mtimes(A, B)))

% 8. Vector column × vector row (operator syntax)
A = [1; 2; 3]
B = [4 5 6]
R = [4 5 6; 8 10 12; 12 15 18]
test_result_mtimes(8,1) = all(all(R == A * B))

% 9. Error: inner dimensions mismatch (operator syntax)
A = [1 2; 3 4]
B = [1 2 3]
R = A * B % error

% 10. Error: non-2-D operand (function syntax)
A = cat(3, [1 2;3 4], [5 6;7 8])
B = [1 2;3 4]
R = mtimes(A, B)   % error

% 11. Error: both operands non-2-D (operator syntax)
A = cat(3, [1 2;3 4], [5 6;7 8])
B = cat(3, [1 2;3 4], [5 6;7 8])
R = A * B          % error

% 12. Error: columns of A ≠ rows of B (function syntax)
A = [1 2 3]
B = [1 2; 3 4]
R = mtimes(A, B)   % error

% 13. Zero-dimension edge case (operator syntax)
A = zeros(2,0)
B = zeros(0,3)
R = zeros(2,3)
test_result_mtimes(9,1) = all(all(R == A * B))

% 14. Scalar and vector combination (function syntax)
A = 4
B = [1 2 3]
R = [4 8 12]
test_result_mtimes(10,1) = all(all(R == mtimes(A, B)))

% 15. Scalar and matrix identity case (operator syntax)
A = eye(2)
B = 3
R = [3 0; 0 3]
test_result_mtimes(11,1) = all(all(R == A * B))

% 16. Two vector operand non-2-D (function call)
A = [1;2]
B = [3 4 5]
R = [3,4,5;6,8,10]
test_result_mtimes(12,1) = all(all(R == mtimes(A, B)))

% 17. Row vector × Column vector resulting scalar
A = [2 4 6]
B = [1; 0; -1]
R = (2*1 + 4*0 + 6*(-1))
test_result_mtimes(13,1) = all(all(R == A * B))

% 18. Column vector × Row vector resulting full matrix
A = [1; 0; -1]
B = [2 4 6]
R = [2 4 6; 0 0 0; -2 -4 -6]
test_result_mtimes(14,1) = all(all(R == A * B))

% 19. Scalar × Row vector (function syntax)
A = 10
B = [1 2 3]
R = [10 20 30]
test_result_mtimes(15,1) = all(all(R == mtimes(A, B)))

% 20. Row vector × Scalar (operator syntax)
A = [1 2 3]
B = 10
R = [10 20 30]
test_result_mtimes(16,1) = all(all(R == A * B))

% 21. Matrix × Identity (function syntax)
A = [4 5; 6 7]
I = eye(2)
R = mtimes(A, I)
test_result_mtimes(17,1) = all(all(R == A))

% 22. Identity × Matrix (operator syntax)
A = [4 5; 6 7]
I = eye(2)
R = I * A
test_result_mtimes(18,1) = all(all(R == A))

% 23. Matrix × Zero matrix
A = [1 2; 3 4]
Z = zeros(2,2)
R = zeros(2,2)
test_result_mtimes(19,1) = all(all(R == A * Z))

% 24. Zero matrix × Matrix
A = [1 2; 3 4]
Z = zeros(2,2)
R = zeros(2,2)
test_result_mtimes(20,1) = all(all(R == Z * A))

% 25. Tall × Wide matrix (valid inner dimension)
A = [1; 2; 3]
B = [4 5 6]
R = [4 5 6; 8 10 12; 12 15 18]
test_result_mtimes(21,1) = all(all(R == A * B))

% 26. Wide × Tall matrix (valid inner dimension)
A = [1 2 3]
B = [4; 5; 6]
R = 32
test_result_mtimes(22,1) = all(all(R == A * B))

% 27. Mixed sign and zero elements
A = [0 -1 2; 3 0 -3]
B = [1 2; 0 -1; -1 0]
R = [-2 1; 6 6]
test_result_mtimes(23,1) = all(all(R == A * B))

% 28. Error: left operand has more than 2 dimensions
A = cat(3, [1 2; 3 4], [5 6; 7 8])
B = [1 2; 3 4]
R = A * B   % error

% 29. Error: right operand has more than 2 dimensions
A = [1 2; 3 4]
B = cat(3, [1 2; 3 4], [5 6; 7 8])
R = mtimes(A, B)   % error

% 30. Error: inner dimensions mismatch (3×2 * 3×3)
A = [1 2; 3 4; 5 6]
B = [1 2 3; 4 5 6; 7 8 9]
R = A * B   % error

% 31. Large square real matrices (16×16 × 16×16)
A = reshape(1:256, 16, 16)
B = reshape(257:512, 16, 16)
R = [
517512,548488,579464,610440,641416,672392,703368,734344,765320,796296,827272,858248,889224,920200,951176,982152;
521744,552976,584208,615440,646672,677904,709136,740368,771600,802832,834064,865296,896528,927760,958992,990224;
525976,557464,588952,620440,651928,683416,714904,746392,777880,809368,840856,872344,903832,935320,966808,998296;
530208,561952,593696,625440,657184,688928,720672,752416,784160,815904,847648,879392,911136,942880,974624,1006368;
534440,566440,598440,630440,662440,694440,726440,758440,790440,822440,854440,886440,918440,950440,982440,1014440;
538672,570928,603184,635440,667696,699952,732208,764464,796720,828976,861232,893488,925744,958000,990256,1022512;
542904,575416,607928,640440,672952,705464,737976,770488,803000,835512,868024,900536,933048,965560,998072,1030584;
547136,579904,612672,645440,678208,710976,743744,776512,809280,842048,874816,907584,940352,973120,1005888,1038656;
551368,584392,617416,650440,683464,716488,749512,782536,815560,848584,881608,914632,947656,980680,1013704,1046728;
555600,588880,622160,655440,688720,722000,755280,788560,821840,855120,888400,921680,954960,988240,1021520,1054800;
559832,593368,626904,660440,693976,727512,761048,794584,828120,861656,895192,928728,962264,995800,1029336,1062872;
564064,597856,631648,665440,699232,733024,766816,800608,834400,868192,901984,935776,969568,1003360,1037152,1070944;
568296,602344,636392,670440,704488,738536,772584,806632,840680,874728,908776,942824,976872,1010920,1044968,1079016;
572528,606832,641136,675440,709744,744048,778352,812656,846960,881264,915568,949872,984176,1018480,1052784,1087088;
576760,611320,645880,680440,715000,749560,784120,818680,853240,887800,922360,956920,991480,1026040,1060600,1095160;
580992,615808,650624,685440,720256,755072,789888,824704,859520,894336,929152,963968,998784,1033600,1068416,1103232
]
test_result_mtimes(24,1) = all(all(R == A * B))


% 32. Large rectangular (20×10 × 10×30)
A = reshape(1:200, 20, 10)
B = reshape(1:300, 10, 30)
R = [
6655,15755,24855,33955,43055,52155,61255,70355,79455,88555,97655,106755,115855,124955,134055,143155,152255,161355,170455,179555,188655,197755,206855,215955,225055,234155,243255,252355,261455,270555;
6710,15910,25110,34310,43510,52710,61910,71110,80310,89510,98710,107910,117110,126310,135510,144710,153910,163110,172310,181510,190710,199910,209110,218310,227510,236710,245910,255110,264310,273510;
6765,16065,25365,34665,43965,53265,62565,71865,81165,90465,99765,109065,118365,127665,136965,146265,155565,164865,174165,183465,192765,202065,211365,220665,229965,239265,248565,257865,267165,276465;
6820,16220,25620,35020,44420,53820,63220,72620,82020,91420,100820,110220,119620,129020,138420,147820,157220,166620,176020,185420,194820,204220,213620,223020,232420,241820,251220,260620,270020,279420;
6875,16375,25875,35375,44875,54375,63875,73375,82875,92375,101875,111375,120875,130375,139875,149375,158875,168375,177875,187375,196875,206375,215875,225375,234875,244375,253875,263375,272875,282375;
6930,16530,26130,35730,45330,54930,64530,74130,83730,93330,102930,112530,122130,131730,141330,150930,160530,170130,179730,189330,198930,208530,218130,227730,237330,246930,256530,266130,275730,285330;
6985,16685,26385,36085,45785,55485,65185,74885,84585,94285,103985,113685,123385,133085,142785,152485,162185,171885,181585,191285,200985,210685,220385,230085,239785,249485,259185,268885,278585,288285;
7040,16840,26640,36440,46240,56040,65840,75640,85440,95240,105040,114840,124640,134440,144240,154040,163840,173640,183440,193240,203040,212840,222640,232440,242240,252040,261840,271640,281440,291240;
7095,16995,26895,36795,46695,56595,66495,76395,86295,96195,106095,115995,125895,135795,145695,155595,165495,175395,185295,195195,205095,214995,224895,234795,244695,254595,264495,274395,284295,294195;
7150,17150,27150,37150,47150,57150,67150,77150,87150,97150,107150,117150,127150,137150,147150,157150,167150,177150,187150,197150,207150,217150,227150,237150,247150,257150,267150,277150,287150,297150;
7205,17305,27405,37505,47605,57705,67805,77905,88005,98105,108205,118305,128405,138505,148605,158705,168805,178905,189005,199105,209205,219305,229405,239505,249605,259705,269805,279905,290005,300105;
7260,17460,27660,37860,48060,58260,68460,78660,88860,99060,109260,119460,129660,139860,150060,160260,170460,180660,190860,201060,211260,221460,231660,241860,252060,262260,272460,282660,292860,303060;
7315,17615,27915,38215,48515,58815,69115,79415,89715,100015,110315,120615,130915,141215,151515,161815,172115,182415,192715,203015,213315,223615,233915,244215,254515,264815,275115,285415,295715,306015;
7370,17770,28170,38570,48970,59370,69770,80170,90570,100970,111370,121770,132170,142570,152970,163370,173770,184170,194570,204970,215370,225770,236170,246570,256970,267370,277770,288170,298570,308970;
7425,17925,28425,38925,49425,59925,70425,80925,91425,101925,112425,122925,133425,143925,154425,164925,175425,185925,196425,206925,217425,227925,238425,248925,259425,269925,280425,290925,301425,311925;
7480,18080,28680,39280,49880,60480,71080,81680,92280,102880,113480,124080,134680,145280,155880,166480,177080,187680,198280,208880,219480,230080,240680,251280,261880,272480,283080,293680,304280,314880;
7535,18235,28935,39635,50335,61035,71735,82435,93135,103835,114535,125235,135935,146635,157335,168035,178735,189435,200135,210835,221535,232235,242935,253635,264335,275035,285735,296435,307135,317835;
7590,18390,29190,39990,50790,61590,72390,83190,93990,104790,115590,126390,137190,147990,158790,169590,180390,191190,201990,212790,223590,234390,245190,255990,266790,277590,288390,299190,309990,320790;
7645,18545,29445,40345,51245,62145,73045,83945,94845,105745,116645,127545,138445,149345,160245,171145,182045,192945,203845,214745,225645,236545,247445,258345,269245,280145,291045,301945,312845,323745;
7700,18700,29700,40700,51700,62700,73700,84700,95700,106700,117700,128700,139700,150700,161700,172700,183700,194700,205700,216700,227700,238700,249700,260700,271700,282700,293700,304700,315700,326700
]
test_result_mtimes(25,1) = all(all(R == A * B))

% 33. Large rectangular transposed (10×20 × 20×10)
A = reshape(1:200, 10, 20)
B = reshape(201:400, 20, 10)
R = [
410810,449210,487610,526010,564410,602810,641210,679610,718010,756410;
415020,453820,492620,531420,570220,609020,647820,686620,725420,764220;
419230,458430,497630,536830,576030,615230,654430,693630,732830,772030;
423440,463040,502640,542240,581840,621440,661040,700640,740240,779840;
427650,467650,507650,547650,587650,627650,667650,707650,747650,787650;
431860,472260,512660,553060,593460,633860,674260,714660,755060,795460;
436070,476870,517670,558470,599270,640070,680870,721670,762470,803270;
440280,481480,522680,563880,605080,646280,687480,728680,769880,811080;
444490,486090,527690,569290,610890,652490,694090,735690,777290,818890;
448700,490700,532700,574700,616700,658700,700700,742700,784700,826700
]
test_result_mtimes(26,1) = all(all(R == A * B))

% 34. One operand complex, large 12×12
A = reshape(1:144, 12, 12) + 1i
B = reshape(145:288, 12, 12)
R = [
122718+1806i,132366+1950i,142014+2094i,151662+2238i,161310+2382i,170958+2526i,180606+2670i,190254+2814i,199902+2958i,209550+3102i,219198+3246i,228846+3390i;
124524+1806i,134316+1950i,144108+2094i,153900+2238i,163692+2382i,173484+2526i,183276+2670i,193068+2814i,202860+2958i,212652+3102i,222444+3246i,232236+3390i;
126330+1806i,136266+1950i,146202+2094i,156138+2238i,166074+2382i,176010+2526i,185946+2670i,195882+2814i,205818+2958i,215754+3102i,225690+3246i,235626+3390i;
128136+1806i,138216+1950i,148296+2094i,158376+2238i,168456+2382i,178536+2526i,188616+2670i,198696+2814i,208776+2958i,218856+3102i,228936+3246i,239016+3390i;
129942+1806i,140166+1950i,150390+2094i,160614+2238i,170838+2382i,181062+2526i,191286+2670i,201510+2814i,211734+2958i,221958+3102i,232182+3246i,242406+3390i;
131748+1806i,142116+1950i,152484+2094i,162852+2238i,173220+2382i,183588+2526i,193956+2670i,204324+2814i,214692+2958i,225060+3102i,235428+3246i,245796+3390i;
133554+1806i,144066+1950i,154578+2094i,165090+2238i,175602+2382i,186114+2526i,196626+2670i,207138+2814i,217650+2958i,228162+3102i,238674+3246i,249186+3390i;
135360+1806i,146016+1950i,156672+2094i,167328+2238i,177984+2382i,188640+2526i,199296+2670i,209952+2814i,220608+2958i,231264+3102i,241920+3246i,252576+3390i;
137166+1806i,147966+1950i,158766+2094i,169566+2238i,180366+2382i,191166+2526i,201966+2670i,212766+2814i,223566+2958i,234366+3102i,245166+3246i,255966+3390i;
138972+1806i,149916+1950i,160860+2094i,171804+2238i,182748+2382i,193692+2526i,204636+2670i,215580+2814i,226524+2958i,237468+3102i,248412+3246i,259356+3390i;
140778+1806i,151866+1950i,162954+2094i,174042+2238i,185130+2382i,196218+2526i,207306+2670i,218394+2814i,229482+2958i,240570+3102i,251658+3246i,262746+3390i;
142584+1806i,153816+1950i,165048+2094i,176280+2238i,187512+2382i,198744+2526i,209976+2670i,221208+2814i,232440+2958i,243672+3102i,254904+3246i,266136+3390i
]
test_result_mtimes(27,1) = all(all(R == A * B))

% 35. Both operands complex, large 15×15
A = reshape(1:225, 15, 15) + 1i * reshape(226:450, 15, 15)
B = reshape(451:675, 15, 15) - 2i
R = [
742350+2274990i,766200+2349465i,790050+2423940i,813900+2498415i,837750+2572890i,861600+2647365i,885450+2721840i,909300+2796315i,933150+2870790i,957000+2945265i,980850+3019740i,1004700+3094215i,1028550+3168690i,1052400+3243165i,1076250+3317640i;
749250+2281830i,773325+2356530i,797400+2431230i,821475+2505930i,845550+2580630i,869625+2655330i,893700+2730030i,917775+2804730i,941850+2879430i,965925+2954130i,990000+3028830i,1014075+3103530i,1038150+3178230i,1062225+3252930i,1086300+3327630i;
756150+2288670i,780450+2363595i,804750+2438520i,829050+2513445i,853350+2588370i,877650+2663295i,901950+2738220i,926250+2813145i,950550+2888070i,974850+2962995i,999150+3037920i,1023450+3112845i,1047750+3187770i,1072050+3262695i,1096350+3337620i;
763050+2295510i,787575+2370660i,812100+2445810i,836625+2520960i,861150+2596110i,885675+2671260i,910200+2746410i,934725+2821560i,959250+2896710i,983775+2971860i,1008300+3047010i,1032825+3122160i,1057350+3197310i,1081875+3272460i,1106400+3347610i;
769950+2302350i,794700+2377725i,819450+2453100i,844200+2528475i,868950+2603850i,893700+2679225i,918450+2754600i,943200+2829975i,967950+2905350i,992700+2980725i,1017450+3056100i,1042200+3131475i,1066950+3206850i,1091700+3282225i,1116450+3357600i;
776850+2309190i,801825+2384790i,826800+2460390i,851775+2535990i,876750+2611590i,901725+2687190i,926700+2762790i,951675+2838390i,976650+2913990i,1001625+2989590i,1026600+3065190i,1051575+3140790i,1076550+3216390i,1101525+3291990i,1126500+3367590i;
783750+2316030i,808950+2391855i,834150+2467680i,859350+2543505i,884550+2619330i,909750+2695155i,934950+2770980i,960150+2846805i,985350+2922630i,1010550+2998455i,1035750+3074280i,1060950+3150105i,1086150+3225930i,1111350+3301755i,1136550+3377580i;
790650+2322870i,816075+2398920i,841500+2474970i,866925+2551020i,892350+2627070i,917775+2703120i,943200+2779170i,968625+2855220i,994050+2931270i,1019475+3007320i,1044900+3083370i,1070325+3159420i,1095750+3235470i,1121175+3311520i,1146600+3387570i;
797550+2329710i,823200+2405985i,848850+2482260i,874500+2558535i,900150+2634810i,925800+2711085i,951450+2787360i,977100+2863635i,1002750+2939910i,1028400+3016185i,1054050+3092460i,1079700+3168735i,1105350+3245010i,1131000+3321285i,1156650+3397560i;
804450+2336550i,830325+2413050i,856200+2489550i,882075+2566050i,907950+2642550i,933825+2719050i,959700+2795550i,985575+2872050i,1011450+2948550i,1037325+3025050i,1063200+3101550i,1089075+3178050i,1114950+3254550i,1140825+3331050i,1166700+3407550i;
811350+2343390i,837450+2420115i,863550+2496840i,889650+2573565i,915750+2650290i,941850+2727015i,967950+2803740i,994050+2880465i,1020150+2957190i,1046250+3033915i,1072350+3110640i,1098450+3187365i,1124550+3264090i,1150650+3340815i,1176750+3417540i;
818250+2350230i,844575+2427180i,870900+2504130i,897225+2581080i,923550+2658030i,949875+2734980i,976200+2811930i,1002525+2888880i,1028850+2965830i,1055175+3042780i,1081500+3119730i,1107825+3196680i,1134150+3273630i,1160475+3350580i,1186800+3427530i;
825150+2357070i,851700+2434245i,878250+2511420i,904800+2588595i,931350+2665770i,957900+2742945i,984450+2820120i,1011000+2897295i,1037550+2974470i,1064100+3051645i,1090650+3128820i,1117200+3205995i,1143750+3283170i,1170300+3360345i,1196850+3437520i;
832050+2363910i,858825+2441310i,885600+2518710i,912375+2596110i,939150+2673510i,965925+2750910i,992700+2828310i,1019475+2905710i,1046250+2983110i,1073025+3060510i,1099800+3137910i,1126575+3215310i,1153350+3292710i,1180125+3370110i,1206900+3447510i;
838950+2370750i,865950+2448375i,892950+2526000i,919950+2603625i,946950+2681250i,973950+2758875i,1000950+2836500i,1027950+2914125i,1054950+2991750i,1081950+3069375i,1108950+3147000i,1135950+3224625i,1162950+3302250i,1189950+3379875i,1216950+3457500i
]
test_result_mtimes(28,1) = all(all(R == A * B))

% 36. Multiply by scalar (large matrix × scalar)
A = reshape(1:256, 16, 16)
R = [
2,34,66,98,130,162,194,226,258,290,322,354,386,418,450,482;
4,36,68,100,132,164,196,228,260,292,324,356,388,420,452,484;
6,38,70,102,134,166,198,230,262,294,326,358,390,422,454,486;
8,40,72,104,136,168,200,232,264,296,328,360,392,424,456,488;
10,42,74,106,138,170,202,234,266,298,330,362,394,426,458,490;
12,44,76,108,140,172,204,236,268,300,332,364,396,428,460,492;
14,46,78,110,142,174,206,238,270,302,334,366,398,430,462,494;
16,48,80,112,144,176,208,240,272,304,336,368,400,432,464,496;
18,50,82,114,146,178,210,242,274,306,338,370,402,434,466,498;
20,52,84,116,148,180,212,244,276,308,340,372,404,436,468,500;
22,54,86,118,150,182,214,246,278,310,342,374,406,438,470,502;
24,56,88,120,152,184,216,248,280,312,344,376,408,440,472,504;
26,58,90,122,154,186,218,250,282,314,346,378,410,442,474,506;
28,60,92,124,156,188,220,252,284,316,348,380,412,444,476,508;
30,62,94,126,158,190,222,254,286,318,350,382,414,446,478,510;
32,64,96,128,160,192,224,256,288,320,352,384,416,448,480,512
]
test_result_mtimes(29,1) = all(all(R == A * 2))

% 37. Multiply by scalar (scalar × large matrix)
A = reshape(1:256, 16, 16)
R = [
3,51,99,147,195,243,291,339,387,435,483,531,579,627,675,723;
6,54,102,150,198,246,294,342,390,438,486,534,582,630,678,726;
9,57,105,153,201,249,297,345,393,441,489,537,585,633,681,729;
12,60,108,156,204,252,300,348,396,444,492,540,588,636,684,732;
15,63,111,159,207,255,303,351,399,447,495,543,591,639,687,735;
18,66,114,162,210,258,306,354,402,450,498,546,594,642,690,738;
21,69,117,165,213,261,309,357,405,453,501,549,597,645,693,741;
24,72,120,168,216,264,312,360,408,456,504,552,600,648,696,744;
27,75,123,171,219,267,315,363,411,459,507,555,603,651,699,747;
30,78,126,174,222,270,318,366,414,462,510,558,606,654,702,750;
33,81,129,177,225,273,321,369,417,465,513,561,609,657,705,753;
36,84,132,180,228,276,324,372,420,468,516,564,612,660,708,756;
39,87,135,183,231,279,327,375,423,471,519,567,615,663,711,759;
42,90,138,186,234,282,330,378,426,474,522,570,618,666,714,762;
45,93,141,189,237,285,333,381,429,477,525,573,621,669,717,765;
48,96,144,192,240,288,336,384,432,480,528,576,624,672,720,768
]
test_result_mtimes(30,1) = all(all(R == 3 * A))

% 38. Identity matrix test (large)
A = reshape(1:225, 15, 15)
I = eye(15)
test_result_mtimes(31,1) = all(all(A == A * I))
test_result_mtimes(32,1) = all(all(A == I * A))

% 39. Zero matrix test (large)
A = reshape(1:225, 15, 15)
Z = zeros(15, 15)
test_result_mtimes(33,1) = all(all(Z == A * Z))
test_result_mtimes(34,1) = all(all(Z == Z * A))

% 40. BlockSize alignment test (block edges not multiple of 8)
A = reshape(1:210, 14, 15)
B = reshape(1:300, 15, 20)
R = [
15800,38075,60350,82625,104900,127175,149450,171725,194000,216275,238550,260825,283100,305375,327650,349925,372200,394475,416750,439025;
15920,38420,60920,83420,105920,128420,150920,173420,195920,218420,240920,263420,285920,308420,330920,353420,375920,398420,420920,443420;
16040,38765,61490,84215,106940,129665,152390,175115,197840,220565,243290,266015,288740,311465,334190,356915,379640,402365,425090,447815;
16160,39110,62060,85010,107960,130910,153860,176810,199760,222710,245660,268610,291560,314510,337460,360410,383360,406310,429260,452210;
16280,39455,62630,85805,108980,132155,155330,178505,201680,224855,248030,271205,294380,317555,340730,363905,387080,410255,433430,456605;
16400,39800,63200,86600,110000,133400,156800,180200,203600,227000,250400,273800,297200,320600,344000,367400,390800,414200,437600,461000;
16520,40145,63770,87395,111020,134645,158270,181895,205520,229145,252770,276395,300020,323645,347270,370895,394520,418145,441770,465395;
16640,40490,64340,88190,112040,135890,159740,183590,207440,231290,255140,278990,302840,326690,350540,374390,398240,422090,445940,469790;
16760,40835,64910,88985,113060,137135,161210,185285,209360,233435,257510,281585,305660,329735,353810,377885,401960,426035,450110,474185;
16880,41180,65480,89780,114080,138380,162680,186980,211280,235580,259880,284180,308480,332780,357080,381380,405680,429980,454280,478580;
17000,41525,66050,90575,115100,139625,164150,188675,213200,237725,262250,286775,311300,335825,360350,384875,409400,433925,458450,482975;
17120,41870,66620,91370,116120,140870,165620,190370,215120,239870,264620,289370,314120,338870,363620,388370,413120,437870,462620,487370;
17240,42215,67190,92165,117140,142115,167090,192065,217040,242015,266990,291965,316940,341915,366890,391865,416840,441815,466790,491765;
17360,42560,67760,92960,118160,143360,168560,193760,218960,244160,269360,294560,319760,344960,370160,395360,420560,445760,470960,496160
]
test_result_mtimes(35,1) = all(all(R == A * B))

% 41. Verify associativity: (A*B)*C == A*(B*C)
A = reshape(1:216, 12, 18)
B = reshape(217:342, 18, 7)
C = reshape(343:419, 7, 11)
R1 = (A * B) * C
R2 = A * (B * C)
test_result_mtimes(36,1) = all(all(R1 == R2))

% 42. Complex conjugate and real part check
A = (1:9)' + 1i * (9:-1:1)'
B = conj(A)'
R = [
-80+18i,-70+26i,-60+34i,-50+42i,-40+50i,-30+58i,-20+66i,-10+74i,82i;
-70+26i,-60+32i,-50+38i,-40+44i,-30+50i,-20+56i,-10+62i,68i,10+74i;
-60+34i,-50+38i,-40+42i,-30+46i,-20+50i,-10+54i,58i,10+62i,20+66i;
-50+42i,-40+44i,-30+46i,-20+48i,-10+50i,52i,10+54i,20+56i,30+58i;
-40+50i,-30+50i,-20+50i,-10+50i,50i,10+50i,20+50i,30+50i,40+50i;
-30+58i,-20+56i,-10+54i,52i,10+50i,20+48i,30+46i,40+44i,50+42i;
-20+66i,-10+62i,58i,10+54i,20+50i,30+46i,40+42i,50+38i,60+34i;
-10+74i,68i,10+62i,20+56i,30+50i,40+44i,50+38i,60+32i,70+26i;
82i,10+74i,20+66i,30+58i,40+50i,50+42i,60+34i,70+26i,80+18i
]
test_result_mtimes(37,1) = all(all(R == A * B))

% 43. Nonconformant dimensions (should error)
A = rand(12, 10)
B = rand(9, 12)
R = A * B   % error

% 44. Non-2D input (should error)
A = rand(4, 4, 2)
B = rand(4, 4, 2)
R = A * B   % error

% 45. Large complex rectangular (16×8 × 8×16)
A = reshape(1:128, 16, 8) + 1i
B = reshape(129:256, 8, 16) - 1i
R = [
61100+604i,64748+668i,68396+732i,72044+796i,75692+860i,79340+924i,82988+988i,86636+1052i,90284+1116i,93932+1180i,97580+1244i,101228+1308i,104876+1372i,108524+1436i,112172+1500i,115820+1564i;
62160+596i,65872+660i,69584+724i,73296+788i,77008+852i,80720+916i,84432+980i,88144+1044i,91856+1108i,95568+1172i,99280+1236i,102992+1300i,106704+1364i,110416+1428i,114128+1492i,117840+1556i;
63220+588i,66996+652i,70772+716i,74548+780i,78324+844i,82100+908i,85876+972i,89652+1036i,93428+1100i,97204+1164i,100980+1228i,104756+1292i,108532+1356i,112308+1420i,116084+1484i,119860+1548i;
64280+580i,68120+644i,71960+708i,75800+772i,79640+836i,83480+900i,87320+964i,91160+1028i,95000+1092i,98840+1156i,102680+1220i,106520+1284i,110360+1348i,114200+1412i,118040+1476i,121880+1540i;
65340+572i,69244+636i,73148+700i,77052+764i,80956+828i,84860+892i,88764+956i,92668+1020i,96572+1084i,100476+1148i,104380+1212i,108284+1276i,112188+1340i,116092+1404i,119996+1468i,123900+1532i;
66400+564i,70368+628i,74336+692i,78304+756i,82272+820i,86240+884i,90208+948i,94176+1012i,98144+1076i,102112+1140i,106080+1204i,110048+1268i,114016+1332i,117984+1396i,121952+1460i,125920+1524i;
67460+556i,71492+620i,75524+684i,79556+748i,83588+812i,87620+876i,91652+940i,95684+1004i,99716+1068i,103748+1132i,107780+1196i,111812+1260i,115844+1324i,119876+1388i,123908+1452i,127940+1516i;
68520+548i,72616+612i,76712+676i,80808+740i,84904+804i,89000+868i,93096+932i,97192+996i,101288+1060i,105384+1124i,109480+1188i,113576+1252i,117672+1316i,121768+1380i,125864+1444i,129960+1508i;
69580+540i,73740+604i,77900+668i,82060+732i,86220+796i,90380+860i,94540+924i,98700+988i,102860+1052i,107020+1116i,111180+1180i,115340+1244i,119500+1308i,123660+1372i,127820+1436i,131980+1500i;
70640+532i,74864+596i,79088+660i,83312+724i,87536+788i,91760+852i,95984+916i,100208+980i,104432+1044i,108656+1108i,112880+1172i,117104+1236i,121328+1300i,125552+1364i,129776+1428i,134000+1492i;
71700+524i,75988+588i,80276+652i,84564+716i,88852+780i,93140+844i,97428+908i,101716+972i,106004+1036i,110292+1100i,114580+1164i,118868+1228i,123156+1292i,127444+1356i,131732+1420i,136020+1484i;
72760+516i,77112+580i,81464+644i,85816+708i,90168+772i,94520+836i,98872+900i,103224+964i,107576+1028i,111928+1092i,116280+1156i,120632+1220i,124984+1284i,129336+1348i,133688+1412i,138040+1476i;
73820+508i,78236+572i,82652+636i,87068+700i,91484+764i,95900+828i,100316+892i,104732+956i,109148+1020i,113564+1084i,117980+1148i,122396+1212i,126812+1276i,131228+1340i,135644+1404i,140060+1468i;
74880+500i,79360+564i,83840+628i,88320+692i,92800+756i,97280+820i,101760+884i,106240+948i,110720+1012i,115200+1076i,119680+1140i,124160+1204i,128640+1268i,133120+1332i,137600+1396i,142080+1460i;
75940+492i,80484+556i,85028+620i,89572+684i,94116+748i,98660+812i,103204+876i,107748+940i,112292+1004i,116836+1068i,121380+1132i,125924+1196i,130468+1260i,135012+1324i,139556+1388i,144100+1452i;
77000+484i,81608+548i,86216+612i,90824+676i,95432+740i,100040+804i,104648+868i,109256+932i,113864+996i,118472+1060i,123080+1124i,127688+1188i,132296+1252i,136904+1316i,141512+1380i,146120+1444i
]
test_result_mtimes(38,1) = all(all(R == A * B))

% 46. Basic 2×2 matrices
A = [1 2; 3 4]
B = [5 6; 7 8]
R = [19 22; 43 50]
test_result_mtimes(39,1) = all(all(R == mtimes(A, B)))

% 47. Rectangular (3×2 × 2×4)
A = [1 2; 3 4; 5 6]
B = [7 8 9 10; 11 12 13 14]
R = [
29,32,35,38;
65,72,79,86;
101,112,123,134
]
test_result_mtimes(40,1) = all(all(R == mtimes(A, B)))

% 48. Complex 3×3 matrices
A = [1+1i 2-2i 3; 4 5+3i 6; 7-1i 8 9]
B = [9 8+1i 7; 6 5 4-2i; 3 2 1]
R = [
30-3i,23-i,14-5i;
84+18i,69+19i,60+2i;
138-9i,115-i,90-23i
]
test_result_mtimes(41,1) = all(all(R == mtimes(A, B)))

% 49. Large real 12×12 × 12×12
A = reshape(1:144, 12, 12)
B = reshape(145:288, 12, 12)
R = [
122718,132366,142014,151662,161310,170958,180606,190254,199902,209550,219198,228846;
124524,134316,144108,153900,163692,173484,183276,193068,202860,212652,222444,232236;
126330,136266,146202,156138,166074,176010,185946,195882,205818,215754,225690,235626;
128136,138216,148296,158376,168456,178536,188616,198696,208776,218856,228936,239016;
129942,140166,150390,160614,170838,181062,191286,201510,211734,221958,232182,242406;
131748,142116,152484,162852,173220,183588,193956,204324,214692,225060,235428,245796;
133554,144066,154578,165090,175602,186114,196626,207138,217650,228162,238674,249186;
135360,146016,156672,167328,177984,188640,199296,209952,220608,231264,241920,252576;
137166,147966,158766,169566,180366,191166,201966,212766,223566,234366,245166,255966;
138972,149916,160860,171804,182748,193692,204636,215580,226524,237468,248412,259356;
140778,151866,162954,174042,185130,196218,207306,218394,229482,240570,251658,262746;
142584,153816,165048,176280,187512,198744,209976,221208,232440,243672,254904,266136
]
test_result_mtimes(42,1) = all(all(R == mtimes(A, B)))

% 50. Multiply by scalar
A = reshape(1:100, 10, 10)
R = [
2,22,42,62,82,102,122,142,162,182;
4,24,44,64,84,104,124,144,164,184;
6,26,46,66,86,106,126,146,166,186;
8,28,48,68,88,108,128,148,168,188;
10,30,50,70,90,110,130,150,170,190;
12,32,52,72,92,112,132,152,172,192;
14,34,54,74,94,114,134,154,174,194;
16,36,56,76,96,116,136,156,176,196;
18,38,58,78,98,118,138,158,178,198;
20,40,60,80,100,120,140,160,180,200
]
test_result_mtimes(43,1) = all(all(R == mtimes(A, 2)))
R = [
3,33,63,93,123,153,183,213,243,273;
6,36,66,96,126,156,186,216,246,276;
9,39,69,99,129,159,189,219,249,279;
12,42,72,102,132,162,192,222,252,282;
15,45,75,105,135,165,195,225,255,285;
18,48,78,108,138,168,198,228,258,288;
21,51,81,111,141,171,201,231,261,291;
24,54,84,114,144,174,204,234,264,294;
27,57,87,117,147,177,207,237,267,297;
30,60,90,120,150,180,210,240,270,300
]
test_result_mtimes(44,1) = all(all(R == mtimes(3, A)))

% 51. Identity matrix test
A = reshape(1:64, 8, 8)
I = eye(8)
test_result_mtimes(45,1) = all(all(A == mtimes(A, I)))
test_result_mtimes(46,1) = all(all(A == mtimes(I, A)))

% 52. Zero matrix test
A = reshape(1:64, 8, 8)
Z = zeros(8, 8)
test_result_mtimes(47,1) = all(all(Z == mtimes(A, Z)))
test_result_mtimes(48,1) = all(all(Z == mtimes(I, Z)))

% 53. BlockSize alignment (non-multiple of 8)
A = reshape(1:210, 14, 15)
B = reshape(1:300, 15, 20)
R = [
15800,38075,60350,82625,104900,127175,149450,171725,194000,216275,238550,260825,283100,305375,327650,349925,372200,394475,416750,439025;
15920,38420,60920,83420,105920,128420,150920,173420,195920,218420,240920,263420,285920,308420,330920,353420,375920,398420,420920,443420;
16040,38765,61490,84215,106940,129665,152390,175115,197840,220565,243290,266015,288740,311465,334190,356915,379640,402365,425090,447815;
16160,39110,62060,85010,107960,130910,153860,176810,199760,222710,245660,268610,291560,314510,337460,360410,383360,406310,429260,452210;
16280,39455,62630,85805,108980,132155,155330,178505,201680,224855,248030,271205,294380,317555,340730,363905,387080,410255,433430,456605;
16400,39800,63200,86600,110000,133400,156800,180200,203600,227000,250400,273800,297200,320600,344000,367400,390800,414200,437600,461000;
16520,40145,63770,87395,111020,134645,158270,181895,205520,229145,252770,276395,300020,323645,347270,370895,394520,418145,441770,465395;
16640,40490,64340,88190,112040,135890,159740,183590,207440,231290,255140,278990,302840,326690,350540,374390,398240,422090,445940,469790;
16760,40835,64910,88985,113060,137135,161210,185285,209360,233435,257510,281585,305660,329735,353810,377885,401960,426035,450110,474185;
16880,41180,65480,89780,114080,138380,162680,186980,211280,235580,259880,284180,308480,332780,357080,381380,405680,429980,454280,478580;
17000,41525,66050,90575,115100,139625,164150,188675,213200,237725,262250,286775,311300,335825,360350,384875,409400,433925,458450,482975;
17120,41870,66620,91370,116120,140870,165620,190370,215120,239870,264620,289370,314120,338870,363620,388370,413120,437870,462620,487370;
17240,42215,67190,92165,117140,142115,167090,192065,217040,242015,266990,291965,316940,341915,366890,391865,416840,441815,466790,491765;
17360,42560,67760,92960,118160,143360,168560,193760,218960,244160,269360,294560,319760,344960,370160,395360,420560,445760,470960,496160
]
test_result_mtimes(49,1) = all(all(R == mtimes(A, B)))

% 54. Real + Complex mixed
A = reshape(1:64, 8, 8)
B = reshape(1:64, 8, 8) + 1i
R = [
1380+232i,3236+232i,5092+232i,6948+232i,8804+232i,10660+232i,12516+232i,14372+232i;
1416+240i,3336+240i,5256+240i,7176+240i,9096+240i,11016+240i,12936+240i,14856+240i;
1452+248i,3436+248i,5420+248i,7404+248i,9388+248i,11372+248i,13356+248i,15340+248i;
1488+256i,3536+256i,5584+256i,7632+256i,9680+256i,11728+256i,13776+256i,15824+256i;
1524+264i,3636+264i,5748+264i,7860+264i,9972+264i,12084+264i,14196+264i,16308+264i;
1560+272i,3736+272i,5912+272i,8088+272i,10264+272i,12440+272i,14616+272i,16792+272i;
1596+280i,3836+280i,6076+280i,8316+280i,10556+280i,12796+280i,15036+280i,17276+280i;
1632+288i,3936+288i,6240+288i,8544+288i,10848+288i,13152+288i,15456+288i,17760+288i
]
test_result_mtimes(50,1) = all(all(R == mtimes(A, B)))

% 55. Complex conjugate test
A = [1+2i 2+3i; 3-1i 4+4i]
B = conj(A)
R = [
8+11i,28+5i;
9+9i,35-11i
]
test_result_mtimes(51,1) = all(all(R == mtimes(A, B)))

% 56. Nonconformant dimensions (should error)
A = rand(12, 10)
B = rand(9, 12)
R = mtimes(A, B)   % error

% 57. Non-2D input (should error)
A = rand(4, 4, 2)
B = rand(4, 4, 2)
R = mtimes(A, B)   % error

% 58. Large block multiplication (16×8 × 8×16)
A = reshape(1:128, 16, 8)
B = reshape(129:256, 8, 16)
R = [
61092,64740,68388,72036,75684,79332,82980,86628,90276,93924,97572,101220,104868,108516,112164,115812;
62152,65864,69576,73288,77000,80712,84424,88136,91848,95560,99272,102984,106696,110408,114120,117832;
63212,66988,70764,74540,78316,82092,85868,89644,93420,97196,100972,104748,108524,112300,116076,119852;
64272,68112,71952,75792,79632,83472,87312,91152,94992,98832,102672,106512,110352,114192,118032,121872;
65332,69236,73140,77044,80948,84852,88756,92660,96564,100468,104372,108276,112180,116084,119988,123892;
66392,70360,74328,78296,82264,86232,90200,94168,98136,102104,106072,110040,114008,117976,121944,125912;
67452,71484,75516,79548,83580,87612,91644,95676,99708,103740,107772,111804,115836,119868,123900,127932;
68512,72608,76704,80800,84896,88992,93088,97184,101280,105376,109472,113568,117664,121760,125856,129952;
69572,73732,77892,82052,86212,90372,94532,98692,102852,107012,111172,115332,119492,123652,127812,131972;
70632,74856,79080,83304,87528,91752,95976,100200,104424,108648,112872,117096,121320,125544,129768,133992;
71692,75980,80268,84556,88844,93132,97420,101708,105996,110284,114572,118860,123148,127436,131724,136012;
72752,77104,81456,85808,90160,94512,98864,103216,107568,111920,116272,120624,124976,129328,133680,138032;
73812,78228,82644,87060,91476,95892,100308,104724,109140,113556,117972,122388,126804,131220,135636,140052;
74872,79352,83832,88312,92792,97272,101752,106232,110712,115192,119672,124152,128632,133112,137592,142072;
75932,80476,85020,89564,94108,98652,103196,107740,112284,116828,121372,125916,130460,135004,139548,144092;
76992,81600,86208,90816,95424,100032,104640,109248,113856,118464,123072,127680,132288,136896,141504,146112
]
test_result_mtimes(52,1) = all(all(R == mtimes(A, B)))

% 59. Complex rectangular (10×8 × 8×12)
A = reshape(1:80, 10, 8) + 2i
B = reshape(81:176, 8, 12) - 1i
R = [
24772+1064i,27076+1192i,29380+1320i,31684+1448i,33988+1576i,36292+1704i,38596+1832i,40900+1960i,43204+2088i,45508+2216i,47812+2344i,50116+2472i;
25448+1056i,27816+1184i,30184+1312i,32552+1440i,34920+1568i,37288+1696i,39656+1824i,42024+1952i,44392+2080i,46760+2208i,49128+2336i,51496+2464i;
26124+1048i,28556+1176i,30988+1304i,33420+1432i,35852+1560i,38284+1688i,40716+1816i,43148+1944i,45580+2072i,48012+2200i,50444+2328i,52876+2456i;
26800+1040i,29296+1168i,31792+1296i,34288+1424i,36784+1552i,39280+1680i,41776+1808i,44272+1936i,46768+2064i,49264+2192i,51760+2320i,54256+2448i;
27476+1032i,30036+1160i,32596+1288i,35156+1416i,37716+1544i,40276+1672i,42836+1800i,45396+1928i,47956+2056i,50516+2184i,53076+2312i,55636+2440i;
28152+1024i,30776+1152i,33400+1280i,36024+1408i,38648+1536i,41272+1664i,43896+1792i,46520+1920i,49144+2048i,51768+2176i,54392+2304i,57016+2432i;
28828+1016i,31516+1144i,34204+1272i,36892+1400i,39580+1528i,42268+1656i,44956+1784i,47644+1912i,50332+2040i,53020+2168i,55708+2296i,58396+2424i;
29504+1008i,32256+1136i,35008+1264i,37760+1392i,40512+1520i,43264+1648i,46016+1776i,48768+1904i,51520+2032i,54272+2160i,57024+2288i,59776+2416i;
30180+1000i,32996+1128i,35812+1256i,38628+1384i,41444+1512i,44260+1640i,47076+1768i,49892+1896i,52708+2024i,55524+2152i,58340+2280i,61156+2408i;
30856+992i,33736+1120i,36616+1248i,39496+1376i,42376+1504i,45256+1632i,48136+1760i,51016+1888i,53896+2016i,56776+2144i,59656+2272i,62536+2400i
]
test_result_mtimes(53,1) = all(all(R == mtimes(A, B)))

% 60. Multiplicação 3D (page-wise) (error)
A = cat(3, [1 2; 3 4], [5 6; 7 8])
B = cat(3, [2 0; 1 2], [1 1; 0 1])
R = A * B % error: operator *: nonconformant arguments (op1 is 2×4, op2 is 2×4)
R(:,:,1) == [4 4; 10 8] % error
R(:,:,2) == [5 11; 7 15] % error

% 61. Multiplicação 3D com mais páginas (error)
A = cat(3, [1 2; 3 4], [2 0; 0 2], [1 1; 1 1])
B = cat(3, [0 1; 1 0], [1 1; 1 1], [2 2; 3 3])
R = A * B % error
R(:,:,1) == [2 1; 4 3] % error
R(:,:,2) == [2 2; 2 2] % error
R(:,:,3) == [5 5; 5 5] % error

% 62. Dimensões inválidas (error)
A = [1 2; 3 4]
B = [1 2 3]
R = A * B % error

% 63. Multiplicação 4D (duas páginas 2×2x2×2) (error)
A = cat(4, cat(3, [1 2; 3 4], [5 6; 7 8]), cat(3, [2 3; 4 5], [6 7; 8 9]))
B = cat(4, cat(3, [0 1; 1 0], [1 0; 0 1]), cat(3, [2 1; 1 2], [0 1; 1 0]))
R = A * B % error
R(:,:,1,1) == [2 1; 4 3] % error
R(:,:,2,1) == [5 6; 7 8] % error
R(:,:,1,2) == [5 5; 13 13] % error
R(:,:,2,2) == [6 7; 8 9] % error

% 64. Multiplicação escalar × array 3D
A = cat(3, [1 2; 3 4], [5 6; 7 8])
R = 2 * A
test_result_mtimes(54,1) = all(all(R(:,:,1) == [2 4; 6 8] & R(:,:,2) == [10 12; 14 16]))

% 65. Multiplicação array 3D × escalar
A = cat(3, [1 2; 3 4], [5 6; 7 8])
R = A * 3
test_result_mtimes(55,1) = all(all(R(:,:,1) == [3 6; 9 12] & R(:,:,2) == [15 18; 21 24]))

% 66. Multiplicação 3D com escalar complexo
A = cat(3, [1+i 2; 3 4-i], [2 1; 1 2])
R = (2 - i) * A
test_result_mtimes(56,1) = all(all(R(:,:,1) == (2 - i) * [1+i 2; 3 4-i] & R(:,:,2) == (2 - i) * [2 1; 1 2]))

% 67. Multiplicação com dimensões singulares
A = [1; 2; 3]
B = [10 20 30]
R = [10 20 30; 20 40 60; 30 60 90]
test_result_mtimes(57,1) = all(all(R == A * B))

% 68. Multiplicação com dimensão unitária 3D
A = reshape(1:4, [2 2 1])
B = reshape([1 0; 0 1], [2 2 1])
R = A * B
test_result_mtimes(58,1) = all(all(R(:,:,1) == [1 3; 2 4]))

% 69. Multiplicação com páginas múltiplas de identidade (error)
I1 = eye(2)
I2 = eye(2)
A = cat(3, [1 2; 3 4], [5 6; 7 8])
B = cat(3, I1, I2)
R = A * B % error
R(:,:,1) == [1 2; 3 4] % error
R(:,:,2) == [5 6; 7 8] % error

% 70. Multiplicação com escalar e identidade
A = eye(3)
R1 = 10 * A
R2 = A * 10
test_result_mtimes(59,1) = all(all(R1 == 10 * eye(3) & R2 == 10 * eye(3)))

% 71. Multiplicação de matrizes nulas em páginas múltiplas
A = cat(3, zeros(2,2), zeros(2,2))
B = cat(3, eye(2), eye(2))
R = A * B % error
R(:,:,1) == zeros(2,2) % error
R(:,:,2) == zeros(2,2) % error

% 72. Multiplicação escalar × vetor linha e vetor coluna
A = [1 2 3]
B = [1; 2; 3]
R1 = [5 10 15]
R2 = [5; 10; 15]
test_result_mtimes(60,1) = all(all(R1 == 5 * A & R2 == 5 * B))

% 73. Multiplicação com escalar negativo
A = [1 2; 3 4]
R = [-2 -4; -6 -8]
test_result_mtimes(61,1) = all(all(R == -2 * A))

% 74. Multiplicação por escalar 0
A = [1 2; 3 4]
R = zeros(2,2)
test_result_mtimes(62,1) = all(all(R == 0 * A))

% Result of complete test
complete_test_result_mtimes = all(test_result_mtimes)

% Show test result
complete_test_result_mtimes

help pagemtimes % Page-wise matrix multiplication (Since R2020b)
help mpower % Matrix power
help sqrtm % Matrix square root
help expm % Matrix exponential
help expmv % Matrix exponential multiplied by vector (Since R2023b)
help logm % Matrix logarithm
help funm % Evaluate general matrix function
help kron % Kronecker tensor product
% 1. Identity matrices (3x3 ⊗ 3x3)
M = eye(3)
N = eye(3)
R = kron(M, N)
test_result_kron(1,1) = all(all(R == eye(9)))

% 2. Scalar times matrix
M = [2]
N = [1 2; 3 4]
R = kron(M, N)
test_result_kron(2,1) = all(all(R == 2 * N))

% 3. Matrix times scalar
M = [1 2; 3 4]
N = [3]
R = kron(M, N)
test_result_kron(3,1) = all(all(R == 3 * M))

% 4. 2x2 ⊗ 2x2
M = [1 2; 3 4]
N = [0 5; 6 7]
R = kron(M, N)
EXPECTED = [0 5 0 10; 6 7 12 14; 0 15 0 20; 18 21 24 28]
test_result_kron(4,1) = all(all(R == EXPECTED))

% 5. Row vector ⊗ column vector
M = [1 2 3]
N = [4; 5; 6]
R = kron(M, N)
EXPECTED = [4 8 12; 5 10 15; 6 12 18]
test_result_kron(5,1) = all(all(R == EXPECTED))

% 6. Column vector ⊗ row vector
M = [1; 2; 3]
N = [4 5 6]
R = kron(M, N)
EXPECTED = [4 5 6; 8 10 12; 12 15 18]
test_result_kron(6,1) = all(all(R == EXPECTED))

% 7. Kronecker product with zero matrix
M = [0 0; 0 0]
N = [1 2; 3 4]
R = kron(M, N)
test_result_kron(7,1) = all(all(R == zeros(4)))

% 8. Kronecker product with identity and non-square matrix
M = eye(2)
N = [1 2 3; 4 5 6]
R = kron(M, N)
EXPECTED = [1 2 3 0 0 0; 4 5 6 0 0 0; 0 0 0 1 2 3; 0 0 0 4 5 6]
test_result_kron(8,1) = all(all(R == EXPECTED))

% 9. Complex matrices
M = [1+1i 2; 3 4-2i]
N = [0 1i; 2 3]
R = kron(M, N)
CHECK = M(1,1)*N
test_result_kron(9,1) = all(all(R(1:2,1:2) == CHECK))

% 10. Random 3x2 ⊗ 2x3 matrix
M = [1 2; 3 4; 5 6]
N = [1 0 2; 0 3 4]
R = kron(M, N)
test_result_kron(10,1) = all(size(R) == [6 6])

% 11. Kronecker product of permutation matrices
M = [0 1; 1 0]
N = [0 0 1; 0 1 0; 1 0 0]
R = kron(M, N)
EXPECTED = [0 0 0 0 0 1; 0 0 0 0 1 0; 0 0 0 1 0 0; ...
             0 0 1 0 0 0; 0 1 0 0 0 0; 1 0 0 0 0 0]
test_result_kron(11,1) = all(all(R == EXPECTED))

% 12. Larger 4x4 test with identity to check block-structure preservation
M = [16,2,3,13;5,11,10,8;9,7,6,12;4,14,15,1]
N = eye(2)
R = kron(M, N)
test_result_kron(12,1) = all(all(R(1:2:end,1:2:end) == M))

% Final result
complete_test_result_kron = all(test_result_kron)

complete_test_result_kron

help cross % Cross product
% Parte 1 - Casos básicos (vetores linha)

A = [1 0 0]
B = [0 1 0]
R = [0 0 1]
test_result_cross(1,1) = all(all(R == cross(A,B)))

A = [0 1 0]
B = [0 0 1]
R = [1 0 0]
test_result_cross(2,1) = all(all(R == cross(A,B)))

A = [0 0 1]
B = [1 0 0]
R = [0 1 0]
test_result_cross(3,1) = all(all(R == cross(A,B)))

% Ordem invertida (anti-simetria)
A = [0 0 1]
B = [1 0 0]
R1 = cross(A,B)
R2 = cross(B,A)
test_result_cross(4,1) = all(all(R1 + R2 == [0 0 0]))

% Parte 2 - Vetores coluna

A = [1;0;0]
B = [0;1;0]
R = [0;0;1]
test_result_cross(5,1) = all(all(R == cross(A,B)))

A = [0;1;0]
B = [0;0;1]
R = [1;0;0]
test_result_cross(6,1) = all(all(R == cross(A,B)))

% Parte 3 - Matrizes (colunas)

A = [1 0; 0 1; 0 0]
B = [0 1; 1 0; 0 0]
R = [0 0; 0 0; 1 -1]
test_result_cross(7,1) = all(all(R == cross(A,B)))
test_result_cross(8,1) = all(all(R == cross(A,B,1)))

% Parte 4 - Matrizes (linhas)

A = [1 0 0; 0 1 0]
B = [0 1 0; 1 0 0]
R = [0 0 1; 0 0 -1]
test_result_cross(9,1) = all(all(R == cross(A,B,2)))

% Parte 5 - Casos degenerados

A = [1 2 3]
B = [2 4 6]
R = [0 0 0]
test_result_cross(10,1) = all(all(R == cross(A,B)))

A = [0 0 0]
B = [1 2 3]
R = [0 0 0]
test_result_cross(11,1) = all(all(R == cross(A,B)))

A = [0 0 0]
B = [0 0 0]
R = [0 0 0]
test_result_cross(12,1) = all(all(R == cross(A,B)))

% Parte 6 - Casos inválidos

A = [1 0]
B = [0 1 0]
C = cross(A,B) % error
test_result_cross(13,1) = true

A = [1 0 0]
B = [0 1 0]
C = cross(A,B,4) % error: dim inválida
test_result_cross(14,1) = true

% Parte 7 - Complexos

A = [1+i, 0, 0]
B = [0, 1, 0]
R = [0, 0, 1+i]
test_result_cross(15,1) = all(all(R == cross(A,B)))

% Caso misto: A real, B complexo
A = [1, 0, 0]
B = [0, 1+i, 0]
R = [0, 0, 1+i]
test_result_cross(16,1) = all(all(R == cross(A,B)))

% Parte 8 - Arrays 3D (dim = 2, error)

A = cat(3, [1 0 0; 0 1 0; 0 0 1], [0 1 0; 0 0 1; 1 0 0])
B = cat(3, [0 1 0; 1 0 0; 0 0 1], [1 0 0; 0 1 0; 0 0 1])
R = cross(A,B,3) % error
test_result_cross(17,1) = true

% Parte 9 - Arrays 3D (dim = 3)

A = cat(3, [1 0 0; 0 1 0; 0 0 1], [0 1 0; 0 0 1; 1 0 0], [1 0 0; 0 1 0; 0 0 1])
B = cat(3, [0 1 0; 1 0 0; 0 0 1], [1 0 0; 0 1 0; 0 0 1], [0 1 0; 0 0 1; 1 0 0])
R = cross(A,B,3)
% verificação geral
test_result_cross(18,1) = all(size(R) == size(A))

% Parte 10 - Transposição e permutações

A = [1 0; 0 1; 0 0]
B = [0 1; 1 0; 0 0]
R = [0 0; 0 0; 1 -1]
test_result_cross(19,1) = all(all(R.' == cross(A.', B.', 2)))

% Parte final - Resultado geral

% Result of complete test
complete_test_result_cross = all(test_result_cross)

% Show cross test result
complete_test_result_cross

help dot % Dot product
A = [4 -1 2]
B = [2 -2 -1]
C = dot(A,B)
C == A(1)*B(1) + A(2)*B(2) + A(3)*B(3) % true
A = [1+i 1-i -1+i -1-i]
B = [3-4i 6-2i 1+2i 4+3i]
C = dot(A,B)
C == 1 - 5i % true
D = dot(A,A)
D == 8 % true
A = [1 2 3;4 5 6;7 8 9]
B = [9 8 7;6 5 4;3 2 1]
C = dot(A,B)
C == [54, 57, 54] % true
D = dot(A,B,2)
D == [46; 73; 46] % true
A = cat(3,[1 1;1 1],[2 3;4 5],[6 7;8 9])
B = cat(3,[2 2;2 2],[10 11;12 13],[14 15; 16 17])
C = dot(A,B,3)
C == [106, 140; 178, 220] % true

% Result of complete test
complete_test_result_dot = all(test_result_dot)
complete_test_result_dot = true

% Show dot test result
complete_test_result_dot

help qr % QR Decomposition

% QR decomposition without pivoting

% 1. Simple 3x3 real matrix
A = [1 2 3; 4 5 6; 7 8 9]
[Q,R] = qr(A);
test_result_qr(1,1) = any(any(Q*R - A)) == false

% 2. Real 3x3 matrix (prototype)
A = [12 -51 4; 6 167 -68; -4 24 -41]
[Q,R] = qr(A)
test_result_qr(2,1) = all(all(R == [-14,-21,14;0,-175,70;0,0,-35]))
test_result_qr(3,1) = all(all(abs(Q*R - A) == zeros(3)))
test_result_qr(4,1) = all(all(arg(Q*R - A) == [0 pi 0; pi 0 0; 0 0 0]))

% 3. Real 3x3 matrix with single return (R)
A = [12 -51 4; 6 167 -68; -4 24 -41]
R = qr(A)
test_result_qr(5,1) = all(all(R == [-14,-21,14;3/7,-175,70;-2/7,(8*9)/(5*10*13),-35]))

% 4. Real 3x3 matrix (prototype)
A = [16 -25 -6; 6 16 -89; -4 74 -21]
[Q,R] = qr(A)
test_result_qr(6,1) = all(all(abs(Q*R - A) == zeros(3)))
test_result_qr(7,1) = all(all(arg(Q*R - A) == [pi 0 0; pi pi 0; 0 pi 0]))

% 5. Real 3x2 matrix (prototype)
A = [16 -25; 6 16; -4 74]
[Q,R] = qr(A)
test_result_qr(8,1) = all(all(abs(Q*R - A) == zeros(3,2)))
test_result_qr(9,1) = all(all(arg(Q*R - A) == [pi 0; pi pi; 0 pi]))

% 6. Real 4x2 matrix
A = [2 1; -1 3; 4 0; 0 -2]
[Q,R] = qr(A);
test_result_qr(10,1) = any(any(Q*R - A)) == false

% 7. Random 5x3 real matrix
A = rand(5,3)
[Q,R] = qr(A)
test_result_qr(11,1) = any(any(Q*R - A)) == false

% 8. Real 3x5 matrix
A = [1 -2 3 0 5; 4 1 -1 2 0; -2 3 2 1 -3]
[Q,R] = qr(A);
test_result_qr(12,1) = any(any(Q*R - A)) == false

% 9. Rotated 2x2 real matrix
A = [cos(pi/6) -sin(pi/6); sin(pi/6) cos(pi/6)]
[Q,R] = qr(A);
test_result_qr(13,1) = any(any(Q*R - A)) == false

% 10. Random 6x4 real matrix
A = rand(6,4) * 10 - 5
[Q,R] = qr(A);
test_result_qr(14,1) = any(any(Q*R - A)) == false

% 11. Real 1x4 matrix
A = [4 -2 7 1]
[Q,R] = qr(A);
test_result_qr(15,1) = any(any(Q*R - A)) == false

% 12. 4x2 complex matrix (prototype)
A = [2+1i, 1-2i; 3-1i, -4+3i; 1+2i, 5-1i; -2+1i, 0.5+4i]
[Q,R] = qr(A)
test_result_qr(16,1) = all(all(abs(Q*R - A) == zeros(4,2)))
arg(Q*R - A)

% 13. 4x3 complex matrix (prototype)
A = [2+1i, 1-2i, 3+2i; 3-1i, -4+3i, -5+3i; 1+2i, 5-1i, 7-4i; -2+1i, 0.5+4i, 0.25+8i]
[Q,R] = qr(A)
test_result_qr(17,1) = all(all(abs(Q*R - A) == zeros(4,3)))
arg(Q*R - A)

% 14. Matriz complexa 3x3 simples
A = [1+2i  3-i   0
     2-i   1+4i 5+2i
     0     1-i  3+3i]
[Q,R] = qr(A);
test_result_qr(18,1) = any(any(Q*R - A)) == false

% 15. Matriz complexa 4x2
A = [2+1i  1-2i
     3-1i -4+3i
     1+2i  5-1i
    -2+1i  0.5+4i]
[Q,R] = qr(A);
test_result_qr(19,1) = any(any(Q*R - A)) == false

% 16. Matriz complexa 5x3 aleatória segura
A = rand(5,3) + 1i*rand(5,3)
[Q,R] = qr(A);
test_result_qr(20,1) = any(any(Q*R - A)) == false

% 17. Matriz complexa 3x5
A = [1+2i  -2+i   3    0+1i 5
     4     1-3i   -1+i 2    0+0i
     -2+i   3     2-2i 1+3i -3]
[Q,R] = qr(A);
test_result_qr(21,1) = any(any(Q*R - A)) == false

% 18. Matriz complexa 2x2 com fase
A = [exp(1i*pi/4)   2-3i
     1+4i          exp(-1i*pi/6)]
[Q,R] = qr(A);
test_result_qr(22,1) = any(any(Q*R - A)) == false

% 19. Matriz complexa 6x4 aleatória
A = (rand(6,4)-0.5)*4 + 1i*((rand(6,4)-0.5)*4)
[Q,R] = qr(A);
test_result_qr(23,1) = any(any(Q*R - A)) == false

% 20. Matriz complexa 1x4
A = [3+2i  -1+4i  6-2i  -2+3i]
[Q,R] = qr(A);
test_result_qr(24,1) = any(any(Q*R - A)) == false

% 21. Matriz complexa 4x4 triangular + ruído
A = [4+1i  2+3i   1     0
     0     3-2i   1+4i  2-i
     0     0      5+2i -1+3i
     0     0      0     2+8i] + 0.01*(rand(4,4)+1i*rand(4,4))
[Q,R] = qr(A);
test_result_qr(25,1) = any(any(Q*R - A)) == false

% Final result
complete_test_result_qr = all(test_result_qr)

complete_test_result_qr

% 1. Simple 3x3 real matrix with evident pivoting.
A = [1 100 3; 2 200 6; 3 300 9]
[Q,R,P] = qr(A)
test_result_qrp(1,1) = any(any(Q*R - A*P)) == false

% 2. Moderately random 3x3 real matrix.
A = [12 -5 7; 3 8 2; -4 1 9]
[Q,R,P] = qr(A)
test_result_qrp(2,1) = any(any(Q*R - A*P)) == false

% 3. Real 4x3 (tall) matrix.
A = [1 4 7; 2 5 8; 3 6 9; 4 1 -1]
[Q,R,P] = qr(A)
test_result_qrp(3,1) = any(any(Q*R - A*P)) == false

% 4. Real 3x5 (wide) matrix.
A = [1 2 3 4 5; 6 7 8 9 10; 0 0 1 0 0]
[Q,R,P] = qr(A)
test_result_qrp(4,1) = any(any(Q*R - A*P)) == false

% 5. Real 5x5 poorly conditioned matrix.
A = [1 1 1 1 1;
     1 1.01 1 1 1;
     1 1 1.01 1 1;
     1 1 1 1.01 1;
     1 1 1 1 1.01]
[Q,R,P] = qr(A)
test_result_qrp(5,1) = any(any(Q*R - A*P)) == false

% 6. Quasi-collinear real 4x4 matrix.
A = [1 1.0001 2 3;
     2 2.0002 4 6;
     3 3.0003 6 9;
     4 4.0004 8 12]
[Q,R,P] = qr(A)
test_result_qrp(6,1) = any(any(Q*R - A*P)) == false

% 7. Real 4x4 singular matrix (rank 2).
A = [1 2 3 4;
     2 4 6 8;
     3 6 9 12;
     4 8 12 16]
[Q,R,P] = qr(A)
test_result_qrp(7,1) = any(any(Q*R - A*P)) == false

% 8. Real matrix with very different scales.
A = [1e-8 1 1e8;
     2e-8 2 2e8;
     3e-8 3 3e8]
[Q,R,P] = qr(A)
test_result_qrp(8,1) = all(all(Q*R-A*P < 1e-300*ones(3)))

% 9. Simple 3x3 complex Matrix.
A = [1+1i 2+0i 3-1i;
     4+0i 5+2i 6+0i;
     7-2i 8+1i 9+3i]
[Q,R,P] = qr(A)
test_result_qrp(9,1) = any(any(Q*R - A*P)) == false

% 10. Complex matrix of random unitary type.
A = [1+0i  0+1i  1-1i;
     0+1i  1+0i  1+1i;
     1-1i  1+1i  0+1i]
[Q,R,P] = qr(A)
test_result_qrp(10,1) = any(any(Q*R - A*P)) == false

% 11. 4x3 complex matrix (tall).
A = [1+2i 2-1i 3+3i;
     4+0i 5+5i 6+0i;
     7-2i 8+1i 9+4i;
     0+1i 1-3i 2+2i]
[Q,R,P] = qr(A)
test_result_qrp(11,1) = any(any(Q*R - A*P)) == false

% 12. 3x4 complex matrix (wide).
A = [1+0i 2+1i 3+0i 0-1i;
     4+2i 5+0i 6+2i 1+0i;
     7+0i 8+3i 9+1i 2-2i]
[Q,R,P] = qr(A)
test_result_qrp(12,1) = any(any(Q*R - A*P)) == false

% 13. Quasi-collinear complex matrix.
A = [1+1i      1.0001+1.0002i   2+2i;
     2+2i      2.0002+2.0004i   4+4i;
     3+3i      3.0003+3.0006i   6+6i]
[Q,R,P] = qr(A)
test_result_qrp(13,1) = any(any(Q*R - A*P)) == false

% 14. Singular complex matrix (rank 1).
A = [1+1i 2+2i 3+3i;
     2+2i 4+4i 6+6i;
     3+3i 6+6i 9+9i]
[Q,R,P] = qr(A)
test_result_qrp(14,1) = any(any(Q*R - A*P)) == false

% 15. Complex matrix with different scales.
A = [1e8+1e8i   1+0i     0+1i;
     2e8-1e8i   2+2i     3+3i;
     3e8+5e7i   4-1i     5+0i]
[Q,R,P] = qr(A)
test_result_qrp(15,1) = all(all(Q*R-A*P < 1e-300*ones(3)))

% 16. Stable random 5x5 complex matrix.
A = [ 1+2i   3+1i   5-2i   1-1i   0+3i;
      2-1i   1+4i   0+1i   3+0i   1-3i;
      0+2i   1-2i   3+3i   2+2i   4+0i;
      3+0i   4+4i   1-1i   0+1i   2+1i;
      1+1i   2+0i   3+0i   4+1i   5-1i ]
[Q,R,P] = qr(A)
test_result_qrp(16,1) = any(any(Q*R - A*P)) == false

% Result of complete test
complete_test_result_qrp = all(test_result_qrp)

% Show qr with pivoting test result
complete_test_result_qrp

help eig

complete_test_result = all([
    complete_test_result_det,
    complete_test_result_inv,
    complete_test_result_lu,
    complete_test_result_mtimes,
    complete_test_result_kron,
    complete_test_result_cross,
    complete_test_result_dot,
    complete_test_result_qr,
    complete_test_result_qrp,
])

complete_test_result
