@@ -30,8 +30,8 @@ namespace AngouriMath.Functions
3030 /// <a href="https://github.com/asc-community/AngouriMath/issues/934">#934</a>.
3131 /// </para>
3232 /// <para>
33- /// Four steps, and only the first is new . The expression is gathered into a single
34- /// quotient — nothing else in the library does that, and without it <c>1/x + 1/y</c> and
33+ /// Four steps. The expression is written as a single fraction, by the step
34+ /// <see cref="Entity.AsSingleFraction"/> takes -- without it <c>1/x + 1/y</c> and
3535 /// <c>(x + y)/(x*y)</c> could never meet. Then the numerator and denominator are divided
3636 /// by their multivariate greatest common divisor, which
3737 /// <see cref="PolynomialGcd"/> computes and verifies. Then both are scaled so that the
@@ -40,12 +40,14 @@ namespace AngouriMath.Functions
4040 /// </para>
4141 /// <para>
4242 /// <b>The domain is preserved rather than assumed away.</b> Cancelling a common factor
43- /// widens the domain — <c>x/x</c> is not <c>1</c> — so where a factor of positive degree
44- /// is cancelled the answer carries the condition that it is nonzero, which is what the
45- /// library already does elsewhere and what keeps "equal trees means equal expressions"
46- /// true rather than nearly true. Gathering over a common denominator does not widen
47- /// anything: a sum is defined exactly where its terms are, and the product of the
48- /// denominators vanishes exactly where one of them does.
43+ /// widens the domain — <c>x/x</c> is not <c>1</c> — and so does dividing by a quotient,
44+ /// which moves its denominator into the numerator: <c>1/(1/x)</c> is not <c>x</c>. So the
45+ /// answer carries the condition that neither vanishes, which is what the library already
46+ /// does elsewhere and what keeps "equal trees means equal expressions" true rather than
47+ /// nearly true; the condition is written canonically too, square-free and without what
48+ /// the denominator already excludes. A sum is defined exactly where its terms are, and the
49+ /// common denominator vanishes exactly where one of theirs does, so gathering one widens
50+ /// nothing. <a href="https://github.com/asc-community/AngouriMath/issues/1618">#1618</a>
4951 /// </para>
5052 /// </remarks>
5153 internal static class RationalFunction
@@ -57,9 +59,10 @@ internal static class RationalFunction
5759 private const int MaxComplexity = 256 ;
5860
5961 /// <summary>
60- /// Raising to a power is where a gathered quotient explodes, so the exponent is
61- /// bounded before <see cref="MultivariatePolynomial.Power"/> is asked; that has its
62- /// own bound on the number of terms, which catches the rest.
62+ /// Raising a quotient to a power is where a gathered quotient explodes, so a power of
63+ /// anything but a polynomial is bounded before anything is multiplied out; a polynomial's
64+ /// power has <see cref="MultivariatePolynomial.Power"/>'s own bound on the number of
65+ /// terms.
6366 /// </summary>
6467 private const int MaxExponent = 32 ;
6568
@@ -84,23 +87,24 @@ internal static bool TryCanonicalize(Entity expr, [NotNullWhen(true)] out Entity
8487 indices [ variables [ i ] ] = i ;
8588 var variableCount = variables . Length ;
8689
87- if ( ! TryGather ( expr , indices , variableCount , out var numerator , out var denominator ) )
90+ if ( ! TryGather ( expr , indices , variableCount , out var numerator , out var denominator , out var excluded ) )
8891 return false ;
89- // A vanishing denominator is not a rational function, and a vanishing numerator
90- // is zero however it was written .
92+ // A vanishing denominator is not a rational function. A vanishing numerator is zero
93+ // wherever the expression has a value: everywhere its denominator does not vanish .
9194 if ( denominator . IsZero )
9295 return false ;
96+ var order = new int [ variableCount ] ;
97+ for ( var i = 0 ; i < order . Length ; i ++ )
98+ order [ i ] = i ;
9399 if ( numerator . IsZero )
94100 {
95- canonical = Integer . Create ( 0 ) ;
101+ if ( excluded . Multiply ( denominator ) is not { } wholeDenominator
102+ || Conditioned ( Integer . Create ( 0 ) , wholeDenominator , MultivariatePolynomial . One ( variableCount ) , order , variables ) is not { } zero )
103+ return false ;
104+ canonical = zero ;
96105 return true ;
97106 }
98107
99- var order = new int [ variableCount ] ;
100- for ( var i = 0 ; i < order . Length ; i ++ )
101- order [ i ] = i ;
102-
103- var cancelled = MultivariatePolynomial . One ( variableCount ) ;
104108 if ( variableCount > 0
105109 && PolynomialGcd . Gcd ( numerator , denominator , order , 0 ) is { } divisor
106110 && ! divisor . IsConstant )
@@ -115,9 +119,11 @@ internal static bool TryCanonicalize(Entity expr, [NotNullWhen(true)] out Entity
115119 || reducedBottom . Multiply ( divisor ) is not { } checkedBottom
116120 || ! checkedBottom . SameAs ( denominator ) )
117121 return false ;
122+ if ( excluded . Multiply ( divisor ) is not { } withTheCancelled )
123+ return false ;
118124 numerator = reducedTop ;
119125 denominator = reducedBottom ;
120- cancelled = divisor ;
126+ excluded = withTheCancelled ;
121127 }
122128
123129 // Scaled so the denominator leads with one, under the same lexicographic monomial
@@ -138,125 +144,86 @@ internal static bool TryCanonicalize(Entity expr, [NotNullWhen(true)] out Entity
138144 var quotient = denominator . IsConstant
139145 ? numerator . ToEntity ( variables )
140146 : numerator . ToEntity ( variables ) / denominator . ToEntity ( variables ) ;
141-
142- canonical = cancelled . IsConstant
143- ? quotient
144- : new Providedf ( quotient , ! cancelled . ToEntity ( variables ) . EqualTo ( 0 ) ) ;
145- return true ;
147+ canonical = Conditioned ( quotient , excluded , denominator , order , variables ) ;
148+ return canonical is not null ;
146149 }
147150
148151 /// <summary>
149- /// <paramref name="expr"/> as a single quotient of polynomials, gathering a sum of
150- /// quotients over a common denominator. The denominator is never zero and never
151- /// simplified away; it is <c>1</c> for a polynomial.
152+ /// <paramref name="quotient"/> with the condition that it is not at a zero of
153+ /// <paramref name="excluded"/>, where the expression it came from had no value, except
154+ /// where <paramref name="denominator"/> vanishing already says so; <see langword="null"/>
155+ /// where a step of the arithmetic declined.
152156 /// </summary>
153157 /// <remarks>
154- /// The common denominator is the product rather than the least common multiple. Both
155- /// are correct and the product is cheaper to build; what it costs is a larger
156- /// intermediate, which the greatest common divisor then removes — so the answer is the
157- /// same and only the work in between differs.
158+ /// The condition is part of the form, so it is canonical too: two expressions with the same
159+ /// value and the same points where they have none have to meet. So the polynomial written
160+ /// is the square-free part of <paramref name="excluded"/>, which vanishes exactly where it
161+ /// does -- <c>x^2/x^2</c> and <c>x/x</c> are both <c>1 provided not x = 0</c> -- without the
162+ /// factors it shares with the denominator, and with whole coprime coefficients and a
163+ /// positive leading one. The square-free part is <c>E / gcd(E, dE/dx_1, …, dE/dx_n)</c>:
164+ /// over the rationals a factor to the power <c>k</c> divides each derivative to the power
165+ /// <c>k - 1</c>, and one of them no further.
158166 /// </remarks>
159- private static bool TryGather (
160- Entity expr , IReadOnlyDictionary < Variable , int > indices , int variableCount ,
161- [ NotNullWhen ( true ) ] out MultivariatePolynomial ? numerator ,
162- [ NotNullWhen ( true ) ] out MultivariatePolynomial ? denominator )
167+ private static Entity ? Conditioned ( Entity quotient , MultivariatePolynomial excluded , MultivariatePolynomial denominator ,
168+ IReadOnlyList < int > order , IReadOnlyList < Variable > variables )
163169 {
164- numerator = denominator = null ;
165-
166- // A polynomial is its own numerator, and this is the common case, so it is tried
167- // before the expression is taken apart.
168- if ( MultivariatePolynomial . TryParse ( expr , indices ) is { } whole )
169- {
170- numerator = whole ;
171- denominator = MultivariatePolynomial . One ( variableCount ) ;
172- return true ;
173- }
174-
175- switch ( expr )
176- {
177- case Sumf ( var left , var right ) :
178- return TryCombine ( left , right , subtract : false , indices , variableCount ,
179- out numerator , out denominator ) ;
180-
181- case Minusf ( var left , var right ) :
182- return TryCombine ( left , right , subtract : true , indices , variableCount ,
183- out numerator , out denominator ) ;
184-
185- case Mulf ( var left , var right ) :
186- {
187- if ( ! TryGather ( left , indices , variableCount , out var leftTop , out var leftBottom )
188- || ! TryGather ( right , indices , variableCount , out var rightTop , out var rightBottom ) )
189- return false ;
190- if ( leftTop . Multiply ( rightTop ) is not { } top
191- || leftBottom . Multiply ( rightBottom ) is not { } bottom )
192- return false ;
193- numerator = top ;
194- denominator = bottom ;
195- return true ;
196- }
197-
198- case Divf ( var left , var right ) :
170+ if ( excluded . IsConstant )
171+ return excluded . IsZero ? null : quotient ;
172+ var repeated = excluded ;
173+ for ( var i = 0 ; i < excluded . VariableCount ; i ++ )
174+ if ( excluded . DegreeIn ( i ) > 0 )
199175 {
200- if ( ! TryGather ( left , indices , variableCount , out var leftTop , out var leftBottom )
201- || ! TryGather ( right , indices , variableCount , out var rightTop , out var rightBottom ) )
202- return false ;
203- // Dividing by a quotient that is identically zero is not a rational
204- // function, and inverting it here would quietly produce one.
205- if ( rightTop . IsZero )
206- return false ;
207- if ( leftTop . Multiply ( rightBottom ) is not { } top
208- || leftBottom . Multiply ( rightTop ) is not { } bottom )
209- return false ;
210- numerator = top ;
211- denominator = bottom ;
212- return true ;
176+ if ( PolynomialGcd . Gcd ( repeated , excluded . DerivativeIn ( i ) , order , 0 ) is not { } common )
177+ return null ;
178+ repeated = common ;
213179 }
214-
215- case Powf ( var @base , Integer power ) :
216- {
217- var exponent = power . EInteger ;
218- if ( exponent . Abs ( ) . CompareTo ( EInteger . FromInt32 ( MaxExponent ) ) > 0 )
219- return false ;
220- if ( ! TryGather ( @base , indices , variableCount , out var baseTop , out var baseBottom ) )
221- return false ;
222- var magnitude = exponent . Abs ( ) . ToInt32Checked ( ) ;
223- var negative = exponent . Sign < 0 ;
224- if ( negative && baseTop . IsZero )
225- return false ;
226- var top = negative ? baseBottom : baseTop ;
227- var bottom = negative ? baseTop : baseBottom ;
228- if ( top . Power ( magnitude ) is not { } raisedTop
229- || bottom . Power ( magnitude ) is not { } raisedBottom )
230- return false ;
231- numerator = raisedTop ;
232- denominator = raisedBottom ;
233- return true ;
234- }
235-
236- default :
237- return false ;
238- }
180+ if ( excluded . DivideExact ( repeated ) is not { } squareFree
181+ || PolynomialGcd . Gcd ( squareFree , denominator , order , 0 ) is not { } shared
182+ || squareFree . DivideExact ( shared ) is not { } left )
183+ return null ;
184+ return left . IsConstant
185+ ? quotient
186+ : new Providedf ( quotient , ! left . Normalized ( ) . ToEntity ( variables ) . EqualTo ( 0 ) ) ;
239187 }
240188
241189 /// <summary>
242- /// A sum or a difference of two quotients, over the product of their denominators.
190+ /// <paramref name="expr"/> as a single quotient of polynomials, and the polynomial whose
191+ /// zeros are the other points where <paramref name="expr"/> has no value: the product of
192+ /// the denominators that dividing by a quotient turned over. The denominator is never
193+ /// zero and never simplified away; it is <c>1</c> for a polynomial.
243194 /// </summary>
244- private static bool TryCombine (
245- Entity left , Entity right , bool subtract ,
246- IReadOnlyDictionary < Variable , int > indices , int variableCount ,
195+ /// <remarks>
196+ /// Gathered by <see cref="SingleQuotient.OverLeastCommonDenominator"/>, the step
197+ /// <see cref="Entity.AsSingleFraction"/> takes, so that the two agree on what a single
198+ /// quotient of an expression is and on where it has a value; this form is that quotient
199+ /// reduced and normalised.
200+ /// </remarks>
201+ private static bool TryGather (
202+ Entity expr , IReadOnlyDictionary < Variable , int > indices , int variableCount ,
247203 [ NotNullWhen ( true ) ] out MultivariatePolynomial ? numerator ,
248- [ NotNullWhen ( true ) ] out MultivariatePolynomial ? denominator )
204+ [ NotNullWhen ( true ) ] out MultivariatePolynomial ? denominator ,
205+ [ NotNullWhen ( true ) ] out MultivariatePolynomial ? excluded )
249206 {
250- numerator = denominator = null ;
251- if ( ! TryGather ( left , indices , variableCount , out var leftTop , out var leftBottom )
252- || ! TryGather ( right , indices , variableCount , out var rightTop , out var rightBottom ) )
253- return false ;
254- if ( leftTop . Multiply ( rightBottom ) is not { } crossLeft
255- || rightTop . Multiply ( leftBottom ) is not { } crossRight
256- || leftBottom . Multiply ( rightBottom ) is not { } bottom )
207+ numerator = denominator = excluded = null ;
208+ foreach ( var node in expr . Nodes )
209+ if ( node is Powf ( var @base , Integer power )
210+ && power . EInteger . Abs ( ) . CompareTo ( EInteger . FromInt32 ( MaxExponent ) ) > 0
211+ && MultivariatePolynomial . TryParse ( @base , indices ) is null )
212+ return false ;
213+ var carried = new List < Entity > ( ) ;
214+ var ( top , bottom ) = SingleQuotient . OverLeastCommonDenominator ( expr , carried ) ;
215+ if ( MultivariatePolynomial . TryParse ( top , indices ) is not { } parsedTop
216+ || MultivariatePolynomial . TryParse ( bottom , indices ) is not { } parsedBottom )
257217 return false ;
258- numerator = subtract ? crossLeft . Subtract ( crossRight ) : crossLeft . Add ( crossRight ) ;
259- denominator = bottom ;
218+ var product = MultivariatePolynomial . One ( variableCount ) ;
219+ foreach ( var turnedOver in carried )
220+ {
221+ if ( MultivariatePolynomial . TryParse ( turnedOver , indices ) is not { } parsed
222+ || product . Multiply ( parsed ) is not { } next )
223+ return false ;
224+ product = next ;
225+ }
226+ ( numerator , denominator , excluded ) = ( parsedTop , parsedBottom , product ) ;
260227 return true ;
261228 }
262229 }
0 commit comments