% Compute the matrix product of A inverse times B
% by solving for X in eq,: A X = B where 
% matrices A (n x n) & B (n x m) are given
% This method requires fewer number of arith.
% operations than explicitly finding A inverse
% AinverseB.m
% K. Ming Leung, 02/04/03

clear all; format short;
A = [1 2 2; 4 4 2; 4 6 4];
B = [3 1  4; 6 2 -3; 10 0 -2]; 
[n n] = size(A); [n m] = size(B); X = zeros(n,m);

% Compute the LU factorization of A:
[L,U] = LU_factor(A,n);

% Solve Ax = b for each column of B
for k=1:m
    xx = forwardSubstitution(L,B(:,k),n);
    X(:,k) = backSubstitution(U,xx,3);
end
display(X);
% Compute X using Matlab's linear equation solver
XMatlab = A\B; display(XMatlab);
