@@ -1494,29 +1494,34 @@ private static bool AlreadyBeingDifferentiated(Entity quotient)
14941494 {
14951495 Entity value = Integer . One ;
14961496 Entity order = Integer . Zero ;
1497- if ( ! Factors ( term , Integer . One , ref value , ref order ) )
1497+ var logs = 0 ;
1498+ if ( ! Factors ( term , Integer . One , ref value , ref order , ref logs ) )
14981499 return null ;
14991500 if ( order . Evaled is not Real { IsNaN : false , IsFinite : true } orderValue )
15001501 return null ;
15011502 if ( value . Evaled is not Real { IsNaN : false , IsFinite : true , IsZero : false } coefficient )
15021503 return null ;
15031504 if ( negated )
15041505 coefficient = - coefficient ;
1505- if ( orderValue . IsPositive )
1506+ // A power of the logarithm of the distance changes nothing beside a power of the distance itself,
1507+ // which outweighs it, but alone it is a pole if it is above and vanishes if it is below. Only from the
1508+ // right, where the logarithm is real, and negative, which is the sign each of its powers carries.
1509+ var signOfLogs = logs % 2 == 0 ? 1 : - 1 ;
1510+ if ( orderValue . IsPositive || orderValue . IsZero && logs < 0 )
15061511 continue ;
1507- if ( orderValue . IsZero )
1512+ if ( orderValue . IsZero && logs == 0 )
15081513 {
15091514 finite += negated ? - value : value ;
15101515 continue ;
15111516 }
15121517 // (x - a)^k for k < 0 is positive to the right of a, and to the left (-1)^k where k is an integer and
15131518 // not a real number at all where it is not.
1514- var signOfPower = side is ApproachFrom . Right ? 1
1519+ var signOfPower = side is ApproachFrom . Right || orderValue . IsZero ? 1
15151520 : orderValue is Integer { EInteger : var k } ? ( k . IsEven ? 1 : - 1 )
15161521 : 0 ;
15171522 if ( signOfPower == 0 )
15181523 return null ;
1519- var sign = signOfPower * ( coefficient . IsPositive ? 1 : - 1 ) ;
1524+ var sign = signOfPower * signOfLogs * ( coefficient . IsPositive ? 1 : - 1 ) ;
15201525 if ( poles != 0 && poles != sign )
15211526 return null ;
15221527 poles = sign ;
@@ -1538,16 +1543,22 @@ private static bool AlreadyBeingDifferentiated(Entity quotient)
15381543 _ => new [ ] { ( expr , negated ) }
15391544 } ;
15401545
1541- // The factors of a product or a quotient, with the powers of the distance to the point added up in order
1542- // and the rest multiplied into value at the point; false where a factor is neither.
1543- bool Factors ( Entity expr , Entity power , ref Entity value , ref Entity order )
1546+ // The factors of a product or a quotient, with the powers of the distance to the point added up in order,
1547+ // and those of its logarithm in logs, and the rest multiplied into value at the point; false where a
1548+ // factor is none of these.
1549+ bool Factors ( Entity expr , Entity power , ref Entity value , ref Entity order , ref int logs )
15441550 {
15451551 switch ( expr )
15461552 {
15471553 case Mulf ( var left , var right ) :
1548- return Factors ( left , power , ref value , ref order ) && Factors ( right , power , ref value , ref order ) ;
1554+ return Factors ( left , power , ref value , ref order , ref logs ) && Factors ( right , power , ref value , ref order , ref logs ) ;
15491555 case Divf ( var left , var right ) :
1550- return Factors ( left , power , ref value , ref order ) && Factors ( right , - power , ref value , ref order ) ;
1556+ return Factors ( left , power , ref value , ref order , ref logs ) && Factors ( right , - power , ref value , ref order , ref logs ) ;
1557+ case Logf ( var logBase , var argument ) when argument == distance && logBase == MathS . e :
1558+ if ( side is not ApproachFrom . Right || power . Evaled is not Integer { EInteger : var logPower } || ! logPower . CanFitInInt32 ( ) )
1559+ return false ;
1560+ logs += logPower . ToInt32Unchecked ( ) ;
1561+ return true ;
15511562 case Powf ( var @base , var exponent ) when ! exponent . ContainsNode ( x ) && @base == distance :
15521563 order += power * exponent ;
15531564 return true ;
@@ -1562,10 +1573,16 @@ bool Factors(Entity expr, Entity power, ref Entity value, ref Entity order)
15621573 return true ;
15631574 // A whole power is its base's order that many times, where c * d^k to the n is c^n * d^(kn) exactly.
15641575 case Powf ( var @base , Integer times ) :
1565- return Factors ( @base , power * times , ref value , ref order ) ;
1576+ return Factors ( @base , power * times , ref value , ref order , ref logs ) ;
15661577 default :
1567- if ( LeadingTerm ( expr ) is not var ( leadingOrder , leadingValue ) )
1578+ if ( LeadingTerm ( expr ) is not var ( leadingOrder , leadingLogs , leadingValue ) )
15681579 return false ;
1580+ if ( leadingLogs != 0 )
1581+ {
1582+ if ( power . Evaled is not Integer { EInteger : var leadingPower } || ! leadingPower . CanFitInInt32 ( ) )
1583+ return false ;
1584+ logs += leadingLogs * leadingPower . ToInt32Unchecked ( ) ;
1585+ }
15691586 order += power * leadingOrder ;
15701587 value *= MathS . Pow ( leadingValue , power ) ;
15711588 return true ;
@@ -1579,39 +1596,55 @@ bool Factors(Entity expr, Entity power, ref Entity value, ref Entity order)
15791596 // -1/2 * (x^(-1/2) * cos(x) / 2 - sqrt(x) * sin(x)) * (e^(2x) - 1)^2 / (sqrt(x) * cos(x) * e^(2x)), whose
15801597 // sum is of order -1/2 and whose e^(2x) - 1 is 2x to the first, so it is of order 1 and tends to 0. A
15811598 // derivative read at the point is the Taylor coefficient only where the function is smooth there, so not
1582- // through an abs, and a value that is not a finite real at the point stops the reading.
1583- ( Entity Order , Entity Value ) ? LeadingTerm ( Entity expr )
1599+ // through an abs, and a value that is not a finite real at the point stops the reading. Of two terms of
1600+ // the same order the one with more powers of the logarithm leads: (1 - cos(x)) * ln(x) + (x - sin(x)) / x
1601+ // is x^2 * ln(x) / 2 to leading order, and (x - sin(x)) * ln(x) / x^3 at 0 from the right steps to it
1602+ // over 3x^2, which is ln(x) / 6 and tends to -oo.
1603+ ( Entity Order , int Logs , Entity Value ) ? LeadingTerm ( Entity expr )
15841604 {
15851605 if ( ItsValueAt ( expr , x , dest ) is { } atThePoint && atThePoint . Evaled is Real { IsZero : true } )
15861606 return ByItsDerivatives ( expr ) ;
15871607 if ( expr is Sumf or Minusf )
15881608 {
15891609 Real ? lowest = null ;
1590- var parts = new List < ( Real Order , Entity Value ) > ( ) ;
1610+ var mostLogs = 0 ;
1611+ var parts = new List < ( Real Order , int Logs , Entity Value ) > ( ) ;
15911612 foreach ( var ( term , negated ) in Terms ( expr , false ) )
15921613 {
15931614 Entity termValue = Integer . One , termOrder = Integer . Zero ;
1594- if ( ! Factors ( term , Integer . One , ref termValue , ref termOrder )
1615+ var termLogs = 0 ;
1616+ if ( ! Factors ( term , Integer . One , ref termValue , ref termOrder , ref termLogs )
15951617 || termOrder . Evaled is not Real { IsNaN : false , IsFinite : true } termOrderValue )
15961618 return null ;
1597- parts . Add ( ( termOrderValue , negated ? - termValue : termValue ) ) ;
1598- if ( lowest is null || termOrderValue < lowest )
1599- lowest = termOrderValue ;
1619+ parts . Add ( ( termOrderValue , termLogs , negated ? - termValue : termValue ) ) ;
1620+ if ( lowest is null || termOrderValue < lowest || termOrderValue == lowest && termLogs > mostLogs )
1621+ ( lowest , mostLogs ) = ( termOrderValue , termLogs ) ;
16001622 }
16011623 if ( lowest is null )
16021624 return null ;
16031625 Entity coefficient = Integer . Zero ;
16041626 foreach ( var part in parts )
1605- if ( part . Order == lowest )
1627+ if ( part . Order == lowest && part . Logs == mostLogs )
16061628 coefficient += part . Value ;
16071629 return coefficient . Evaled is Real { IsNaN : false , IsFinite : true , IsZero : false }
1608- ? ( lowest , coefficient . InnerSimplified ) : null ;
1630+ ? ( lowest , mostLogs , coefficient . InnerSimplified ) : null ;
1631+ }
1632+ // The logarithm of c * d^k, c > 0 and k not 0, is k * ln(d) + ln(c) and its error, which ln(d) outweighs:
1633+ // ln(cos(x) * sqrt(x)) is ln(x) / 2 to leading order.
1634+ if ( expr is Logf ( var logBase , var argument ) && logBase == MathS . e && side is ApproachFrom . Right )
1635+ {
1636+ Entity argumentValue = Integer . One , argumentOrder = Integer . Zero ;
1637+ var argumentLogs = 0 ;
1638+ if ( Factors ( argument , Integer . One , ref argumentValue , ref argumentOrder , ref argumentLogs ) && argumentLogs == 0
1639+ && argumentValue . Evaled is Real { IsNaN : false , IsFinite : true , IsPositive : true }
1640+ && argumentOrder . Evaled is Real { IsNaN : false , IsFinite : true , IsZero : false } k )
1641+ return ( Integer . Zero , 1 , k ) ;
16091642 }
16101643 return null ;
16111644 }
16121645
16131646 // The first derivative that is not 0 at the point, over its factorial, and its order.
1614- ( Entity Order , Entity Value ) ? ByItsDerivatives ( Entity expr )
1647+ ( Entity Order , int Logs , Entity Value ) ? ByItsDerivatives ( Entity expr )
16151648 {
16161649 if ( expr . Nodes . Any ( static node => node is Absf ) )
16171650 return null ;
@@ -1624,7 +1657,7 @@ bool Factors(Entity expr, Entity power, ref Entity value, ref Entity order)
16241657 if ( ItsValueAt ( derivative , x , dest ) is not { } derivativeThere || derivativeThere . Evaled is not Real { IsNaN : false , IsFinite : true } read )
16251658 return null ;
16261659 if ( ! read . IsZero )
1627- return ( Integer . Create ( k ) , ( derivativeThere / factorial ) . InnerSimplified ) ;
1660+ return ( Integer . Create ( k ) , 0 , ( derivativeThere / factorial ) . InnerSimplified ) ;
16281661 }
16291662 return null ;
16301663 }
0 commit comments