简体   繁体   English

将float数组传递给(void *)函数参数

[英]pass float array to (void*) function argument

I am using cblas_icamax . 我正在使用cblas_icamax I want to pass a float vector z13] to cblas_icamax (see below). 我想将浮点向量z13]传递给cblas_icamax(请参见下文)。 My code includes the following. 我的代码包括以下内容。

    float z[13] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f ,0.f, 0.f, 0.f, 0.f, 0.f};
//...

    for (i = 0; i < 13; i++) {
        printf("%.4f\n", z[i]);
    }
    int cardIndex = cblas_icamax(13,(float*) z,1);
    NSLog(@"cardIndex: %d", cardIndex);

I also tried this code with the same result. 我也尝试了相同的结果此代码。

int cardIndex = cblas_icamax(13,&z,1);

And the result of the print is as follows for which the maximum absolute value is 138.1086 which is position 10, but the function cblas_icamax is returning 5. What is wrong? 打印结果如下,最大绝对值为138.1086,位置10,但是函数cblas_icamax返回5。出了什么问题?

-1.2624
74.1524
52.3533
89.9426
28.8639
-7.6203
-30.2820
48.9747
124.8693
29.4351
138.1086
36.2638
-45.0410

cblas_icamax cblas_icamax

Returns the index of the element with the largest absolute value in a vector (single-precision complex). 返回向量(单精度复数)中具有最大绝对值的元素的索引。

int cblas_icamax (
   const int N,
   const void *X,
   const int incX
);

You have 13 floats in your array. 您的数组中有13个floats

You are promising the library that you have 13 float complex values in your array (ie 26 floats ). 您向图书馆保证,数组中有13个float complex值(即26个floats )。 It should not be surprising that the behavior is not as expected. 行为不符合预期也就不足为奇了。 Either pass an array of complex data as inputs, or use cblas_isamax instead. 可以将复杂数据数组作为输入传递,或者使用cblas_isamax代替。

To more precisely explain what is happening: cblas_icamax returns the index of the complex float with maximum L1 norm. 为了更精确地解释正在发生的情况: cblas_icamax返回具有最大L1范数的复杂浮点的索引。 Each complex float is a pair of floating-point values from the input. 每个复数浮点都是来自输入的一对浮点值。 If we look at the L1 norms of your inputs: 如果我们看一下您输入的L1规范:

index        value            L1 norm
0      -1.2624 + 74.1524i     75.4148
1      52.3533 + 89.9426i    142.2959
2      28.8639 -  7.6203i     36.4842
3     -30.2820 + 48.9747i     79.2567
4     124.8693 + 29.4351i    154.3044
5     138.1086 + 36.2638i    174.3724
6     -45.0410 + ???i           ???
7          ??? + ???i           ???
8          ??? + ???i           ???
9          ??? + ???i           ???
10         ??? + ???i           ???
11         ??? + ???i           ???
12         ??? + ???i           ???

The '???' '???' fields indicate uninitialized data. 字段指示未初始化的数据。 So the result that cblas_icamax is returning certainly seems reasonable; 因此, cblas_icamax返回的结果肯定是合理的; out of the fields that are initialized, it has the maximum L1 norm. 在初始化的字段中,它具有最大的L1范数。 Of course, the actual behavior is undefined, since you're telling the function to read from uninitialized (and potentially unmapped) memory. 当然,实际行为是不确定的,因为您要告诉函数从未初始化的(并且可能是未映射的)内存中读取。

我应该叫cblas_isamax,而不是cblas_icamax,因为我的数组不是复数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM