You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

example_DGELS_rowmajor.c 2.9 kB

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