1

I have an array of size 10 which has values from 0 to 4 as real parts (integers) and from 5 to 9 as imaginary parts (integers) from a file which had 5 complex no.'s (I created a new array of twice the size to hold real and imaginary parts from the original complex stream). I have to multiply them with another array of integers (non-complex), how do I go about it?

for illustration

complex_file = 2 + 5j, 1 + 6j, 3 + 7j;
new_array = 2,1,3,5,6,7 (first 3 as real parts, the next 3 as imag ones)
mult_coeff = 11,12,13 (diff integer array which needs to be multiplied with the complex values)

Here's the catch, output needs to be represented as complex.

melpomene
  • 84,125
  • 8
  • 85
  • 148
samz12
  • 61
  • 1
  • 1
  • 7

2 Answers2

1

A complex value multiplied by a strictly real value changes the magnitude but not the phase angle of the complex value. Thus you can multiply the components (real part and “imaginary” part) of the complex values separately by the real value since there are no cross products that would result in a change of phase angle.

So if you consider the format of your input to be a complex data type, the multiplied output will be similarly complex, after multiplying all the components by a strictly real scale factor. If not, you can shuffle or repack the components into whatever resulting complex vector format or data type you require.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

How about storing real and imaginary parts in different arrays? This will simplify your problem:

int re[]={2, 1, 3}, im[]={5, 6, 7};
int coeff[]={11, 12, 13};
int size=3     // No of items, for this example it's 3
for(int i=0; i<size; i++){
    re[i]*=coeff[i];
    im[i]*=coeff[i];
}

However if you would like to return the result from a function, you better use built in complex library of C :

#include <stdio.h>      /* Standard Library of Input and Output */
#include <complex.h>    /* Standard Library of Complex Numbers */

int main() {

    double complex nums[]={2+5*I, 1+6*I, 3+7*I};
    int coeff[]={11, 12, 13}, size=3;
    for(int i=0; i<size; i++) nums[i]*=coeff[i];

    for(int i=0; i<size; i++) printf("%.1f %+.1fi\n", creal(nums[i]), cimag(nums[i]));

    return 0;
}

output:

22.0 +55.0i
12.0 +72.0i
39.0 +91.0i
tzman
  • 174
  • 1
  • 1
  • 11
  • 1
    OP is using C, not C++. What's overloading or a constructor? (And C has built in complex numbers and C++ has `std::complex`... Why reinvent the wheel? – Shawn Sep 28 '19 at 21:30
  • @Shawn thanks for pointing that out, I've edited my answer. – tzman Sep 28 '19 at 22:30