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\
85 For example, given the matrix @code{@var{A} = [1, 2; 3, 4]},\n\
88 [@var{Q}, @var{R}] = qr (@var{A})\n\
108 The @code{qr} factorization has applications in the solution of least\n\
112 \\min_x \\left\\Vert A x - b \\right\\Vert_2\n\
122 for overdetermined systems of equations (i.e.,\n\
129 is a tall, thin matrix). The QR@tie{}factorization is\n\
131 $QR = A$ where $Q$ is an orthogonal matrix and $R$ is upper triangular.\n\
134 @code{@var{Q} * @var{R} = @var{A}} where @var{Q} is an orthogonal matrix and\n\
135 @var{R} is upper triangular.\n\
138 If given a second argument of @qcode{'0'}, @code{qr} returns an economy-sized\n\
139 QR@tie{}factorization, omitting zero rows of @var{R} and the corresponding\n\
140 columns of @var{Q}.\n\
142 If the matrix @var{A} is full, the permuted QR@tie{}factorization\n\
143 @code{[@var{Q}, @var{R}, @var{P}] = qr (@var{A})} forms the\n\
144 QR@tie{}factorization such that the diagonal entries of @var{R} are\n\
145 decreasing in magnitude order. For example, given the matrix\n\
146 @code{a = [1, 2; 3, 4]},\n\
149 [@var{Q}, @var{R}, @var{P}] = qr (@var{A})\n\
174 The permuted @code{qr} factorization\n\
175 @code{[@var{Q}, @var{R}, @var{P}] = qr (@var{A})} factorization allows the\n\
176 construction of an orthogonal basis of @code{span (A)}.\n\
178 If the matrix @var{A} is sparse, then compute the sparse\n\
179 QR@tie{}factorization of @var{A}, using @sc{CSparse}. As the matrix @var{Q}\n\
180 is in general a full matrix, this function returns the @var{Q}-less\n\
181 factorization @var{R} of @var{A}, such that\n\
182 @code{@var{R} = chol (@var{A}' * @var{A})}.\n\
184 If the final argument is the scalar @code{0} and the number of rows is\n\
185 larger than the number of columns, then an economy factorization is\n\
186 returned. That is @var{R} will have only @code{size (@var{A},1)} rows.\n\
188 If an additional matrix @var{B} is supplied, then @code{qr} returns\n\
189 @var{C}, where @code{@var{C} = @var{Q}' * @var{B}}. This allows the\n\
190 least squares approximation of @code{@var{A} \\ @var{B}} to be calculated\n\
195 [@var{C}, @var{R}] = qr (@var{A}, @var{B})\n\
196 x = @var{R} \\ @var{C}\n\
199 @seealso{chol, hess, lu, qz, schur, svd, qrupdate, qrinsert, qrdelete, qrshift}\n\
204 int nargin = args.
length ();
206 if (nargin < 1 || nargin > (args(0).is_sparse_type () ? 3 : 2))
216 if (arg_is_empty < 0)
221 bool economy =
false;
222 bool is_cmplx =
false;
230 if (args(nargin-1).is_scalar_type ())
232 int val = args(nargin-1).int_value ();
236 have_b = (nargin > 2 ? 2 : 0);
239 if (have_b > 0 && args(have_b).is_complex_type ())
245 if (have_b && nargout < 2)
246 error (
"qr: incorrect number of output arguments");
254 retval(1) = q.R (economy);
255 retval(0) = q.C (args(have_b).complex_matrix_value ());
257 warning (
"qr: non minimum norm solution for under-determined problem");
259 else if (nargout > 1)
261 retval(1) = q.R (economy);
265 retval(0) = q.R (economy);
275 retval(1) = q.R (economy);
276 retval(0) = q.C (args(have_b).matrix_value ());
277 if (args(0).rows () < args(0).columns ())
278 warning (
"qr: non minimum norm solution for under-determined problem");
280 else if (nargout > 1)
282 retval(1) = q.R (economy);
286 retval(0) = q.R (economy);
311 retval(0) = fact.
R ();
319 retval(0) = fact.
Q ();
327 retval(2) = fact.
Pvec ();
329 retval(2) = fact.
P ();
331 retval(0) = fact.
Q ();
349 retval(0) = fact.
R ();
357 retval(0) = fact.
Q ();
365 retval(2) = fact.
Pvec ();
367 retval(2) = fact.
P ();
369 retval(0) = fact.
Q ();
390 retval(0) = fact.
R ();
398 retval(0) = fact.
Q ();
406 retval(2) = fact.
Pvec ();
408 retval(2) = fact.
P ();
410 retval(0) = fact.
Q ();
428 retval(0) = fact.
R ();
436 retval(0) = fact.
Q ();
444 retval(2) = fact.
Pvec ();
446 retval(2) = fact.
P ();
448 retval(0) = fact.
Q ();
747 bool allow_ecf =
false)
753 && (m == k || (allow_ecf && k == n && k < m)));
765 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrupdate (@var{Q}, @var{R}, @var{u}, @var{v})\n\
766 Given a QR@tie{}factorization of a real or complex matrix\n\
767 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
768 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization of\n\
769 @w{@var{A} + @var{u}*@var{v}'}, where @var{u} and @var{v} are column vectors\n\
770 (rank-1 update) or matrices with equal number of columns\n\
771 (rank-k update). Notice that the latter case is done as a sequence of rank-1\n\
772 updates; thus, for k large enough, it will be both faster and more accurate\n\
773 to recompute the factorization from scratch.\n\
775 The QR@tie{}factorization supplied may be either full (Q is square) or\n\
776 economized (R is square).\n\
778 @seealso{qr, qrinsert, qrdelete, qrshift}\n\
820 retval(0) = fact.
Q ();
833 retval(0) = fact.
Q ();
853 retval(0) = fact.
Q ();
866 retval(0) = fact.
Q ();
871 error (
"qrupdate: Q and R dimensions don't match");
874 error (
"qrupdate: Q, R, U, and V must be numeric");
945 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrinsert (@var{Q}, @var{R}, @var{j}, @var{x}, @var{orient})\n\
946 Given a QR@tie{}factorization of a real or complex matrix\n\
947 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
948 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization of\n\
949 @w{[A(:,1:j-1) x A(:,j:n)]}, where @var{u} is a column vector to be inserted\n\
950 into @var{A} (if @var{orient} is @qcode{\"col\"}), or the\n\
951 QR@tie{}factorization of @w{[A(1:j-1,:);x;A(:,j:n)]}, where @var{x} is a row\n\
952 vector to be inserted into @var{A} (if @var{orient} is @qcode{\"row\"}).\n\
954 The default value of @var{orient} is @qcode{\"col\"}. If @var{orient} is\n\
955 @qcode{\"col\"}, @var{u} may be a matrix and @var{j} an index vector\n\
956 resulting in the QR@tie{}factorization of a matrix @var{B} such that\n\
957 @w{B(:,@var{j})} gives @var{u} and @w{B(:,@var{j}) = []} gives @var{A}.\n\
958 Notice that the latter case is done as a sequence of k insertions;\n\
959 thus, for k large enough, it will be both faster and more accurate to\n\
960 recompute the factorization from scratch.\n\
962 If @var{orient} is @qcode{\"col\"}, the QR@tie{}factorization supplied may\n\
963 be either full (Q is square) or economized (R is square).\n\
965 If @var{orient} is @qcode{\"row\"}, full factorization is needed.\n\
966 @seealso{qr, qrupdate, qrdelete, qrshift}\n\
972 if (nargin < 4 || nargin > 5)
985 && (nargin < 5 || args(4).is_string ()))
987 std::string orient = (nargin < 5) ?
"col" : args(4).string_value ();
989 bool col = orient ==
"col";
991 if (col || orient ==
"row")
993 && (col || argx.
rows () == 1))
1023 retval(0) = fact.
Q ();
1040 retval(0) = fact.
Q ();
1066 retval(0) = fact.
Q ();
1082 retval(0) = fact.
Q ();
1088 error (
"qrinsert: invalid index J");
1091 error (
"qrinsert: dimension mismatch");
1094 error (
"qrinsert: ORIENT must be \"col\" or \"row\"");
1164 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrdelete (@var{Q}, @var{R}, @var{j}, @var{orient})\n\
1165 Given a QR@tie{}factorization of a real or complex matrix\n\
1166 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
1167 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization of\n\
1168 @w{[A(:,1:j-1) A(:,j+1:n)]}, i.e., @var{A} with one column deleted\n\
1169 (if @var{orient} is @qcode{\"col\"}), or the QR@tie{}factorization of\n\
1170 @w{[A(1:j-1,:);A(j+1:n,:)]}, i.e., @var{A} with one row deleted (if\n\
1171 @var{orient} is @qcode{\"row\"}).\n\
1173 The default value of @var{orient} is @qcode{\"col\"}.\n\
1175 If @var{orient} is @qcode{\"col\"}, @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}. Notice that the latter case is done as\n\
1178 a sequence of k deletions; thus, for k large enough, it will be both faster\n\
1179 and more accurate to recompute the factorization from scratch.\n\
1181 If @var{orient} is @qcode{\"col\"}, the QR@tie{}factorization supplied may\n\
1182 be either full (Q is square) or economized (R is square).\n\
1184 If @var{orient} is @qcode{\"row\"}, full factorization is needed.\n\
1185 @seealso{qr, qrupdate, qrinsert, qrshift}\n\
1191 if (nargin < 3 || nargin > 4)
1202 && (nargin < 4 || args(3).is_string ()))
1204 std::string orient = (nargin < 4) ?
"col" : args(3).string_value ();
1206 bool col = orient ==
"col";
1208 if (col || orient ==
"row")
1236 retval(0) = fact.
Q ();
1251 retval(0) = fact.
Q ();
1273 retval(0) = fact.
Q ();
1288 retval(0) = fact.
Q ();
1293 error (
"qrdelete: invalid index J");
1296 error (
"qrdelete: dimension mismatch");
1299 error (
"qrdelete: ORIENT must be \"col\" or \"row\"");
1428 @deftypefn {Loadable Function} {[@var{Q1}, @var{R1}] =} qrshift (@var{Q}, @var{R}, @var{i}, @var{j})\n\
1429 Given a QR@tie{}factorization of a real or complex matrix\n\
1430 @w{@var{A} = @var{Q}*@var{R}}, @var{Q}@tie{}unitary and\n\
1431 @var{R}@tie{}upper trapezoidal, return the QR@tie{}factorization\n\
1432 of @w{@var{A}(:,p)}, where @w{p} is the permutation @*\n\
1433 @code{p = [1:i-1, shift(i:j, 1), j+1:n]} if @w{@var{i} < @var{j}} @*\n\
1435 @code{p = [1:j-1, shift(j:i,-1), i+1:n]} if @w{@var{j} < @var{i}}. @*\n\
1437 @seealso{qr, qrupdate, qrinsert, qrdelete}\n\
1477 retval(0) = fact.
Q ();
1488 retval(0) = fact.
Q ();
1504 retval(0) = fact.
Q ();
1515 retval(0) = fact.
Q ();
1520 error (
"qrshift: invalid index I or J");
1523 error (
"qrshift: dimensions mismatch");
1526 error (
"qrshift: Q and R must be numeric");
void insert_col(const ColumnVector &u, octave_idx_type j)
void delete_row(octave_idx_type j)
bool is_real_type(void) const
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
octave_idx_type rows(void) const
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
OCTINTERP_API void print_usage(void)
static octave_value get_qr_r(const base_qr< MT > &fact)
octave_idx_type length(void) const
void shift_cols(octave_idx_type i, octave_idx_type j)
bool is_scalar_type(void) const
bool is_numeric_type(void) const
void update(const ComplexColumnVector &u, const ComplexColumnVector &v)
int int_value(bool req_int=false, bool frc_str_conv=false) const
RowVector Pvec(void) const
void error(const char *fmt,...)
void delete_row(octave_idx_type j)
RowVector row(octave_idx_type i) const
void insert_row(const FloatRowVector &u, octave_idx_type j)
int empty_arg(const char *, octave_idx_type nr, octave_idx_type nc)
ComplexRowVector row(octave_idx_type i) const
void shift_cols(octave_idx_type i, octave_idx_type j)
FloatRowVector row(octave_idx_type i) const
void shift_cols(octave_idx_type i, octave_idx_type j)
void insert_row(const RowVector &u, octave_idx_type j)
void delete_row(octave_idx_type j)
octave_idx_type columns(void) const
FloatRowVector Pvec(void) const
static const type economy
bool is_sparse_type(void) const
RowVector Pvec(void) const
Array< octave_idx_type > octave_idx_type_vector_value(bool req_int=false, bool frc_str_conv=false, bool frc_vec_conv=false) const
bool is_complex_type(void) const
void shift_cols(octave_idx_type i, octave_idx_type j)
void insert_row(const ComplexRowVector &u, octave_idx_type j)
void delete_col(octave_idx_type j)
void insert_col(const FloatComplexColumnVector &u, octave_idx_type j)
SparseComplexMatrix sparse_complex_matrix_value(bool frc_str_conv=false) const
Matrix matrix_value(bool frc_str_conv=false) const
static bool check_index(const octave_value &i, bool vector_allowed=false)
FloatRowVector Pvec(void) const
void delete_col(octave_idx_type j)
void warning(const char *fmt,...)
void insert_col(const FloatColumnVector &u, octave_idx_type j)
void update(const FloatColumnVector &u, const FloatColumnVector &v)
void insert_col(const ComplexColumnVector &u, octave_idx_type j)
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
void delete_col(octave_idx_type j)
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
void delete_row(octave_idx_type j)
void insert_row(const FloatComplexRowVector &u, octave_idx_type j)
F77_RET_T const octave_idx_type const octave_idx_type const octave_idx_type double const octave_idx_type double const octave_idx_type double * Q
#define DEFUN_DLD(name, args_name, nargout_name, doc)
SparseMatrix sparse_matrix_value(bool frc_str_conv=false) const
bool is_single_type(void) const
FloatComplexRowVector row(octave_idx_type i) const
void delete_col(octave_idx_type j)
void update(const ColumnVector &u, const ColumnVector &v)
F77_RET_T const double * x
void update(const FloatComplexColumnVector &u, const FloatComplexColumnVector &v)
static bool check_qr_dims(const octave_value &q, const octave_value &r, bool allow_ecf=false)
bool is_integer_type(void) const