function [Abar,Bbar,Q] = model_reduction(A,B,method)
% PURPOSE: reduces dimension of the problem of finding matrices R, P, such that AR = BRP
%
% this problem is transformed to the problem of finding matrices
% T, P such that Abar T = BBar T P
% then R = Q T
%
% ---------------------------------------------------
% USAGE: [Abar,Bbar,Q]=model_reduction(A,B,method)
% where:
% A,B quadratic matrices, such that the
% matrix pair (A,B) is regular
% method method to calculate null spaces
% 1 - qr with pivoting
% 2 - svd
%
% Abar,Bbar quadratic matrice forming lower dimension problem
% Q matrix that determines solution to the
% original problem
%
% COMMENTS:
%
% Copyright (c) Pawel Kowal (2006)
% All rights reserved
% LREM_SOLVE toolbox is available free for noncommercial academic use only.
% pkowal3@sgh.waw.pl
[B,U,k] = putv(B,method);
A = U'*A;
Q = null2(A(k+1:end,:),method);
Abar = A(1:k,:)*Q;
Bbar = B*Q;
if size(Abar,1)~=size(Abar,2)
error('The model is not regular');
end