@@ -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
0 commit comments