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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 (version 3.5.0) --
  22. -- LAPACK is a software package provided by Univ. of Tennessee, --
  23. -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
  24. February 2012
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <lapacke.h>
  30. #include "lapacke_example_aux.h"
  31. /* Main program */
  32. int main(int argc, char **argv) {
  33. /* Locals */
  34. lapack_int n, nrhs, lda, ldb, info;
  35. int i, j;
  36. double normr, normb;
  37. /* Local arrays */
  38. double *A, *b, *Acopy, *bcopy;
  39. lapack_int *ipiv;
  40. /* Default Value */
  41. n = 5; nrhs = 1;
  42. /* Arguments */
  43. for( i = 1; i < argc; i++ ) {
  44. if( strcmp( argv[i], "-n" ) == 0 ) {
  45. n = atoi(argv[i+1]);
  46. i++;
  47. }
  48. if( strcmp( argv[i], "-nrhs" ) == 0 ) {
  49. nrhs = atoi(argv[i+1]);
  50. i++;
  51. }
  52. }
  53. /* Initialization */
  54. lda=n, ldb=nrhs;
  55. A = (double *)malloc(n*n*sizeof(double)) ;
  56. if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
  57. b = (double *)malloc(n*nrhs*sizeof(double)) ;
  58. if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
  59. ipiv = (lapack_int *)malloc(n*sizeof(lapack_int)) ;
  60. if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }
  61. for( i = 0; i < n; i++ ) {
  62. for( j = 0; j < n; j++ ) A[i*lda+j] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
  63. }
  64. for(i=0;i<n*nrhs;i++)
  65. b[i] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
  66. /* Print Entry Matrix */
  67. print_matrix_rowmajor( "Entry Matrix A", n, n, A, lda );
  68. /* Print Right Rand Side */
  69. print_matrix_rowmajor( "Right Rand Side b", n, nrhs, b, ldb );
  70. printf( "\n" );
  71. /* Executable statements */
  72. printf( "LAPACKE_dgesv (row-major, high-level) Example Program Results\n" );
  73. /* Solve the equations A*X = B */
  74. info = LAPACKE_dgesv( LAPACK_ROW_MAJOR, n, nrhs, A, lda, ipiv,
  75. b, ldb );
  76. /* Check for the exact singularity */
  77. if( info > 0 ) {
  78. printf( "The diagonal element of the triangular factor of A,\n" );
  79. printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
  80. printf( "the solution could not be computed.\n" );
  81. exit( 1 );
  82. }
  83. if (info <0) exit( 1 );
  84. /* Print solution */
  85. print_matrix_rowmajor( "Solution", n, nrhs, b, ldb );
  86. /* Print details of LU factorization */
  87. print_matrix_rowmajor( "Details of LU factorization", n, n, A, lda );
  88. /* Print pivot indices */
  89. print_vector( "Pivot indices", n, ipiv );
  90. exit( 0 );
  91. } /* End of LAPACKE_dgesv Example */