Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
17 changes: 15 additions & 2 deletions Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ public Matrix(Func<int[], Entity> operation, params int[] dims) : this(GenTensor
private static Matrix ToMatrix(Entity entity)
=> entity is Matrix m ? m : MathS.Scalar(entity);

/// <summary>
/// <paramref name="simplified"/>, a simplification of a matrix of <paramref name="source"/>'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
/// </summary>
internal static Matrix OfTheShapeOf(Matrix source, Entity simplified)
=> simplified is Matrix m ? m
: source.RowCount == 1 && source.ColumnCount == 1 ? MathS.Scalar(simplified)
: (Matrix)simplified;

/// <summary>
/// Access the tensor if it is a matrix
/// </summary>
Expand Down Expand Up @@ -571,7 +584,7 @@ public IEnumerator<Entity> GetEnumerator()
/// </summary>
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<Matrix> @ref;

Expand All @@ -580,7 +593,7 @@ public IEnumerator<Entity> GetEnumerator()
/// </summary>
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<Matrix> rref;

Expand Down
40 changes: 40 additions & 0 deletions Sources/Tests/UnitTests/Algebra/OneByOneMatrixTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A 1x1 matrix has a rank and row echelon forms, where they threw <c>InvalidCastException</c>: a 1x1 matrix simplifies
/// to its entry, and the simplified matrix was cast back.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2269">#2269</a>
/// </summary>
[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)));
}
}
Loading