void qr_solve(double x[], int m, int n, double a[], double b[]) /******************************************************************************/ /* Purpose: QR_SOLVE solves a linear system in the least squares sense. Discussion: If the matrix A has full column rank, then the solution X should be the unique vector that minimizes the Euclidean norm of the residual. If the matrix A does not have full column rank, then the solution is not unique; the vector X will minimize the residual norm, but so will various other vectors. Licensing: This code is distributed under the GNU LGPL license. Modified: 11 September 2012 Author: John Burkardt Reference: David Kahaner, Cleve Moler, Steven Nash, Numerical Methods and Software, Prentice Hall, 1989, ISBN: 0-13-627258-4, LC: TA345.K34. Parameters: Input, int M, the number of rows of A. Input, int N, the number of columns of A. Input, double A[M*N], the matrix. Input, double B[M], the right hand side. Output, double QR_SOLVE[N], the least squares solution. */ { double a_qr[n * m], qraux[n], r[m], tol; int ind, itask, jpvt[n], kr, lda; r8mat_copy(a_qr, m, n, a); lda = m; tol = r8_epsilon() / r8mat_amax(m, n, a_qr); itask = 1; ind = dqrls(a_qr, lda, m, n, tol, &kr, b, x, r, jpvt, qraux, itask); UNUSED(ind); }
void test04 ( ) /******************************************************************************/ /* Purpose: TEST04 tests DQRLS. Licensing: This code is distributed under the GNU LGPL license. Modified: 11 September 2012 Author: John Burkardt */ { double *a; double b[5] = { 1.0, 2.3, 4.6, 3.1, 1.2 }; int i; int ind; int itask; int j; int *jpvt; int kr; int m = 5; int n = 3; double *qraux; double tol; double *x; a = ( double * ) malloc ( m * n * sizeof ( double ) ); jpvt = ( int * ) malloc ( n * sizeof ( int ) ); qraux = ( double * ) malloc ( n * sizeof ( double ) ); x = ( double * ) malloc ( n * sizeof ( double ) ); /* Set up least-squares problem quadratic model, equally-spaced points */ printf ( "\n" ); printf ( "TEST04\n" ); printf ( " DQRLS solves a linear system A*x = b in the least squares sense.\n" ); for ( i = 0; i < m; i++ ) { a[i+0*m] = 1.0; for ( j = 1; j < n; j++ ) { a[i+j*m] = a[i+(j-1)*m] * ( double ) ( i + 1 ); } } tol = 1.0E-06; r8mat_print ( m, n, a, " Coefficient matrix A:" ); r8vec_print ( m, b, " Right hand side b:" ); /* Solve least-squares problem */ itask = 1; ind = dqrls ( a, m, m, n, tol, &kr, b, x, b, jpvt, qraux, itask ); /* Print results */ printf ( "\n" ); printf ( " Error code = %d\n", ind ); printf ( " Estimated matrix rank = %d\n", kr ); r8vec_print ( n, x, " Least squares solution x:" ); r8vec_print ( m, b, " Residuals A*x-b" ); free ( a ); free ( jpvt ); free ( qraux ); free ( x ); return; }