代码之家  ›  专栏  ›  技术社区  ›  Ferit

如何在C#中创建表示Vandermonde系统的大型Func数组?

  •  0
  • Ferit  · 技术社区  · 7 年前

    我正在努力创造一个巨大的 Vandermonde

    Func<double[], double>[] vandermondeSystem =
    {
        x =>  x[0]*Math.Pow(1, 0) + x[1]*Math.Pow(1, 1) + x[2]*Math.Pow(1, 2),
        x =>  x[0]*Math.Pow(2, 0) + x[1]*Math.Pow(2, 1) + x[2]*Math.Pow(2, 2),
        x =>  x[0]*Math.Pow(3, 0) + x[1]*Math.Pow(3, 1) + x[2]*Math.Pow(3, 2),
        x =>  x[0]*Math.Pow(4, 0) + x[1]*Math.Pow(4, 1) + x[2]*Math.Pow(4, 2)
    }
    

    但是像这样编写大型(比如100x50)系统是不可行的,所以我认为我需要使用某种循环或递归,但我不知道怎么做。

    This page 解释了如何创建匿名递归来实现斐波那契函数,但我不知道如何利用那里解释的方法。

    1 回复  |  直到 7 年前
        1
  •  1
  •   degant    7 年前

    基于您当前的代码,您可以轻松地修改它,以支持100x50等更大的系统。这样怎么样:

    Func<double[], double>[] bigVandermondeSystem = new Func<double[], double>[100];
    
    // Constructing a 100 x 50 Vandermonde System
    for (int i = 0; i < 100; i++)
    {
        var i1 = i;
        bigVandermondeSystem[i] = x => Enumerable
            .Range(0, 50)
            .Sum(number => x[number] * Math.Pow(i1 + 1, number));
    }
    
    推荐文章