我在维基百科上看到了这一点: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);
&#13;
<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>
&#13;
我知道二项式系数和阶乘,但有一个问题是因子不能用于负数,所以我试图用技巧绕过它,然后失败......
并且()()链接在一起的部分,我认为它是二进制之间的乘法()*(),并且与x ^(a + n)相乘,如上所述,但它仍然不起作用。
谷歌使用的词语如下:&#34;一般平滑步骤&#34;,&#34;平滑步骤总和&#34;但仍然没有回复任何关于它的好解释......
任何人都知道为什么我的代码不起作用,以及如何在Javascript中实现一般的smoothStep函数
答案 0 :(得分:2)
您可以使用Pascal Triangle代替二项式系数来解决问题。这里是Pascal Triangle更多细节的链接。
https://trans4mind.com/personal_development/mathematics/series/pascalsTriangle.htm https://trans4mind.com/personal_development/mathematics/series/pascalGenCoefficients.gif
以下是使用Pascal Triangle的解决方案。
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));
答案 1 :(得分:0)
您可以使用使用正数的变量替换负二项式系数。基于另一篇维基百科文章中描述的generalization of negative binomial coefficients,使用正二项系数的smoothstep方程将是:
结果代码如下:
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));
&#13;
然而,由于此版本计算阶乘,因此该函数开始变得不那么准确,比Tushar Ghosh对 a 的较大值的答案中的代码要快得多。