Tu, directly in the code:
#include <string> #include "lapacke.h" #include "lapack_aux.h" int main (int argc,char** argv) {setlocale (Lc_all, "" );d ouble a[] = {3,-1,-1,4,-2,-1,-3,2,1};int m = 3;int n = 3;int lda = 3;int ipiv[3];int Info;print_matrix ("a", M,n,a,lda); I NFO = LAPACKE_DGETRF (lapack_row_major,m,n,a,lda,ipiv);p Rint_matrix ("a", m,n,a,lda); info = Lapacke_dgetri (LAPACK_ROW _MAJOR,M,A,LDA,IPIV);p Rint_matrix ("a", M,n,a,lda); return 0;}
the output results are as follows:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz2lzzwxpdgu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
You can also use the following methods:
#include <string> #include "lapacke.h" #include "lapack_aux.h" int main (int argc,char** argv) {setlocale (Lc_all, "" );d ouble a[] = {3,4,-3,-1,-2,2,-1,-1,1};int m = 3;int n = 3;int lda = 3;int ipiv[3];int Info;print_matrix ("a", M,n,a,lda); L APACK_DGETRF (&m,&n,a,&lda,ipiv,&info);p Rint_matrix ("a", M,n,a,lda);d ouble *b =new double[m] ();// The inverse matrix of the common Matrix Lapack_dgetri (&m,a,&lda,ipiv,b,&n,&info);p Rint_matrix ("Inv (A)", M,n,a,lda); return 0;}
The output results are as follows:
The advantage of such a method is that the definition of API interface is consistent with the corresponding Fortran interface, for example Dgetri, we are able to explain the function of double precision (http://www.netlib.org/lapack/double/) Find DGETRI.F in the document, open this Fortran file, you will be able to know the meaning of the corresponding parameters.
Just here to pay attention to the storage matrix. The difference between the two methods. In the first method, we can tell the interface of Lapack through the main sequence that our matrix is in the main sequence of behavior , that is, in the array, the matrix is stored by row, for a 3x3 matrix. The 9 elements entered. The first 3 number is a matrix of the second row, followed by the matrix of the next line, and finally the third row of the Matrix, and another method, there is no main sequence of this parameter, the study found that the FORTRAN default is to be listed as the main sequence , In other words, when we enter a 3x3 matrix with an array, the first 3 numbers represent the 1th column, the 3 numbers are the 2nd column, and the last 3 are the 3rd columns . So at the time of the given matrix, we need to enter by column.
So the array A in Method 2, which is listed as the main order, represents the matrix that is actually this:
3 -1 -1 4 -2 -1 -3 2 1
This is equivalent to changing the main sequence in the first method to lapack_col_major, such as the following:
#include <string> #include "lapacke.h" #include "lapack_aux.h" int main (int argc,char** argv) {setlocale (Lc_all, "" );d ouble a[] = {3,4,-3,-1,-2,2,-1,-1,1};int m = 3;int n = 3;int lda = 3;int ipiv[3];int Info;print_matrix ("a", M,n,a,lda); I NFO = LAPACKE_DGETRF (lapack_col_major,m,n,a,lda,ipiv);p Rint_matrix ("a", m,n,a,lda); info = Lapacke_dgetri (LAPACK_COL _MAJOR,M,A,LDA,IPIV);p Rint_matrix ("a", M,n,a,lda); return 0;}
Finally, we verify in MATLAB, such as the following:
>> a = [3,4,-3,-1,-2,2,-1,-1,1]a = 3 4 -3 -1 -2 2 -1 -1 1>> a = reshape (a,3,3) A = 3 -1 -1 4 -2 -1 -3 2 1>> inv (a) ans = 0 1 1 1 0 1 -2 3 2
We can see that the results of MATLAB are consistent.
Auxiliary functions are attached:
#include <stdio.h> #include "lapack_aux.h"/* auxiliary routine:printing a matrix */void print_matrix (char* desc, LA Pack_int m, Lapack_int N, double* A, Lapack_int lda) {lapack_int I, j;printf ("\ n%s\n", desc); for (i = 0; i < m; i++ {for (j = 0; J < N; j + +) printf ("%6.2f", A[i*lda+j]);p rintf ("\ n");}}
Documentation:
http://blog.csdn.net/kevinzhangyang/article/details/6859246
http://blog.csdn.net/daiyuchao/article/details/2026173
http://blog.csdn.net/daiyuchao/article/details/2026162
Http://www.cnblogs.com/xunxun1982/archive/2010/05/12/1734001.html
Http://www.cnblogs.com/xunxun1982/archive/2010/05/13/1734809.html
Http://hi.baidu.com/data2009/item/50bce0704cf57a14d0dcb3e8
Http://blog.sina.com.cn/s/blog_40b056950100htpt.html
http://blog.csdn.net/cleverysm/article/details/1925553
http://blog.csdn.net/cleverysm/article/details/1925549
Http://www.cnblogs.com/Jedimaster/archive/2008/06/22/1227656.html
Using the Lapack library inverse matrix