Skip to content

Commit f570b35

Browse files
A 1x1 matrix has a rank and row echelon forms (#2272)
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) <noreply@anthropic.com>
1 parent 19305ba commit f570b35

5 files changed

Lines changed: 71 additions & 3 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,18 @@ except where shown.
253253
| `([[1, 2], [2, 4]] ^ (-1)).Evaled` | throws `InvalidDeterminantException` | `[[1, 2], [2, 4]] ^ (-1)` |
254254
| `([[1, 2], [2, 4]] ^ (-1)).Simplify()` | throws `InvalidDeterminantException` | `1 / [[1, 2], [2, 4]]` |
255255

256+
### A 1x1 matrix has a rank and row echelon forms
257+
258+
**Not silent: what threw now answers.** For a 1x1 matrix, `Rank`, `RowEchelonForm` and `ReducedRowEchelonForm` threw
259+
`InvalidCastException`, and so did `MathS.Matrices.PointwiseMultiplication` of two 1x1 matrices. A 1x1 matrix simplifies
260+
to its entry, and these cast the simplified matrix back where the product already wrapped it
261+
([#2269](https://github.com/asc-community/AngouriMath/issues/2269)).
262+
263+
| Input | Was (2.6.0) | Now |
264+
|---|---|---|
265+
| `MathS.Matrix(new Entity[,] { { 2 } }).Rank` | throws `InvalidCastException` | `1` |
266+
| `MathS.Matrix(new Entity[,] { { 2 } }).ReducedRowEchelonForm` | throws `InvalidCastException` | `[1]` |
267+
256268
### A sum of fractions that is a constant over one denominator simplifies to it
257269

258270
**Silent: an identity in fractions reaches its value, and conditions are written shorter.** `Simplify` gathered

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
9090
[Tests/UnitTests/Algebra/ExponentialTwoBasesTest.cs]
9191
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
9292

93+
[Tests/UnitTests/Algebra/OneByOneMatrixTest.cs]
94+
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
95+
9396
[Tests/UnitTests/Core/Transformations/*.cs]
9497
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
9598

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5972,7 +5972,7 @@ public static Matrix PointwiseMultiplication(Matrix m1, Matrix m2)
59725972
// dictionary, so the entry has to be there before several threads read it.
59735973
// See Functions.GenTensorGuard.
59745974
Functions.GenTensorGuard.EnsurePiecewiseCacheWarmed();
5975-
return (Matrix)new Matrix(GenTensor.PiecewiseMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified;
5975+
return Entity.Matrix.OfTheShapeOf(m1, new Matrix(GenTensor.PiecewiseMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified);
59765976
}
59775977

59785978
/// <summary>

‎Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@ public Matrix(Func<int[], Entity> operation, params int[] dims) : this(GenTensor
187187
private static Matrix ToMatrix(Entity entity)
188188
=> entity is Matrix m ? m : MathS.Scalar(entity);
189189

190+
/// <summary>
191+
/// <paramref name="simplified"/>, a simplification of a matrix of <paramref name="source"/>'s shape, as a matrix. A
192+
/// 1x1 matrix simplifies to its entry, and a cast threw InvalidCastException for the row echelon forms, and so for
193+
/// the rank, of every 1x1 matrix. A larger one simplifies to something that is not a matrix only where an entry has
194+
/// no value, which is a fault and still raises: wrapping it as a 1x1 matrix would give it a rank of 1.
195+
/// https://github.com/asc-community/AngouriMath/issues/2269,
196+
/// https://github.com/asc-community/AngouriMath/issues/2270
197+
/// </summary>
198+
internal static Matrix OfTheShapeOf(Matrix source, Entity simplified)
199+
=> simplified is Matrix m ? m
200+
: source.RowCount == 1 && source.ColumnCount == 1 ? MathS.Scalar(simplified)
201+
: (Matrix)simplified;
202+
190203
/// <summary>
191204
/// Access the tensor if it is a matrix
192205
/// </summary>
@@ -571,7 +584,7 @@ public IEnumerator<Entity> GetEnumerator()
571584
/// </summary>
572585
public Matrix RowEchelonForm =>
573586
@ref.GetValue(static @this =>
574-
(Matrix)new Matrix(@this.InnerMatrix.RowEchelonFormSafeDivision()).InnerSimplified,
587+
OfTheShapeOf(@this, new Matrix(@this.InnerMatrix.RowEchelonFormSafeDivision()).InnerSimplified),
575588
this);
576589
private LazyPropertyA<Matrix> @ref;
577590

@@ -580,7 +593,7 @@ public IEnumerator<Entity> GetEnumerator()
580593
/// </summary>
581594
public Matrix ReducedRowEchelonForm =>
582595
rref.GetValue(static @this =>
583-
(Matrix)new Matrix(@this.InnerMatrix.ReducedRowEchelonFormSafeDivision()).InnerSimplified,
596+
OfTheShapeOf(@this, new Matrix(@this.InnerMatrix.ReducedRowEchelonFormSafeDivision()).InnerSimplified),
584597
this);
585598
private LazyPropertyA<Matrix> rref;
586599

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath;
9+
using Xunit;
10+
11+
namespace AngouriMath.Tests.Algebra
12+
{
13+
/// <summary>
14+
/// A 1x1 matrix has a rank and row echelon forms, where they threw <c>InvalidCastException</c>: a 1x1 matrix simplifies
15+
/// to its entry, and the simplified matrix was cast back.
16+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2269">#2269</a>
17+
/// </summary>
18+
[Trait("Area", "Algebra")]
19+
public sealed class OneByOneMatrixTest
20+
{
21+
private static Entity.Matrix Of(Entity entry) => MathS.Matrix(new Entity[,] { { entry } });
22+
23+
[Theory]
24+
[InlineData("2", 1, "1")]
25+
[InlineData("1/2", 1, "1")]
26+
[InlineData("-3", 1, "1")]
27+
[InlineData("0", 0, "0")]
28+
public void ItHasARankAndARowEchelonForm(string entry, int rank, string reduced)
29+
{
30+
var matrix = Of(entry);
31+
Assert.Equal(rank, matrix.Rank);
32+
Assert.Equal(Of(reduced), matrix.ReducedRowEchelonForm);
33+
Assert.Equal(1, matrix.RowEchelonForm.RowCount);
34+
}
35+
36+
[Fact]
37+
public void APointwiseProductOfTwoIsOne()
38+
=> Assert.Equal(Of(6), MathS.Matrices.PointwiseMultiplication(Of(2), Of(3)));
39+
}
40+
}

0 commit comments

Comments
 (0)