Skip to content

A matrix is divided, raised to a power and compared as a matrix - #2155

Merged
Rafael-SOWNet merged 2 commits into
masterfrom
a-matrix-is-divided-and-compared-as-a-matrix
Oct 27, 2026
Merged

Rafael-SOWNet merged 2 commits into
masterfrom
a-matrix-is-divided-and-compared-as-a-matrix

Conversation

@Rafael-SOWNet

@Rafael-SOWNet Rafael-SOWNet commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Fixes #2141.

Wrong answers about matrices. Where an operation has no answer of its own for two matrices, or for a matrix and a number, ExpandOnTwoArguments applies it entry by entry. That is right for addition and subtraction, and for a matrix over a number, but not for division, powers or equality.

Input Was (2.6.0) Now
[[1, 2], [3, 4]] / [[1, 2], [3, 4]] [[1, 1], [1, 1]] [[1, 0], [0, 1]]
2 / [[1, 2], [3, 4]] [[2, 1], [2/3, 1/2]] [[-4, 2], [3, -1]]
2 / [1, 2] [2, 1] 2 / [1, 2]
[1, 2] ^ 2 [1, 4] [1, 2] ^ 2
([1, 2] * [1, 2]).Simplify() [1, 4] [1, 2] ^ 2
[1, 2] = [1, 3] [True, False] False
[a, b] = [c, d] [a = c, b = d] a = c and b = d
[[1, 2], [2, 4]] ^ (-1) throws InvalidDeterminantException stays written

Division by a matrix multiplies by its inverse, as the wiki's Matrices page says. A divisor with no inverse, singular or not square, leaves the quotient written.

A number over a matrix is the number times the inverse too. I first spread it over the entries, to match 2 - M and M / 2, and that gave a wrong answer:

  • Simplify rewrites x^(-1) and 1 / x into each other.
  • So a singular M^(-1), which stays written, became 1 / M.
  • Spread over the entries, that is the matrix of the entries' reciprocals.

x / y and x * y^(-1) have to agree, and y^(-1) of a matrix is its inverse.

A power of anything but a square matrix stays written. A negative power of a singular matrix stays written too; it used to throw from Evaled and Simplify. This also fixes [1, 2] * [1, 2], which Simplify rewrites as [1, 2]^2.

Two matrices are equal when they have one shape and are equal entry by entry. The equality is now one statement, where it was a vector of statements.

Unchanged:

  • Addition and subtraction entry by entry.
  • A matrix over a number, and a number times a matrix.
  • Order comparisons such as <, which are still elementwise.
  • [1, 2] = 1.

Native builds. The first push asked Matrix.Inverse, and failed every AOT publish with IL3053. Matrix.Inverse goes through GenericTensor's InvertMatrix, and Determinant can fall back to its DeterminantLaplace; both are compiled at run time, and reaching them from division made them reachable from every node. Whether a matrix may have an inverse is now asked of the exact elimination (PolynomialDeterminant). The inverse is Pow(-1), which master already reaches.

Checked:

  • New rows in MatrixTest, with all 147 matrix tests passing.
  • The C++ wrapper's native publish succeeds, and the trimmed and AOT smoke tests publish and pass 45 of 45, all run locally.
  • The full unit suite: 17,098 passed, 0 failed, 13 skipped.
  • The BREAKING-CHANGES entry was measured on the 2.6.0 package and on this branch. It sits at its own anchor, away from A negative whole power is inverted to its roots, not to none #2154's.

Found while reading Modenov's Problems in Geometry for #2131.

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

Where an operation had no answer of its own for two matrices, or a
matrix and a number, ExpandOnTwoArguments applied it entry by entry.
That is what addition and subtraction mean, and a matrix over a number,
but not division, powers or equality: [[1, 2], [3, 4]] divided by itself
was [[1, 1], [1, 1]], [1, 2]^2 was [1, 4], and [1, 2] = [1, 3] was the
vector [True, False] while [1, 2] = [1, 2] was True. Simplify writes
x * x as x^2, so the undefined product [1, 2] * [1, 2] became [1, 4]
too.

Dividing by a matrix now multiplies by its inverse, as the wiki says it
does, and a divisor with no inverse leaves the quotient written. A
number over a matrix is that number times the inverse as well: x / y
and x * y^(-1) are rewritten into each other, and y^(-1) of a matrix is
its inverse, so reading 1 / M entry by entry disagreed with M^(-1). A
version that spread the number over the entries instead turned a
singular M^(-1), which Simplify writes as 1 / M, into the reciprocals of
its entries.

A power of anything but a square matrix stays written, and so does a
negative power of a singular one, which threw InvalidDeterminantException
from Evaled and Simplify. Two matrices are equal when they have one
shape and are equal entry by entry, as one statement.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
Asking Matrix.Inverse from division and from a power made GenericTensor's
InvertMatrix, and through Determinant its DeterminantLaplace, reachable
from every node. Both are compiled at run time and are not safe in a
native build, so every AOT publish failed with IL3053. Whether a matrix
may have an inverse is now asked of the exact elimination, which does
not touch GenericTensor, and the inverse itself is Pow(-1), which was
reachable before. A determinant the elimination cannot write is not
known to be zero.

The C++ wrapper's native publish and the trimmed and AOT smoke tests
pass locally, the latter 45 of 45.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
@Rafael-SOWNet
Rafael-SOWNet merged commit 4ae8d25 into master Oct 27, 2026
34 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 27, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the a-matrix-is-divided-and-compared-as-a-matrix branch October 27, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

A / A for a matrix is computed elementwise, and so is the undefined product of two columns

1 participant