Continuation of this issue Get rid of the implicit conversion

After the comments indicated to me, I read the theory and based on the theory I read I changed my code

Here is the source where I got the formulas

enter image description here

And so I have a U (jw) -one in my GetSpectralDensityOfAmplitude(GetSpectralDensity(U, angularFrequency, T)) method GetSpectralDensityOfAmplitude(GetSpectralDensity(U, angularFrequency, T))

What was done by me

On the basis of the theory I read, I realized that I needed to get the values ​​U '(w) and U' '(w) I added fields to the code that get them and did the calculation as on formula 4.10 from theory

that is, took U '(w) from U' '(w) and planned to substitute the obtained values ​​into the formula. No phase, as I was told earlier, I do not find separately, everything as I understood it is in the found value of ValFurieTrans

Now, actually, to the software implementation of the Problems consists in the syntax of the formula, maybe I do not display it in the code

 public static double reverstrans(double U, double anguarFrequency, double T) { NumericalIntegration integration = new NumericalIntegration(); Complex u1 = U * integration.Calculate(t => Math.Cos(anguarFrequency), 0, T);//U'(w) Complex u2 = U * integration.Calculate(t => Math.Sin(anguarFrequency), 0, T);//U''(w) Complex img = Complex.ImaginaryOne.Imaginary;// инициализируем новый экземпляр объекта complex samvalue(1,0) Complex ValFurieTrans = u1 - img * u2; //Вычисляем U(w)e^-j*fi(w)= U'(w) - U''(w) return (1 / (2 * Math.PI)) *GetSpectralDensity(U, anguarFrequency, T) * integration.Calculate(w => Math.Cos(anguarFrequency * w - ValFurieTrans), 0, T) + Complex.ImaginaryOne * (1 / (2 * Math.PI))* GetSpectralDensity(U, anguarFrequency,T)* integration.Calculate(w => Math.Sin(anguarFrequency * w - ValFurieTrans), 0, T); } 

Swears on Math.Cos(anguarFrequency * w -ValFurieTrans) -transformation of type from "System.Numerics.Complex" to "double" is impossible

And now my attempts that I brought to correct the situation.

If instead of Math.Sin,Math.Cos I write Complex.Sin,Complex.Cos then the compiler will Math.Sin,Math.Cos such an error

Cannot convert lambda expression to delegate System.Func because some of the result types returned by the block cannot be implicitly converted to the result type returned by the delegate

It is understandable because the Calculate method contains Func and I do not think that I need to redo the whole class and methods in Complex.

I did separate Calculate1 methods with the Complex type but the compiler is relentless.

And if I do this

  return (1 / (2 * Math.PI)) * integration.Calculate(w => Math.Cos(Convert.ToDouble(ValFurieTrans)), 0, T) + Complex.ImaginaryOne * (1 / (2 * Math.PI)) * integration.Calculate(w => Math.Sin(Convert.ToDouble(ValFurieTrans)), 0, T); } 

That the compiler will miss it, but another problem will arise, when the program is running, an exception appears in the upper line.

 Необработанное исключение типа "System.InvalidCastException" в mscorlib.dll Дополнительные сведения: Не удалось привести тип объекта "System.Numerics.Complex" к типу "System.IConvertible" 

But the problem with the conversion from Complex to double moves to this line. The problem is in the syntax. Therefore, there are problems with the conversion.

  • And how do you want to convert Complex to double? The complex variable contains the real and imaginary parts. - Vladimir Paliukhovich
  • If the complex number has no imaginary part, then double VFT = ValFurieTrans.RealOne, otherwise it is impossible to mathematically convert. You can find the radius as Math.Sqrt (ValFurieTrans.RealOne ^ 2 + ValFurieTrans.ImagineOne ^ 2) but this will not be a representation of this number - Vladimir Paliukhovich
  • It does not help even if I only express the real part, for example, I substitute VatFurieTrans.Real in the formula and still swears - beginner
  • 2
    You should not aim to shut up the compiler. The compiler tells you that you have something wrong with the program. You must understand the meaning of the operations you are performing. What should be the value of ValFurieTrans ? If a complex number, why are you trying to assign it to a double variable? - VladD pm
  • 2
    If you need a cosine of a complex number, use Complex.Cos , yes. The fact that while doing something else does not work, the problem is not calculating the cosine, but the code that does not work. In principle, Math.Cos work at this point. Just like he can't calculate the cosine of a string or date. - VladD

0