2012-05-23 3 views
0

좋아요, 그래서 모든 객관적인 C로 작성된 코드를 가지고 있습니다 (예, objc는 기술적으로 C라는 것을 알고 있습니다.하지만 메시지와 물건으로 쓰여졌습니다. 자바 배경과 평범한 오래된 C에 대해 많이 알지는 못하지만 믿을 수 없을만큼 천천히 달렸다. 그래서 나는 썼습니다 (제가 생각한 것)는 같은 코드였습니다. 그러나 이제는이 루프 세트가 다른 값 (일부 숫자 만)을 생성하고 나 자신의 삶에 대해 다른 것을 알아낼 수 없습니다. 내가하고있는 일은 10 번 반복하고 matricies 사이에 1 곱셈과 1 덧셈을하는 것이다. 두 언어로 된 배경을 가진 사람이 필자가 잘못 복사 한 코드 부분을 찾아 낼 수 있기를 바랍니다. 나는 어떤 배열에 대해서도 미리 변경하지 않았기 때문에 (A1과 A2는 하드 코딩되어 영향을받지 않았다.) 그래서 A1, A2 등은 코드의 두 부분에서 같은 값을 가진다. C에서목표 C에서 C로 변환 매트릭스 조작

현재 코드 :

for (int m = 0; m < 10; m++) { 

    //Do matrix multiplication between A1 and A2. Store in temporary B1 
    for(int i = 0; i < 13; i++) 
     for(int j = 0; j < 43; j++) { 
      double tempTotal = 0; 
      for(int k = 0; k < 43; k++){ 
       tempTotal = tempTotal + A1[i][k] * A2[k][j]; 
      } 
      B1[i][j] = tempTotal; 
     } 

    //Assign B1 data back into A1 after the multiplication is finished 
    for(int i = 0; i < 13; i++) 
     for(int j = 0; j<43; j++) 
      A1[i][j] = B1[i][j]; 

    //Add C1 and A1. Store into C1. 
    for (int l = 0; l < 13; l++) 
     for (int n = 0; n < 43; n++) 
      C1[l][n] = C1[l][n] + A1[l][n]; 

}//end m for loop 

이전의 Obj C 코드이었다

이 문제는 0의 일부 계산에 사용되는 원인 이전에 메모리 관리 문제와 함께해야 할 일을했을
for (int m = 0; m < 10; m++) { 
    //multiply A1 and A2. Store into A1 
    A1 = [LCA_Computation multiply:A1 withArray:A2]; //LCA_Computation is the name of the .m class file in which this all happens. 

    //Add C1 and A1. Store into C1 
    for (int i = 0; i < 13; i++) 
     for (int j = 0; j < 43; j++) 
      [[C1 objectAtIndex:i] replaceObjectAtIndex:j withObject:[NSNumber numberWithDouble: [[[C1 objectAtIndex: i] objectAtIndex: j] doubleValue] + [[[A1 objectAtIndex: i] objectAtIndex: j] doubleValue]]]; 

}//end m for loop 

//multiply method 
    + (NSMutableArray*)multiply:(NSMutableArray*)a1 withArray:(NSMutableArray*)a2 
{ 
    int a1_rowNum = [a1 count]; 
    int a2_rowNum = [a2 count]; 
    int a2_colNum = [[a2 objectAtIndex:0] count]; 
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:a1_rowNum]; 
    for (int i = 0; i < a1_rowNum; i++) { 
     NSMutableArray *tempRow = [NSMutableArray arrayWithCapacity:a2_colNum]; 
     for (int j = 0; j < a2_colNum; j++) { 
      double tempTotal = 0; 
      for (int k = 0; k < a2_rowNum; k++) { 
       double temp1 = [[[a1 objectAtIndex:i] objectAtIndex:k] doubleValue]; 
       double temp2 = [[[a2 objectAtIndex:k] objectAtIndex:j] doubleValue]; 
       tempTotal += temp1 * temp2; 
      } 
      //the String format is intentional. I convert them all to strings later. I just put it in the method here where as it is done later in the C code 
      [tempRow addObject:[NSString stringWithFormat:@"%f",tempTotal]]; 
     } 
     [result addObject:tempRow]; 
    } 
    return result; 
} 
+1

추가 및 곱셈 코드가 모두 적합합니다. 할당을 확인하고 어레이를 지우고 valgrind를 통해 실행하여 미묘한 메모리 오류가 없는지 확인하십시오. – dasblinkenlight

+1

낯선 사람에게 검사하여 코드의 오류를 찾아내는 것은 생산성이 떨어집니다. 테스트 할 작은 간단한 데이터 세트를 사용하여 디버거 (또는 print 문)를 사용하여 코드 동작이 다른 부분을 식별해야합니다. –

+0

xcode에 디버거가 있습니까? 그렇다면 어떻게 사용합니까? NSLog는 다양한 장소에서 테스트를 실행했지만 아직 성공하지 못했습니다. – MrHappyAsthma

답변