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 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*******************************************************************************
  2. * Copyright (C) 2009-2011 Intel Corporation. All Rights Reserved.
  3. * The information and material ("Material") provided below is owned by Intel
  4. * Corporation or its suppliers or licensors, and title to such Material remains
  5. * with Intel Corporation or its suppliers or licensors. The Material contains
  6. * proprietary information of Intel or its suppliers and licensors. The Material
  7. * is protected by worldwide copyright laws and treaty provisions. No part of
  8. * the Material may be copied, reproduced, published, uploaded, posted,
  9. * transmitted, or distributed in any way without Intel's prior express written
  10. * permission. No license under any patent, copyright or other intellectual
  11. * property rights in the Material is granted to or conferred upon you, either
  12. * expressly, by implication, inducement, estoppel or otherwise. Any license
  13. * under such intellectual property rights must be express and approved by Intel
  14. * in writing.
  15. *
  16. ********************************************************************************
  17. */
  18. /*
  19. LAPACKE_dgesv Example.
  20. ======================
  21. The program computes the solution to the system of linear
  22. equations with a square matrix A and multiple
  23. right-hand sides B, where A is the coefficient matrix:
  24. 6.80 -6.05 -0.45 8.32 -9.67
  25. -2.11 -3.30 2.58 2.71 -5.14
  26. 5.66 5.36 -2.70 4.35 -7.26
  27. 5.97 -4.44 0.27 -7.17 6.08
  28. 8.23 1.08 9.04 2.14 -6.87
  29. and B is the right-hand side matrix:
  30. 4.02 -1.56 9.81
  31. 6.19 4.00 -4.09
  32. -8.22 -8.67 -4.57
  33. -7.57 1.75 -8.61
  34. -3.03 2.86 8.99
  35. Description.
  36. ============
  37. The routine solves for X the system of linear equations A*X = B,
  38. where A is an n-by-n matrix, the columns of matrix B are individual
  39. right-hand sides, and the columns of X are the corresponding
  40. solutions.
  41. The LU decomposition with partial pivoting and row interchanges is
  42. used to factor A as A = P*L*U, where P is a permutation matrix, L
  43. is unit lower triangular, and U is upper triangular. The factored
  44. form of A is then used to solve the system of equations A*X = B.
  45. Example Program Results.
  46. ========================
  47. LAPACKE_dgesv (row-major, high-level) Example Program Results
  48. Solution
  49. -0.80 -0.39 0.96
  50. -0.70 -0.55 0.22
  51. 0.59 0.84 1.90
  52. 1.32 -0.10 5.36
  53. 0.57 0.11 4.04
  54. Details of LU factorization
  55. 8.23 1.08 9.04 2.14 -6.87
  56. 0.83 -6.94 -7.92 6.55 -3.99
  57. 0.69 -0.67 -14.18 7.24 -5.19
  58. 0.73 0.75 0.02 -13.82 14.19
  59. -0.26 0.44 -0.59 -0.34 -3.43
  60. Pivot indices
  61. 5 5 3 4 5
  62. */
  63. #include <stdlib.h>
  64. #include <stdio.h>
  65. #include "lapacke.h"
  66. /* Auxiliary routines prototypes */
  67. extern void print_matrix( char* desc, lapack_int m, lapack_int n, double* a, lapack_int lda );
  68. extern void print_int_vector( char* desc, lapack_int n, lapack_int* a );
  69. /* Parameters */
  70. #define N 5
  71. #define NRHS 3
  72. #define LDA N
  73. #define LDB NRHS
  74. /* Main program */
  75. int main() {
  76. /* Locals */
  77. lapack_int n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
  78. /* Local arrays */
  79. lapack_int ipiv[N];
  80. double a[LDA*N] = {
  81. 6.80, -6.05, -0.45, 8.32, -9.67,
  82. -2.11, -3.30, 2.58, 2.71, -5.14,
  83. 5.66, 5.36, -2.70, 4.35, -7.26,
  84. 5.97, -4.44, 0.27, -7.17, 6.08,
  85. 8.23, 1.08, 9.04, 2.14, -6.87
  86. };
  87. double b[LDB*N] = {
  88. 4.02, -1.56, 9.81,
  89. 6.19, 4.00, -4.09,
  90. -8.22, -8.67, -4.57,
  91. -7.57, 1.75, -8.61,
  92. -3.03, 2.86, 8.99
  93. };
  94. /* Print Entry Matrix */
  95. print_matrix( "Entry Matrix A", n, n, a, lda );
  96. /* Print Right Rand Side */
  97. print_matrix( "Right Rand Side", n, nrhs, b, ldb );
  98. printf( "\n" );
  99. /* Executable statements */
  100. printf( "LAPACKE_dgesv (row-major, high-level) Example Program Results\n" );
  101. /* Solve the equations A*X = B */
  102. info = LAPACKE_dgesv( LAPACK_ROW_MAJOR, n, nrhs, a, lda, ipiv,
  103. b, ldb );
  104. /* Check for the exact singularity */
  105. if( info > 0 ) {
  106. printf( "The diagonal element of the triangular factor of A,\n" );
  107. printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
  108. printf( "the solution could not be computed.\n" );
  109. exit( 1 );
  110. }
  111. /* Print solution */
  112. print_matrix( "Solution", n, nrhs, b, ldb );
  113. /* Print details of LU factorization */
  114. print_matrix( "Details of LU factorization", n, n, a, lda );
  115. /* Print pivot indices */
  116. print_int_vector( "Pivot indices", n, ipiv );
  117. exit( 0 );
  118. } /* End of LAPACKE_dgesv Example */
  119. /* Auxiliary routine: printing a matrix */
  120. void print_matrix( char* desc, lapack_int m, lapack_int n, double* a, lapack_int lda ) {
  121. lapack_int i, j;
  122. printf( "\n %s\n", desc );
  123. for( i = 0; i < m; i++ ) {
  124. for( j = 0; j < n; j++ ) printf( " %6.2f", a[i*lda+j] );
  125. printf( "\n" );
  126. }
  127. }
  128. /* Auxiliary routine: printing a vector of integers */
  129. void print_int_vector( char* desc, lapack_int n, lapack_int* a ) {
  130. lapack_int j;
  131. printf( "\n %s\n", desc );
  132. for( j = 0; j < n; j++ ) printf( " %6i", a[j] );
  133. printf( "\n" );
  134. }