zerowords
zerowords

Reputation: 3113

pass float array to (void*) function argument

I am using cblas_icamax. I want to pass a float vector z13] to cblas_icamax (see below). 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?

-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

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
);

Upvotes: 0

Views: 643

Answers (2)

Stephen Canon
Stephen Canon

Reputation: 106197

You have 13 floats in your array.

You are promising the library that you have 13 float complex values in your array (i.e. 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.

To more precisely explain what is happening: cblas_icamax returns the index of the complex float with maximum L1 norm. Each complex float is a pair of floating-point values from the input. If we look at the L1 norms of your inputs:

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; out of the fields that are initialized, it has the maximum L1 norm. Of course, the actual behavior is undefined, since you're telling the function to read from uninitialized (and potentially unmapped) memory.

Upvotes: 2

zerowords
zerowords

Reputation: 3113

I should have called cblas_isamax, not cblas_icamax because my array is not complex numbers.

Upvotes: 1

Related Questions