absolute value for complex matrix

Hi everyone.

What is the best way to find absolute value of each of the elements of a complex matrix in c++? In this case, I generate my complex matrix (real and imaginary) as below:


int main()
{
int i, j;
int N = 2;
complex <float> a[2][2], b[2][2];

a[0][0].real(20.0); a[0][0].imag(0.0);
a[0][1].real(4.0); a[0][1].imag(2.0);
a[1][0].real(2.0); a[1][0].imag(-4.0);
a[1][1].real(20.0); a[1][1].imag(0.0);

for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
b[i][j]= abs(a[i][j]);
cout << "absolute a[i][j]=" << b[i][j]<< endl;
}
}

cout << "\n";
system("pause");
return 0;
}

Thank you.
What do you mean "what is the best way"? It's probably best to store the absolute value in a float since it's just a scalar value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>
#include <complex>
using namespace std;

int main() {
    const int N = 2;
    complex<float> a[N][N] = {
        { {20,0}, {4, 2} },
        { {2,-4}, {20,0} }
    };
    float b[N][N];

    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            b[i][j] = abs(a[i][j]);

    cout << fixed << setprecision(2);
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            cout << a[i][j] << ' ';
        cout << '\n';
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            cout << b[i][j] << ' ';
        cout << '\n';
    }
}

Thank you. But how I am going to calculate absoulte value manually? What I mean is:

Absolute value of a[i][j] = sqrt(a.real*areal + a.imag*a.imag).

How I am going to seperate the real and imaginary parts so that I can fix into he equation above? I would like to do this because I faced a problem to use "abs" in Vivado HLS which is for hardware implementation.
Last edited on
I don't know anything about Vivado HLS. I assume it has it's own C++ compiler? Is it giving you error messages? If so, post them.
Actually I want to compare the result of complex matrix with the reference matrix which is also a complex matrix. So, what I would like to do is to compare the absolute value of each of these matrices. From the errors, I think I should write the correct command that suit with the hardware. These are the errors that appreared when I compiled my algorithm in Vivado:



no matching function for call to 'abs(cfloat_t&)'
../../../../complexmultiplication_tb.cpp:48:17: note: candidates are:
c:\xilinx\vivado\2018.1\msys\bin\../lib/gcc/mingw32/4.6.2/../../../../include/stdlib.h:374:37: note: int abs(int)
c:\xilinx\vivado\2018.1\msys\bin\../lib/gcc/mingw32/4.6.2/../../../../include/stdlib.h:374:37: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'int'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:1219:14: note: template<int I> ap_uint<_AP_W2> hls::abs(ap_uint<_AP_W2>)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:1215:13: note: template<int I> ap_int<_AP_W2> hls::abs(ap_int<_AP_W2>)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:1211:18: note: template<int W, int I> ap_ufixed<W, I> hls::abs(ap_ufixed<W, I>)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:1207:17: note: template<int W, int I> ap_fixed<W, I> hls::abs(ap_fixed<W, I>)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:155:12: note: uint32_t hls::abs(uint32_t)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:155:12: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'uint32_t {aka unsigned int}'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:153:12: note: uint16_t hls::abs(uint16_t)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:153:12: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'uint16_t {aka short unsigned int}'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:152:11: note: int16_t hls::abs(int16_t)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:152:11: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'int16_t {aka short int}'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:151:11: note: uint8_t hls::abs(uint8_t)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:151:11: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'uint8_t {aka unsigned char}'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:150:10: note: int8_t hls::abs(int8_t)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:150:10: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'int8_t {aka signed char}'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:147:10: note: half hls::abs(half)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:147:10: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'half'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:146:10: note: float hls::abs(float)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:146:10: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'float'
C:/Xilinx/Vivado/2018.1/include/hls_math.h:145:10: note: double hls::abs(double)
C:/Xilinx/Vivado/2018.1/include/hls_math.h:145:10: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'double'
C:/Xilinx/Vivado/2018.1/include/hls/utils/x_hls_utils.h:149:14: note: long int hls::abs(long int)
C:/Xilinx/Vivado/2018.1/include/hls/utils/x_hls_utils.h:149:14: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'long int'
C:/Xilinx/Vivado/2018.1/include/hls/utils/x_hls_utils.h:145:9: note: int32_t hls::abs(int32_t)
C:/Xilinx/Vivado/2018.1/include/hls/utils/x_hls_utils.h:145:9: note: no known conversion for argument 1 from 'cfloat_t {aka hls::x_complex<float>}' to 'int32_t {aka int}'
../../../../complexmultiplication_tb.cpp:48:39: error: no matching function for call to 'abs(cfloat_t&)'
Last edited on
Try
 
    b[i][j] = sqrt(a.real() * a.real() + a.imag() * a.imag());

But in the first place, may I know how to define real and imaginary part of z in the header file.

complex<float> a[N][N] = {
{ {20,0}, {4, 2} },
{ {2,-4}, {20,0} }
};

There are still errors:
1. sqrt - "Invalid argument: Candidates are - Double sqrt(double)"
2. real - "Method 'real' could not be resolved"
3. imag - "Method 'imag' could not be resolved"
Last edited on
Topic archived. No new replies allowed.