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_DGESV_rowmajor.c 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. LAPACKE_dgesv Example
  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. The routine solves for X the system of linear equations A*X = B,
  11. where A is an n-by-n matrix, the columns of matrix B are individual
  12. right-hand sides, and the columns of X are the corresponding
  13. solutions.
  14. The LU decomposition with partial pivoting and row interchanges is
  15. used to factor A as A = P*L*U, where P is a permutation matrix, L
  16. is unit lower triangular, and U is upper triangular. The factored
  17. form of A is then used to solve the system of equations A*X = B.
  18. LAPACKE Interface
  19. =================
  20. LAPACKE_dgesv (row-major, high-level) Example Program Results
  21. -- LAPACKE Example routine --
  22. -- LAPACK is a software package provided by Univ. of Tennessee, --
  23. -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <lapacke.h>
  29. #include "lapacke_example_aux.h"
  30. /* Main program */
  31. int main(int argc, char **argv) {
  32. /* Locals */
  33. lapack_int n, nrhs, lda, ldb, info;
  34. int i, j;
  35. /* Local arrays */
  36. double *A, *b;
  37. lapack_int *ipiv;
  38. /* Default Value */
  39. n = 5; nrhs = 1;
  40. /* Arguments */
  41. for( i = 1; i < argc; i++ ) {
  42. if( strcmp( argv[i], "-n" ) == 0 ) {
  43. n = atoi(argv[i+1]);
  44. i++;
  45. }
  46. if( strcmp( argv[i], "-nrhs" ) == 0 ) {
  47. nrhs = atoi(argv[i+1]);
  48. i++;
  49. }
  50. }
  51. /* Initialization */
  52. lda=n, ldb=nrhs;
  53. A = (double *)malloc(n*n*sizeof(double)) ;
  54. if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
  55. b = (double *)malloc(n*nrhs*sizeof(double)) ;
  56. if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
  57. ipiv = (lapack_int *)malloc(n*sizeof(lapack_int)) ;
  58. if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }
  59. for( i = 0; i < n; i++ ) {
  60. for( j = 0; j < n; j++ ) A[i*lda+j] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
  61. }
  62. for(i=0;i<n*nrhs;i++)
  63. b[i] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
  64. /* Print Entry Matrix */
  65. print_matrix_rowmajor( "Entry Matrix A", n, n, A, lda );
  66. /* Print Right Rand Side */
  67. print_matrix_rowmajor( "Right Rand Side b", n, nrhs, b, ldb );
  68. printf( "\n" );
  69. /* Executable statements */
  70. printf( "LAPACKE_dgesv (row-major, high-level) Example Program Results\n" );
  71. /* Solve the equations A*X = B */
  72. info = LAPACKE_dgesv( LAPACK_ROW_MAJOR, n, nrhs, A, lda, ipiv,
  73. b, ldb );
  74. /* Check for the exact singularity */
  75. if( info > 0 ) {
  76. printf( "The diagonal element of the triangular factor of A,\n" );
  77. printf( "U(%" LAPACK_IFMT ",%" LAPACK_IFMT ") is zero, so that A is singular;\n", info, info );
  78. printf( "the solution could not be computed.\n" );
  79. exit( 1 );
  80. }
  81. if (info <0) exit( 1 );
  82. /* Print solution */
  83. print_matrix_rowmajor( "Solution", n, nrhs, b, ldb );
  84. /* Print details of LU factorization */
  85. print_matrix_rowmajor( "Details of LU factorization", n, n, A, lda );
  86. /* Print pivot indices */
  87. print_vector( "Pivot indices", n, ipiv );
  88. exit( 0 );
  89. } /* End of LAPACKE_dgesv Example */