private static function createPlayerTeamAssignmentMatrix(&$teamAssignmentsList, $totalPlayers) { // The team assignment matrix is often referred to as the "A" matrix. It's a matrix whose rows represent the players // and the columns represent teams. At Matrix[row, column] represents that player[row] is on team[col] // Positive values represent an assignment and a negative value means that we subtract the value of the next // team since we're dealing with pairs. This means that this matrix always has teams - 1 columns. // The only other tricky thing is that values represent the play percentage. // For example, consider a 3 team game where team1 is just player1, team 2 is player 2 and player 3, and // team3 is just player 4. Furthermore, player 2 and player 3 on team 2 played 25% and 75% of the time // (e.g. partial play), the A matrix would be: // A = this 4x2 matrix: // | 1.00 0.00 | // | -0.25 0.25 | // | -0.75 0.75 | // | 0.00 -1.00 | $playerAssignments = array(); $totalPreviousPlayers = 0; $teamAssignmentsListCount = count($teamAssignmentsList); $currentColumn = 0; for ($i = 0; $i < $teamAssignmentsListCount - 1; $i++) { $currentTeam = $teamAssignmentsList[$i]; // Need to add in 0's for all the previous players, since they're not // on this team $playerAssignments[$currentColumn] = $totalPreviousPlayers > 0 ? \array_fill(0, $totalPreviousPlayers, 0) : array(); foreach ($currentTeam->getAllPlayers() as $currentPlayer) { $playerAssignments[$currentColumn][] = PartialPlay::getPartialPlayPercentage($currentPlayer); // indicates the player is on the team $totalPreviousPlayers++; } $rowsRemaining = $totalPlayers - $totalPreviousPlayers; $nextTeam = $teamAssignmentsList[$i + 1]; foreach ($nextTeam->getAllPlayers() as $nextTeamPlayer) { // Add a -1 * playing time to represent the difference $playerAssignments[$currentColumn][] = -1 * PartialPlay::getPartialPlayPercentage($nextTeamPlayer); $rowsRemaining--; } for ($ixAdditionalRow = 0; $ixAdditionalRow < $rowsRemaining; $ixAdditionalRow++) { // Pad with zeros $playerAssignments[$currentColumn][] = 0; } $currentColumn++; } $playerTeamAssignmentsMatrix = Matrix::fromColumnValues($totalPlayers, $teamAssignmentsListCount - 1, $playerAssignments); return $playerTeamAssignmentsMatrix; }
public function testInverse() { // see http://www.mathwords.com/i/inverse_of_a_matrix.htm $a = new SquareMatrix(4, 3, 3, 2); $b = new SquareMatrix(-2, 3, 3, -4); $aInverse = $a->getInverse(); $this->assertTrue($b->equals($aInverse)); $identity2x2 = new IdentityMatrix(2); $aaInverse = Matrix::multiply($a, $aInverse); $this->assertTrue($identity2x2->equals($aaInverse)); $c = new SquareMatrix(1, 2, 3, 0, 4, 5, 1, 0, 6); $cInverse = $c->getInverse(); $d = Matrix::scalarMultiply(1.0 / 22, new SquareMatrix(24, -12, -2, 5, 3, -5, -4, 2, 4)); $this->assertTrue($d->equals($cInverse)); $identity3x3 = new IdentityMatrix(3); $ccInverse = Matrix::multiply($c, $cInverse); $this->assertTrue($identity3x3->equals($ccInverse)); }