8

I have two matrices: A and B.

  1. How can I store them?
  2. How can I calculate the inverse matrix of matrix A using the Accelerate framework?
  3. How can find the product of A*B?
  4. How can I transpose matrix A using the Accelerate framework?

Thank you for answering my questions!

Helper file

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

@interface Working_with_matrices : NSObject
-(int)invert_matrix:(int) N andWithMatrix:(double*) matrix;
@end

Implementation file

#import "Working_with_matrices.h"
#include <Accelerate/Accelerate.h>

@implementation Working_with_matrices
-(int) matrix_invert:(int) N andWithMatrix:(double*)matrix
{    
int error=0;
int *pivot = malloc(N*N*sizeof(int));
double *workspace = malloc(N*sizeof(double));

dgetrf_(&N, &N, matrix, &N, pivot, &error);

if (error != 0) {
    NSLog(@"Error 1");
    return error;
}

dgetri_(&N, matrix, &N, pivot, workspace, &N, &error);

if (error != 0) {
    NSLog(@"Error 2");
    return error;
}

free(pivot);
free(workspace);
return error;
}

Call my code from main function

#import <Foundation/Foundation.h>
#import "Working_with_matrices.h"

int main(int argc, const char * argv[])
{
int N = 3;
double A[9];
Working_with_matrices* wm=[[Working_with_matrices alloc]init];

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;
[wm invert_matrix:N andWithMatrix:A];
//        [ -1.25  -1.0   3.25 ]
// A^-1 = [  0.5    1.0  -1.5  ]
//        [  0.25   0.0  -0.25 ] 
for (int i=0; i<9; i++) 
{
    NSLog(@"%f", A[i]);
}
return 0;
}
cjohnson318
  • 3,154
  • 30
  • 33
mr.M
  • 851
  • 6
  • 23
  • 41

1 Answers1

8

I'm still kinda new to using the accelerate framework but I'll answer what I can.

  1. The accelerate framework expects the matrices to be passed in as a 1D array. So if you have a 4x4 matrix, the first row would be placed in indexes 0-3 of your array, the second rouw would be placed in indexes 4-7 and so on.
  2. I've never done it but this answer looks like a good starting point. https://stackoverflow.com/a/11321499/385017
  3. The method you'll want to use is vDSP_mmul for single precision or vDSP_mmulD for double precision. You might want to look at the documentation for it to get a better unerstanding of how to use it but heres an example to get you started.

    float *matrixA;  //set by you
    float *matrixB;  //set by you
    float *matrixAB; //the matrix that the answer will be stored in
    
    vDSP_mmul( matrixA, 1, matrixB, 1, matrixAB, 1, 4, 4, 4 );
    // the 1s should be left alone in most situations
    // The 4s in order are:
    //     the number of rows in matrix A
    //     the number of columns in matrix B
    //     the number of columns in matrix A and the number of rows in matrix B.
    
Community
  • 1
  • 1
Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • Thank you for your replay! I'd like to know doest this library work in iOS project? And if it does, how can I include this library into my Xcode project? – mr.M Aug 14 '12 at 18:57
  • 1
    Yes, the accelerate framework is available for iOS. You can add it by following these instructions. http://stackoverflow.com/a/3377682/385017 – Craig Siemens Aug 14 '12 at 19:02
  • That's perfect! I've compiled this example (http://stackoverflow.com/questions/11282746/how-to-perform-matrix-inverse-operation-using-the-accelerate-framework/11321499#11321499). But, when I wrote the class wrapper for this functions, I couldn't compile my project due to error message "to uncaught exception 'NSInvalidArgumentException', reason: '-[Working_with_matrices invert_matrix:andWithMatrix:]: unrecognized selector sent to instance 0x10750dd90'". Would you be so kind to explaine how can I fix this error? Thank you! – mr.M Aug 15 '12 at 14:32
  • The error usually means that the object that your're calling `invert_matrix:andWithMatrix:` on does not implement that method. I'd suggest logging the object that your calling the method on to see if its the correct kind. Other than that, its tough to say without seeing your code. – Craig Siemens Aug 15 '12 at 14:52
  • Would you be so kind to see my code in the beginning of this topic(I didn't post it into comments). – mr.M Aug 15 '12 at 14:56
  • 1
    It looks like in you header your have `invert_matrix` and in your implementation you have `matrix_invert` when they should be the same. You should be getting a complier warning about this. – Craig Siemens Aug 15 '12 at 15:13
  • OMG!!1 I need to relax=))Thank for your reply! I really appreciate it!)) – mr.M Aug 15 '12 at 15:39
  • @CleverError, with that code I get the following warning "Incompatible pointer types passing 'float **' to parameter of type 'float *'; remove &". Is this suppose to happen? – dwbrito May 19 '13 at 13:11