2016-12-12 6 views
1

Предположим, у меня есть функция с тремя параметрами:Как использовать функцию нескольких параметров как функцию с одним параметром?

f(x, y, z) 
{ 
    return x*x + y*y + z*z; 
} 

и у меня есть минимальная функция поиска золотой() работает только для функции с одним параметром.

 //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; 
     } 

Как я могу передать f с тремя параметрами для func с одним параметром.

я судимый обернуть п следующим образом:

wrapFunc(x) 
{ 
    f(x, 0, 0); 
} 

Но я использую постоянные у: 0, г: 0 здесь. Я хочу сделать y, и z assiginable?

Мне нужно искать в направлениях x, y, z отдельно. И база поиска находится на предыдущем поиске.

Например. Первое основание (1,1,1) x поиск направления -> (0, 1, 1), затем y поиск в режиме поиска -> (0, 0, 1), затем поиск направления z -> (0,0,0);

Язык программирования - это javascript. Любая помощь будет оценена по достоинству. Спасибо

+1

зависит от расчета, вы делаете. добавьте остальные, а также посмотрите здесь [mcve]. –

+0

@NinaScholz Я редактировал его. –

ответ

2

Вы можете использовать currying. Например.

function curry (y,z) { 
    return function (x) 
    { 
     console.log(x + y + z); 
    } 
} 

var addToThis = curry(1,2); 
addToThis(3); // 6 
addToThis(5); //8 

Edit: Вы добавили еще немного кода, поэтому более конкретно ...

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); 
+0

является обязательным? –

+0

Nope просто привычка! –

+0

выглядит хорошо, я проверю его. Спасибо. –

1

вы можете просто позвонить f с одним параметром. все остальные Титулы будут иметь значение «undefined»:

f(5); // x=5, y=undefined, z=undefined 
+0

Пришли сюда, чтобы сказать это –

+0

Как я ищу в направлении y? И как я могу искать в направлении x от начала {x: 4, y: 5, z: 4}? –