function x=forwardSubstitutionR(L,b,n)
% Solving a lower triangular system L x = b by forward-substitution.
% Input matrix L is an n by n lower triangular matrix
% Input vector b is n by 1
% Input scalar n specifies the dimensions of the arrays
% Output vector x is the solution to the linear system
% This version uses row processing (good for C or C++).
% K. Ming Leung, 02/13/04

x = zeros(n,1); % initialization for efficiency purpose only
for j = 1:n
    if (L(j,j)==0) error('Matrix is singular!'); end;
    % Row processing: involves the jth row of L
    % to the left of the main diagonal.
    b(j) = b(j)-L(j,1:j-1)*(x(1:j-1));  % involves a dot-product
    x(j) = b(j)/L(j,j);
end       
