@@ -639,6 +639,11 @@ internal static Entity InLowestTermsOverTheSymbols(Entity constant)
639639 if ( expandedAbove == Integer . Zero || expandedAbove . Evaled is Complex { IsZero : true } || IsZeroAtPinnedSymbols ( expandedAbove ) )
640640 return Integer . Zero ;
641641 var expandedBelow = Bare ( below . Expand ( ) . InnerSimplified ) ;
642+ // A number is folded already, and stays a number.
643+ if ( ( expandedAbove . Vars . Any ( ) || expandedBelow . Vars . Any ( ) )
644+ && ( HoldsTheImaginaryUnit ( expandedAbove ) || HoldsTheImaginaryUnit ( expandedBelow ) )
645+ && InLowestTermsOverTheGaussianRationals ( expandedAbove , expandedBelow ) is { } gaussian )
646+ return gaussian ;
642647 // With room for the coefficient a fourth-order block leaves: the series of
643648 // `x^4 (A + B x)/(1 + x^2)` at `-a/b` had `(a^2 + b^2)^3` in common at the third
644649 // order and eleven hundred nodes, and left uncancelled it carried into every
@@ -654,6 +659,117 @@ internal static Entity InLowestTermsOverTheSymbols(Entity constant)
654659 return WithThePrimitiveDenominator ( expandedBelow == Integer . One ? expandedAbove : expandedAbove / expandedBelow ) ;
655660 }
656661
662+ /// <summary>Whether a number off the real line is among the nodes of <paramref name="expr"/>.</summary>
663+ private static bool HoldsTheImaginaryUnit ( Entity expr )
664+ => expr . Nodes . Any ( node => node is Complex number && number is not Real && ! number . ImaginaryPart . IsZero ) ;
665+
666+ /// <summary>
667+ /// <paramref name="above"/> over <paramref name="below"/>, with the imaginary unit among
668+ /// their coefficients, as <c>(P + i Q)/R</c> for polynomials <c>P</c>, <c>Q</c> and
669+ /// <c>R</c> over the rationals in the symbols, with no factor common to the three and
670+ /// <c>R</c> primitive; null where a side does not read as a polynomial so.
671+ /// </summary>
672+ /// <remarks>
673+ /// The polynomial gcd reads its coefficients in the rationals, and <c>i</c> is not one, so
674+ /// a constant with it in was left as the expansion gave it and nothing in it cancelled.
675+ /// Newton's iteration for the inverse modulo a power of a quadratic squares its iterate
676+ /// every round, and the split of <c>2 d^3 t^2/((t^2 - c - i d)^3 (t^2 - c + i d))</c>,
677+ /// which the root substitution makes of Rubi's 4.3.2.1
678+ /// <c>(c + d tan(x))^(5/2)/(a + i a tan(x))^2</c>, came out with <c>d^572</c> in its
679+ /// coefficients and an answer of 240,000 characters, which the default precision could
680+ /// not evaluate.
681+ /// https://github.com/asc-community/AngouriMath/issues/1788
682+ /// The unit is read as a symbol <c>j</c> with <c>j^2 = -1</c>, so each side is
683+ /// <c>A + j B</c> with <c>A</c> and <c>B</c> free of it; the denominator times its
684+ /// conjugate is real, <c>B0^2 + B1^2</c>, and what the three polynomials over the
685+ /// rationals then share is their gcd. With the denominator real and that gcd one the
686+ /// form is unique, so a constant that is zero comes out zero.
687+ /// </remarks>
688+ private static Entity ? InLowestTermsOverTheGaussianRationals ( Entity above , Entity below )
689+ {
690+ if ( ( long ) above . Complexity + below . Complexity > 4096 )
691+ return null ;
692+ var unit = Variable . CreateUnique ( above + below , "j" ) ;
693+ Entity WithTheUnitNamed ( Entity side )
694+ => side . Replace ( node => node is Complex number && number is not Real ? number . RealPart + number . ImaginaryPart * unit : node ) ;
695+ var namedAbove = WithTheUnitNamed ( above ) ;
696+ var namedBelow = WithTheUnitNamed ( below ) ;
697+ var variables = namedAbove . Vars . Concat ( namedBelow . Vars ) . Distinct ( ) . OrderBy ( v => v . Name , System . StringComparer . Ordinal ) . ToArray ( ) ;
698+ if ( variables . Length > MultivariatePolynomial . MaxVariables )
699+ return null ;
700+ var indices = new Dictionary < Variable , int > ( variables . Length ) ;
701+ for ( var i = 0 ; i < variables . Length ; i ++ )
702+ indices [ variables [ i ] ] = i ;
703+ if ( MultivariatePolynomial . TryParse ( namedAbove , indices ) is not { } top
704+ || MultivariatePolynomial . TryParse ( namedBelow , indices ) is not { } bottom || bottom . IsZero )
705+ return null ;
706+ var ( a0 , a1 ) = RealAndImaginaryParts ( top , indices [ unit ] ) ;
707+ var ( b0 , b1 ) = RealAndImaginaryParts ( bottom , indices [ unit ] ) ;
708+ MultivariatePolynomial p , q , r ;
709+ if ( b1 . IsZero )
710+ ( p , q , r ) = ( a0 , a1 , b0 ) ;
711+ else if ( a0 . Multiply ( b0 ) is { } a0b0 && a1 . Multiply ( b1 ) is { } a1b1
712+ && a1 . Multiply ( b0 ) is { } a1b0 && a0 . Multiply ( b1 ) is { } a0b1
713+ && b0 . Multiply ( b0 ) is { } b0b0 && b1 . Multiply ( b1 ) is { } b1b1 )
714+ ( p , q , r ) = ( a0b0 . Add ( a1b1 ) , a1b0 . Subtract ( a0b1 ) , b0b0 . Add ( b1b1 ) ) ;
715+ else
716+ return null ;
717+ if ( r . IsZero )
718+ return null ;
719+ var order = new int [ variables . Length ] ;
720+ for ( var i = 0 ; i < order . Length ; i ++ )
721+ order [ i ] = i ;
722+ if ( PolynomialGcd . Gcd ( p , q , order , 0 ) is not { } shared || PolynomialGcd . Gcd ( shared , r , order , 0 ) is not { } common )
723+ return null ;
724+ if ( ! common . IsConstant )
725+ {
726+ // Multiplied back, as the gcd's own cancellation is: nothing is divided out that
727+ // has not been seen to divide.
728+ if ( p . DivideExact ( common ) is not { } reducedP || q . DivideExact ( common ) is not { } reducedQ
729+ || r . DivideExact ( common ) is not { } reducedR
730+ || reducedP . Multiply ( common ) is not { } backP || ! backP . SameAs ( p )
731+ || reducedQ . Multiply ( common ) is not { } backQ || ! backQ . SameAs ( q )
732+ || reducedR . Multiply ( common ) is not { } backR || ! backR . SameAs ( r ) )
733+ return null ;
734+ ( p , q , r ) = ( reducedP , reducedQ , reducedR ) ;
735+ }
736+ var primitive = r . Normalized ( out var scale ) ;
737+ p = p . ScaleBy ( scale ) ;
738+ q = q . ScaleBy ( scale ) ;
739+ // `c - i d` rather than `c + i (-d)`: the imaginary part goes in with its sign taken
740+ // out where its first term, as written, is negative.
741+ var negative = ! q . IsZero && q . Terms . OrderByDescending ( term => term . Key ) . First ( ) . Value . Sign < 0 ;
742+ Entity numerator ;
743+ if ( q . IsZero )
744+ numerator = p . ToEntity ( variables ) ;
745+ else
746+ {
747+ var imaginary = MathS . i * ( negative ? q . ScaleBy ( ERational . FromInt32 ( - 1 ) ) : q ) . ToEntity ( variables ) ;
748+ numerator = p . IsZero ? ( negative ? - imaginary : imaginary ) : negative ? p . ToEntity ( variables ) - imaginary : p . ToEntity ( variables ) + imaginary ;
749+ }
750+ return primitive . IsConstant ? numerator : numerator / primitive . ToEntity ( variables ) ;
751+ }
752+
753+ /// <summary>
754+ /// <paramref name="polynomial"/>, in which <paramref name="unit"/> stands for the
755+ /// imaginary unit, as <c>A + j B</c> with <c>j^2 = -1</c> and both free of it.
756+ /// </summary>
757+ private static ( MultivariatePolynomial Real , MultivariatePolynomial Imaginary ) RealAndImaginaryParts ( MultivariatePolynomial polynomial , int unit )
758+ {
759+ var real = MultivariatePolynomial . Zero ( polynomial . VariableCount ) ;
760+ var imaginary = MultivariatePolynomial . Zero ( polynomial . VariableCount ) ;
761+ foreach ( var pair in polynomial . CoefficientsIn ( unit ) )
762+ {
763+ // j^k is 1, j, -1, -j as k is 0, 1, 2, 3 modulo 4.
764+ var term = pair . Key % 4 >= 2 ? pair . Value . ScaleBy ( ERational . FromInt32 ( - 1 ) ) : pair . Value ;
765+ if ( pair . Key % 2 == 0 )
766+ real = real . Add ( term ) ;
767+ else
768+ imaginary = imaginary . Add ( term ) ;
769+ }
770+ return ( real , imaginary ) ;
771+ }
772+
657773 /// <summary>
658774 /// Whether a constant in the symbols is zero as a value, decided as
659775 /// <see cref="InLowestTermsOverTheSymbols"/> decides it: over one bar, its numerator
0 commit comments