English 中文(简体)
将目标C转换为 C 矩阵操纵
原标题:Converting Objective C to C Matrix Manipulation

(是的,我知道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;
}
最佳回答

这个问题与先前的内存管理问题有关,这些问题导致在某些计算中使用0秒。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签