如何将复数(复数)与 C 中的整数相乘?

How to multiply a complex number(complex integer) with an integer in C?

我有一个大小为 10 的数组,它的值从 0 到 4 作为实部(整数),从 5 到 9 作为虚部(整数)来自一个文件,该文件有 5 个复数(我创建一个两倍大小的新数组,用于保存原始复杂流中的实部和虚部)。我必须将它们与另一个整数数组(非复数)相乘,我该怎么做?

用于说明

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)

关键在于,输出需要表示为复数。

一个复数值乘以一个严格的实数值会改变复数值的幅度而不是相位角。因此,您可以将复数值的分量(实部和“虚部”)分别乘以实数值,因为没有会导致相角变化的叉积。

因此,如果您将输入的格式视为复杂的数据类型,则在将所有分量乘以严格的实数比例因子后,相乘后的输出将同样复杂。如果没有,您可以将组件洗牌或重新打包成您需要的任何结果复杂矢量格式或数据类型。

如何将实部和虚部存储在不同的数组中?这将简化您的问题:

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];
}

但是,如果您想 return 函数的结果,您最好使用 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;
}

输出:

22.0 +55.0i
12.0 +72.0i
39.0 +91.0i