Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here you go.</p> <p>I place this code into the public domain.</p> <pre><code># Function to make an array of 'width' zeros function makerow($width){ $row=array(); for($x=0;$x&lt;$width;$x++){ $row[$x]=0; } return $row; } # Function to make a width*height matrix function makematrix($width,$height){ $matrix=array(); for($y=0;$y&lt;$height;$y++){ $matrix[$y]=array(); for($x=0;$x&lt;$width;$x++){ $matrix[$y][$x]=0; } } return $matrix; } # Adds one matrix to another function matrixadd(&amp;$matrixdest,&amp;$matrixsrc){ for($i=0;$i&lt;count($matrixdest);$i++){ for($j=0;$j&lt;count($matrixdest[$i]);$j++){ $matrixdest[$i][$j]+=$matrixsrc[$i][$j]; } } } # Multiplies a matrix by a scalar function matrixmultiply(&amp;$matrix,$scalar){ for($i=0;$i&lt;count($matrix);$i++){ for($j=0;$j&lt;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(&amp;$stacks, $places=-1){ if($places==-1){ # replace places with the stack count $places=count($stacks); } if(count($stacks)&lt;=1){ return array(array(1)); } $totalStacks=0; for($i=0;$i&lt;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&lt;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&lt;count($stacks);$i++){ $matrix[$i][0]=$stacks[$i]*1.0/$totalStacks; } for($i=0;$i&lt;count($stacks);$i++){ for($j=0;$j&lt;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&lt;count($stacks);$i++){ $probabilities[$i]=$stacks[$i]*1.0/$totalStacks; } #echo(count($stacks)." ".$places."\n"); $subequities=array(); for($i=0;$i&lt;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&lt;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&lt;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&lt;count($stacks);$i++){ # Multiply the probabilities matrixmultiply($subequities[$i],$probabilities[$i]); # Add the subequity matrixadd($equities,$subequities[$i]); } return $equities; } </code></pre> <p>Example:</p> <pre><code>$mystacks=array(10,40,30,20); print_r(equitymatrix($mystacks)); </code></pre> <hr> <p>As to the use of matrices:</p> <p>In PHP, a matrix can be represented as an array of arrays. You can see that in the function <code>makematrix</code>, which returns an array of length <code>height</code>, with each element being an array of <code>width</code> zeros. Your problem uses the following matrix operations, both of which are simple:</p> <ul> <li>Adding two matrices (<code>matrixadd</code>). Here, just add the elements of one matrix to the corresponding elements of the other matrix.</li> <li>Multiplying a matrix by a single number (a scalar) (<code>matrixmultiply</code>) simply involves multiplying each element of the matrix by that number.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload