2011-12-22 209 views
17

我试图将数学公式转换为PHP代码。如何在Haskell或Python中转换此数学公式? (翻译成PHP)

你可以看到在这里接受的答案公式:Applying a Math Formula in a more elegant way (maybe a recursive call would do the trick)

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

我们开始吧。

还有一个包含球员的筹码向量:我觉得一个二维数组应该在这里做的工作。我会添加一个密钥来识别每个玩家。

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

现在,他想创造价值的一个矩阵,我做我的研究,结果发现一个PEAR包叫做Math_Matrix,安装它,但我不知道如何创建那种矩阵。

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

你能帮助我吗?

编辑:OLD BOUNTY REWARD

我想你建议什么,但我觉得浪费,因为我那可怜的编程技能,我的时间。

我已经决定提供50 BOUNTY如果有人想帮我通过翻译这个公式在PHP。

需要注意的是,如果你认为在Python翻译更容易/更适合/其他,请提供给我一个办法,包括PHP脚本里面的Python脚本,因为我打算在网站上使用这个公式。

+0

我只能说WOW .....这是一个非数学导向的编程语言的一些严肃的数学。 – 2011-12-22 14:25:16

+0

你认为有人可以在PHP中解决这个问题吗? – KingBOB 2011-12-22 14:30:56

+0

或者我可以用PHP实现哪种语言可以用来解决这个问题? – KingBOB 2011-12-22 14:35:30

回答

14

在这里你去。

我把这个代码进入公共领域。

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

实施例:

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

至于使用矩阵:

在PHP中,矩阵可被表示为数组的数组。你可以看到 ,在功能makematrix,它返回长度height, 的其中每个元素的width零的阵列的阵列。您的问题,使用以下 矩阵运算,这两者都是简单:

  • 添加两个矩阵(matrixadd)。这里,只需将一个矩阵的元素添加到另一个矩阵的相应元素即可。
  • 将矩阵乘以单个数字(标量)(matrixmultiply)只需 包括将矩阵的每个元素乘以该数字。
+0

如果它可行 - phenominal。做得好! – 2011-12-29 03:11:56

+0

你是我的偶像! :) – KingBOB 2011-12-29 07:36:39

+0

我分析过你的代码,它太棒了! :) 唯一的问题是数学算法本身:太慢。 是否可以通过只计算确定数量的地点资产来加速代码? 我的意思是:在有6名球员的情况下,上面的代码会计算每个球员在每个球员中的平等,直到第6名。 如果我们只需要他们在第一和第二位的权益,我们可以跳过剩余代码,对吧? 有一些像print_r(equitymatrix($ mystacks,2))是有用的; 如果数字保留在我们想要计算权益的地方。 – KingBOB 2011-12-29 08:31:49

0

我想最大的问题是你打算如何使用它。最后,我真的建议不要使用PHP。它不是为这种类型的工作设计的,你最终会在以后为自己造成很多工作。我建议使用Octave(MATLAB的OpenSource版本)如果你真的想围绕它构建一个程序,你应该使用NumPy模块来查看Python:http://numpy.scipy.org/


如果你有能力,我会建议使用mod_python来运行NumPy来处理这个问题。这可能是最简单的方法,因为NumPy可以本地处理矩阵。除此之外,您应该查看以下用于处理PHP中的矩阵的类。有些人已经开发了一些专门为操纵矩阵而设计的类。

http://www.phpkode.com/scripts/item/matrix-new/ http://www.phpclasses.org/package/2859-PHP-Perform-operations-with-matrices.html

+0

我想创建一个这样的网站:http://www.icmpoker.com/Calculator.aspx 它似乎使用类似的算法来计算股票。 – KingBOB 2011-12-22 18:49:39

+0

继续前进,并添加了一些特定于您的内容的更多信息,以及专门为PHP中的Matrix Math设计的类的一些链接。我仍然认为Python会更好地处理它,但根据您对虚拟主机的控制,实施起来会更困难一些。 – Drahkar 2011-12-23 13:18:56

+0

谢谢大家,我会在PHP中做一些研究来实现这一点,因为我不知道Python并在网站中实现它应该使它更加复杂... – KingBOB 2011-12-23 13:35:44

0

如果你有Matlab的安装,用符号数学工具箱,

可以使用ccode功能,以便把这种公式(或任何其他)到C代码(这是非常相似的PHP )。