@@ -868,28 +868,28 @@ public static Integer GreatestCommonDivisor(Integer a, Integer b)
868868
869869 /// <summary><a href="https://en.wikipedia.org/wiki/Gamma_function"/></summary>
870870 /// <param name="a">Argument node of which gamma function will be taken</param>
871- /// <returns>Factorial node with one added to the argument</returns>
871+ /// <returns>Factorial node with one subtracted from the argument</returns>
872872 /// <example>
873873 /// <code>
874874 /// using System;
875875 /// using static AngouriMath.MathS;
876876 /// var expr = Factorial(5);
877877 /// Console.WriteLine(expr);
878878 /// Console.WriteLine(expr.Evaled);
879- /// var expr2 = Gamma(4 );
879+ /// var expr2 = Gamma(6 );
880880 /// Console.WriteLine(expr2);
881881 /// Console.WriteLine(expr2.Evaled);
882882 /// </code>
883883 /// Prints
884884 /// <code>
885885 /// 5!
886886 /// 120
887- /// (4 + 1)!
887+ /// (6 - 1)!
888888 /// 120
889889 /// </code>
890890 /// </example>
891891 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) , NativeExport ]
892- public static Entity Gamma ( Entity a ) => new Factorialf ( a + 1 ) ;
892+ public static Entity Gamma ( Entity a ) => new Factorialf ( a - 1 ) ;
893893
894894 /// <summary>https://en.wikipedia.org/wiki/Sign_function</summary>
895895 /// <param name="a">Argument node of which Signum function will be taken</param>
@@ -1533,8 +1533,8 @@ public static class Hyperbolic
15331533 /// 1/2 * ln((1 + x) / (1 - x))
15341534 /// 10
15351535 /// ----------------------
1536- /// 1/2 * ln((x - 1) / (x + 1))
1537- /// - 10
1536+ /// 1/2 * ln((x + 1) / (x - 1))
1537+ /// 10
15381538 /// ----------------------
15391539 /// ln(1 / x + sqrt(1 / x ^ 2 - 1))
15401540 /// 10
@@ -1585,8 +1585,8 @@ public static class Hyperbolic
15851585 /// 1/2 * ln((1 + x) / (1 - x))
15861586 /// 10
15871587 /// ----------------------
1588- /// 1/2 * ln((x - 1) / (x + 1))
1589- /// - 10
1588+ /// 1/2 * ln((x + 1) / (x - 1))
1589+ /// 10
15901590 /// ----------------------
15911591 /// ln(1 / x + sqrt(1 / x ^ 2 - 1))
15921592 /// 10
@@ -1637,8 +1637,8 @@ public static class Hyperbolic
16371637 /// 1/2 * ln((1 + x) / (1 - x))
16381638 /// 10
16391639 /// ----------------------
1640- /// 1/2 * ln((x - 1) / (x + 1))
1641- /// - 10
1640+ /// 1/2 * ln((x + 1) / (x - 1))
1641+ /// 10
16421642 /// ----------------------
16431643 /// ln(1 / x + sqrt(1 / x ^ 2 - 1))
16441644 /// 10
@@ -1689,8 +1689,8 @@ public static class Hyperbolic
16891689 /// 1/2 * ln((1 + x) / (1 - x))
16901690 /// 10
16911691 /// ----------------------
1692- /// 1/2 * ln((x - 1) / (x + 1))
1693- /// - 10
1692+ /// 1/2 * ln((x + 1) / (x - 1))
1693+ /// 10
16941694 /// ----------------------
16951695 /// ln(1 / x + sqrt(1 / x ^ 2 - 1))
16961696 /// 10
@@ -1700,7 +1700,7 @@ public static class Hyperbolic
17001700 /// </code>
17011701 /// </example>
17021702 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) , NativeExport ]
1703- public static Entity Arcotanh ( Entity x ) => 0.5 * Ln ( ( x - 1 ) / ( x + 1 ) ) ;
1703+ public static Entity Arcotanh ( Entity x ) => 0.5 * Ln ( ( x + 1 ) / ( x - 1 ) ) ;
17041704
17051705 /// <summary>
17061706 /// Inverse hyperbolic secant:
@@ -1741,8 +1741,8 @@ public static class Hyperbolic
17411741 /// 1/2 * ln((1 + x) / (1 - x))
17421742 /// 10
17431743 /// ----------------------
1744- /// 1/2 * ln((x - 1) / (x + 1))
1745- /// - 10
1744+ /// 1/2 * ln((x + 1) / (x - 1))
1745+ /// 10
17461746 /// ----------------------
17471747 /// ln(1 / x + sqrt(1 / x ^ 2 - 1))
17481748 /// 10
@@ -1793,8 +1793,8 @@ public static class Hyperbolic
17931793 /// 1/2 * ln((1 + x) / (1 - x))
17941794 /// 10
17951795 /// ----------------------
1796- /// 1/2 * ln((x - 1) / (x + 1))
1797- /// - 10
1796+ /// 1/2 * ln((x + 1) / (x - 1))
1797+ /// 10
17981798 /// ----------------------
17991799 /// ln(1 / x + sqrt(1 / x ^ 2 - 1))
18001800 /// 10
@@ -5564,42 +5564,31 @@ public sealed record NewtonSetting
55645564 complexityCriteria ??= new Func < Entity , double > ( expr =>
55655565 {
55665566 // Those are of the 2nd power to avoid problems with floating numbers
5567- static double TinyWeight ( double w ) => w * 0.5 ;
5568- static double MinorWeight ( double w ) => w * 1.0 ;
5569- static double Weight ( double w ) => w * 2.0 ;
5570- static double MajorWeight ( double w ) => w * 4.0 ;
5571- static double HeavyWeight ( double w ) => w * 8.0 ;
5572- static double ExtraHeavyWeight ( double w ) => w * 12.0 ;
5573-
5574- // Number of nodes
5575- var res = Weight ( expr . Complexity ) ;
5576-
5577- // Number of variables
5578- res += Weight ( expr . Nodes . Count ( entity => entity is Variable ) ) ;
5579-
5580- // Number of divides
5581- res += MinorWeight ( expr . Nodes . Count ( entity => entity is Divf ) ) ;
5582-
5583- // Number of rationals with unit numerator
5584- res += Weight ( expr . Nodes . Count ( entity => entity is Rational rat and not Integer
5585- && ( rat . Numerator == 1 || rat . Numerator == - 1 ) ) ) ;
5586-
5587- // Number of negative powers
5588- res += HeavyWeight ( expr . Nodes . Count ( entity => entity is Powf ( _ , Real { IsNegative : true } ) ) ) ;
5589-
5590- // Number of logarithms
5591- res += TinyWeight ( expr . Nodes . Count ( entity => entity is Logf ) ) ;
5592-
5593- // Number of phi functions
5594- res += ExtraHeavyWeight ( expr . Nodes . Count ( entity => entity is Phif ) ) ;
5595-
5596- // Number of negative reals
5597- res += MajorWeight ( expr . Nodes . Count ( entity => entity is Real { IsNegative : true } ) ) ;
5598-
5599- // 0 < x is bad. x > 0 is good.
5600- res += Weight ( expr . Nodes . Count ( entity => entity is ComparisonSign && entity . DirectChildren [ 0 ] == 0 ) ) ;
5601-
5602- return res ;
5567+ const double TinyWeight = 0.5 ;
5568+ const double MinorWeight = 1.0 ;
5569+ const double Weight = 2.0 ;
5570+ const double MajorWeight = 4.0 ;
5571+ const double HeavyWeight = 8.0 ;
5572+ const double ExtraHeavyWeight = 12.0 ;
5573+
5574+ static double DefaultCriteria ( Entity expr ) => expr switch {
5575+ // Weigh provided predicates much less but nested provideds heavy
5576+ Providedf ( var inner , var predicate ) =>
5577+ DefaultCriteria ( inner ) + 0.1 * DefaultCriteria ( predicate ) + ExtraHeavyWeight * ( inner . Nodes . Count ( n => n is Providedf ) + predicate . Nodes . Count ( n => n is Providedf ) ) ,
5578+ Entity . Piecewise { Cases : var cases } =>
5579+ cases . Sum ( @case =>
5580+ DefaultCriteria ( @case . Expression ) + 0.1 * DefaultCriteria ( @case . Predicate ) + ExtraHeavyWeight * ( @case . Expression . Nodes . Count ( n => n is Providedf ) + @case . Predicate . Nodes . Count ( n => n is Providedf ) ) ) ,
5581+ Variable => Weight , // Number of variables
5582+ Divf => MinorWeight + expr . DirectChildren . Sum ( DefaultCriteria ) , // Number of divides
5583+ Rational ( Integer ( 1 or - 1 ) , _) and not Integer => Weight + expr . DirectChildren . Sum ( DefaultCriteria ) , // Number of rationals with unit numerator
5584+ Powf ( _ , Real { IsNegative : true } ) => HeavyWeight + expr . DirectChildren . Sum ( DefaultCriteria ) , // Number of negative powers
5585+ Logf => TinyWeight + expr . DirectChildren . Sum ( DefaultCriteria ) , // Number of logarithms
5586+ Phif => ExtraHeavyWeight + expr . DirectChildren . Sum ( DefaultCriteria ) , // Number of phi functions
5587+ Real { IsNegative : true } => MajorWeight + expr . DirectChildren . Sum ( DefaultCriteria ) , // Number of negative reals
5588+ ComparisonSign when expr . DirectChildren [ 0 ] == 0 => Weight + expr . DirectChildren . Sum ( DefaultCriteria ) , // 0 < x is bad. x > 0 is good.
5589+ _ => expr . DirectChildren . Sum ( DefaultCriteria )
5590+ } + Weight ; // Number of nodes
5591+ return DefaultCriteria ( expr ) ;
56035592 } ) ;
56045593 [ ThreadStatic ] private static Setting < Func < Entity , double > > ? complexityCriteria ;
56055594
0 commit comments