给你。
我将此代码置于公共领域。
# 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