diff --git a/Sources/.editorconfig b/Sources/.editorconfig index b9d119287..92f736e7c 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -270,6 +270,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Algebra/SolveTest/SpuriousSystemRowTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n +[Tests/UnitTests/Convenience/DocCommentPlacementTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + [Tests/UnitTests/Common/LambdaArrowSyntaxTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index 732f7adf6..171d367bf 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -6036,20 +6036,6 @@ public sealed record NewtonSetting /// public static Setting PrecisionErrorZeroRange { get; } = EDecimal.Create(1, -16); - /// - /// The tolerance within which downcasting rounds a number onto a nearby integer. - /// - /// - /// This is whenever a caller has set it, - /// but its default of 1e-16 is far too coarse to be applied blindly: numbers are - /// evaluated to digits, 100 by default, so - /// 1e-16 sits nowhere near the noise floor. Applying it destroyed every legitimate - /// small value -- 1e-20 parsed to zero and e^(-40) evaluated to zero. - /// Residuals genuinely left over by exact cancellation, such as the one from - /// sin(pi), land around 1e-99, so half the working digits separates the two - /// cases with room to spare. Never looser than the setting's own default, so that - /// lowering the precision cannot silently widen the tolerance. - /// /// /// as a double, for the cheap check in front of /// the exact one: the conversion of the decimal costs more than the check. @@ -6067,6 +6053,20 @@ internal static double DowncastingToleranceAsDouble } } + /// + /// The tolerance within which downcasting rounds a number onto a nearby integer. + /// + /// + /// This is whenever a caller has set it, + /// but its default of 1e-16 is far too coarse to be applied blindly: numbers are + /// evaluated to digits, 100 by default, so + /// 1e-16 sits nowhere near the noise floor. Applying it destroyed every legitimate + /// small value -- 1e-20 parsed to zero and e^(-40) evaluated to zero. + /// Residuals genuinely left over by exact cancellation, such as the one from + /// sin(pi), land around 1e-99, so half the working digits separates the two + /// cases with room to spare. Never looser than the setting's own default, so that + /// lowering the precision cannot silently widen the tolerance. + /// internal static EDecimal DowncastingTolerance { get diff --git a/Sources/AngouriMath/Core/Transformations/Matching/MatchPattern.cs b/Sources/AngouriMath/Core/Transformations/Matching/MatchPattern.cs index a82524217..8dac59d37 100644 --- a/Sources/AngouriMath/Core/Transformations/Matching/MatchPattern.cs +++ b/Sources/AngouriMath/Core/Transformations/Matching/MatchPattern.cs @@ -1382,10 +1382,6 @@ private Entity Leftover(List operands, bool[] used) return rest ?? (OverSum ? Entity.Number.Integer.Zero : Entity.Number.Integer.One); } - /// - /// Shared across one call so the bound is on the whole search and - /// not on each branch of it, which is the difference between a bound and a suggestion. - /// /// Choosing which operands the parts claim is the whole of what it does. internal override bool IsDeterministic => false; @@ -1439,6 +1435,10 @@ private Entity Combine(Entity left, Entity right) private bool IsIdentity(Entity operand) => operand.Equals(OverSum ? Entity.Number.Integer.Zero : Entity.Number.Integer.One); + /// + /// Shared across one call so the bound is on the whole search and + /// not on each branch of it, which is the difference between a bound and a suggestion. + /// private sealed class Budget { private int left; diff --git a/Sources/AngouriMath/Core/Transformations/Matching/MatchedRule.cs b/Sources/AngouriMath/Core/Transformations/Matching/MatchedRule.cs index 4430dcd0c..d12360c5c 100644 --- a/Sources/AngouriMath/Core/Transformations/Matching/MatchedRule.cs +++ b/Sources/AngouriMath/Core/Transformations/Matching/MatchedRule.cs @@ -632,7 +632,6 @@ internal Soundness Soundness /// internal IReadOnlyList RulesByPriority => byPriority; - /// One rewrite at this node only, leaving children alone. /// /// These rules as values, so that a set written as data can /// appear in the registry beside the sets written as a switch. @@ -695,6 +694,7 @@ internal IReadOnlyList AsAddressable() return built; } + /// One rewrite at this node only, leaving children alone. internal Entity ApplyHere(Entity expr) { // The rules this node's type can possibly match, rather than all of them. See the diff --git a/Sources/AngouriMath/Core/Transformations/RewriteRules.cs b/Sources/AngouriMath/Core/Transformations/RewriteRules.cs index ba8a32618..b9d585fc2 100644 --- a/Sources/AngouriMath/Core/Transformations/RewriteRules.cs +++ b/Sources/AngouriMath/Core/Transformations/RewriteRules.cs @@ -446,14 +446,16 @@ public static class RewriteRules /// Opens a trigonometric function of a multiplied angle. /// /// + /// /// Written out, sin(4x) is far longer than it started, which is why the /// simplifier offers the result as a candidate rather than taking it. - /// - /// + /// + /// /// Run by the matcher rather than by the switch — /// . See the note on /// for what that costs and why the switch /// stays. + /// /// public static RewriteRuleSet ExpandMultipleAngle { get; } = new( nameof(ExpandMultipleAngle), diff --git a/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs b/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs index 13f29f623..4d6395984 100644 --- a/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs +++ b/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs @@ -132,14 +132,14 @@ public static Transformation FactorizationAtLevel(int level) /// Offered on its own as well as inside , because it is /// the one step of factorisation whose answer a caller might want without the rest. /// - /// - /// + /// /// Held in a nested class rather than in a static field of this one, and that is not a /// style choice. is declared above this and calls /// in its own initialiser, which reads this — and a /// static field initialiser runs in declaration order, so it would read /// and fail as a TypeInitializationException from somewhere else entirely. A nested /// type initialises on first use instead, whatever order the fields are written in. + /// /// public static Transformation NumericContentExtraction => Held.Instance; @@ -307,8 +307,7 @@ public static Transformation RuleBasedFactorizationAtLevel(int level) /// and anything whose answer is not a product — a refusal leaves the expression exactly as /// the rules left it, so nothing that factored before can stop factoring. /// - /// - /// + /// /// Held in a nested class rather than in a static field of this one. Static field /// initialisers run in declaration order, and — declared /// above — is eager, so a field here would still be when its @@ -316,6 +315,7 @@ public static Transformation RuleBasedFactorizationAtLevel(int level) /// combinator rather than as anything naming the real cause, and this file has had that /// failure before. A nested type is initialised on first touch, whatever order this /// one's members are written in. + /// /// public static Transformation PolynomialFactorization => PolynomialFactorizationHolder.Instance; @@ -575,8 +575,6 @@ public static Transformation EqualitySaturation(WorkBudget budget, CostModel cos /// in the library calls this — like it is offered, not /// applied. /// - /// - /// /// /// Built on rather than beside it. The graph does /// not sort a commutative operand pair: the rules that do build their replacement in code, diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index f9f38feec..1fdab705f 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -3995,39 +3995,6 @@ node is Powf(var radicand, Number.Rational r) && r is not Number.Integer && radi ?.Pipe(i => constantPart * i); } - /// - /// Reads as a numerator over a denominator, in either of the two - /// spellings a quotient has here. - /// - /// - /// - /// 1/(1 + x^3) is a Divf and (1 + x^3)^(-1) is a Powf, and - /// they are the same integrand. matched the first - /// and declined the second, so which of them was asked decided whether the integral came - /// back — and the second is not an exotic way to write it: it is what - /// builds whenever it takes a factor out of a - /// quotient. That is why a/(1 + x^3) had no antiderivative while - /// 1/(1 + x^3) and a * (1/(1 + x^3)) both did. - /// - /// - /// And the third spelling, a product with a negative power in it. - /// 3 u (4 - u^3)^(-1) is what Simplify makes of 3u/(4 - u^3), and it - /// is neither a Divf nor a bare Powf, so it was declined here -- and then - /// taken by integration by parts with v' = (4 - u^3)^(-1), which integrates to a - /// hundred-node sum of logarithms and arctangents that the search then spent thirty - /// seconds failing to integrate against 3u. Read as the quotient it is, the same - /// integrand is answered in a fifth of a second. The factors are gathered and one quotient - /// rebuilt from them, which is what makes the verdict independent of how the product - /// happens to be associated. - /// - /// - /// Only a negative whole power. A positive one is not a quotient; a fractional or - /// symbolic exponent is not one either, and (a/b)^(1/2) is not sqrt(a)/sqrt(b) - /// on the branch cut. Nothing here rewrites a quotient back into a power, so this cannot - /// re-enter the mutual recursion records. - /// - /// https://github.com/asc-community/AngouriMath/issues/718 - /// /// /// A rational function with rational coefficients whose denominator the splits above /// could not take apart -- irreducible over the rationals past degree two, or with @@ -4340,6 +4307,39 @@ private static bool HasTheVariableBelowABar(Entity expr, Entity.Variable x) => expr.Nodes.Any(node => node is Entity.Divf(_, var divisor) && divisor.ContainsNode(x) || node is Entity.Powf(var @base, Number.Integer { EInteger.Sign: < 0 }) && @base.ContainsNode(x)); + /// + /// Reads as a numerator over a denominator, in either of the two + /// spellings a quotient has here. + /// + /// + /// + /// 1/(1 + x^3) is a Divf and (1 + x^3)^(-1) is a Powf, and + /// they are the same integrand. matched the first + /// and declined the second, so which of them was asked decided whether the integral came + /// back — and the second is not an exotic way to write it: it is what + /// builds whenever it takes a factor out of a + /// quotient. That is why a/(1 + x^3) had no antiderivative while + /// 1/(1 + x^3) and a * (1/(1 + x^3)) both did. + /// + /// + /// And the third spelling, a product with a negative power in it. + /// 3 u (4 - u^3)^(-1) is what Simplify makes of 3u/(4 - u^3), and it + /// is neither a Divf nor a bare Powf, so it was declined here -- and then + /// taken by integration by parts with v' = (4 - u^3)^(-1), which integrates to a + /// hundred-node sum of logarithms and arctangents that the search then spent thirty + /// seconds failing to integrate against 3u. Read as the quotient it is, the same + /// integrand is answered in a fifth of a second. The factors are gathered and one quotient + /// rebuilt from them, which is what makes the verdict independent of how the product + /// happens to be associated. + /// + /// + /// Only a negative whole power. A positive one is not a quotient; a fractional or + /// symbolic exponent is not one either, and (a/b)^(1/2) is not sqrt(a)/sqrt(b) + /// on the branch cut. Nothing here rewrites a quotient back into a power, so this cannot + /// re-enter the mutual recursion records. + /// + /// https://github.com/asc-community/AngouriMath/issues/718 + /// private static bool TryReadAsQuotient(Entity expr, out Entity numerator, out Entity denominator) { // An exponent read evaluated: the normalisation writes `(u^2 - 1)^5` below the bar @@ -23004,13 +23004,6 @@ private static IEnumerable CandidateDenominators(Entity denominator, Ent yield return denominator; } - /// - /// with each written factor taken into its irreducible - /// factors over the rationals and equal factors gathered into one power, or - /// where the written factors already have that structure -- the - /// same count of distinct factors with the same multiplicities -- so that a - /// denominator which is already factored keeps its own spelling. - /// /// /// with each written base that is a polynomial in /// with rational coefficients, of degree at least two and not a @@ -23829,6 +23822,13 @@ static Entity Reduced(Entity constant) return besideASymbol && split ? product : null; } + /// + /// with each written factor taken into its irreducible + /// factors over the rationals and equal factors gathered into one power, or + /// where the written factors already have that structure -- the + /// same count of distinct factors with the same multiplicities -- so that a + /// denominator which is already factored keeps its own spelling. + /// private static Entity? TryWriteInIrreducibleFactors(Entity denominator, Entity.Variable x) { // Only where the written bases, each taken once, share a factor among them or @@ -24626,12 +24626,6 @@ private static (Entity Affine, int Power)? AReciprocalPowerOfALogarithm(Entity f return solve(inU, u)?.Substitute(u, linear).InnerSimplified; } - /// - /// Whether is A + B ln(c x^r) with A, B, - /// c and r free of and the logarithm present: one - /// logarithm of in it, the expression linear in that logarithm, - /// and the logarithm's argument a constant times a power of . - /// /// /// Whether is built from and constants by /// products, quotients and powers with exponents free of , however @@ -24646,6 +24640,12 @@ private static bool IsAProductOfPowersOfTheVariable(Entity expr, Entity.Variable _ => false, }; + /// + /// Whether is A + B ln(c x^r) with A, B, + /// c and r free of and the logarithm present: one + /// logarithm of in it, the expression linear in that logarithm, + /// and the logarithm's argument a constant times a power of . + /// private static bool IsAffineInALogarithmOfAPowerOfX(Entity expr, Entity.Variable x) { Entity? logarithm = null; @@ -25686,6 +25686,14 @@ static Entity WithoutConditions(Entity coefficient) return answer; } + /// + /// The highest power of the quadratic below the bar that + /// takes: with six symbols the fifth + /// ran past twenty seconds in the elimination, where the reduction a power at a time + /// declines it in half a second. + /// + private const int LargestPowerSolvedAtOnce = 4; + /// /// The reduction of /// as one linear system over the polynomials in the symbols: P/(A^n sqrt(B)) is @@ -25716,14 +25724,6 @@ static Entity WithoutConditions(Entity coefficient) /// https://github.com/asc-community/AngouriMath/issues/233 /// /// - /// - /// The highest power of the quadratic below the bar that - /// takes: with six symbols the fifth - /// ran past twenty seconds in the elimination, where the reduction a power at a time - /// declines it in half a second. - /// - private const int LargestPowerSolvedAtOnce = 4; - private static Entity? ReducedOverThePolynomialsInItsSymbols(Entity[] coefficients, int power, Entity a2, Entity a1, Entity a0, Entity b2, Entity b1, Entity b0, Entity quadratic, Entity radicand, Entity constant, Entity.Variable x, Entity expr) { @@ -29654,51 +29654,6 @@ private static bool TryReadAPowerTimesARadicalQuadratic(Entity expr, Entity.Vari return null; } - /// - /// A rational function of e^(k x), turned into a rational function of one variable - /// by u = e^(k x). - /// - /// - /// - /// With u = e^(k x) we have dx = du/(k u), and every exponential in the - /// integrand becomes a whole power of u, so a quotient built from them is a - /// quotient of polynomials — which the rational integrator answers. - /// - /// - /// What was missing. 1/(1 + e^x) had no antiderivative, and neither did - /// 1/(e^x + e^(-x)). e^x/(1 + e^x) did, which is the shape of the gap: the - /// general substitution answers an exponential integrand only when the numerator happens - /// to be the derivative of something in it, and declines the rest for want of anything to - /// substitute for. - /// - /// - /// It is also how the hyperbolic functions get integrated, since they are not nodes - /// here — tanh(x) is built as (e^(2x) - 1)/(e^(2x) + 1) and sech(x) - /// as 2/(e^x + e^(-x)), so both are rational functions of an exponential, and - /// tanh, coth, sech and csch had no antiderivative at all. - /// - /// - /// The answers come out in the exponential rather than as ln(cosh(x)) or - /// 2 arctan(e^x), and unfolded — the rational integrator writes a logarithm as - /// ln((2ax + b - D)/(2ax + b + D)) and leaves the arithmetic in the coefficients - /// standing. That is its shape on plain rational integrands too, not something this - /// rewrite introduces; 1/(x(x+1)) comes out as - /// ln((2x + 1 - 1)/(2x + 1 + 1)) with nothing exponential in sight. - /// - /// - /// One k for the whole integrand. The exponents are read as linear in the - /// variable and their slopes taken together by greatest common divisor, so e^x - /// beside e^(2x) gives u and u^2 under one substitution. A slope that - /// is not a whole number, or an exponent that is not linear — e^(x^2), whose - /// integral is not elementary at all — is declined. - /// - /// - /// No condition is owed. e^(k x) is positive for every real x and - /// never zero, so the substitution is invertible on the whole line and introduces no - /// interval of its own; u is a genuine change of variable rather than a branch. - /// - /// https://github.com/asc-community/AngouriMath/issues/718 - /// /// /// A quotient by an exponential, handed on as the product with its reciprocal: /// N/b^(f(x)) as N * b^(-f(x)). @@ -33675,6 +33630,52 @@ static bool IsAMonomialIn(Entity expr, Entity.Variable x) return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer; } + /// + /// A rational function of e^(k x), turned into a rational function of one variable + /// by u = e^(k x). + /// + /// + /// + /// With u = e^(k x) we have dx = du/(k u), and every exponential in the + /// integrand becomes a whole power of u, so a quotient built from them is a + /// quotient of polynomials — which the rational integrator answers. + /// + /// + /// What was missing. 1/(1 + e^x) had no antiderivative, and neither did + /// 1/(e^x + e^(-x)). e^x/(1 + e^x) did, which is the shape of the gap: the + /// general substitution answers an exponential integrand only when the numerator happens + /// to be the derivative of something in it, and declines the rest for want of anything to + /// substitute for. + /// + /// + /// It is also how the hyperbolic functions get integrated, since they are not nodes + /// here — tanh(x) is built as (e^(2x) - 1)/(e^(2x) + 1) and sech(x) + /// as 2/(e^x + e^(-x)), so both are rational functions of an exponential, and + /// tanh, coth, sech and csch had no antiderivative at all. + /// + /// + /// The answers come out in the exponential rather than as ln(cosh(x)) or + /// 2 arctan(e^x), and unfolded — the rational integrator writes a logarithm as + /// ln((2ax + b - D)/(2ax + b + D)) and leaves the arithmetic in the coefficients + /// standing. That is its shape on plain rational integrands too, not something this + /// rewrite introduces; 1/(x(x+1)) comes out as + /// ln((2x + 1 - 1)/(2x + 1 + 1)) with nothing exponential in sight. + /// + /// + /// One k for the whole integrand. The exponents are read as linear in the + /// variable and their slopes taken together by greatest common divisor, so e^x + /// beside e^(2x) gives u and u^2 under one substitution, and + /// e^(x/2) beside e^x gives u = e^(x/2). Symbolic slopes are taken + /// where they are rational multiples of one another. An exponent that is not linear — + /// e^(x^2), whose integral is not elementary at all — is declined. + /// + /// + /// No condition is owed. e^(k x) is positive for every real x and + /// never zero, so the substitution is invertible on the whole line and introduces no + /// interval of its own; u is a genuine change of variable rather than a branch. + /// + /// https://github.com/asc-community/AngouriMath/issues/718 + /// internal static Entity? SolveByExponentialSubstitution(Entity expr, Entity.Variable x, bool integrateByParts) { // Rational slopes, so that `e^(x/2)` beside `e^x` is read: the base is `e^(k x)` with @@ -35094,10 +35095,6 @@ private static bool HoldsARepeatedSymbolicFactor(Entity denominator, Entity.Vari return dropped && !other ? written.InnerSimplified : null; } - /// - /// Whether both read as polynomials in with the numerator's - /// degree below the denominator's; false where either does not read. - /// /// /// False where is shown not to be a constant multiple of /// , both read as polynomials in : their @@ -35124,6 +35121,10 @@ private static bool MayBeAConstantMultiple(Entity numerator, Entity derivative, return true; } + /// + /// Whether both read as polynomials in with the numerator's + /// degree below the denominator's; false where either does not read. + /// private static bool IsProperByDegree(Entity numerator, Entity denominator, Entity.Variable x) => TreeAnalyzer.TryGetPolynomial(numerator, x, out var above) && above.Count > 0 && TreeAnalyzer.TryGetPolynomial(denominator, x, out var below) && below.Count > 0 @@ -36862,39 +36863,6 @@ static bool IsASumOfPowersOfX(Entity sum, Entity.Variable x) !factor.ContainsNode(x) || factor == x || factor is Powf(var @base, var exponent) && @base == x && !exponent.ContainsNode(x))); } - /// - /// Several square roots of polynomials in the variable, written as one: - /// sqrt(1 + x^2) sqrt(1 - x^2) is sqrt(1 - x^4), and a root below the bar - /// is a root above it over its own base, 1/sqrt(B) = sqrt(B)/B. - /// - /// - /// - /// x/(sqrt(1 + x^2) sqrt(1 - x^2)) had no antiderivative and x/sqrt(1 - x^4) - /// is one substitution. Nothing reads two radicals: the substitution wants one subtree - /// to replace, Euler's rule wants one root of one quadratic, and each rule looked at - /// this integrand and saw two of what it takes one of. It is what by parts leaves from - /// arcsin(x)/(1 + x^2)^(3/2) and from ln(x + sqrt(1 + x^2))/(1 - x^2)^(3/2), - /// and from every other pairing of an inverse function's radical with the one in - /// the power it stands over. - /// - /// - /// When the identity holds. On the principal branch, sqrt(P) sqrt(Q) = sqrt(PQ) - /// whenever at most one of P and Q is negative: with both negative the - /// left is i sqrt|P| * i sqrt|Q| = -sqrt|PQ| and the right is +sqrt|PQ|. - /// For k roots the same count applies -- i^k against i^(k mod 2) -- - /// so the rule asks whether there is a real x at which two of the bases are - /// negative, and only rewrites when there is not. The bases are polynomials with - /// numeric coefficients; their real roots cut the line into intervals on each of which - /// every base keeps its sign, so the count at one point of each interval is the count - /// on the interval. Where the solver cannot give the roots, the rule declines. - /// - /// - /// A rewriting rule, unscoped: what it hands on has strictly fewer radicals than what - /// it was given, and it is asked one level down more often than at the top, since the - /// two-radical shape is what a by-parts step produces. - /// - /// https://github.com/asc-community/AngouriMath/issues/718 - /// /// /// A square root of a polynomial with a repeated factor, the factor taken out of the /// root: sqrt(9 + 3x - 5x^2 + x^3) is sqrt((x - 3)^2 (x + 1)), which is @@ -37011,6 +36979,16 @@ static bool IsASumOfPowersOfX(Entity sum, Entity.Variable x) return answer.Nodes.Any(node => node == MathS.NaN) ? null : answer; } + /// + /// asked first where a power of + /// the function stands above or below the binomial: the rules before it in the dispatcher spent + /// the budget on `sinh(y)^4/(a + b sinh(y)^3)`, and the bare reciprocal they answer is left to them. + /// + internal static Entity? SolveAPowerOverABinomialInAPowerOfASineOverItsRoots(Entity expr, Entity.Variable x) + => Integration.AnsweringTheQuestionAsked && TryReadAsQuotient(expr, out var above, out _) && above.ContainsNode(x) + ? SolveAReciprocalOfABinomialInAPowerOfASineOverItsRoots(expr, x) + : null; + /// /// The reciprocal of a + b s^n, s a sine or a cosine of a linear and a symbol in /// a or b, over the roots of the binomial: with q = 2 for an even n @@ -37027,16 +37005,6 @@ static bool IsASumOfPowersOfX(Entity sum, Entity.Variable x) /// 1/(a + b sin(x)^5), 1/(a - b cos(x)^6) and the like were declined. /// https://github.com/asc-community/AngouriMath/issues/718 /// - /// - /// asked first where a power of - /// the function stands above or below the binomial: the rules before it in the dispatcher spent - /// the budget on `sinh(y)^4/(a + b sinh(y)^3)`, and the bare reciprocal they answer is left to them. - /// - internal static Entity? SolveAPowerOverABinomialInAPowerOfASineOverItsRoots(Entity expr, Entity.Variable x) - => Integration.AnsweringTheQuestionAsked && TryReadAsQuotient(expr, out var above, out _) && above.ContainsNode(x) - ? SolveAReciprocalOfABinomialInAPowerOfASineOverItsRoots(expr, x) - : null; - internal static Entity? SolveAReciprocalOfABinomialInAPowerOfASineOverItsRoots(Entity expr, Entity.Variable x) { if (!TryReadAsQuotient(expr, out var numerator, out var denominator)) @@ -40019,6 +39987,39 @@ private static (int, int) SignOfTheTwoRoots(Entity node, List radicands) return (signs[0], signs[1]); } + /// + /// Several square roots of polynomials in the variable, written as one: + /// sqrt(1 + x^2) sqrt(1 - x^2) is sqrt(1 - x^4), and a root below the bar + /// is a root above it over its own base, 1/sqrt(B) = sqrt(B)/B. + /// + /// + /// + /// x/(sqrt(1 + x^2) sqrt(1 - x^2)) had no antiderivative and x/sqrt(1 - x^4) + /// is one substitution. Nothing reads two radicals: the substitution wants one subtree + /// to replace, Euler's rule wants one root of one quadratic, and each rule looked at + /// this integrand and saw two of what it takes one of. It is what by parts leaves from + /// arcsin(x)/(1 + x^2)^(3/2) and from ln(x + sqrt(1 + x^2))/(1 - x^2)^(3/2), + /// and from every other pairing of an inverse function's radical with the one in + /// the power it stands over. + /// + /// + /// When the identity holds. On the principal branch, sqrt(P) sqrt(Q) = sqrt(PQ) + /// whenever at most one of P and Q is negative: with both negative the + /// left is i sqrt|P| * i sqrt|Q| = -sqrt|PQ| and the right is +sqrt|PQ|. + /// For k roots the same count applies -- i^k against i^(k mod 2) -- + /// so the rule asks whether there is a real x at which two of the bases are + /// negative, and only rewrites when there is not. The bases are polynomials with + /// numeric coefficients; their real roots cut the line into intervals on each of which + /// every base keeps its sign, so the count at one point of each interval is the count + /// on the interval. Where the solver cannot give the roots, the rule declines. + /// + /// + /// A rewriting rule, unscoped: what it hands on has strictly fewer radicals than what + /// it was given, and it is asked one level down more often than at the top, since the + /// two-radical shape is what a by-parts step produces. + /// + /// https://github.com/asc-community/AngouriMath/issues/718 + /// internal static Entity? SolveByCombiningRadicals(Entity expr, Entity.Variable x, bool integrateByParts) { // A secant or cosecant under a root is the reciprocal of a cosine or sine there: @@ -47530,22 +47531,6 @@ 2 when NotWhole(sums[0].Base) != NotWhole(sums[1].Base) => NotWhole(sums[0].Base return inTermsOfS.Substitute(s, @base); } - /// - /// A + i A tan(z) is A e^(i z)/cos(z), and A + i A cot(z) is - /// i A e^(-i z)/sin(z) -- exactly, wherever the tangent is defined, since - /// cos(z) + i sin(z) is e^(i z). A power of one of them below the bar is - /// then a power of the cosine times an exponential, and beside a polynomial that is a - /// shape the closed rules answer: (c + d x)^2/(a + i a tan(pe + f x))^2 is a - /// search past the budget as written and a second once rewritten. - /// - /// - /// The four signs are the four identities: A - i A tan(z) is - /// A e^(-i z)/cos(z) and A - i A cot(z) is -i A e^(i z)/sin(z). - /// Rubi's 4.3.10 and 4.4.10, where every such row carries the imaginary unit in the - /// coefficient and nothing else reads it. The same question, not one of its own: what is - /// handed on is the integrand in another spelling. - /// https://github.com/asc-community/AngouriMath/issues/718 - /// /// /// integrated in u = g + f x where x stands only in trigonometric /// functions of that one linear, other than x itself, and a root of something in x stands: dx = du/f. Null @@ -47589,6 +47574,22 @@ 2 when NotWhole(sums[0].Base) != NotWhole(sums[1].Base) => NotWhole(sums[0].Base return answer.Substitute(u, linear); } + /// + /// A + i A tan(z) is A e^(i z)/cos(z), and A + i A cot(z) is + /// i A e^(-i z)/sin(z) -- exactly, wherever the tangent is defined, since + /// cos(z) + i sin(z) is e^(i z). A power of one of them below the bar is + /// then a power of the cosine times an exponential, and beside a polynomial that is a + /// shape the closed rules answer: (c + d x)^2/(a + i a tan(pe + f x))^2 is a + /// search past the budget as written and a second once rewritten. + /// + /// + /// The four signs are the four identities: A - i A tan(z) is + /// A e^(-i z)/cos(z) and A - i A cot(z) is -i A e^(i z)/sin(z). + /// Rubi's 4.3.10 and 4.4.10, where every such row carries the imaginary unit in the + /// coefficient and nothing else reads it. The same question, not one of its own: what is + /// handed on is the integrand in another spelling. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// internal static Entity? SolveByWritingAnImaginaryTangentAsAnExponential(Entity expr, Entity.Variable x, bool integrateByParts) { // Below the bar only: `(A + i A tan(z))^3` above it is a polynomial in the tangent, @@ -49673,10 +49674,6 @@ private static Entity SimplifiedWithoutTheImaginaryUnit(Entity written, Entity i private static Entity WithoutItsCondition(Entity integrandInU) => integrandInU is Providedf(var inner, _) ? inner : integrandInU; // TODO: singularities ignored but not handled properly - /// - /// Attempts to solve an integral using u-substitution. - /// Looks for patterns where f(g(x)) * g'(x) can be integrated as F(g(x)). - /// /// /// Whether holds an even root of something in /// other than : the only way it can be real where the radicand is @@ -49846,6 +49843,10 @@ private static bool IsAFunctionOfTheTangentAlone(Entity expr, Entity.Variable u) /// Whether a linear renaming is being asked as the same question on this thread. [System.ThreadStatic] private static bool renamingLinearly; + /// + /// Attempts to solve an integral using u-substitution. + /// Looks for patterns where f(g(x)) * g'(x) can be integrated as F(g(x)). + /// internal static Entity? SolveBySubstitution(Entity expr, Entity.Variable x, bool integrateByParts = true) { // A rational function over written linear factors with symbols in their @@ -50703,18 +50704,6 @@ Entity Term(Entity? sum, Entity coefficient, int power) /// The largest sum the substitution rule offers as a candidate. private const int LargestSumOffered = 30; - /// - /// Finds potential substitution candidates u = g(x) from the expression. - /// For example, common patterns to try: - /// 1. f(ax + b) * a -> u = ax + b - /// 2. f(x^n) * x^(n-1) -> u = x^n - /// 3. f(g(x)) * g'(x) -> u = g(x) - /// - /// - /// The special functions of - /// #1501: the error - /// functions and the exponential, logarithmic, sine, cosine and hyperbolic integrals. - /// /// /// Set while the terms of a remainder after a special function are asked as questions of /// their own, so that the asking is not nested; see TryIntegrateByPartsOnce. @@ -50738,6 +50727,11 @@ private static bool IntroducesAnIncompleteGammaFunctionOfANonlinear(Entity integ return false; } + /// + /// The special functions of + /// #1501: the error + /// functions and the exponential, logarithmic, sine, cosine and hyperbolic integrals. + /// private static bool IsASpecialFunction(Entity node) => node is Entity.Erff or Entity.Erfcf or Entity.Erfif or Entity.Eif or Entity.Lif or Entity.Sif or Entity.Cif or Entity.Shif or Entity.Chif; @@ -50844,6 +50838,13 @@ private static bool TheDifferentialCanBeThere(Entity special, Entity expr, Entit }; } + /// + /// Finds potential substitution candidates u = g(x) from the expression. + /// For example, common patterns to try: + /// 1. f(ax + b) * a -> u = ax + b + /// 2. f(x^n) * x^(n-1) -> u = x^n + /// 3. f(g(x)) * g'(x) -> u = g(x) + /// private static IEnumerable FindSubstitutionCandidates(Entity expr, Entity.Variable x) { var candidates = new List(); diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs index 0127fae31..d9b98842a 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs @@ -1319,17 +1319,6 @@ private static bool IsExponentialRate(Entity expr, Entity.Variable x, out Entity return true; } - /// - /// The antiderivative of sqrt(a x^2 + b x + c): - /// (2ax + b) sqrt(Q) / (4a) + ((4ac - b^2) / (8a)) times the integral of - /// 1/sqrt(Q) -- integration by parts once, leaving the reciprocal form that - /// already knows. - /// - /// - /// Only where the leading coefficient is a number other than zero. With a = 0 this - /// is the square root of something linear, which the ordinary power rule already - /// integrates, and dividing by a would not be allowed anyway. - /// /// /// Reads k / (x^2 * sqrt(ax^2 + c)), giving back k, the radicand and its /// constant term. The radicand has to be a quadratic in x with no linear term and @@ -1372,6 +1361,17 @@ when root is null return true; } + /// + /// The antiderivative of sqrt(a x^2 + b x + c): + /// (2ax + b) sqrt(Q) / (4a) + ((4ac - b^2) / (8a)) times the integral of + /// 1/sqrt(Q) -- integration by parts once, leaving the reciprocal form that + /// already knows. + /// + /// + /// Only where the leading coefficient is a number other than zero. With a = 0 this + /// is the square root of something linear, which the ordinary power rule already + /// integrates, and dividing by a would not be allowed anyway. + /// private static Entity IntegrateRootOfQuadratic( Entity a, Entity b, Entity c, Entity radicand, Entity.Variable x) => (2 * a * x + b) * MathS.Sqrt(radicand) / (4 * a) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs b/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs index e8c7ec7ae..66f24d44a 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/RothsteinTrager.cs @@ -84,12 +84,6 @@ internal static class RothsteinTrager /// private const int MaxReducedLogarithmicDegree = 16; - /// - /// The integral of over , - /// both polynomials in with rational coefficients and the fraction - /// proper; where they are not, or where the answer does not - /// differentiate back to the integrand at the sampled points. - /// /// /// , for a denominator past only: what the /// rest of the integrator never answered, asked before it searches. @@ -109,6 +103,12 @@ internal static class RothsteinTrager internal static Entity? Integrate(Entity numerator, Entity denominator, Variable x) => IntegrateUpToDegree(numerator, denominator, x, MaxDegree); + /// + /// The integral of over , + /// both polynomials in with rational coefficients and the fraction + /// proper; where they are not, or where the answer does not + /// differentiate back to the integrand at the sampled points. + /// private static Entity? IntegrateUpToDegree(Entity numerator, Entity denominator, Variable x, int degree) { if (!PolynomialFactoring.TryGetRationalCoefficients(numerator, x, 1, 0, degree, out var aboveCoefficients) diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs index 820c750ff..dc63d9658 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs @@ -54,18 +54,6 @@ internal static Set Solve(Entity equation, Variable x) return solutions; } - /// - /// Solves a system of equations by solving one after another with substitution, e.g.
- /// let { x - y + a = 0, y + 2a = 0 } be a system of equations for variables { x, y }
- /// Then we first find y from the first equation,
- /// y = x + a
- /// then we substitute it to all others
- /// x + a + 2a = 0
- /// then we find x
- /// x = -3a
- /// Then we substitute back
- /// y = -3a + a = -2a
- ///
/// /// What a whole system solve is allowed to spend before it declines. /// @@ -94,6 +82,31 @@ internal static Set Solve(Entity equation, Variable x) internal static readonly WorkBudget SystemSolveBudget = new() { Steps = 10_000, Time = TimeSpan.FromSeconds(60) }; + /// + /// Solves a system of equations for , one row per solution, or + /// where it cannot. + /// + /// + /// + /// Tried in order: a Gröbner basis, where the system is polynomial over Q; row reduction, + /// where a linear system has fewer equations than unknowns; once for each factor, where + /// an equation is a product in more than one unknown; , + /// where the count is not square or an unknown is in no equation; and elimination. + /// + /// + /// Elimination solves one equation after another with substitution, e.g.
+ /// let { x - y + a = 0, y + 2a = 0 } be a system of equations for variables { x, y }
+ /// Then we first find y from the first equation,
+ /// y = x + a
+ /// then we substitute it to all others
+ /// x + a + 2a = 0
+ /// then we find x
+ /// x = -3a
+ /// Then we substitute back
+ /// y = -3a + a = -2a
+ /// and each row it answers is put back into the equations as they were written. + ///
+ ///
internal static Matrix? SolveSystem(IEnumerable inputEquations, ReadOnlySpan vars) { var written = inputEquations.ToList(); diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs index 6e3acfd84..8953f763a 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs @@ -594,17 +594,6 @@ static Entity Expand_(Entity e, int level) => Expand_(TreeAnalyzer.MultiHangBinary(expChildren, (a, b) => new Sumf(a, b)), level).InnerSimplified); } - /// - /// Adds up the terms of an expanded sum that differ only by a numeric factor, so - /// that expanding actually finishes: (x+1)^2 * (x+1)^2 multiplied out gives - /// sixteen terms, of which only five are distinct. - /// - /// - /// Deliberately not , which is far too expensive to run - /// inside . Each term is reduced to a coefficient and a - /// product of powers, and terms whose products agree are added together -- - /// enough to collect like terms and nothing more. - /// /// /// Whether every node of the expression is one that monomial collection describes: /// numbers, variables, and the operations that build a polynomial out of them. @@ -621,6 +610,17 @@ private static bool IsPolynomialShaped(Entity expression) => expression.Nodes.All(node => node is Number or Variable or Mulf or Powf or Sumf or Minusf or Divf); + /// + /// Adds up the terms of an expanded sum that differ only by a numeric factor, so + /// that expanding actually finishes: (x+1)^2 * (x+1)^2 multiplied out gives + /// sixteen terms, of which only five are distinct. + /// + /// + /// Deliberately not , which is far too expensive to run + /// inside . Each term is reduced to a coefficient and a + /// product of powers, and terms whose products agree are added together -- + /// enough to collect like terms and nothing more. + /// private static Entity CollectLikeTerms(Entity expanded) { if (expanded is not Sumf and not Minusf) diff --git a/Sources/AngouriMath/Functions/InternalAMExtensions.cs b/Sources/AngouriMath/Functions/InternalAMExtensions.cs index 35574154b..b3ef7078e 100644 --- a/Sources/AngouriMath/Functions/InternalAMExtensions.cs +++ b/Sources/AngouriMath/Functions/InternalAMExtensions.cs @@ -597,7 +597,6 @@ public static EDecimal Arccosecant(this EDecimal x, EContext context) return EDecimal.One.Divide(x.Arcsin(context), context); } - /// Truncates to [-2*, 2*] /// Analogy of /// /// arcsin(x) = arctan(x/sqrt(1 - x^2)), with 1 - x^2 as (1 - x)(1 + x) diff --git a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs index aa6b36265..47a9c7b8e 100644 --- a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs +++ b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs @@ -133,9 +133,9 @@ internal static Entity PerfectSquareRules(Entity x) /// -- a polynomial trinomial is collapsed by the rules above -- and to keep the cost /// of that Simplify off every three-term sum in every tree. /// + /// Internal so that the data form of this set calls it rather than repeating it. /// /// when the sum is not a square, so the rule does not fire. - /// Internal so that the data form of this set calls it rather than repeating it. internal static Entity? CollapseToPerfectSquare(Entity expr) { if (!expr.Nodes.Any(node => node is Powf(_, Rational and not Integer))) @@ -208,11 +208,6 @@ [ConstantField] private static readonly WorkBudget PerfectSquareProofBudget { EDecimal.FromString("-1.7"), EDecimal.FromString("-0.6"), EDecimal.FromString("0.8"), EDecimal.FromString("2.3") }; - /// - /// Whether a proposed collapse is the same number as what it replaces, checked at - /// with each free variable offset from the - /// last so that two of them never take the same value. - /// /// /// Whether the sum evaluates to a number at the sample points at all, which /// requires of any collapse this proposes. @@ -281,6 +276,11 @@ private static bool DiffersNumerically(Entity left, Entity right) return false; } + /// + /// Whether a proposed collapse is the same number as what it replaces, checked at + /// with each free variable offset from the + /// last so that two of them never take the same value. + /// private static bool AgreesNumerically(Entity original, Entity candidate) { var variables = original.Vars.ToList(); @@ -316,13 +316,15 @@ private static bool AgreesNumerically(Entity original, Entity candidate) /// (a + b) * (c + d) on the next pass. /// /// + /// /// The pairwise rules only ever see two adjacent terms of the sum tree, so a sum of /// four was left half-factored at a*(c + d) + b*c + b*d -- that is /// https://github.com/asc-community/AngouriMath/issues/531. Sums /// of two are left to those rules, which are older and better tested. + /// + /// Internal so the data form of this set calls it rather than repeating it. /// /// when no factor is shared, so the rule does not fire. - /// Internal so the data form of this set calls it rather than repeating it. internal static Entity? CollectCommonFactors(Entity expr) { var terms = Sumf.LinearChildren(expr).ToList(); diff --git a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Factorial.cs b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Factorial.cs index 9f4f3e6ae..daceb7837 100644 --- a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Factorial.cs +++ b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Factorial.cs @@ -17,12 +17,14 @@ internal static partial class Patterns /// writing the product out is not a simplification. /// /// + /// /// A static method rather than the local function it was, so that the switch below /// is the whole of the method and its arms can be read as data. See /// and /// #825. + /// + /// Internal so that the data form of this set calls it rather than repeating it. /// - /// Internal so that the data form of this set calls it rather than repeating it. internal static Entity CancelFactorials(Entity whole, Entity x, Entity x2, Number num, Number den) { static Entity Add(Entity a, Number b) => diff --git a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs index 139eb7c57..5f7f862bd 100644 --- a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs +++ b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs @@ -416,18 +416,6 @@ internal static Entity GatherPowersOfOneBase(Entity x) return result ?? x; } - /// - /// Reads a factor of a product as a base and an exponent. - /// - /// - /// The nested case is not cosmetic: writes a - /// divisor as (...)^(-1), so x^3 / x^2 arrives as - /// x^3 * (x^2)^(-1), whose second factor has base x^2 rather than - /// x and would be read as an unrelated base. Unwrapping it is - /// (a^b)^n = a^(b*n), which holds for a whole n whatever the sign of - /// a -- the same guard the ({}^{})^{} rule above carries, and for the - /// same reason. https://github.com/asc-community/AngouriMath/issues/752 - /// /// /// Whether is a real strictly above zero, decided rather than /// assumed. ln of such a number is a real, so a real multiple of it stays on the @@ -474,6 +462,18 @@ private static bool MayBeTakenAsReal(Entity entity) || IsKnownReal(entity) || entity.Evaled is Real { EDecimal.IsFinite: true }; + /// + /// Reads a factor of a product as a base and an exponent. + /// + /// + /// The nested case is not cosmetic: writes a + /// divisor as (...)^(-1), so x^3 / x^2 arrives as + /// x^3 * (x^2)^(-1), whose second factor has base x^2 rather than + /// x and would be read as an unrelated base. Unwrapping it is + /// (a^b)^n = a^(b*n), which holds for a whole n whatever the sign of + /// a -- the same guard the ({}^{})^{} rule above carries, and for the + /// same reason. https://github.com/asc-community/AngouriMath/issues/752 + /// private static (Entity Base, Entity Exponent) Decompose(Entity factor) { if (factor is not Powf(var @base, var exponent)) diff --git a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Rational.cs b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Rational.cs index 3524b4ab0..c30a2dd68 100644 --- a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Rational.cs +++ b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Rational.cs @@ -84,33 +84,6 @@ internal static Entity PairwiseGroupedQuotient( private static bool IsSurd(Entity node) => node is Powf(_, Rational and not Integer); - /// - /// num / (a + b) becomes num * (a - b) / (a^2 - b^2), which clears a - /// square root out of a two-term denominator: - /// (5 - sqrt(3)) / (5 + sqrt(3)) is 14/11 - 5/11 * sqrt(3). - /// - /// - /// - /// The library already prefers a denominator without a surd -- 1/sqrt(2) has - /// always come back as sqrt(2)/2 -- so this extends an existing preference to - /// the binomial case. https://github.com/asc-community/AngouriMath/issues/205 - /// - /// - /// Three conditions, and each is load-bearing. The denominator must be constant, - /// because multiplying numerator and denominator by a - b is only valid where - /// that is non-zero, and for a symbolic denominator it is not decidable -- rewriting - /// there would either lose a value or attach a condition to every such quotient. - /// a^2 - b^2 must fold to a rational, which is what says the root was - /// actually cleared: a conjugate does nothing for a cube root, where - /// 1 - 2^(2/3) is no better than 1 + 2^(1/3). And it must be non-zero, - /// which rules out a = b and, since it is (a-b)(a+b), guarantees the - /// original denominator was non-zero too. - /// - /// - /// Terminates because the rewritten denominator is a rational and this rule requires - /// a surd in the denominator to fire at all. - /// - /// /// /// p * value / q for a rational p/q, written so that the rational is /// split across the quotient rather than left as a factor: a unit-numerator rational @@ -162,6 +135,31 @@ internal static Entity GatherNumericCoefficientOverASurd(Entity expr) /// num / (a + b) becomes num * (a - b) / (a^2 - b^2) where that clears a /// surd out of the denominator, and where it does not. /// + /// + /// + /// (5 - sqrt(3)) / (5 + sqrt(3)) is 14/11 - 5/11 * sqrt(3). + /// + /// + /// The library already prefers a denominator without a surd -- 1/sqrt(2) has + /// always come back as sqrt(2)/2 -- so this extends an existing preference to + /// the binomial case. https://github.com/asc-community/AngouriMath/issues/205 + /// + /// + /// Three conditions, and each is load-bearing. The denominator must be constant, + /// because multiplying numerator and denominator by a - b is only valid where + /// that is non-zero, and for a symbolic denominator it is not decidable -- rewriting + /// there would either lose a value or attach a condition to every such quotient. + /// a^2 - b^2 must fold to a rational, which is what says the root was + /// actually cleared: a conjugate does nothing for a cube root, where + /// 1 - 2^(2/3) is no better than 1 + 2^(1/3). And it must be non-zero, + /// which rules out a = b and, since it is (a-b)(a+b), guarantees the + /// original denominator was non-zero too. + /// + /// + /// Terminates because the rewritten denominator is a rational and this rule requires + /// a surd in the denominator to fire at all. + /// + /// internal static Entity MultiplyByTheConjugate(Entity expr) { if (expr is not Divf(var num, var den)) diff --git a/Sources/Tests/UnitTests/Convenience/DocCommentPlacementTest.cs b/Sources/Tests/UnitTests/Convenience/DocCommentPlacementTest.cs new file mode 100644 index 000000000..5fd7c17c5 --- /dev/null +++ b/Sources/Tests/UnitTests/Convenience/DocCommentPlacementTest.cs @@ -0,0 +1,45 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Xunit; + +namespace AngouriMath.Tests.Convenience +{ + /// + /// Each member's documentation describes that member. + /// + /// + /// A member added between a doc comment and the declaration it was written for takes that + /// comment on top of its own, and the compiler says nothing: the member it described is left + /// undocumented and the new one shows two summaries. Twenty-three comments had drifted that + /// way, one since the commit that removed the method it was for, and seven members had their + /// remarks written as two. A second <summary>, <remarks>, + /// <returns> or <value> is the trace either leaves. A member may + /// have several examples, which runs one by one. + /// + [Trait("Area", "Convenience")] + public sealed class DocCommentPlacementTest + { + [Fact] + public void NoMemberCarriesASecondSummaryOrRemarks() + { + var path = Path.Combine(Path.GetDirectoryName(typeof(MathS).Assembly.Location)!, "AngouriMath.xml"); + var repeated = XDocument.Load(path).Descendants("member") + .Where(member => member.Elements() + .GroupBy(element => element.Name.LocalName) + .Any(group => group.Key is "summary" or "remarks" or "returns" or "value" && group.Count() > 1)) + .Select(member => member.Attribute("name")?.Value) + .ToList(); + Assert.True(repeated.Count == 0, + "A doc comment that belongs to another member, or one element written twice:\n" + + string.Join("\n", repeated)); + } + } +}