是否存在生成随机矩阵的特定形式或算法,其中所有列的范数均等于1?
我将在c / obj-c中执行算法。然而,如何做到这一点的一般想法将适合我!
答案 0 :(得分:0)
我做到了。
在那个回答中有两件事是错的:
这是正确的代码。顺便说一句,我使用了来自this库的代码段,它实现了Ziggurat算法。
+ (NSMutableArray *) generateNormalPseudoRandomNumbers:(NSInteger)n {
NSMutableArray *randomNumberArray = [[NSMutableArray alloc] initWithCapacity:n];
unsigned long int seed;
for (NSInteger j = 0; j < 3; j++ )
{
if ( ( j % 2 ) == 0 )
{
seed = 123456789;
}
else
{
seed = 987654321;
}
for (NSInteger i = 1; i <= 10; i++ )
{
float value = [Utils r4_uni:&seed];
[randomNumberArray addObject:[NSNumber numberWithFloat:value]];
}
}
return randomNumberArray;
}
+ (double) computeVectorNorm:(NSMutableArray *) matrix {
NSInteger x = [matrix count];
double norm = 0;
for(NSInteger i=0; i<x;i++){
norm += pow([[matrix objectAtIndex:i] doubleValue], 2.0f);
}
norm = sqrt(norm);
return norm;
}
+ (NSMutableArray *) normalizeRandomNumberArray:(NSMutableArray *) randomNumberArray {
double norm = [self computeVectorNorm:randomNumberArray];
for(NSInteger i=0; i< [randomNumberArray count]; i++) {
NSNumber *normalizedNumber = [NSNumber numberWithDouble:([[randomNumberArray objectAtIndex:i] doubleValue] / norm)];
[randomNumberArray replaceObjectAtIndex:i withObject:normalizedNumber];
}
return randomNumberArray;
}
+ (double *) generateAMatrix:(NSInteger)k d:(NSInteger)d {
double *matrix = (double *) malloc(k * d * sizeof(double));
for(NSInteger i=0; i < k; i++) {
NSMutableArray *column = [self normalizeRandomNumberArray:[self generateNormalPseudoRandomNumbers:d]];
for(NSInteger j=0; j<d; j++) {
matrix[i*d+j] = [[column objectAtIndex:j] doubleValue];
}
}
return matrix;
}