LAPACKE_sgels example

LAPACKE_sgels example

I am trying to follow example available on http://www.nag.com/lapack-ex/node55.html

where they have:

 

DGGLSE Example Program Data

  6      4      2           :Values of M, N and P

 -0.57  -1.28  -0.39   0.25
 -1.93   1.08  -0.31  -2.14
  2.30   0.24   0.40  -0.35
 -1.93   0.64  -0.66   0.08
  0.15   0.30   0.15  -2.13
 -0.02   1.03  -1.43   0.50 :End of matrix A

  1.00   0.00  -1.00   0.00
  0.00   1.00   0.00  -1.00 :End of matrix B

 -1.50
 -2.14
  1.23
 -0.54
 -1.68
  0.82                      :End of vector c

  0.00
  0.00                      :End of vector d

 

 DGGLSE Example Program Results

 Constrained least-squares solution
      0.4890     0.9975     0.4890     0.9975

 Square root of the residual sum of squares
      2.51E-02

 

I am using following code:

                int LAPACK_ROW_MAJOR = 101;
                int LAPACK_COL_MAJOR = 102;
                int M = 6;
                int N = 4;
                int NRHS = 2;
                int LDA = M;
                int LDB = M;
                int m = M, n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;

                double[] a = new double[6 * 4] { //LDA *N
                    -0.57,  -1.28,  -0.39,   0.25,
                    -1.93,   1.08 , -0.31,  -2.14,
                    2.30 ,  0.24 ,  0.40 , -0.35,
                    -1.93,   0.64,  -0.66 ,  0.08,
                    0.15 ,  0.30  , 0.15 , -2.13,
                    -0.02 ,  1.03 , -1.43 ,  0.50 
                  };
                double[] b = new double[4 * 2]  {  //LDB*NRHS
                1.00,   0.00,  -1.00,   0.00,
                0.00,   1.00,   0.00 ,-1.00
                };
                double[] c = new double[6 ]  {  //LDB*NRHS
                -1.50,
                -2.14,
                 1.23,
                -0.54,
                -1.68,
                 0.82  
                };
                double[] d = new double[2]  {  //LDB*NRHS
                0.00,
                0.00 
                };
                double[] x = new double[4] {  //LDB*NRHS
                0.00,
                0.00,
                0.00,
                0.00 
                };
                /* Executable statements */
                Console.WriteLine("LAPACKE_dgglse (column-major, high-level) ");
                /* Solve the equations A*X = B */
                info = MKLImports.LAPACKE_dgglse(LAPACK_COL_MAJOR, m, n, nrhs, a, lda, b, ldb, c, d, x);
                Console.WriteLine(info);
                
                Console.WriteLine("Least squares solution");
                for (int j = 0; j < n; j++)
                    Console.Write(" \t" +x[j]);

However I am getting 

LAPACKE_dgglse (column-major, high-level)

0

Least squares solution
        0.214694309349513       0.104791270039643       0.16310231377302       0.127794231755662

Which is not correct, could you please tell me what am I doing wrong?

Thanks

 

I am using function like

 static extern int LAPACKE_dgglse( 
             int matrix_order, 
             int m,               //number of rows of the matrix A (m ≥ 0).             
             int n,               //number of columns of the matrices A and B (n ≥ 0). 
             int p,               //number of rows of the matrix B (0 ≤ p ≤ n ≤ m+p).
             [In, Out] double[] a,  //contains the m-by-n matrix A.
             int lda,              //The second dimension of a must be at least max(1, n).
             [In, Out] double[] b,  // contains the p-by-n matrix B.
             int ldb,              //The second dimension of b must be at least max(1, n).
             [In, Out] double[] c,  //dimension at least max(1, m), contains the right hand side vector for the least squares part of the LSE problem. 
             double[] d,  //dimension at least max(1, p), contains the right hand side vector for the constrained equation. 
             [In, Out] double[] x 
             );

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

The matrix b is 2-by-4 column-major, but you defined ldb=6, which does not look right. I'd think ldb=2.
 

One more thing. All your matrices are row-major (because it is C or C++ code). But the original example from NAG is column-major (it came from FORTRAN code sample). So you really want to call LAPACKE_dgglse with 'LAPACK_ROW_MAJOR' to be consistent with the actual memory layout of your matrices.

Leave a Comment

Please sign in to add a comment. Not a member? Join today