objective-c - 将 Objective C 转换为 C 矩阵操作

标签 objective-c c xcode matrix matrix-multiplication

好吧,所以我用所有 Objective C 编写了工作代码(是的,我知道 objc 在技术上只是 C。但我的意思是我用消息和东西编写了它。我只有 java 背景,了解不多关于普通的旧C)但它运行得非常慢。所以我写出(我认为的)相同的代码,但现在这组循环产生不同的值(仅针对某些数字),而我一生都无法弄清楚有什么不同。我正在做的是循环 10 次并在矩阵之间进行 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

这是旧的 Obj c 代码:

 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。

关于objective-c - 将 Objective C 转换为 C 矩阵操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10724078/

相关文章:

xcode - 缺少Base SDK怎么办?

ios - Swift 3 代码中的这些小方 block 符号是什么?

ios - VoiceOver 焦点将 UICollectionView 重置为第一个条目?

iphone - 如何使用 Objective-C 在 iPhone 中查看未接来电?

ios - 我可以在 Realm 的线程中使用被忽略的属性吗?

在 linux 中创建文件时选择 BSD 或 sys5 风格

C、GTK : display stream of RGB images at < 60 fps

html - 在 iOS 上显示 HTML 内容和 native 内容的最佳解决方案是什么?

c - 将函数分配给 packetbuf_copyfrom

iphone - 使用自动布局时动画 View 宽度