function [AA] = InverseModular(A)
% function [AA] = InverseModular(A)
% A,AA are 2-by-2 matrices
% If det(A) has a reciprocal number, the function returns the inverse modular matrix A^(-1)
% Otherwise, the function returns an empty matrix

detA = mod(det(A),26);

switch detA
case 1,
    detAA = 1;
case 3,
    detAA = 9;
case 5,
    detAA = 21;
case 7,
    detAA = 15;
case 9,
    detAA = 3;
case 11,
    detAA = 19;
case 15,
    detAA = 7;
case 17,
    detAA = 23;
case 19,
    detAA = 11;
case 21,
    detAA = 5;
case 23,
    detAA = 17;
case 25,
    detAA = 25;
otherwise,
    detAA = 0;
end

if (detAA == 0)
    disp('The matrix is not invertible modulo 26!');
    AA = []; % return an empty matrix
else
    AA = zeros(2,2);
    AA(1,1) = mod(detAA*A(2,2),26);
    AA(2,2) = mod(detAA*A(1,1),26);
    AA(1,2) = mod(-detAA*A(1,2),26);
    AA(2,1) = mod(-detAA*A(2,1),26);
end

