C#-Func<>

C#-Action十分相似, Func<> 也是c#内置的委托类型,不同的是, Func<> 只能用来引用具有返回值的方法,也就是说,在使用它时,至少需要在尖括号内指定一种类型,当仅指定一种类型时,表示引用具有返回值但没有参数的方法,当指定多种类型时,其中最后一个类型表示返回值类型,前面的表示所引用方法的参数列表的类型。

有一下两个简单的例子:

指定一个类型时

复制代码

 1         static void Main(string[] args)
 2         {
 3             Func<string> a=Gettomorrow;
 4             Console.WriteLine(a());
 5 
 6         }
 7         public static string Gettomorrow()
 8         {
 9             return DateTime.Now.AddDays(1).Date.ToString();
10         }
11 
12         // 2020/8/5 0:00:00

复制代码

当指定两个类型时

复制代码

 1         static void Main(string[] args)
 2         {
 3             Func<int, double> calc = Factorial;
 4             Console.WriteLine(calc(5));
 5 
 6         }
 7 
 8         public static double Factorial(int num)
 9         {
10             //求一个整数的阶乘
11             int res = 1;
12             for(int i=2;i<=num;i++)
13             {
14                 res *= i;
15             }
16             return res;
17         }
18         // 120

复制代码

与 Action 一样, Func<> 最多支持16个参数。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非线性回归是一种用于拟合非线性模型的方法,其目的是通过已知的数据集来估计模型的参数。在C#中,可以使用MathNet.Numerics库来实现非线性回归方法。 该库提供了类似于MATLAB的功能,可以使用矩阵和向量进行计算。下面是一个使用MathNet.Numerics库实现非线性回归的示例代码: ```csharp using System; using MathNet.Numerics.LinearAlgebra; public class NonlinearRegression { public static Vector<double> Fit(Func<Vector<double>, Vector<double>> modelFunction, Vector<double> xData, double[] yData) { int numParams = xData.Count; // create an initial guess for the parameters Vector<double> initialGuess = Vector<double>.Build.Dense(numParams, 1.0); // use the Levenberg-Marquardt algorithm to fit the model var solver = new MathNet.Numerics.Optimization.LevenbergMarquardt(modelFunction, initialGuess); var result = solver.Solve(xData, Vector<double>.Build.DenseOfArray(yData)); return result.MinimizingPoint; } } ``` 该方法接收三个参数:模型函数(`Func<Vector<double>, Vector<double>>`类型)、自变量(`Vector<double>`类型)和因变量(`double[]`类型)。在该方法中,我们使用Levenberg-Marquardt算法来拟合模型。 模型函数应该是一个将自变量作为输入并返回预测值的函数,该函数的类型为`Func<Vector<double>, Vector<double>>`。自变量是一个`Vector<double>`类型的对象,因变量是一个`double`类型的数组。 使用示例: ```csharp // define the model function Func<Vector<double>, Vector<double>> modelFunction = x => Vector<double>.Build.DenseOfArray(new double[] { Math.Exp(x[0] * x[1]), Math.Log(x[0] + x[1]) }); // define the input data Vector<double> xData = Vector<double>.Build.DenseOfArray(new double[] { 1.0, 2.0 }); double[] yData = new double[] { 4.0, 3.0 }; // fit the model to the data Vector<double> parameters = NonlinearRegression.Fit(modelFunction, xData, yData); // print the fitted parameters Console.WriteLine(parameters.ToString()); ``` 在此示例中,我们使用一个包含两个参数的模型函数,并将其应用于两个自变量。我们使用Levenberg-Marquardt算法来拟合模型,并输出拟合参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值