How to perform inverse matrix operation using acceleration view?

I would like to find the inverse matrix.

I know that this involves LU factorization first and then the inverse step, but I cannot find the function I need by searching for apple 10.7 documents!

This seems like a useful entry for Symmetric matrix inversion in C using CBLAS / LAPACK , indicating that the sgetrf_ and sgetri_ functions should be used. However, when searching for these terms, I find nothing in the Xcode docs.

Does anyone have a boiler plate code for this matrix operation?

+5
source share
1 answer

Apple doesn't document LAPACK code at all, I think, because they just implement the standard interface from netlib.org . It’s a shame that you cannot look for these function names from the built-in Xcode documents, however the solution is pretty straightforward: just provide the function name in the URL, for example. for dgetrf_() go to http://www.netlib.org/clapack/what/double/dgetrf.c .

To invert the matrix, two LAPACK functions are needed: dgetrf_() , which performs LU factorization, and dgetri_() , which displays the result of the previous function and performs the actual inversion.

I created a standard application project using Xcode, added the Accelerate Framework, created two C files: matinv.h, matinv.c, and edited the main.m file to remove Cocoa stuff:

 // main.m #import "matinv.h" int main(int argc, char *argv[]) { int N = 3; double A[N*N]; A[0] = 1; A[1] = 1; A[2] = 7; A[3] = 1; A[4] = 2; A[5] = 1; A[6] = 1; A[7] = 1; A[8] = 3; matrix_invert(N, A); // [ -1.25 -1.0 3.25 ] // A^-1 = [ 0.5 1.0 -1.5 ] // [ 0.25 0.0 -0.25 ] return 0; } 

Now the header file,

 // matinv.h int matrix_invert(int N, double *matrix); 

and then the source file,

 int matrix_invert(int N, double *matrix) { int error=0; int *pivot = malloc(N*sizeof(int)); // LAPACK requires MIN(M,N), here M==N, so N will do fine. double *workspace = malloc(N*sizeof(double)); /* LU factorisation */ dgetrf_(&N, &N, matrix, &N, pivot, &error); if (error != 0) { NSLog(@"Error 1"); free(pivot); free(workspace); return error; } /* matrix inversion */ dgetri_(&N, matrix, &N, pivot, workspace, &N, &error); if (error != 0) { NSLog(@"Error 2"); free(pivot); free(workspace); return error; } free(pivot); free(workspace); return error; } 
+13
source

All Articles