Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Inverting a matrix with Accelerate and Objective-C

Inverting a matrix with Accelerate and Objective-C
Thread Tools
Fresh-Faced Recruit
Join Date: Feb 2010
Status: Offline
Reply With Quote
Feb 11, 2010, 04:36 PM
 
Hi. After an exhaustive search on the internet, I didn't find an answer to my problem. Let me explain it with a simple example.

Suppose I need to solve the system of equations shown below in matrix form: Ax=b.

A = [1 4 3; 2 5 8; 7 6 9 ]
b = [2; 7; 5]

How can I implement the Apple's Accelerate Framework and Objective-C to find x? I mean, x = Inverse(A)*b ?

It's very important to me a solution written entirely in Objective-C. Anyway, any help/solution will be greatly appreciated!

Many thanks in advance.

PS: By the way, x = [-47/80; -9/40; 93/80].

Thank you!
     
Fresh-Faced Recruit
Join Date: Feb 2010
Status: Offline
Reply With Quote
Feb 11, 2010, 09:34 PM
 
Hi. I called a friend mine and finally gave me the answer. Anyway, thanks for those who read the thread and wanted to help.

But if someone knows how to reduce the time it takes to write the matrix A, please let me know!

Thank you!


The code is:

------------------------------------------------------------------
#import <Foundation/Foundation.h>
#import <Accelerate/Accelerate.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// N = number of columns of A.
// NRHS = number of columns of B, usually 1.
// LDA = number of rows of A.
// IPIV = pivot indices.
// LDB = number of rows of B.

int NDIM = 3, N = NDIM, NRHS = 1, LDA = NDIM, LDB = NDIM;
int IPIV[NDIM], INFO;

double *A = (double*)malloc(NDIM*NDIM*sizeof(double));

A[0] = 1; A[3] = 4; A[6] = 3;
A[1] = 2; A[4] = 5; A[7] = 8;
A[2] = 7; A[5] = 6; A[8] = 9;

double *B = (double*)malloc(NDIM*sizeof(double));

B[0] = 2; B[1] = 7; B[2] = 5;

NSLog(@"B = [%f, %f, %f].", B[0], B[1], B[2]);

dgesv_(&N, &NRHS, A, &LDA, IPIV, B, &LDB, &INFO);

NSLog(@"X = [%f, %f, %f].", B[0], B[1], B[2]);


[pool drain];
return 0;
}
------------------------------------------------------------------
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:54 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2