一般平滑步方程

General smoothstep equation

我在维基百科上看到了这个:Generalization of higher-order equations

我尝试实现时的代码:

function generalSmoothStep(a, x) { //Generalized smoothstep
  var result = 0;
  for (var n = 0; n < a - 1; n ++) {
    result += binom(-a, n) * binom(2 * a - 1, a - n - 1) * Math.pow(x, a + n);
  }
  return result;
}

function smoothStep(x) { //Normal smoothstep
  return -2 * Math.pow(x, 3) + 3 * Math.pow(x, 2);
  //I know about x * x * (3 - 2 * x);
}

function binom(a, b) { //Binomial coefficient
  return Math.factorial(a) / (Math.factorial(a-b) *  Math.factorial(b));
}

Math.factorial = function(value) { //Factorial
  var result = 1;
  if (value > 0) {
    for (var count = 1; count <= value; count ++) {
      result *= count;
    }
  } else if (value < 0) {
    for (var count = -1; count >= value; count --) {
      result *= count;
    }
  } else {
    result = 1;
  }
  return result;
};

document.getElementById("normalStep").innerHTML = "smoothStep(0.2) = " + smoothStep(0.2);
document.getElementById("generalStep").innerHTML = "generalSmoothStep(2, 0.2) = " + generalSmoothStep(2, 0.2);
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>
  <script>
    MathJax.Hub.Config({
      tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}
    });
  </script>
</head>

<body>
  <p>The function: $\operatorname{S}_a(x) = \sum_{n=0}^{a-1} \binom{-a}{n}\binom{2a - 1}{a - n - 1}x^{a + n}$</p>
  
  <p>Here is the result when I run:</p>
  
  <p id="normalStep"></p>
  <p id="generalStep"></p>
</body>

我知道二项式系数和阶乘,但一个问题是阶乘不能与负数一起使用,所以我试图用技巧绕过它,但失败了...

还有()()串在一起的部分,我觉得是binom之间的乘法()*(),再和x^(a+n)相乘,如上,还是不行。

将 google 与以下词语一起使用:"general smooth step"、"smooth step sum" 但仍然 return 对此没有任何好的解释...

任何人都知道为什么我的代码不起作用,以及如何在 Javascript

中实现通用的 smoothStep 函数

您可以使用帕斯卡三角代替二项式系数来解决问题。这里是 link 帕斯卡三角的更多细节。

https://trans4mind.com/personal_development/mathematics/series/pascalsTriangle.htm https://trans4mind.com/personal_development/mathematics/series/pascalGenCoefficients.gif

这是使用帕斯卡三角的解决方案。

function generalSmoothStep(a, x) { //Generalized smoothstep
  var result = 0;
  for (var n = 0; n <= a - 1; n++) {
    result += (pascalTriangle(-a, n) * pascalTriangle(2 * a - 1, a - n - 1) * Math.pow(x, a + n));
  }
  return result;
}

function pascalTriangle(a, b){
  var result = 1; 
  for(var i = 1; i <= b; i++){
    result *= ((a - (i - 1)) / i);
  }
  return result;
}

function smoothStep(x) { //Normal smoothstep
  return -2 * Math.pow(x, 3) + 3 * Math.pow(x, 2);
}

console.log("smoothStep(0.3) = " + smoothStep(0.3));
console.log("generalSmoothStep(2, 0.3) = " + generalSmoothStep(2, 0.3));

您可以将负二项式系数替换为使用正数的变体。根据另一篇维基百科文章中描述的 generalization of negative binomial coefficients,使用正二项式系数的 smoothstep 方程为:

Smoothstep using Positive Polynomials

因此生成的代码将是:

function generalSmoothStep(a, x) { //Generalized smoothstep
  var result = 0;
  for (var n = 0; n <= a - 1; n ++) {
    // Change negative binom coeff to positive equivalent
    result += Math.pow(-1, n) * binom(a + n - 1, n) * binom(2 * a - 1, a - n - 1) * Math.pow(x, a + n);
  }
  return result;
}

function binom(a, b) { //Binomial coefficient
  return Math.factorial(a) / (Math.factorial(a-b) *  Math.factorial(b));
}

Math.factorial = function(value) { //Factorial
  var result = 1;
  if (value > 0) {
    for (var count = 1; count <= value; count ++) {
      result *= count;
    }
  } else if (value < 0) {
    for (var count = -1; count >= value; count --) {
      result *= count;
    }
  } else {
    result = 1;
  }
  return result;
}

function smoothStep(x) { //Normal smoothstep
  return -2 * Math.pow(x, 3) + 3 * Math.pow(x, 2);
}

console.log("smoothStep(0.3) = " + smoothStep(0.3));
console.log("generalSmoothStep(2, 0.3) = " + generalSmoothStep(2, 0.3));

但是,由于此版本计算阶乘,因此此函数将开始变得不太准确,比 Tushar Ghosh 对 a 的较大值的答案中的代码快得多。