如何在Haskell或Python中翻译这个数学公式? (用PHP翻译) [英] How to translate this Math Formula in Haskell or Python? (Was translated in PHP)

查看:151
本文介绍了如何在Haskell或Python中翻译这个数学公式? (用PHP翻译)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你可以在接受的答案中看到这个公式:将数学公式应用于更优雅的方式(也许一个递归调用可以做到这一点)

我不是一个专业的编码器,所以我尽我所能翻译它但我的技能是有限的,我遇到了几个问题。



让我们开始吧。



玩家堆栈:我认为一个二维阵列应该在这里完成这项工作。

  $ array = array(1 => 2000,3 => 5000 ...); 

现在他想创建一个Matrix的值,我做了我的研究并找到了一个名为Math_Matrix的PEAR包,安装它,但我想知道如何创建这种矩阵。



我担心我无法翻译整个代码,因为他使用先进的方法,如递归调用等。



你能帮我吗?

编辑:OLD BOUNTY REWARD



我尝试了您的建议,但由于编程技巧不佳,我觉得浪费时间。



我决定提供一个50英尺的空间,以帮助我通过在PHP中翻译该公式。

注意,如果您认为翻译在Python中更容易/更合适/其他,请为我提供一种将Python脚本包含在PHP脚本中的方法,因为我打算在网站中使用此公式。

h2_lin>解决方案

在这里,你去。



是代码进入公有领域。

$ $ p $ $ $ $ c $#$ $ $ $ $ $ $ $ $ $宽度){
$ row = array(); ($ x = 0; $ x< $ width; $ x ++){
$ row [$ x] = 0;
;
}
返回$ row;
}

