【问题标题】:Populate an array of complex numbers error填充复数数组错误
【发布时间】:2015-01-28 06:15:51
【问题描述】:

我有两个数组。一个包含“真实”值,另一个包含“虚构”值。这两个数组需要组合成一个复数数组。我尝试了以下方法:

Complex[] complexArray = new Complex[16384];

for (int i = 0; i <16384; i++)
(
    complexArray[i].Real = realArray[i];
    complexArray[i].Imaginary = imaginaryArray[i];
}

它不起作用。它给出了错误:不能将属性或索引器“System.Numerics.Complex.Real”分配给——它是只读的 我知道复数是不可变的,但是如何创建这样的数组呢?

更重要的是,一旦我有了这个数组,我想在其中移动值。

【问题讨论】:

标签: c# complex-numbers


【解决方案1】:

只需使用Complex的构造函数:

Complex[] complexArray = new Complex[16384];
for (int i = 0; i < complexArray.Length; i++)
(
    complexArray[i] = new Complex(realArray[i], imaginaryArray[i]);
}

然后,您可以选择使用 LINQ 减少代码量(轻微的性能成本):

var complexArray = realArray.Zip(imaginaryArray, (a, b) => new Complex(a, b)).ToArray();

要移动数组中的值,请执行与 intdouble 相同的操作:

int i = 5;
int j = 7;
// Swap positions i and j
var temp = complex[i];
complex[i] = complex[j];
complex[j] = temp;

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多