(是的,我知道bojc在技术上只是C。但我的意思是,我用信息和其他东西写了它。我只有java背景,不太了解普通的C),但是它跑得非常慢。所以我写了(我所想的)代码是相同的代码,但现在这组环形的代码产生了不同的值(只对其中的一些数字),而现在我无法在我生命中找出不同的值。我正在做的是循环10次,在母体之间做1次倍数和1次加1次。我希望具有两种语言背景的人能够挑出我错误转出的代码部分。我没有事先为任何一个阵列(那些是硬码和未受到影响的)改变任何东西,所以A1,A2,等等在代码的两部分都有相同的值。
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
这是旧的ObjC代码:
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;
}