1function [Q,R] = qr2(varargin)
2% This routine performs a qr decomposition of matrix X such that the
3% diagonal scalars of the upper-triangular matrix R are positive. If X
4% is a full (column) rank matrix, then R is also the cholesky
5% factorization of X'X. This property is needed for the Del Negro
6% & Schorfheides's identification scheme.
7%
8% INPUTS
9%   See matlab's documentation for QR decomposition.
10%
11% OUTPUTS
12%   See matlab's documentation for QR decomposition.
13%
14% ALGORITHM
15%   None.
16%
17% SPECIAL REQUIREMENTS
18%   None.
19
20% Copyright (C) 2006-2017 Dynare Team
21%
22% This file is part of Dynare.
23%
24% Dynare is free software: you can redistribute it and/or modify
25% it under the terms of the GNU General Public License as published by
26% the Free Software Foundation, either version 3 of the License, or
27% (at your option) any later version.
28%
29% Dynare is distributed in the hope that it will be useful,
30% but WITHOUT ANY WARRANTY; without even the implied warranty of
31% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32% GNU General Public License for more details.
33%
34% You should have received a copy of the GNU General Public License
35% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
36
37[Q,R] = qr(varargin{:});
38indx = find(diag(R)<0);
39if ~isempty(indx)
40    Q(:,indx) = -Q(:,indx);
41    R(indx,:) = -R(indx,:);
42end