gpt4 book ai didi

objective-c - Objective-C 中的矩阵乘法

转载 作者:行者123 更新时间:2023-12-03 17:28:36 24 4
gpt4 key购买 nike

这些是我在项目中进行矩阵计算的一些代码。
它们是两个类方法和一个实例方法,用于创建矩阵并进行矩阵乘法运算。
矩阵乘法的方法不太好用,结果是错误的。

+ (NSMutableArray *)arrayOfWidth:(NSInteger)width andHeight:(NSInteger)height {
return [[self alloc] initWithWidth:width andHeight:height];
}

- (id)initWithWidth:(NSInteger)width andHeight:(NSInteger)height {
if((self = [self initWithCapacity:height])) {
for(int i = 0; i < height; i++) {
NSMutableArray *inner = [[NSMutableArray alloc] initWithCapacity:width];
[self addObject:inner];
}
}
return self;
}

+ (NSMutableArray *)matrixA:(NSMutableArray *)matrixA multiplyMatrixB:(NSMutableArray *)matrixB {
int aRow = [matrixA count];
int aColumn = [[matrixA objectAtIndex:0] count];
int bRow = [matrixB count];
int bColumn = [[matrixB objectAtIndex:0] count];
NSMutableArray *newArray = [NSMutableArray arrayOfWidth:aRow andHeight:bColumn];

for (int i = 0; i < aRow; i++) {
for (int j = 0; j < bColumn; j++) {
double sum = 0.0;
for (int k = 0; k < aColumn; k++) {
NSMutableArray *innerA = [matrixA objectAtIndex:i];
double numA = [[innerA objectAtIndex:k] doubleValue];
NSMutableArray * innerB = [matrixB objectAtIndex:k];
double numB = [[innerB objectAtIndex:j] doubleValue];
sum += numA * numB;
}
NSNumber *result = [NSNumber numberWithDouble:sum];
[[newArray objectAtIndex:i] insertObject:result atIndex:j];
}
}
return newArray;
}

代码有问题吗?我该如何解决它?

<小时/>
    //First, I create a array to hold the numbers
NSNumber *num11 = [NSNumber numberWithDouble:-2.0];
NSNumber *num12 = [NSNumber numberWithDouble:1.0];
NSNumber *num13 = [NSNumber numberWithDouble:-1.0];
NSNumber *num14 = [NSNumber numberWithDouble:2.0];
NSNumber *num21 = [NSNumber numberWithDouble:-7.0];
NSNumber *num22 = [NSNumber numberWithDouble:0.0];
NSNumber *num23 = [NSNumber numberWithDouble:-1.0];
NSNumber *num24 = [NSNumber numberWithDouble:-4.0];
NSNumber *num31 = [NSNumber numberWithDouble:-2.0];
NSNumber *num32 = [NSNumber numberWithDouble:-1.0];
NSNumber *num33 = [NSNumber numberWithDouble:0.0];
NSNumber *num34 = [NSNumber numberWithDouble:-2.0];
NSNumber *num41 = [NSNumber numberWithDouble:-3.0];
NSNumber *num42 = [NSNumber numberWithDouble:-2.0];
NSNumber *num43 = [NSNumber numberWithDouble:0.0];
NSNumber *num44 = [NSNumber numberWithDouble:-3.0];

NSMutableArray *temp = [NSMutableArray arrayWithObjects:num11, num12, num13, num14, num21, num22, num23, num24, num31, num32, num33, num34, num41, num42, num43, num44, nil];

//Second, I create the matrix and get the elements from that array
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
double c = [[temp objectAtIndex:4*i+j] doubleValue];
NSNumber *object = [NSNumber numberWithDouble:c];
[[matrix objectAtIndex:i] insertObject:object atIndex:j];
}
}

//Then, I do the multiplication for matrix and itself
NSMutableArray *multiMatrix = [NSMutableArray matrixA:matrix multiplyMatrixB:matrix];

//get all the elements from the multiMatrix
double m11 = [[[multiMatrix objectAtIndex:0] objectAtIndex:0] doubleValue];
double m12 = [[[multiMatrix objectAtIndex:0] objectAtIndex:1] doubleValue];
double m13 = [[[multiMatrix objectAtIndex:0] objectAtIndex:2] doubleValue];
double m14 = [[[multiMatrix objectAtIndex:0] objectAtIndex:3] doubleValue];

double m21 = [[[multiMatrix objectAtIndex:1] objectAtIndex:0] doubleValue];
double m22 = [[[multiMatrix objectAtIndex:1] objectAtIndex:1] doubleValue];
double m23 = [[[multiMatrix objectAtIndex:1] objectAtIndex:2] doubleValue];
double m24 = [[[multiMatrix objectAtIndex:1] objectAtIndex:3] doubleValue];

double m31 = [[[multiMatrix objectAtIndex:2] objectAtIndex:0] doubleValue];
double m32 = [[[multiMatrix objectAtIndex:2] objectAtIndex:1] doubleValue];
double m33 = [[[multiMatrix objectAtIndex:2] objectAtIndex:2] doubleValue];
double m34 = [[[multiMatrix objectAtIndex:2] objectAtIndex:3] doubleValue];

double m41 = [[[multiMatrix objectAtIndex:3] objectAtIndex:0] doubleValue];
double m42 = [[[multiMatrix objectAtIndex:3] objectAtIndex:1] doubleValue];
double m43 = [[[multiMatrix objectAtIndex:3] objectAtIndex:2] doubleValue];
double m44 = [[[multiMatrix objectAtIndex:3] objectAtIndex:3] doubleValue];

//Or you can use the NSLog to check the result
NSString *lineOne = [NSString stringWithFormat:@"%f, %f, %f, %f", m11, m12, m13, m14];
NSString *lineTwo= [NSString stringWithFormat:@"%f, %f, %f, %f", m21, m22, m23, m24];
NSString *lineThree = [NSString stringWithFormat:@"%f, %f, %f, %f", m31, m32, m33, m34];
NSString *lineFour = [NSString stringWithFormat:@"%f, %f, %f, %f", m41, m42, m43, m44];

@rooftop,这就是所有代码

最佳答案

如果您使用的是 iOS,您可能需要考虑 GLKit 中的矩阵例程。

关于objective-c - Objective-C 中的矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9207989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com