LAPACK  3.6.1
LAPACK: Linear Algebra PACKage
example_DGELS_colmajor.c
Go to the documentation of this file.
1 /*
2  LAPACKE Example : Calling DGELS using col-major layout
3  =====================================================
4 
5  The program computes the solution to the system of linear
6  equations with a square matrix A and multiple
7  right-hand sides B, where A is the coefficient matrix
8  and b is the right-hand side matrix:
9 
10  Description
11  ===========
12 
13  In this example, we wish solve the least squares problem min_x || B - Ax ||
14  for two right-hand sides using the LAPACK routine DGELS. For input we will
15  use the 5-by-3 matrix
16 
17  ( 1 1 1 )
18  ( 2 3 4 )
19  A = ( 3 5 2 )
20  ( 4 2 5 )
21  ( 5 4 3 )
22  and the 5-by-2 matrix
23 
24  ( -10 -3 )
25  ( 12 14 )
26  B = ( 14 12 )
27  ( 16 16 )
28  ( 18 16 )
29  We will first store the input matrix as a static C two-dimensional array,
30  which is stored in col-major layout, and let LAPACKE handle the work space
31  array allocation. The LAPACK base name for this function is gels, and we
32  will use double precision (d), so the LAPACKE function name is LAPACKE_dgels.
33 
34  lda=5 and ldb=5. The output for each right hand side is stored in b as
35  consecutive vectors of length 3. The correct answer for this problem is
36  the 3-by-2 matrix
37 
38  ( 2 1 )
39  ( 1 1 )
40  ( 1 2 )
41 
42  A complete C program for this example is given below. Note that when the arrays
43  are passed to the LAPACK routine, they must be dereferenced, since LAPACK is
44  expecting arrays of type double *, not double **.
45 
46 
47  LAPACKE Interface
48  =================
49 
50  LAPACKE_dgels (col-major, high-level) Example Program Results
51 
52  -- LAPACKE Example routine (version 3.6.0) --
53  -- LAPACK is a software package provided by Univ. of Tennessee, --
54  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
55  November 2015
56 
57 */
58 /* Calling DGELS using col-major layout */
59 
60 /* Includes */
61 #include <stdio.h>
62 #include <lapacke.h>
63 #include "lapacke_example_aux.h"
64 
65 /* Main program */
66 int main (int argc, const char * argv[])
67 {
68  /* Locals */
69  double A[5][3] = {{1,2,3},{4,5,1},{3,5,2},{4,1,4},{2,5,3}};
70  double b[5][2] = {{-10,12},{14,16},{18,-3},{14,12},{16,16}};
71  lapack_int info,m,n,lda,ldb,nrhs;
72 
73  /* Initialization */
74  m = 5;
75  n = 3;
76  nrhs = 2;
77  lda = 5;
78  ldb = 5;
79 
80  /* Print Entry Matrix */
81  print_matrix_colmajor( "Entry Matrix A", m, n, *A, lda );
82  /* Print Right Rand Side */
83  print_matrix_colmajor( "Right Hand Side b", n, nrhs, *b, ldb );
84  printf( "\n" );
85 
86  /* Executable statements */
87  printf( "LAPACKE_dgels (col-major, high-level) Example Program Results\n" );
88  /* Solve least squares problem*/
89  info = LAPACKE_dgels(LAPACK_COL_MAJOR,'N',m,n,nrhs,*A,lda,*b,ldb);
90 
91  /* Print Solution */
92  print_matrix_colmajor( "Solution", n, nrhs, *b, ldb );
93  printf( "\n" );
94  exit( info );
95 } /* End of LAPACKE_dgels Example */
lapack_int LAPACKE_dgels(int matrix_layout, char trans, lapack_int m, lapack_int n, lapack_int nrhs, double *a, lapack_int lda, double *b, lapack_int ldb)
Definition: lapacke_dgels.c:36
void print_matrix_colmajor(char *desc, lapack_int m, lapack_int n, double *mat, lapack_int ldm)
#define LAPACK_COL_MAJOR
Definition: lapacke.h:120
#define lapack_int
Definition: lapacke.h:47
int main(int argc, const char *argv[])