简体   繁体   English

一般平滑步方程

[英]General smoothstep equation

I saw this in Wikipedia: Generalization of higher-order equations 我在Wikipedia中看到了这一点: 高阶方程的一般化

My code when I tried to implement it: 我尝试执行的代码:

 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(ab) * 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> 

I know about binomial coefficient and factorial, but one problem is factorial can't use with negative number, so I tried to use tricks to bypass it, and failed... 我知道二项式系数和阶乘,但是一个问题是阶乘不能与负数一起使用,因此我尝试使用技巧绕过它,但失败了...

And also the part with ()() chained together, I think it is multiplication ()*() between binom, and multiply with x^(a+n), as above, but it still not work. 还有()()链接在一起的部分,我认为这是二项式之间的()*()乘以x ^(a + n),如上所述,但是仍然无法正常工作。

Used google with words like: "general smooth step", "smooth step sum" but still not return any good explanation about it... 用过google的词,例如:“一般平滑步长”,“平滑步长和”,但仍未返回有关此的任何良好解释...

Anyone have any idea why my code not work, and also how to implement general smoothStep function in Javascript 任何人都知道为什么我的代码不起作用,以及如何在Javascript中实现常规的smoothStep函数

You can use Pascal Triangle instead of binomial coefficient to solve the issue. 您可以使用Pascal Triangle而不是二项式系数来解决此问题。 Here is the link for more detail of Pascal Triangle. 这是有关Pascal Triangle的更多详细信息的链接。

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

Here is the solution using Pascal Triangle. 这是使用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)); 

You can replace the negative binomial coefficient with a variant that uses positive numbers. 您可以将负二项式系数替换为使用正数的变体。 Based on the generalization of negative binomial coefficients as described on another Wikipedia article, the smoothstep equation using positive binomial coefficients would be: 根据另一篇Wikipedia文章所述的负二项式系数一般化,使用正二项式系数的平滑步方程为:

使用正多项式进行平滑步

So the resulting code would be: 因此,结果代码将是:

 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(ab) * 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)); 

However, since this version computes factorials, this function will begin to become less accurate a lot quicker than the code in Tushar Ghosh's answer for larger values of a . 但是,由于此版本计算阶乘,因此此函数的准确性将比Tushar Ghosh的答案中的a值更大的代码快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM