In order not to litter the topic, I act on the principle of a separate issue-a separate topic

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.Calculate1(t => Complex.Cos(anguarFrequency * t - ValFurieTrans), 0, T) + Complex.ImaginaryOne * (1 / (2 * Math.PI)) * GetSpectralDensity(U, anguarFrequency, T) * integration.Calculate1(t => Complex.Sin(anguarFrequency * t - ValFurieTrans), 0, T); } 

I changed the code and added Complex.Cos and Complex.Sin but there was a problem with the lambda expression. In the line with return he writes where C is omplex.Cos и Complex.Sin

Cannot convert lambda expression to delegate type "System.Func" because some return types in the block are not implicitly convertible to the delegate return type

WHAT I HAVE BEEN DONE

Given that I have Calculate methods (they are double) and I really need them. I decided to create a separate Calculate for complex numbers. But even then there were big problems.

 interface NumericalIntegrationRule { double Calculate(Func<double, double> func, double min, double max, double subintervalsCount); Complex Calculate1(Func<double, Complex> func, double min, double max, double subintervalsCount); } class NumericalIntegration { public readonly NumericalIntegrationRule DefaultRule = null; public readonly int DefaultSubintervalsCount = 100;//100000 public NumericalIntegration() { DefaultRule = new TrapezoidalRule(); } public NumericalIntegration(NumericalIntegrationRule defaultRule) { DefaultRule = defaultRule; } public NumericalIntegration(NumericalIntegrationRule defaultRule, int defaultSubintervalsCount) : this(defaultRule) { DefaultSubintervalsCount = defaultSubintervalsCount; } public double Calculate(NumericalIntegrationRule rule, Func<double, double> func, double min, double max, double subintervalsCount) { return rule.Calculate(func, min, max, subintervalsCount);--- Неявное преобразование типа "System.Numerics.Complex" в "double" невозможно } public double Calculate(Func<double, double> func, double min, double max) { return Calculate(DefaultRule, func, min, max, DefaultSubintervalsCount); } public double Calculate1(NumericalIntegrationRule rule, Func<double, Complex> func, double min, double max, double subintervalsCount) { return rule.Calculate1(func, min, max, subintervalsCount); } public double Calculate1(Func<double, Complex> func, double min, double max) { return Calculate1(DefaultRule, func, min, max, DefaultSubintervalsCount); } } class TrapezoidalRule : NumericalIntegrationRule { public double Calculate(Func<double,double> func, double min, double max, double subintervalsCount) { double result = 0; double step = (max - min) / subintervalsCount; for (double i = min; i < max; i += step) result += ((func(i) + func(i + step)) / 2) * step; return result; } public Complex Calculate1(Func<double, Complex> func, double min, double max, double subintervalsCount)---"MainFormProg1.FurierTransformReverse.TrapezoidalRule.Calculate1(System.Func<double,System.Numerics.Complex>, double, double, double)": не все ветви кода возвращают значение { Complex result = 0; double step = (max - min) / subintervalsCount; for (double i = min; i < max; i += step) return result += ((func(i) + func(i + step)) / 2) * step; } } 
  • And why does the second Calculate1 get Func<Complex, Complex> , is that necessary? - VladD 6:50 pm
  • Yes Calculate1 I specifically made a separate method for counting Complex.Cos () and Complex.Sin (). As if I had left <double, double> Func, it immediately became a curse on the lambda expression. He now swears at them. I just created a new method and a complete copy of Calculate, but with the return type Complex - beginner
  • I understand about the returning complex value, and the incoming should be exactly complex? Are you sure that you don't really need Func<double, Complex> ? - VladD
  • one
    That's better. “Not all the code branches return value” is true. Look, you make a return at the first iteration of the loop . This is clearly not what you want, right? - VladD
  • one
    Regarding "Implicit type conversion" System.Numerics.Complex "in" double "is impossible" - well, yes, you have rule.Calculate1 returns Complex , and NumericalIntegration.Calculate1 for some reason double . The compiler swears at this. Change to Complex . - VladD

1 answer 1

To solve the problems of this issue is:

  1. Compile1 method to make the input Func<double, Complex> .
  2. The cycle in TrapezoidalRule.Calculate1 arranged in the same form as the cycle in Calculate :

     for (double i = min; i < max; i += step) result += ((func(i) + func(i + step)) / 2) * step; return result; 
  3. Fix NumericalIntegration.Calculate1 to return Complex , not double .