Skip to content

Commit 40dd98f

Browse files
Decide a symbolic pivot before choosing it (#2350)
The row echelon forms were GenericTensor's, whose elimination asked whether a pivot was the literal 0. An entry the elimination had made itself, like c + -c / a * a, is 0 without being written so: it was chosen and divided by, so the reduced form of [[a, b], [c, d]] simplified to NaN, and the rank of [[a, b], [2a, 2b]], whose second row is twice the first, was 2. 2.6.0 does the same. The elimination is written over the entries, with GenericTensor's steps: the first entry of a column that is not 0, from the current row down, for its pivot, rows swapped up, and the reduced form's pivot rows divided by their pivot. Each entry it makes is normalised, a rational function over the rationals to RationalFunction's canonical quotient, where 0 is the literal 0, and anything else is simplified when asked whether it is 0. A symbolic matrix's rank is its rank for general values of its symbols: [[a, b], [2a, 2b]] has rank 1, and [[a, b, c], [d, f, g], [a + d, b + f, c + g]] rank 2. Matrices of numbers come out as they did: the 177 matrix tests pass unchanged, and matrixcheck is clean on 1200 random ones. Part of #2345 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0731fc1 commit 40dd98f

3 files changed

Lines changed: 185 additions & 16 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,23 @@ value too. No issue was filed; a check of compiled expressions against evaluatio
445445
| `"binomial(10000, 1.43)".EvalNumerical()` | throws `CannotEvalException` | `414189.539...` |
446446
| `"binomial(pi, 2000)".EvalNumerical()` | `NaN` | `2.10439...E-14` |
447447

448+
### A symbolic matrix's rank and row echelon forms are decided pivot by pivot
449+
450+
**Silent: a different answer.** The elimination asked whether a pivot was the literal 0. An entry it had made itself,
451+
like `c + -c / a * a`, is 0 without being written so, and it was chosen and divided by: the rank of `[[a, b], [2a, 2b]]`,
452+
whose second row is twice the first, was 2, and the reduced form of every symbolic matrix in the table simplified to
453+
`NaN`. Each entry the elimination makes is now normalised, a rational function over the rationals to its canonical
454+
quotient, where 0 is the literal 0, and anything else is simplified before it is taken for a pivot. A symbolic
455+
matrix's rank is its rank for general values of its symbols. Matrices of numbers are reduced as before.
456+
https://github.com/asc-community/AngouriMath/issues/2345
457+
458+
| Input | Was (2.6.0) | Now |
459+
|---|---|---|
460+
| `[[a, b], [2a, 2b]].Rank` | `2` | `1` |
461+
| `[[a, b, c], [d, f, g], [a + d, b + f, c + g]].Rank` | `3` | `2` |
462+
| `[[x, x^2], [1, x]].Rank` | `2` | `1` |
463+
| `[[a, b], [c, d]].ReducedRowEchelonForm.Simplify()` | `NaN` | `[[1, 0], [0, 1]]` |
464+
448465
### The row echelon forms divide exactly
449466

450467
**Not silent for numbers: what threw or did not return now answers. Silent for symbols: the forms are written

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

Lines changed: 101 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -583,41 +583,126 @@ public IEnumerator<Entity> GetEnumerator()
583583
/// Matrix's form, transformed via Gaussian elimination.
584584
/// </summary>
585585
/// <remarks>
586-
/// Both forms divide as they go. GenericTensor's SafeDivision variants keep every entry as a numerator and a
587-
/// denominator that are never reduced against each other, which is for element types whose division truncates,
588-
/// and an Entity divides exactly. Unreduced, the pairs grew with every row operation and the back substitution
589-
/// squared them: one 7x7 of small entries took two minutes, and an 8x8 did not return in ten, and the 4x4
590-
/// of https://github.com/asc-community/AngouriMath/issues/2270 ended with a denominator near 1e-246, which
591-
/// https://github.com/asc-community/AngouriMath/issues/2277 made a NaN.
586+
/// Both forms divide as they go, each entry an exact quotient: GenericTensor's SafeDivision variants kept
587+
/// every entry as a numerator and a denominator never reduced against each other, the pairs grew with every
588+
/// row operation, and the 4x4 of https://github.com/asc-community/AngouriMath/issues/2270 ended with a
589+
/// denominator near 1e-246, which https://github.com/asc-community/AngouriMath/issues/2277 made a NaN. A
590+
/// pivot is the first entry of its column, from the current row down, that is not zero, and a symbolic
591+
/// entry is simplified to say so; see <see cref="Eliminate"/>.
592592
/// </remarks>
593593
public Matrix RowEchelonForm =>
594594
@ref.GetValue(static @this =>
595-
OfTheShapeOf(@this, new Matrix(@this.InnerMatrix.RowEchelonFormSimple()).InnerSimplified),
595+
OfTheShapeOf(@this, MathS.Matrix(Eliminate(@this, reduced: false).Entries).InnerSimplified),
596596
this);
597597
private LazyPropertyA<Matrix> @ref;
598598

599599
/// <summary>
600600
/// Reduced row echelon form via Gaussian elimination.
601601
/// </summary>
602+
/// <remarks>
603+
/// Each pivot row is divided by its pivot, so that where a pivot is symbolic the form holds where that
604+
/// pivot is not 0, and the quotients say so as their simplification does.
605+
/// </remarks>
602606
public Matrix ReducedRowEchelonForm =>
603607
rref.GetValue(static @this =>
604-
OfTheShapeOf(@this, new Matrix(@this.InnerMatrix.ReducedRowEchelonFormSimple()).InnerSimplified),
608+
OfTheShapeOf(@this, MathS.Matrix(Eliminate(@this, reduced: true).Entries).InnerSimplified),
605609
this);
606610
private LazyPropertyA<Matrix> rref;
607611

608612
/// <summary>
609-
/// The number of linearly independent rows
613+
/// The number of linearly independent rows: of the elimination's pivots. For a symbolic matrix it is the
614+
/// rank for general values of the symbols, as over the field of rational functions in them: the rank of
615+
/// <c>[[a, b], [2 a, 2 b]]</c> is 1, which is 0 where <c>a = b = 0</c>.
610616
/// </summary>
611617
public int Rank =>
612-
rank.GetValue(static @this =>
618+
rank.GetValue(static @this => Eliminate(@this, reduced: false).Pivots, this);
619+
private LazyPropertyA<int> rank;
620+
621+
/// <summary>
622+
/// Gaussian elimination over the entries, to the row echelon form, or with <paramref name="reduced"/> to the
623+
/// reduced one, and the number of pivots it found.
624+
/// </summary>
625+
/// <remarks>
626+
/// GenericTensor's elimination asked whether a pivot was the literal 0, and an entry the elimination had
627+
/// itself made, like <c>c + -c / a * a</c>, is 0 without being written so: it was chosen and divided by, and
628+
/// the reduced form of <c>[[a, b], [c, d]]</c> simplified to NaN and the rank of <c>[[a, b], [2 a, 2 b]]</c>
629+
/// was 2. Each entry the elimination makes is normalised, a rational function over the rationals to its
630+
/// canonical quotient (<see cref="Functions.RationalFunction"/>), where being 0 is being the literal 0, and
631+
/// the third row of <c>[[a, b, c], [d, e, f], [a + d, b + e, c + f]]</c> is 0 as it should be; anything
632+
/// else is simplified when it is asked whether it is 0.
633+
/// https://github.com/asc-community/AngouriMath/issues/2345
634+
/// </remarks>
635+
private static (Entity[,] Entries, int Pivots) Eliminate(Matrix matrix, bool reduced)
636+
{
637+
var (rows, columns) = (matrix.RowCount, matrix.ColumnCount);
638+
var entries = new Entity[rows, columns];
639+
for (var r = 0; r < rows; r++)
640+
for (var c = 0; c < columns; c++)
641+
entries[r, c] = matrix[r, c];
642+
var row = 0;
643+
for (var column = 0; column < columns && row < rows; column++)
613644
{
614-
for (int i = 0; i < @this.RowCount; i++)
615-
if (@this.ReducedRowEchelonForm.InnerMatrix.RowGetLeadingElement(i) is null)
616-
return i;
617-
return @this.RowCount;
645+
var pivotRow = -1;
646+
for (var r = row; r < rows && pivotRow < 0; r++)
647+
if (IsZeroEntry(entries[r, column]))
648+
entries[r, column] = Number.Integer.Zero;
649+
else
650+
pivotRow = r;
651+
if (pivotRow < 0)
652+
continue;
653+
if (pivotRow != row)
654+
for (var c = 0; c < columns; c++)
655+
(entries[row, c], entries[pivotRow, c]) = (entries[pivotRow, c], entries[row, c]);
656+
var pivot = entries[row, column];
657+
if (reduced)
658+
{
659+
for (var c = column + 1; c < columns; c++)
660+
entries[row, c] = Normalised(entries[row, c] / pivot);
661+
entries[row, column] = pivot = Number.Integer.One;
662+
}
663+
for (var r = reduced ? 0 : row + 1; r < rows; r++)
664+
{
665+
if (r == row || entries[r, column] is Number.Complex { IsZero: true })
666+
continue;
667+
var factor = Normalised(entries[r, column] / pivot);
668+
for (var c = column + 1; c < columns; c++)
669+
entries[r, c] = Normalised(entries[r, c] - factor * entries[row, c]);
670+
entries[r, column] = Number.Integer.Zero;
671+
}
672+
row++;
618673
}
619-
, this);
620-
private LazyPropertyA<int> rank;
674+
return (entries, row);
675+
}
676+
677+
/// <summary>
678+
/// An entry the elimination made, normalised: a number as it is, a rational function over the rationals as
679+
/// its canonical quotient, which keeps the entries from growing with each row operation, and anything else
680+
/// as its inner simplification.
681+
/// </summary>
682+
private static Entity Normalised(Entity entry)
683+
{
684+
var inner = entry.InnerSimplified;
685+
if (inner is Number)
686+
return inner;
687+
return Functions.RationalFunction.TryCanonicalize(inner, out var canonical, wholeCoefficients: true) ? canonical : inner;
688+
}
689+
690+
/// <summary>
691+
/// Whether an entry is 0: a number exactly, a rational function over the rationals by its canonical quotient,
692+
/// and anything else once simplified, a condition on the zero set aside, since an entry that is 0 wherever
693+
/// it has a value is no pivot to divide by.
694+
/// </summary>
695+
private static bool IsZeroEntry(Entity entry)
696+
{
697+
if (entry is Number.Complex number)
698+
return number.IsZero;
699+
if (Functions.RationalFunction.TryCanonicalize(entry, out var canonical))
700+
return canonical is Number.Complex { IsZero: true };
701+
var simplified = entry.Simplify();
702+
while (simplified is Providedf(var expression, _))
703+
simplified = expression;
704+
return simplified is Number.Complex { IsZero: true };
705+
}
621706

622707
/// <summary>
623708
/// Adjugate form of a matrix
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.Extensions;
9+
using Xunit;
10+
using static AngouriMath.Entity;
11+
12+
namespace AngouriMath.Tests.Algebra
13+
{
14+
/// <summary>
15+
/// The row echelon forms asked whether a pivot was the literal 0, and an entry the elimination had made, like
16+
/// <c>c + -c / a * a</c>, is 0 without being written so: it was chosen and divided by. The reduced form of
17+
/// <c>[[a, b], [c, d]]</c> simplified to NaN, and the rank of <c>[[a, b], [2 a, 2 b]]</c>, whose second row is
18+
/// twice the first, was 2. The rank of a symbolic matrix is the rank for general values of its symbols.
19+
/// https://github.com/asc-community/AngouriMath/issues/2345
20+
/// </summary>
21+
[Trait("Area", "Algebra")]
22+
public sealed class ASymbolicPivotIsDecidedBeforeItIsChosenTest
23+
{
24+
[Theory]
25+
[InlineData("[[a, b], [2a, 2b]]", 1)]
26+
[InlineData("[[a, b, c], [d, f, g], [a + d, b + f, c + g]]", 2)]
27+
[InlineData("[[a, b, c], [d, f, g], [2a - d, 2b - f, 2c - g]]", 2)]
28+
[InlineData("[[x, x^2], [1, x]]", 1)]
29+
[InlineData("[[sin(t)^2 + cos(t)^2, 1], [1, 1]]", 1)]
30+
// Independent, to the end.
31+
[InlineData("[[a, b], [c, d]]", 2)]
32+
[InlineData("[[x, 1, 0], [1, x, 1], [0, 1, x]]", 3)]
33+
public void HasItsRank(string matrix, int rank)
34+
=> Assert.Equal(rank, ((Matrix)matrix.ToEntity()).Rank);
35+
36+
[Theory]
37+
[InlineData("[[a, b], [c, d]]")]
38+
[InlineData("[[1, b], [c, d]]")]
39+
public void AnIndependentMatrixReducesToTheIdentity(string matrix)
40+
=> Assert.Equal(MathS.IdentityMatrix(2), ((Matrix)matrix.ToEntity()).ReducedRowEchelonForm.Simplify());
41+
42+
[Fact]
43+
public void ADependentRowReducesToZeros()
44+
{
45+
var reduced = (Matrix)((Matrix)"[[a, b], [2a, 2b]]".ToEntity()).ReducedRowEchelonForm.Simplify();
46+
Assert.Equal(Number.Integer.One, reduced[0, 0]);
47+
// b / a, which holds where a is not 0, and says so.
48+
var difference = (reduced[0, 1] - "b / a".ToEntity()).Simplify();
49+
while (difference is Providedf(var inner, _))
50+
difference = inner;
51+
Assert.Equal(Number.Integer.Zero, difference);
52+
Assert.Equal(Number.Integer.Zero, reduced[1, 0]);
53+
Assert.Equal(Number.Integer.Zero, reduced[1, 1]);
54+
}
55+
56+
[Theory]
57+
[InlineData("[[a, b], [c, d]]")]
58+
[InlineData("[[a, b], [2a, 2b]]")]
59+
[InlineData("[[a, b, c], [d, f, g], [a + d, b + f, c + g]]")]
60+
public void NoFormHasANaN(string matrix)
61+
{
62+
var m = (Matrix)matrix.ToEntity();
63+
Assert.DoesNotContain("NaN", m.RowEchelonForm.Simplify().Stringize());
64+
Assert.DoesNotContain("NaN", m.ReducedRowEchelonForm.Simplify().Stringize());
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)