#使用宽度*高度矩阵的函数
函数makematrix($ width,$ height){
$ matrix = array();
for($ y = 0; $ y <$ height; $ y ++){
$ matrix [$ y] = array(); ($ x = 0; $ x< $ width; $ x ++){
$ matrix [$ y] [$ x] = 0;

}
}
return $ matrix;


#将一个矩阵添加到另一个
函数matrixadd(& $ matrixdest,& $ matrixsrc){
for($ i = 0; $ i< ; count($ matrixdest); $ i ++){
for($ j = 0; $ j< count($ matrixdest [$ i]); $ j ++){
$ matrixdest [$ i] [ $ J] + = $ matrixsrc [$ i]于[$ D];



$ b $将矩阵乘以标量
函数matrixmultiply(& $ matrix,$ scalar){
对于($ j = 0; $ j< count($ matrix [$ i]); $ j ++){$ b($ i = 0; $ i< count($ matrix); $ i ++) $ b $矩阵[$ i] [$ j] * = $标量;
}
}
}

#计算每个地方的权益。行显示玩家;
#列表示地点(0是第一名,1是第二名,依此类推)
#参数'places'是可选的。如果没有给出,则使用
#个堆栈。
函数equitymatrix(& $ stacks,$ places = -1){
if($ places == - 1){
#用堆栈数替换地方
$地方=计数($栈); $(b)

if(count($ stacks)< = 1){
return array(array(1));
}
$ totalStacks = 0;
for($ i = 0; $ i< count($ stacks); $ i ++){
$ totalStacks + = $ stacks [$ i];

#优化如果只有一个地方
if($ places == 1){
$ matrix = makematrix(1,count($ stacks)); ($ i = 0; $ i< count($ stacks); $ i ++){
$ matrix [$ i] [0] = $ stacks [$ i] * 1.0 / $ totalStacks;
;
}
return $ matrix;

#优化两个地方的情况
if($ places == 2){
$ matrix = makematrix(2,count($ stacks)); ($ i = 0; $ i< count($ stacks); $ i ++){
$ matrix [$ i] [0] = $ stacks [$ i] * 1.0 / $ totalStacks;
; ($ j = 0; $ j
如果($ i!= $ j){
$ matrix [$ i] [1] + = $ matrix [$ j] [0] *($ stacks [$ i] * 1.0 /($ $ totalStacks-堆叠[$ j]));
}
}
}
return $ matrix;
}
#计算每个玩家获得第一名的概率
$ probabilities = array(); ($ i = 0; $ i< count($ stacks); $ i ++){
$ probabilities [$ i] = $ stacks [$ i] * 1.0 / $ totalStacks;
;
}
#echo(count($ stacks)。。$ places。\\\
);
$ subequities = array();
for($ i = 0; $ i< count($ stacks); $ i ++){
$ substacks = array(); ($ j = 0; $ j< count($ stacks); $ j ++)
#假设那个玩家我会处于第一位
#用我的堆栈创建一个新数组
{
if($ j!= $ i){
array_push($ substacks,$ stacks [$ j]);


#查找其余玩家的子元素
$ subequities [$ i] = equitymatrix($ substacks,
min($ places,count($子堆))); ($ j = 0; $ j< count($ subequities [$ i]); $ j ++){
array_unshift($ subequities [$ i] [$ j],0)
;
}
#添加玩家我回
$ newrow = makerow($地方);
$ newrow [0] = 1;
array_splice($ subequities [$ i],$ i,0,array($ newrow));
}
$ equities = makematrix($ places,count($ stacks));
for $($ i = 0; $ i< count($ stacks); $ i ++){
#乘以概率
matrixmultiply($ subequities [$ i],$ probabilities [$ i ]);
#添加子元素
matrixadd($ equities,$ subequities [$ i]);
}
返回$股票;

$ / code>

示例:

  $ mystacks =阵列(10,40,30,20); 
print_r(equitymatrix($ mystacks));






至于使用矩阵:



在PHP中,矩阵可以表示为一个数组数组。你可以在函数 makematrix 中看到
,它返回一个长度为 height ,$ b $的数组b,每个元素都是 width 零的数组。您的问题使用以下
矩阵操作,它们都很简单:


  • 添加两个矩阵( matrixadd )。在这里,只需将一个矩阵的元素添加到另一个矩阵的
    对应元素中。
  • 将矩阵乘以单个数字(标量)( matrixmultiply )简单地
    包含将矩阵的每个元素乘以该数字。

    I'm trying to convert a Math Formula into PHP code.

    You can see the formula in the accepted answer here: Applying a Math Formula in a more elegant way (maybe a recursive call would do the trick).

    I'm not a professional coder so I'm trying my best to translate it but my skills are limited and I've encountered several problems.

    Let's start.

    There's a vector containing players' stacks: I think a bidimensional array should do the work here. I'd add a key to identify each player.

    $array = array(1 => 2000, 3 => 5000 ...);
    

    Now he wants to create a Matrix of values, I did my researches and found a PEAR package called Math_Matrix, installed it but I'm wondering how to create that sort of matrix.

    I'm worried that I won't be able to translate the entire code because he uses advances methods like recursive calls etc.

    Could you help me?

    EDIT: OLD BOUNTY REWARD

    I tried what you've suggested but I feel like wasting my time because of my poor-programming skills.

    I'VE DECIDED TO OFFER A 50 BOUNTY IF SOMEONE WANTS TO HELP ME BY TRANSLATING THAT FORMULA IN PHP.

    Note that if you think that translating in Python is easier/more suitable/other, please provide me a way to include the Python script inside a PHP script since I'm planning to use this formula in a website.

    解决方案

    Here you go.

    I place this code into the public domain.

    # Function to make an array of 'width' zeros
    function makerow($width){
     $row=array();
     for($x=0;$x<$width;$x++){
       $row[$x]=0;
     }
     return $row;
    }
    
    # Function to make a width*height matrix
    function makematrix($width,$height){
     $matrix=array();
     for($y=0;$y<$height;$y++){
      $matrix[$y]=array();
      for($x=0;$x<$width;$x++){
       $matrix[$y][$x]=0;
      }
     }
     return $matrix;
    }
    
    # Adds one matrix to another
    function matrixadd(&$matrixdest,&$matrixsrc){
     for($i=0;$i<count($matrixdest);$i++){
      for($j=0;$j<count($matrixdest[$i]);$j++){
       $matrixdest[$i][$j]+=$matrixsrc[$i][$j];
      }
     }
    }
    
    # Multiplies a matrix by a scalar
    function matrixmultiply(&$matrix,$scalar){
     for($i=0;$i<count($matrix);$i++){
      for($j=0;$j<count($matrix[$i]);$j++){
       $matrix[$i][$j]*=$scalar;
      }
     }
    }
    
    # Calculates the equity of each place. Rows indicate players;
    # columns indicate places (0 is 1st place, 1 is second, and so on)
    # The parameter 'places' is optional.  If not given, uses the 
    # number of stacks.
    function equitymatrix(&$stacks, $places=-1){
     if($places==-1){
      # replace places with the stack count
      $places=count($stacks);
     }
     if(count($stacks)<=1){
      return array(array(1));
     }  
     $totalStacks=0;
     for($i=0;$i<count($stacks);$i++){
      $totalStacks+=$stacks[$i];
     }
     # Optimize for case where there is only one place
     if($places==1){
      $matrix=makematrix(1,count($stacks));
      for($i=0;$i<count($stacks);$i++){
       $matrix[$i][0]=$stacks[$i]*1.0/$totalStacks;
      }
      return $matrix;
     }
     # Optimize for case where there are two places
     if($places==2){
      $matrix=makematrix(2,count($stacks));
      for($i=0;$i<count($stacks);$i++){
       $matrix[$i][0]=$stacks[$i]*1.0/$totalStacks;
      }
      for($i=0;$i<count($stacks);$i++){
       for($j=0;$j<count($stacks);$j++){
        if($i!=$j){
         $matrix[$i][1]+=$matrix[$j][0]*($stacks[$i]*1.0/($totalStacks-$stacks[$j]));
        }
       }
      }
      return $matrix;
     }
     # Calculate the probabilities of each player getting first place
     $probabilities=array();
     for($i=0;$i<count($stacks);$i++){
      $probabilities[$i]=$stacks[$i]*1.0/$totalStacks;
     }
     #echo(count($stacks)." ".$places."\n");
     $subequities=array();
     for($i=0;$i<count($stacks);$i++){
      $substacks=array();
      # Assume that player i would be in first place
      # Create a new array with i's stack removed
      for($j=0;$j<count($stacks);$j++){
       if($j!=$i){
        array_push($substacks,$stacks[$j]);
       }
      }
      # Find the subequity of the remaining players
      $subequities[$i]=equitymatrix($substacks,
        min($places,count($substacks)));
      for($j=0;$j<count($subequities[$i]);$j++){
       array_unshift($subequities[$i][$j],0);
      }
      # Add player i back
      $newrow=makerow($places);
      $newrow[0]=1;
      array_splice($subequities[$i],$i,0,array($newrow));
     }
     $equities=makematrix($places,count($stacks));
     for($i=0;$i<count($stacks);$i++){
      # Multiply the probabilities
      matrixmultiply($subequities[$i],$probabilities[$i]);
      # Add the subequity
      matrixadd($equities,$subequities[$i]);
     }
     return $equities;
    }
    

    Example:

    $mystacks=array(10,40,30,20);
    print_r(equitymatrix($mystacks));
    


    As to the use of matrices:

    In PHP, a matrix can be represented as an array of arrays. You can see that in the function makematrix, which returns an array of length height, with each element being an array of width zeros. Your problem uses the following matrix operations, both of which are simple:

    • Adding two matrices (matrixadd). Here, just add the elements of one matrix to the corresponding elements of the other matrix.
    • Multiplying a matrix by a single number (a scalar) (matrixmultiply) simply involves multiplying each element of the matrix by that number.

    这篇关于如何在Haskell或Python中翻译这个数学公式? (用PHP翻译)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