From 9bf2c40145643d9927df226a885c8815b13146c1 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 30 Oct 2026 19:42:31 +0000 Subject: [PATCH] A 1x1 matrix has a rank and row echelon forms Rank, RowEchelonForm and ReducedRowEchelonForm threw InvalidCastException for every 1x1 matrix, and so did PointwiseMultiplication of two. A 1x1 matrix simplifies to its entry, and each cast the simplified matrix back to Matrix. OfTheShapeOf wraps the entry back for a 1x1 source only. A larger matrix simplifies to something that is not a matrix only where an entry has no value. That is #2270's NaN, and it still raises: wrapping it as a 1x1 matrix would give that matrix a rank of 1. OneByOneMatrixTest takes the rank and both forms of [2], [1/2], [-3] and [0], and a pointwise product. Five cases fail on master. The suite, the trio, F#, AOT and the eight harnesses pass. work/matrixcheck's 88 findings were all this. Fixes #2269. Co-Authored-By: Claude Opus 5.5 (1M context) --- BREAKING-CHANGES.md | 12 ++++++ Sources/.editorconfig | 3 ++ Sources/AngouriMath/Convenience/MathS.cs | 2 +- .../Core/Entity/Omni/Entity.Matrix.cs | 17 +++++++- .../UnitTests/Algebra/OneByOneMatrixTest.cs | 40 +++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 Sources/Tests/UnitTests/Algebra/OneByOneMatrixTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index e2d1048ee..2b7251190 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -226,6 +226,18 @@ except where shown. | `([[1, 2], [2, 4]] ^ (-1)).Evaled` | throws `InvalidDeterminantException` | `[[1, 2], [2, 4]] ^ (-1)` | | `([[1, 2], [2, 4]] ^ (-1)).Simplify()` | throws `InvalidDeterminantException` | `1 / [[1, 2], [2, 4]]` | +### A 1x1 matrix has a rank and row echelon forms + +**Not silent: what threw now answers.** For a 1x1 matrix, `Rank`, `RowEchelonForm` and `ReducedRowEchelonForm` threw +`InvalidCastException`, and so did `MathS.Matrices.PointwiseMultiplication` of two 1x1 matrices. A 1x1 matrix simplifies +to its entry, and these cast the simplified matrix back where the product already wrapped it +([#2269](https://github.com/asc-community/AngouriMath/issues/2269)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `MathS.Matrix(new Entity[,] { { 2 } }).Rank` | throws `InvalidCastException` | `1` | +| `MathS.Matrix(new Entity[,] { { 2 } }).ReducedRowEchelonForm` | throws `InvalidCastException` | `[1]` | + ### A sum of fractions that is a constant over one denominator simplifies to it **Silent: an identity in fractions reaches its value, and conditions are written shorter.** `Simplify` gathered diff --git a/Sources/.editorconfig b/Sources/.editorconfig index a846f2a78..c747ebf48 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -87,6 +87,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Algebra/ExponentialTwoBasesTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n +[Tests/UnitTests/Algebra/OneByOneMatrixTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + [Tests/UnitTests/Core/Transformations/*.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index 56980e290..c1537e472 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -5972,7 +5972,7 @@ public static Matrix PointwiseMultiplication(Matrix m1, Matrix m2) // dictionary, so the entry has to be there before several threads read it. // See Functions.GenTensorGuard. Functions.GenTensorGuard.EnsurePiecewiseCacheWarmed(); - return (Matrix)new Matrix(GenTensor.PiecewiseMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified; + return Entity.Matrix.OfTheShapeOf(m1, new Matrix(GenTensor.PiecewiseMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified); } /// diff --git a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs index df6a70e63..a5f07c3b6 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs @@ -187,6 +187,19 @@ public Matrix(Func operation, params int[] dims) : this(GenTensor private static Matrix ToMatrix(Entity entity) => entity is Matrix m ? m : MathS.Scalar(entity); + /// + /// , a simplification of a matrix of 's shape, as a matrix. A + /// 1x1 matrix simplifies to its entry, and a cast threw InvalidCastException for the row echelon forms, and so for + /// the rank, of every 1x1 matrix. A larger one simplifies to something that is not a matrix only where an entry has + /// no value, which is a fault and still raises: wrapping it as a 1x1 matrix would give it a rank of 1. + /// https://github.com/asc-community/AngouriMath/issues/2269, + /// https://github.com/asc-community/AngouriMath/issues/2270 + /// + internal static Matrix OfTheShapeOf(Matrix source, Entity simplified) + => simplified is Matrix m ? m + : source.RowCount == 1 && source.ColumnCount == 1 ? MathS.Scalar(simplified) + : (Matrix)simplified; + /// /// Access the tensor if it is a matrix /// @@ -571,7 +584,7 @@ public IEnumerator GetEnumerator() /// public Matrix RowEchelonForm => @ref.GetValue(static @this => - (Matrix)new Matrix(@this.InnerMatrix.RowEchelonFormSafeDivision()).InnerSimplified, + OfTheShapeOf(@this, new Matrix(@this.InnerMatrix.RowEchelonFormSafeDivision()).InnerSimplified), this); private LazyPropertyA @ref; @@ -580,7 +593,7 @@ public IEnumerator GetEnumerator() /// public Matrix ReducedRowEchelonForm => rref.GetValue(static @this => - (Matrix)new Matrix(@this.InnerMatrix.ReducedRowEchelonFormSafeDivision()).InnerSimplified, + OfTheShapeOf(@this, new Matrix(@this.InnerMatrix.ReducedRowEchelonFormSafeDivision()).InnerSimplified), this); private LazyPropertyA rref; diff --git a/Sources/Tests/UnitTests/Algebra/OneByOneMatrixTest.cs b/Sources/Tests/UnitTests/Algebra/OneByOneMatrixTest.cs new file mode 100644 index 000000000..0748b78ae --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/OneByOneMatrixTest.cs @@ -0,0 +1,40 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath; +using Xunit; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// A 1x1 matrix has a rank and row echelon forms, where they threw InvalidCastException: a 1x1 matrix simplifies + /// to its entry, and the simplified matrix was cast back. + /// #2269 + /// + [Trait("Area", "Algebra")] + public sealed class OneByOneMatrixTest + { + private static Entity.Matrix Of(Entity entry) => MathS.Matrix(new Entity[,] { { entry } }); + + [Theory] + [InlineData("2", 1, "1")] + [InlineData("1/2", 1, "1")] + [InlineData("-3", 1, "1")] + [InlineData("0", 0, "0")] + public void ItHasARankAndARowEchelonForm(string entry, int rank, string reduced) + { + var matrix = Of(entry); + Assert.Equal(rank, matrix.Rank); + Assert.Equal(Of(reduced), matrix.ReducedRowEchelonForm); + Assert.Equal(1, matrix.RowEchelonForm.RowCount); + } + + [Fact] + public void APointwiseProductOfTwoIsOne() + => Assert.Equal(Of(6), MathS.Matrices.PointwiseMultiplication(Of(2), Of(3))); + } +}