diff --git a/.auto-claude-security.json b/.auto-claude-security.json new file mode 100644 index 00000000..1712c7eb --- /dev/null +++ b/.auto-claude-security.json @@ -0,0 +1,183 @@ +{ + "base_commands": [ + ".", + "[", + "[[", + "ag", + "awk", + "basename", + "bash", + "bc", + "break", + "cat", + "cd", + "chmod", + "clear", + "cmp", + "column", + "comm", + "command", + "continue", + "cp", + "curl", + "cut", + "date", + "df", + "diff", + "dig", + "dirname", + "du", + "echo", + "egrep", + "env", + "eval", + "exec", + "exit", + "expand", + "export", + "expr", + "false", + "fd", + "fgrep", + "file", + "find", + "fmt", + "fold", + "gawk", + "gh", + "git", + "grep", + "gunzip", + "gzip", + "head", + "help", + "host", + "iconv", + "id", + "jobs", + "join", + "jq", + "kill", + "killall", + "less", + "let", + "ln", + "ls", + "lsof", + "man", + "mkdir", + "mktemp", + "more", + "mv", + "nl", + "paste", + "pgrep", + "ping", + "pkill", + "popd", + "printenv", + "printf", + "ps", + "pushd", + "pwd", + "read", + "readlink", + "realpath", + "reset", + "return", + "rev", + "rg", + "rm", + "rmdir", + "sed", + "seq", + "set", + "sh", + "shuf", + "sleep", + "sort", + "source", + "split", + "stat", + "tail", + "tar", + "tee", + "test", + "time", + "timeout", + "touch", + "tr", + "tree", + "true", + "type", + "uname", + "unexpand", + "uniq", + "unset", + "unzip", + "watch", + "wc", + "wget", + "whereis", + "which", + "whoami", + "xargs", + "yes", + "yq", + "zip", + "zsh" + ], + "stack_commands": [ + "composer", + "ipython", + "jupyter", + "node", + "notebook", + "npm", + "npx", + "pdb", + "php", + "phpunit", + "pip", + "pip3", + "pipx", + "pudb", + "python", + "python3" + ], + "script_commands": [ + "./parallel.sh" + ], + "custom_commands": [], + "detected_stack": { + "languages": [ + "python", + "javascript", + "php" + ], + "package_managers": [ + "composer" + ], + "frameworks": [ + "phpunit" + ], + "databases": [], + "infrastructure": [], + "cloud_providers": [], + "code_quality_tools": [], + "version_managers": [] + }, + "custom_scripts": { + "npm_scripts": [], + "make_targets": [], + "poetry_scripts": [], + "cargo_aliases": [], + "shell_scripts": [ + "parallel.sh" + ] + }, + "project_dir": "/home/jordan/Projects/Fermat", + "created_at": "2026-01-17T02:59:58.884479", + "project_hash": "ff04866add36ffa1116480549ed508fe", + "inherited_from": "/home/jordan/Projects/Fermat" +} \ No newline at end of file diff --git a/.auto-claude-status b/.auto-claude-status new file mode 100644 index 00000000..b5861a45 --- /dev/null +++ b/.auto-claude-status @@ -0,0 +1,25 @@ +{ + "active": true, + "spec": "001-matrix-minors-and-cofactors", + "state": "building", + "subtasks": { + "completed": 6, + "total": 7, + "in_progress": 1, + "failed": 0 + }, + "phase": { + "current": "Add Comprehensive Tests", + "id": null, + "total": 3 + }, + "workers": { + "active": 0, + "max": 1 + }, + "session": { + "number": 1, + "started_at": "2026-01-17T17:25:02.226019" + }, + "last_update": "2026-01-17T17:25:02.286953" +} \ No newline at end of file diff --git a/.claude_settings.json b/.claude_settings.json new file mode 100644 index 00000000..234ec972 --- /dev/null +++ b/.claude_settings.json @@ -0,0 +1,39 @@ +{ + "sandbox": { + "enabled": true, + "autoAllowBashIfSandboxed": true + }, + "permissions": { + "defaultMode": "acceptEdits", + "allow": [ + "Read(./**)", + "Write(./**)", + "Edit(./**)", + "Glob(./**)", + "Grep(./**)", + "Read(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/**)", + "Write(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/**)", + "Edit(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/**)", + "Glob(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/**)", + "Grep(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/**)", + "Read(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/.auto-claude/specs/001-matrix-minors-and-cofactors/**)", + "Write(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/.auto-claude/specs/001-matrix-minors-and-cofactors/**)", + "Edit(/home/jordan/Projects/Fermat/.auto-claude/worktrees/tasks/001-matrix-minors-and-cofactors/.auto-claude/specs/001-matrix-minors-and-cofactors/**)", + "Read(/home/jordan/Projects/Fermat/.auto-claude/**)", + "Write(/home/jordan/Projects/Fermat/.auto-claude/**)", + "Edit(/home/jordan/Projects/Fermat/.auto-claude/**)", + "Glob(/home/jordan/Projects/Fermat/.auto-claude/**)", + "Grep(/home/jordan/Projects/Fermat/.auto-claude/**)", + "Bash(*)", + "WebFetch(*)", + "WebSearch(*)", + "mcp__context7__resolve-library-id(*)", + "mcp__context7__get-library-docs(*)", + "mcp__graphiti-memory__search_nodes(*)", + "mcp__graphiti-memory__search_facts(*)", + "mcp__graphiti-memory__add_episode(*)", + "mcp__graphiti-memory__get_episodes(*)", + "mcp__graphiti-memory__get_entity_edge(*)" + ] + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9af31763..91fe11b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,13 @@ -.idea -vendor/ -composer.lock -build -.phpunit.result.cache -site -tools/ -.php-cs-fixer.cache -.phive/ -phive.phar.asc \ No newline at end of file +.idea +vendor/ +composer.lock +build +.phpunit.result.cache +site +tools/ +.php-cs-fixer.cache +.phive/ +phive.phar.asc + +# Auto Claude data directory +.auto-claude/ diff --git a/src/Samsara/Fermat/LinearAlgebra/Types/Matrix.php b/src/Samsara/Fermat/LinearAlgebra/Types/Matrix.php index 207cf938..ff0bf0e7 100644 --- a/src/Samsara/Fermat/LinearAlgebra/Types/Matrix.php +++ b/src/Samsara/Fermat/LinearAlgebra/Types/Matrix.php @@ -59,7 +59,14 @@ public function getDeterminant(): ImmutableDecimal ); } - if ($this->numRows > 2) { + if ($this->numRows === 0) { + // The determinant of a 0x0 matrix is defined as 1 by convention + $determinant = Numbers::makeOne(); + } elseif ($this->numRows === 1) { + $determinant = $this->rows[0]->get(0); + } elseif ($this->numRows === 2) { + $determinant = $this->rows[0]->get(0)->multiply($this->rows[1]->get(1))->subtract($this->rows[1]->get(0)->multiply($this->rows[0]->get(1))); + } else { $determinant = Numbers::makeZero(); foreach ($this->rows[0]->toArray() as $key => $value) { @@ -67,8 +74,6 @@ public function getDeterminant(): ImmutableDecimal $determinant = $determinant->add($value->multiply($childMatrix->getDeterminant())->multiply(SequenceProvider::nthPowerNegativeOne($key))); } - } else { - $determinant = $this->rows[0]->get(0)->multiply($this->rows[1]->get(1))->subtract($this->rows[1]->get(0)->multiply($this->rows[0]->get(1))); } return $determinant; @@ -84,7 +89,6 @@ public function getInverseMatrix(): static $determinant = $this->getDeterminant(); $inverseDeterminant = new ImmutableFraction(Numbers::makeOne(), $determinant); - // TODO: Implement minors & cofactors method https://www.mathsisfun.com/algebra/matrix-inverse-minors-cofactors-adjugate.html if ($this->getRowCount() > 2) { $minors = $this->getMatrixOfMinors(); $cofactors = $minors->applyAlternatingSigns(); @@ -114,6 +118,31 @@ public function getMatrixOfMinors(): static return $this->mapFuction($fn); } + /** + * Returns a new collection that contains the matrix of minors for each element in the current collection. + * + * This is an alias for getMatrixOfMinors(). + * + * @return static A new collection containing the matrix of minors. + */ + public function minors(): static + { + return $this->getMatrixOfMinors(); + } + + /** + * Returns a new matrix containing the cofactors of each element in the current matrix. + * + * The matrix of cofactors is obtained by taking the matrix of minors and applying alternating signs + * based on position. The sign pattern follows a checkerboard pattern starting with + at position (0,0). + * + * @return static A new matrix containing the cofactors. + */ + public function cofactors(): static + { + return $this->getMatrixOfMinors()->applyAlternatingSigns(); + } + /** * Adds a MatrixInterface object to the current matrix and returns a new matrix with the sum. * diff --git a/src/Samsara/Fermat/LinearAlgebra/Types/Traits/Matrix/ShapeTrait.php b/src/Samsara/Fermat/LinearAlgebra/Types/Traits/Matrix/ShapeTrait.php index e2e70735..5397b060 100644 --- a/src/Samsara/Fermat/LinearAlgebra/Types/Traits/Matrix/ShapeTrait.php +++ b/src/Samsara/Fermat/LinearAlgebra/Types/Traits/Matrix/ShapeTrait.php @@ -30,11 +30,16 @@ public function getAdjoint(): static } /** + * Returns the adjugate matrix (also known as classical adjoint). + * + * The adjugate is the transpose of the cofactor matrix. + * This is different from getAdjoint() which is just the transpose. + * * @return static */ public function getAdjugate(): static { - return $this->getAdjoint(); + return $this->cofactors()->getAdjoint(); } /** diff --git a/tests/Samsara/Fermat/LinearAlgebra/Types/MatrixTest.php b/tests/Samsara/Fermat/LinearAlgebra/Types/MatrixTest.php index b09966fa..138245fc 100644 --- a/tests/Samsara/Fermat/LinearAlgebra/Types/MatrixTest.php +++ b/tests/Samsara/Fermat/LinearAlgebra/Types/MatrixTest.php @@ -512,10 +512,10 @@ public function testAdjugate() $matrix = new ImmutableMatrix($matrixData); $adjugate = $matrix->getAdjugate(); - $this->assertEquals('3', $adjugate->getRow(0)->get(0)->getValue()); - $this->assertEquals('4', $adjugate->getRow(0)->get(1)->getValue()); - $this->assertEquals('5', $adjugate->getRow(1)->get(0)->getValue()); - $this->assertEquals('2', $adjugate->getRow(1)->get(1)->getValue()); + $this->assertEquals('2', $adjugate->getRow(0)->get(0)->getValue()); + $this->assertEquals('-5', $adjugate->getRow(0)->get(1)->getValue()); + $this->assertEquals('-4', $adjugate->getRow(1)->get(0)->getValue()); + $this->assertEquals('3', $adjugate->getRow(1)->get(1)->getValue()); } @@ -571,4 +571,98 @@ public function testShiftColumn() $this->assertEquals('4', $column->get(1)->getValue()); } + public function testMinors() + { + // Test 2x2 matrix + $matrixData = [ + new NumberCollection([Numbers::make(Numbers::IMMUTABLE, '3'), Numbers::make(Numbers::IMMUTABLE, '5')]), // row 1 + new NumberCollection([Numbers::make(Numbers::IMMUTABLE, '4'), Numbers::make(Numbers::IMMUTABLE, '2')]), // row 2 + ]; + + $matrix = new ImmutableMatrix($matrixData); + $minorMatrix = $matrix->minors(); + + $this->assertEquals('2', $minorMatrix->getRow(0)->get(0)->getValue()); + $this->assertEquals('4', $minorMatrix->getRow(0)->get(1)->getValue()); + $this->assertEquals('5', $minorMatrix->getRow(1)->get(0)->getValue()); + $this->assertEquals('3', $minorMatrix->getRow(1)->get(1)->getValue()); + + // Test 3x3 matrix + $matrixData = [ + new NumberCollection([new ImmutableDecimal(3), new ImmutableDecimal(0), new ImmutableDecimal(2)]), + new NumberCollection([new ImmutableDecimal(2), new ImmutableDecimal(0), new ImmutableDecimal(-2)]), + new NumberCollection([new ImmutableDecimal(0), new ImmutableDecimal(1), new ImmutableDecimal(1)]) + ]; + + $matrix = new ImmutableMatrix($matrixData); + $minorMatrix = $matrix->minors(); + + $this->assertEquals('2', $minorMatrix->getRow(0)->get(0)->getValue()); + $this->assertEquals('2', $minorMatrix->getRow(0)->get(1)->getValue()); + $this->assertEquals('2', $minorMatrix->getRow(0)->get(2)->getValue()); + $this->assertEquals('-2', $minorMatrix->getRow(1)->get(0)->getValue()); + $this->assertEquals('3', $minorMatrix->getRow(1)->get(1)->getValue()); + $this->assertEquals('3', $minorMatrix->getRow(1)->get(2)->getValue()); + $this->assertEquals('0', $minorMatrix->getRow(2)->get(0)->getValue()); + $this->assertEquals('-10', $minorMatrix->getRow(2)->get(1)->getValue()); + $this->assertEquals('0', $minorMatrix->getRow(2)->get(2)->getValue()); + + // Test 1x1 matrix + $matrixData = [ + new NumberCollection([Numbers::make(Numbers::IMMUTABLE, '5')]) + ]; + + $matrix = new ImmutableMatrix($matrixData); + $minorMatrix = $matrix->minors(); + + $this->assertEquals('1', $minorMatrix->getRow(0)->get(0)->getValue()); + } + + public function testCofactors() + { + // Test 2x2 matrix + $matrixData = [ + new NumberCollection([Numbers::make(Numbers::IMMUTABLE, '3'), Numbers::make(Numbers::IMMUTABLE, '5')]), // row 1 + new NumberCollection([Numbers::make(Numbers::IMMUTABLE, '4'), Numbers::make(Numbers::IMMUTABLE, '2')]), // row 2 + ]; + + $matrix = new ImmutableMatrix($matrixData); + $cofactorMatrix = $matrix->cofactors(); + + $this->assertEquals('2', $cofactorMatrix->getRow(0)->get(0)->getValue()); + $this->assertEquals('-4', $cofactorMatrix->getRow(0)->get(1)->getValue()); + $this->assertEquals('-5', $cofactorMatrix->getRow(1)->get(0)->getValue()); + $this->assertEquals('3', $cofactorMatrix->getRow(1)->get(1)->getValue()); + + // Test 3x3 matrix + $matrixData = [ + new NumberCollection([new ImmutableDecimal(3), new ImmutableDecimal(0), new ImmutableDecimal(2)]), + new NumberCollection([new ImmutableDecimal(2), new ImmutableDecimal(0), new ImmutableDecimal(-2)]), + new NumberCollection([new ImmutableDecimal(0), new ImmutableDecimal(1), new ImmutableDecimal(1)]) + ]; + + $matrix = new ImmutableMatrix($matrixData); + $cofactorMatrix = $matrix->cofactors(); + + $this->assertEquals('2', $cofactorMatrix->getRow(0)->get(0)->getValue()); + $this->assertEquals('-2', $cofactorMatrix->getRow(0)->get(1)->getValue()); + $this->assertEquals('2', $cofactorMatrix->getRow(0)->get(2)->getValue()); + $this->assertEquals('2', $cofactorMatrix->getRow(1)->get(0)->getValue()); + $this->assertEquals('3', $cofactorMatrix->getRow(1)->get(1)->getValue()); + $this->assertEquals('-3', $cofactorMatrix->getRow(1)->get(2)->getValue()); + $this->assertEquals('0', $cofactorMatrix->getRow(2)->get(0)->getValue()); + $this->assertEquals('10', $cofactorMatrix->getRow(2)->get(1)->getValue()); + $this->assertEquals('0', $cofactorMatrix->getRow(2)->get(2)->getValue()); + + // Test 1x1 matrix + $matrixData = [ + new NumberCollection([Numbers::make(Numbers::IMMUTABLE, '5')]) + ]; + + $matrix = new ImmutableMatrix($matrixData); + $cofactorMatrix = $matrix->cofactors(); + + $this->assertEquals('1', $cofactorMatrix->getRow(0)->get(0)->getValue()); + } + } diff --git a/vendor b/vendor new file mode 120000 index 00000000..f42b6d55 --- /dev/null +++ b/vendor @@ -0,0 +1 @@ +/home/jordan/Projects/Fermat/vendor \ No newline at end of file