52 if (R.is_square () && fact.
regular ())
77 @deftypefn {Loadable Function} {[@var{Q}, @var{R}, @var{P}] =} qr (@var{A})\n\
78 @deftypefnx {Loadable Function} {[@var{Q}, @var{R}, @var{P}] =} qr (@var{A}, '0')\n\
79 @deftypefnx {Loadable Function} {[@var{C}, @var{R}] =} qr (@var{A}, @var{B})\n\
80 @deftypefnx {Loadable Function} {[@var{C}, @var{R}] =} qr (@var{A}, @var{B}, '0')\n\
81 @cindex QR factorization\n\
82 Compute the QR@tie{}factorization of @var{A}, using standard @sc{lapack}\n\
83 subroutines. For example, given the matrix @code{@var{A} = [1, 2; 3, 4]},\n\
86 [@var{Q}, @var{R}] = qr (@var{A})\n\
106 The @code{qr} factorization has applications in the solution of least\n\
110 \\min_x \\left\\Vert A x - b \\right\\Vert_2\n\
120 for overdetermined systems of equations (i.e.,\n\
127 is a tall, thin matrix). The QR@tie{}factorization is\n\
129 $QR = A$ where $Q$ is an orthogonal matrix and $R$ is upper triangular.\n\
132 @code{@var{Q} * @var{Q} = @var{A}} where @var{Q} is an orthogonal matrix and\n\
133 @var{R} is upper triangular.\n\
136 If given a second argument of @qcode{'0'}, @code{qr} returns an economy-sized\n\
137 QR@tie{}factorization, omitting zero rows of @var{R} and the corresponding\n\
138 columns of @var{Q}.\n\
140 If the matrix @var{A} is full, the permuted QR@tie{}factorization\n\
141 @code{[@var{Q}, @var{R}, @var{P}] = qr (@var{A})} forms the\n\
142 QR@tie{}factorization such that the diagonal entries of @var{R} are\n\
143 decreasing in magnitude order. For example, given the matrix @code{a = [1,\n\
147 [@var{Q}, @var{R}, @var{P}] = qr (@var{A})\n\
172 The permuted @code{qr} factorization @code{[@var{Q}, @var{R}, @var{P}] = qr\n\
173 (@var{A})} factorization allows the construction of an orthogonal basis of\n\
176 If the matrix @var{A} is sparse, then compute the sparse\n\
177 QR@tie{}factorization of @var{A}, using @sc{CSparse}. As the matrix @var{Q}\n\
178 is in general a full matrix, this function returns the @var{Q}-less\n\
179 factorization @var{R} of @var{A}, such that @code{@var{R} = chol (@var{A}' *\n\
182 If the final argument is the scalar @code{0} and the number of rows is\n\
183 larger than the number of columns, then an economy factorization is\n\
184 returned. That is @var{R} will have only @code{size (@var{A},1)} rows.\n\
186 If an additional matrix @var{B} is supplied, then @code{qr} returns\n\
187 @var{C}, where @code{@var{C} = @var{Q}' * @var{B}}. This allows the\n\
188 least squares approximation of @code{@var{A} \\ @var{B}} to be calculated\n\
193 [@var{C}, @var{R}] = qr (@var{A}, @var{B})\n\
194 x = @var{R} \\ @var{C}\n\
197 @seealso{chol, hess, lu, qz, schur, svd, qrupdate, qrinsert, qrdelete, qrshift}\n\
202 int nargin = args.
length ();
204 if (nargin < 1 || nargin > (args(0).is_sparse_type () ? 3 : 2))
214 if (arg_is_empty < 0)
219 bool economy =
false;
220 bool is_cmplx =
false;
228 if (args(nargin-1).is_scalar_type ())
230 int val = args(nargin-1).int_value ();
234 have_b = (nargin > 2 ? 2 : 0);
237 if (have_b > 0 && args(have_b).is_complex_type ())
243 if (have_b && nargout < 2)
244 error (
"qr: incorrect number of output arguments");
252 retval(1) = q.R (economy);
253 retval(0) = q.C (args(have_b).complex_matrix_value ());
255 warning (
"qr: non minimum norm solution for under-determined problem");
257 else if (nargout > 1)
259 retval(1) = q.R (economy);
263 retval(0) = q.R (economy);
273 retval(1) = q.R (economy);
274 retval(0) = q.C (args(have_b).matrix_value ());
275 if (args(0).rows () < args(0).columns ())
276 warning (
"qr: non minimum norm solution for under-determined problem");
278 else if (nargout > 1)
280 retval(1) = q.R (economy);
284 retval(0) = q.R (economy);
309 retval(0) = fact.
R ();
317 retval(0) = fact.
Q ();
325 retval(2) = fact.
Pvec ();
327 retval(2) = fact.
P ();
329 retval(0) = fact.
Q ();
347 retval(0) = fact.
R ();
355 retval(0) = fact.
Q ();
363 retval(2) = fact.
Pvec ();
365 retval(2) = fact.
P ();
367 retval(0) = fact.
Q ();
388 retval(0) = fact.
R ();
396 retval(0) = fact.
Q ();
404 retval(2) = fact.
Pvec ();
406 retval(2) = fact.
P ();
408 retval(0) = fact.
Q ();
426 retval(0) = fact.
R ();
434 retval(0) = fact.
Q ();
442 retval(2) = fact.
Pvec ();
444 retval(2) = fact.
P ();
446 retval(0) = fact.
Q ();
745 bool allow_ecf =
false)
749 && (m == k || (allow_ecf && k == n && k < m)));
761 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrupdate (@var{Q}, @var{R}, @var{u}, @var{v})\n\
762 Given a QR@tie{}factorization of a real or complex matrix\n\
763 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
764 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization\n\
765 of @w{@var{A} + @var{u}*@var{v}'}, where @var{u} and @var{v} are\n\
766 column vectors (rank-1 update) or matrices with equal number of columns\n\
767 (rank-k update). Notice that the latter case is done as a sequence of rank-1\n\
768 updates; thus, for k large enough, it will be both faster and more accurate\n\
769 to recompute the factorization from scratch.\n\
771 The QR@tie{}factorization supplied may be either full\n\
772 (Q is square) or economized (R is square).\n\
774 @seealso{qr, qrinsert, qrdelete, qrshift}\n\
816 retval(0) = fact.
Q ();
829 retval(0) = fact.
Q ();
849 retval(0) = fact.
Q ();
862 retval(0) = fact.
Q ();
867 error (
"qrupdate: Q and R dimensions don't match");
870 error (
"qrupdate: Q, R, U, and V must be numeric");
941 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrinsert (@var{Q}, @var{R}, @var{j}, @var{x}, @var{orient})\n\
942 Given a QR@tie{}factorization of a real or complex matrix\n\
943 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
944 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization of\n\
945 @w{[A(:,1:j-1) x A(:,j:n)]}, where @var{u} is a column vector to be\n\
946 inserted into @var{A} (if @var{orient} is @qcode{\"col\"}), or the\n\
947 QR@tie{}factorization of @w{[A(1:j-1,:);x;A(:,j:n)]}, where @var{x}\n\
948 is a row vector to be inserted into @var{A} (if @var{orient} is\n\
951 The default value of @var{orient} is @qcode{\"col\"}.\n\
952 If @var{orient} is @qcode{\"col\"},\n\
953 @var{u} may be a matrix and @var{j} an index vector\n\
954 resulting in the QR@tie{}factorization of a matrix @var{B} such that\n\
955 @w{B(:,@var{j})} gives @var{u} and @w{B(:,@var{j}) = []} gives @var{A}.\n\
956 Notice that the latter case is done as a sequence of k insertions;\n\
957 thus, for k large enough, it will be both faster and more accurate to\n\
958 recompute the factorization from scratch.\n\
960 If @var{orient} is @qcode{\"col\"},\n\
961 the QR@tie{}factorization supplied may be either full\n\
962 (Q is square) or economized (R is square).\n\
964 If @var{orient} is @qcode{\"row\"}, full factorization is needed.\n\
965 @seealso{qr, qrupdate, qrdelete, qrshift}\n\
971 if (nargin < 4 || nargin > 5)
984 && (nargin < 5 || args(4).is_string ()))
986 std::string orient = (nargin < 5) ?
"col" : args(4).string_value ();
988 bool col = orient ==
"col";
990 if (col || orient ==
"row")
992 && (col || argx.
rows () == 1))
1022 retval(0) = fact.
Q ();
1039 retval(0) = fact.
Q ();
1065 retval(0) = fact.
Q ();
1081 retval(0) = fact.
Q ();
1087 error (
"qrinsert: invalid index J");
1090 error (
"qrinsert: dimension mismatch");
1093 error (
"qrinsert: ORIENT must be \"col\" or \"row\"");
1163 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrdelete (@var{Q}, @var{R}, @var{j}, @var{orient})\n\
1164 Given a QR@tie{}factorization of a real or complex matrix\n\
1165 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
1166 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization of\n\
1167 @w{[A(:,1:j-1) A(:,j+1:n)]}, i.e., @var{A} with one column deleted\n\
1168 (if @var{orient} is @qcode{\"col\"}), or the QR@tie{}factorization of\n\
1169 @w{[A(1:j-1,:);A(j+1:n,:)]}, i.e., @var{A} with one row deleted (if\n\
1170 @var{orient} is @qcode{\"row\"}).\n\
1172 The default value of @var{orient} is @qcode{\"col\"}.\n\
1174 If @var{orient} is @qcode{\"col\"},\n\
1175 @var{j} may be an index vector\n\
1176 resulting in the QR@tie{}factorization of a matrix @var{B} such that\n\
1177 @w{A(:,@var{j}) = []} gives @var{B}.\n\
1178 Notice that the latter case is done as a sequence of k deletions;\n\
1179 thus, for k large enough, it will be both faster and more accurate to\n\
1180 recompute the factorization from scratch.\n\
1182 If @var{orient} is @qcode{\"col\"},\n\
1183 the QR@tie{}factorization supplied may be either full\n\
1184 (Q is square) or economized (R is square).\n\
1186 If @var{orient} is @qcode{\"row\"}, full factorization is needed.\n\
1187 @seealso{qr, qrupdate, qrinsert, qrshift}\n\
1193 if (nargin < 3 || nargin > 4)
1204 && (nargin < 4 || args(3).is_string ()))
1206 std::string orient = (nargin < 4) ?
"col" : args(3).string_value ();
1208 bool col = orient ==
"col";
1210 if (col || orient ==
"row")
1238 retval(0) = fact.
Q ();
1253 retval(0) = fact.
Q ();
1275 retval(0) = fact.
Q ();
1290 retval(0) = fact.
Q ();
1295 error (
"qrdelete: invalid index J");
1298 error (
"qrdelete: dimension mismatch");
1301 error (
"qrdelete: ORIENT must be \"col\" or \"row\"");
1430 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrshift (@var{Q}, @var{R}, @var{i}, @var{j})\n\
1431 Given a QR@tie{}factorization of a real or complex matrix\n\
1432 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
1433 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization\n\
1434 of @w{@var{A}(:,p)}, where @w{p} is the permutation @*\n\
1435 @code{p = [1:i-1, shift(i:j, 1), j+1:n]} if @w{@var{i} < @var{j}} @*\n\
1437 @code{p = [1:j-1, shift(j:i,-1), i+1:n]} if @w{@var{j} < @var{i}}. @*\n\
1439 @seealso{qr, qrupdate, qrinsert, qrdelete}\n\
1479 retval(0) = fact.
Q ();
1490 retval(0) = fact.
Q ();
1506 retval(0) = fact.
Q ();
1517 retval(0) = fact.
Q ();
1522 error (
"qrshift: invalid index I or J");
1525 error (
"qrshift: dimensions mismatch");
1528 error (
"qrshift: Q and R must be numeric");