Javascript 如何使用多参数函数作为单参数函数?

Javascript 如何使用多参数函数作为单参数函数?,javascript,function,function-parameter,Javascript,Function,Function Parameter,假设我有一个具有三个参数的函数: f(x, y, z) { return x*x + y*y + z*z; } 我有一个最小搜索函数golden()只适用于带有一个参数的函数 //Given a function myFunc, and a bracketing triplet of abscissas ax bx cx(such that bx is between ax and cx, and myFunc(bx) is less than both myFunc(ax)

假设我有一个具有三个参数的函数:

f(x, y, z)
{
  return x*x + y*y + z*z;
}
我有一个最小搜索函数golden()只适用于带有一个参数的函数

      //Given a function myFunc, and a bracketing triplet of abscissas ax bx cx(such that bx is between ax and cx, and myFunc(bx) is less than both myFunc(ax) and myFunc(cx)). This routine performs a golden section searhc for the minimum, isolating it to a fractional precision of about tol. The abscissa of the minumum is xmin.
      function golden(ax, bx, cx, myFunc, tol)
      {
        var r = 0.61803399;
        var c = 1.0 - r;
        var f1, f2, x0, x1, x2, x3, xmin;

        x0 = ax;            //At any given time we will keep track of four points, x0, x1, x2, x3.
        x3 = cx;
        if(Math.abs(cx - bx) > Math.abs(bx - ax))   //Make x0 to x1 the smaller segment
        {
            x1 = bx;
            x2 = bx + c * (cx - bx);                 //and fill in the new poit to be tried
        }else
        {
            x2 = bx;
            x1 = bx - c * (bx - ax);
        }
        f1 = myFunc(x1);        //the initial funciton evaluations. Note that we never neeed to evaluate the function at the original endpoints.
        f2 = myFunc(x2);
        while(Math.abs(x3 - x0) > tol * (Math.abs(x1) + Math.abs(x2)))
        {
          if(f2 < f1)           //One possible outcome,
          {
            x0 = x1; x1 = x2; x2 = r  * x1 + c * x3;    //its housekeeping,
            f1 = f2; f2 = myFunc(x2);                   //and a new funciton evaluation
          }else                 //The other outcome,
          {
            x3 = x2; x2 = x1; x1 = r * x2 + c * x0;
            f2 = f1; f1 = myFunc(x1);                   //and its new funciton evaluation.
          }
        }                       //Back to see if we are done.
        if(f1 < f2)             //We are done. Output the best of the two current values.
        {
            xmin = x1;
            //return f1;
        }else
        {
            xmin = x2;
            //return f2;
        }
        return xmin;
      }
但我在这里使用常数y:0,z:0。我想让y和z成为可协助的

我需要在x,y,z方向分别搜索。搜索的基础是先前的搜索

比如说。 第一个基是(1,1,1)x方向搜索->(0,1,1),然后是y方向搜索->(0,0,1),然后是z方向搜索->(0,0,0)

编程语言是javascript。
任何帮助都将不胜感激。谢谢

您只需使用一个参数调用
f
。所有其他参数将具有值“
未定义”

f(5); // x=5, y=undefined, z=undefined
你可以用。例如

编辑:您添加了更多的代码,所以更具体地说

