Developer Reference

ID 766877
Date 12/20/2021
Public

## DGELS Example Program in C

/*******************************************************************************
*  The information and material ("Material") provided below is owned by Intel
*  Corporation or its suppliers or licensors, and title to such Material remains
*  with Intel Corporation or its suppliers or licensors. The Material contains
*  proprietary information of Intel or its suppliers and licensors. The Material
*  is protected by worldwide copyright laws and treaty provisions. No part of
*  the Material may be copied, reproduced, published, uploaded, posted,
*  transmitted, or distributed in any way without Intel's prior express written
*  property rights in the Material is granted to or conferred upon you, either
*  expressly, by implication, inducement, estoppel or otherwise. Any license
*  under such intellectual property rights must be express and approved by Intel
*  in writing.
*
********************************************************************************
*/
/*
DGELS Example.
==============

Program computes the least squares solution to the overdetermined linear
system A*X = B with full rank matrix A using QR factorization,
where A is the coefficient matrix:

1.44  -7.84  -4.39   4.53
-9.96  -0.28  -3.24   3.83
-7.55   3.24   6.27  -6.64
8.34   8.09   5.28   2.06
7.08   2.52   0.74  -2.47
-5.45  -5.70  -1.19   4.70

and B is the right-hand side matrix:

8.58   9.35
8.26  -4.43
8.48  -0.70
-5.28  -0.26
5.72  -7.36
8.93  -2.52

Description.
============

The routine solves overdetermined or underdetermined real linear systems
involving an m-by-n matrix A, or its transpose, using a QR or LQ
factorization of A. It is assumed that A has full rank.

Several right hand side vectors b and solution vectors x can be handled
in a single call; they are stored as the columns of the m-by-nrhs right
hand side matrix B and the n-by-nrhs solution matrix X.

Example Program Results.
========================

DGELS Example Program Results

Solution
-0.45   0.25
-0.85  -0.90
0.71   0.63
0.13   0.14

Residual sum of squares for the solution
195.36 107.06

Details of QR factorization
-17.54  -4.76  -1.96   0.42
-0.52  12.40   7.88  -5.84
-0.40  -0.14  -5.75   4.11
0.44  -0.66  -0.20  -7.78
0.37  -0.26  -0.17  -0.15
-0.29   0.46   0.41   0.24
*/
#include <stdlib.h>
#include <stdio.h>

/* DGELS prototype */
extern void dgels( char* trans, int* m, int* n, int* nrhs, double* a, int* lda,
double* b, int* ldb, double* work, int* lwork, int* info );
/* Auxiliary routines prototypes */
extern void print_matrix( char* desc, int m, int n, double* a, int lda );
extern void print_vector_norm( char* desc, int m, int n, double* a, int lda );

/* Parameters */
#define M 6
#define N 4
#define NRHS 2
#define LDA M
#define LDB M

/* Main program */
int main() {
/* Locals */
int m = M, n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info, lwork;
double wkopt;
double* work;
/* Local arrays */
double a[LDA*N] = {
1.44, -9.96, -7.55,  8.34,  7.08, -5.45,
-7.84, -0.28,  3.24,  8.09,  2.52, -5.70,
-4.39, -3.24,  6.27,  5.28,  0.74, -1.19,
4.53,  3.83, -6.64,  2.06, -2.47,  4.70
};
double b[LDB*NRHS] = {
8.58,  8.26,  8.48, -5.28,  5.72,  8.93,
9.35, -4.43, -0.70, -0.26, -7.36, -2.52
};
/* Executable statements */
printf( " DGELS Example Program Results\n" );
/* Query and allocate the optimal workspace */
lwork = -1;
dgels( "No transpose", &m, &n, &nrhs, a, &lda, b, &ldb, &wkopt, &lwork,
&info );
lwork = (int)wkopt;
work = (double*)malloc( lwork*sizeof(double) );
/* Solve the equations A*X = B */
dgels( "No transpose", &m, &n, &nrhs, a, &lda, b, &ldb, work, &lwork,
&info );
/* Check for the full rank */
if( info > 0 ) {
printf( "The diagonal element %i of the triangular factor ", info );
printf( "of A is zero, so that A does not have full rank;\n" );
printf( "the least squares solution could not be computed.\n" );
exit( 1 );
}
/* Print least squares solution */
print_matrix( "Least squares solution", n, nrhs, b, ldb );
/* Print residual sum of squares for the solution */
print_vector_norm( "Residual sum of squares for the solution", m-n, nrhs,
&b[n], ldb );
/* Print details of QR factorization */
print_matrix( "Details of QR factorization", m, n, a, lda );
/* Free workspace */
free( (void*)work );
exit( 0 );
} /* End of DGELS Example */

/* Auxiliary routine: printing a matrix */
void print_matrix( char* desc, int m, int n, double* a, int lda ) {
int i, j;
printf( "\n %s\n", desc );
for( i = 0; i < m; i++ ) {
for( j = 0; j < n; j++ ) printf( " %6.2f", a[i+j*lda] );
printf( "\n" );
}
}

/* Auxiliary routine: printing norms of matrix columns */
void print_vector_norm( char* desc, int m, int n, double* a, int lda ) {
int i, j;
double norm;
printf( "\n %s\n", desc );
for( j = 0; j < n; j++ ) {
norm = 0.0;
for( i = 0; i < m; i++ ) norm += a[i+j*lda] * a[i+j*lda];
printf( " %6.2f", norm );
}
printf( "\n" );
}