2012-08-14 15 views
7

Tengo dos matrices: A y B.Trabajar con matrices, acelerar marco, el IOS

  1. ¿Cómo puedo almacenar?
  2. ¿Cómo puedo calcular la matriz inversa de la matriz A usando el marco Accelerate?
  3. ¿Cómo se puede encontrar el producto de A * B?
  4. ¿Cómo puedo transponer la matriz A usando el marco Accelerate?

¡Gracias por responder mis preguntas!

archivo Asistente de

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

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

archivo de implementación

#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; 
} 

Llame a mi código de función principal

#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; 
} 

Respuesta

8

Todavía estoy un poco familiarizado con el uso del marco acelerar pero voy responde lo que pueda

  1. El marco de aceleración espera que las matrices se pasen como una matriz 1D . Entonces, si tiene una matriz de 4x4, la primera fila se ubicará en en los índices 0-3 de su matriz, la segunda regla se colocará en los índices 4-7, y así sucesivamente.
  2. Nunca lo he hecho, pero esta respuesta parece un buen punto de partida. https://stackoverflow.com/a/11321499/385017
  3. El método que desea utilizar es vDSP_mmul para precisión simple o vDSP_mmulD para precisión doble. Es posible que desee consultar el documentation para obtener una mejor comprensión de cómo usarlo, pero aquí hay un ejemplo para comenzar.

    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. 
    
+0

Gracias por su repetición! Me gustaría saber si esta biblioteca funciona en el proyecto de iOS. Y si lo hace, ¿cómo puedo incluir esta biblioteca en mi proyecto de Xcode? –

+1

Sí, el marco de aceleración está disponible para iOS. Puede agregarlo siguiendo estas instrucciones. http://stackoverflow.com/a/3377682/385017 –

+0

¡Eso es perfecto! He compilado este ejemplo (http://stackoverflow.com/questions/11282746/how-to-perform-matrix-inverse-operation-using-the-accelerate-framework/11321499#11321499). Pero, cuando escribí el contenedor de clase para estas funciones, no pude compilar mi proyecto debido al mensaje de error "excepción no detectada 'NSInvalidArgumentException', razón: '- [Working_with_matrices invert_matrix: andWithMatrix:]: selector no reconocido enviado a instancia 0x10750dd90' ". ¿Sería tan amable de explicar cómo puedo solucionar este error? ¡Gracias! –

Cuestiones relacionadas