function presetGoldenBxCx(bx, cx) {
    return function golden(ax, myFunc, tol)
          {
            var r = 0.61803399;
            var c = 1.0 - r;
            var f1, f2, x0, x1, x2, x3, xmin;

            x0 = ax;            //At any given time we will keep track of four points, x0, x1, x2, x3.
            x3 = cx;
            if(Math.abs(cx - bx) > Math.abs(bx - ax))   //Make x0 to x1 the smaller segment
            {
                x1 = bx;
                x2 = bx + c * (cx - bx);                 //and fill in the new poit to be tried
            }else
            {
                x2 = bx;
                x1 = bx - c * (bx - ax);
            }
            f1 = myFunc(x1);        //the initial funciton evaluations. Note that we never neeed to evaluate the function at the original endpoints.
            f2 = myFunc(x2);
            while(Math.abs(x3 - x0) > tol * (Math.abs(x1) + Math.abs(x2)))
            {
              if(f2 < f1)           //One possible outcome,
              {
                x0 = x1; x1 = x2; x2 = r  * x1 + c * x3;    //its housekeeping,
                f1 = f2; f2 = myFunc(x2);                   //and a new funciton evaluation
              }else                 //The other outcome,
              {
                x3 = x2; x2 = x1; x1 = r * x2 + c * x0;
                f2 = f1; f1 = myFunc(x1);                   //and its new funciton evaluation.
              }
            }                       //Back to see if we are done.
            if(f1 < f2)             //We are done. Output the best of the two current values.
            {
                xmin = x1;
                //return f1;
            }else
            {
                xmin = x2;
                //return f2;
            }
            return xmin;
          }
}

const golden11= presetGoldenBxCx(1, 1);
const answer = golden11(1);
功能预设goldenbxcx(bx,cx){
返回函数golden(ax、myFunc、tol)
{
var r=0.61803399;
var c=1.0-r;
变量f1,f2,x0,x1,x2,x3,xmin;
x0=ax;//在任何给定时间,我们将跟踪四个点,x0,x1,x2,x3。
x3=cx;
if(Math.abs(cx-bx)>Math.abs(bx-ax))//使x0到x1成为较小的段
{
x1=bx;
x2=bx+c*(cx-bx);//并填写要尝试的新点
}否则
{
x2=bx;
x1=bx-c*(bx-ax);
}
f1=myFunc(x1);//初始函数求值。请注意,我们从来不需要在原始端点求值函数。
f2=myFunc(x2);
而(Math.abs(x3-x0)>tol*(Math.abs(x1)+Math.abs(x2)))
{
如果(f2
这取决于您所做的计算。请把剩下的也加上,看看这里。@NinaScholz我已经编辑过了。来这里是想说我是如何在y方向搜索的?我怎样才能从{x:4,y:5,z:4}开始在x方向上搜索?常量是必要的吗?看起来不错,我会检查它。谢谢,谢谢。节省我很多时间,否则我得重建我的鳕鱼。
function curry (y,z) {
    return function (x)
    {
       console.log(x +  y + z);
    }
}

var addToThis = curry(1,2);
addToThis(3); // 6
addToThis(5); //8
function presetGoldenBxCx(bx, cx) {
    return function golden(ax, myFunc, tol)
          {
            var r = 0.61803399;
            var c = 1.0 - r;
            var f1, f2, x0, x1, x2, x3, xmin;

            x0 = ax;            //At any given time we will keep track of four points, x0, x1, x2, x3.
            x3 = cx;
            if(Math.abs(cx - bx) > Math.abs(bx - ax))   //Make x0 to x1 the smaller segment
            {
                x1 = bx;
                x2 = bx + c * (cx - bx);                 //and fill in the new poit to be tried
            }else
            {
                x2 = bx;
                x1 = bx - c * (bx - ax);
            }
            f1 = myFunc(x1);        //the initial funciton evaluations. Note that we never neeed to evaluate the function at the original endpoints.
            f2 = myFunc(x2);
            while(Math.abs(x3 - x0) > tol * (Math.abs(x1) + Math.abs(x2)))
            {
              if(f2 < f1)           //One possible outcome,
              {
                x0 = x1; x1 = x2; x2 = r  * x1 + c * x3;    //its housekeeping,
                f1 = f2; f2 = myFunc(x2);                   //and a new funciton evaluation
              }else                 //The other outcome,
              {
                x3 = x2; x2 = x1; x1 = r * x2 + c * x0;
                f2 = f1; f1 = myFunc(x1);                   //and its new funciton evaluation.
              }
            }                       //Back to see if we are done.
            if(f1 < f2)             //We are done. Output the best of the two current values.
            {
                xmin = x1;
                //return f1;
            }else
            {
                xmin = x2;
                //return f2;
            }
            return xmin;
          }
}

const golden11= presetGoldenBxCx(1, 1);
const answer = golden11(1);