modified linalg_fwd to make it work with MATLAB (tested)