Hello. Tell me, please, I am trying to convert a double array into a complex array, as I was told earlier, in the real part we write the values ​​of the double array, and assign the imaginary part to zero. I created the Complex structure in the project, declared a constructor, which takes re (an array of double) as a parameter, assigns the imaginary part to zero, then when I try to convert the double array to a complex using the method and return the result to the signal variable, the result is not returned yet tell me, please, what is my problem?

public struct Complex { // переменная, хранящая реальную часть комплексного числа private double[] m_real; // переменная, хранящая мнимую часть комплексного числа private double m_imag; public Complex(double[] re) { m_real = re; m_imag = 0.0; } // Свойства устанавливающие значения public double[] Re { get { return m_real; } set { m_real = value; } } public double Im { get { return m_imag; } set {m_imag = value; } } //Метод преобразующий массив double в complex public Complex[] ConvertToComplex(Double[] data) { Complex[] signal; Complex c1 = new Complex(data); signal = c1; return signal; } }` 
  • 2
    A complex number consists of one real part and one imaginary part. Why do you have double[] m_real instead of double m_real stored in Complex ? - Regent
  • @Regent, understood, I corrected - Andrey273

1 answer 1

The method of converting arrays is better to make static, since it will logically be part of the state of the Complex type, rather than instances of this type:

 //Метод преобразующий массив double в complex public static Complex[] ConvertToComplex(Double[] data) { Complex[] signal = new Complex[data.Length]; for (var i = 0; i < data.Length; i++) { var value = data[i]; Complex c1 = new Complex(value); signal[i] = c1; } return signal; } 

or even better, use the implicit conversion operator:

 public static implicit operator Complex(double value) { return new Complex(value); } 

The constructor of your structure will look like this:

 public Complex(double re) : this() { Re = re; Im = 0.0; } 

In the case of using an implicit conversion operator, you can now write like this:

 double[] doubleArr = ... Complex[] complexArr = ... for (var i = 0; i < doubleArr.Length; i++) { complexArr[i] = doubleArr[i]; }