From e98a1bf0a7b775914856984a4330babe596ea705 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 10:16:35 +0000 Subject: [PATCH] A conditional expression is differentiated where its condition holds `F provided P` is differentiated with P known to hold, and so is each case of a piecewise, since the derivative is claimed only there. A power whose base what is known keeps positive is read as real, so under a strict bound such as `x > 0` the sign, the modulus, max and min of an expression in a root of x differentiate as on the real line: sgn(x^(1/3) + 2) provided x > 0 is 0 there, where it was left as written. Without the bound nothing changes, since for x < 0 the roots are complex. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 15 +++++++ .../Functions/Continuous/Differentiation.cs | 8 +++- .../Functions/TreeAnalyzer/RealValued.cs | 40 +++++++++++++++++-- .../UnitTests/Calculus/DerivativeGapsTest.cs | 24 +++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index dbfa4ae0c..0a3a7c7b7 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1487,6 +1487,21 @@ below are one indeterminate. Rubi's 7.5.1 and 7.6.1 | `"x^2 * asech(c * x)".ToEntity().Integrate("x")` | `integral(...)`, the same | an antiderivative | | `"x / sqrt(1 + 1/(c*x)^2)".ToEntity().Integrate("x")` | `integral(x / sqrt(1 + 1 / (c * x) ^ 2), x)` | an antiderivative, `provided c^2 > 0` | +### A conditional expression is differentiated where its condition holds + +**Answers where there were none.** `F provided P` is differentiated with `P` known to hold, and +so is each case of a piecewise. Under a strict bound such as `x > 0`, a root of `x` is real, so +the sign, the modulus, `max` and `min` of an expression in it are differentiated as on the real +line, where without the bound they are left as written: for `x < 0` the roots are complex. The +answer is still claimed only where `P` holds. + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"sgn(x^(1/3) + 2) provided x > 0".ToEntity().Differentiate("x")` | `derivative(sgn(x ^ (1/3) + 2), x) provided x > 0` | `0 provided x > 0 and not x ^ (1/3) + 2 = 0` | +| `"abs(sqrt(x) - 1) provided x > 0".ToEntity().Differentiate("x")` | `derivative(abs(sqrt(x) - 1), x) provided x > 0` | `sgn(sqrt(x) - 1) * 1/2 * x ^ (-1/2) provided x > 0 and not sqrt(x) - 1 = 0` | +| `"max(sqrt(x), 1) provided x > 0".ToEntity().Differentiate("x")` | `derivative(max(sqrt(x), 1), x) provided x > 0` | `(1/2 * x ^ (-1/2) + sgn(sqrt(x) - 1) * 1/2 * x ^ (-1/2)) / 2 provided x > 0 and not sqrt(x) - 1 = 0` | +| `"piecewise(abs(sqrt(x) - 1) provided x > 0, 0)".ToEntity().Differentiate("x")` | `piecewise((derivative(abs(sqrt(x) - 1), x)) provided (x > 0), 0 provided True)` | the same with that case's derivative written out | + ### `arsech` and `arcsch` below the bar are integrated `1/(x^2 (a + b arcsch(c x)))` and `1/(x^4 (a + b arsech(c x)))` were left unevaluated. The diff --git a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs index 9d16d3b6d..0920070d9 100644 --- a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs +++ b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs @@ -606,16 +606,20 @@ protected override Entity InnerDifferentiate(Variable variable) partial record Providedf { + // Differentiated with its condition known to hold, since the derivative is claimed + // only there: under `x > 0`, sgn(x^(1/3) + 2) is flat, where for x < 0 the cube + // root is complex and nothing about it is. /// protected override Entity InnerDifferentiate(Variable variable) - => Expression.InnerDifferentiate(variable).Provided(Predicate); + => TreeAnalyzer.Assuming(Predicate, () => Expression.InnerDifferentiate(variable)).Provided(Predicate); } partial record Piecewise { + // Each case with its own condition known to hold, as a `provided` is. /// protected override Entity InnerDifferentiate(Variable variable) - => New(Cases.Select(c => c.New(c.Expression.InnerDifferentiate(variable), c.Predicate))); + => New(Cases.Select(c => c.New(TreeAnalyzer.Assuming(c.Predicate, () => c.Expression.InnerDifferentiate(variable)), c.Predicate))); } partial record Application diff --git a/Sources/AngouriMath/Functions/TreeAnalyzer/RealValued.cs b/Sources/AngouriMath/Functions/TreeAnalyzer/RealValued.cs index dc85c5430..343f7fba8 100644 --- a/Sources/AngouriMath/Functions/TreeAnalyzer/RealValued.cs +++ b/Sources/AngouriMath/Functions/TreeAnalyzer/RealValued.cs @@ -5,6 +5,7 @@ // Website: https://am.angouri.org. // +using System; using static AngouriMath.Entity; using static AngouriMath.Entity.Number; @@ -14,9 +15,10 @@ internal static partial class TreeAnalyzer { /// /// Whether the expression is real wherever it is defined, given that - /// is. A free variable is read as complex by this library, so one appearing anywhere but - /// under a modulus settles nothing and the answer is no. Nothing is claimed for a shape - /// not listed: the list costs coverage and never correctness. + /// is, and given what says is known to hold. A free variable is + /// read as complex by this library, so one appearing anywhere but under a modulus + /// settles nothing and the answer is no. Nothing is claimed for a shape not listed: the + /// list costs coverage and never correctness. /// /// /// Asked by the limit reader, for which a bound or an oscillation is a fact about a real @@ -53,6 +55,12 @@ internal static bool IsRealValued(Entity expr, Variable x) case Powf(var constantBase, var exponent) when !constantBase.ContainsNode(x) && IsARealNumber(constantBase.Evaled) && ((Complex)constantBase.Evaled).RealPart.IsPositive: return IsRealValued(exponent, x); + // So is a base that what is known keeps positive: x ^ (1/3) under `x > 0`. Only a + // strict bound is read as one, so the base is positive on an open set around the + // point, which is what a derivative needs. + case Powf(var @base, var exponent) when assumed is { } known + && PredicateEntailment.Entails(known, new Greaterf(@base, Integer.Zero)): + return IsRealValued(@base, x) && IsRealValued(exponent, x); case Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Arccotanf or Signumf or Erff or Erfcf or Erfif or Eif or Sif or Shif: return IsRealValued(expr.DirectChildren[0], x); @@ -64,6 +72,32 @@ internal static bool IsRealValued(Entity expr, Variable x) } } + /// + /// What is known to hold wherever the expression being read has a value, or + /// : the condition of the provided, or of the piecewise + /// case, being differentiated. Thread-static, as the limit machinery's approach is, and + /// swapped rather than set, so a nested one restores the outer. + /// + [ThreadStatic] private static Entity? assumed; + + /// + /// Runs with known to hold, as well + /// as whatever was known already. + /// + internal static T Assuming(Entity condition, Func work) + { + var outer = assumed; + assumed = outer is null ? condition : outer & condition; + try + { + return work(); + } + finally + { + assumed = outer; + } + } + // A real number, or a complex one whose imaginary part is zero: with the downcasting // off, `e` evaluates to the latter, and it is no less real for it. private static bool IsARealNumber(Entity evaled) diff --git a/Sources/Tests/UnitTests/Calculus/DerivativeGapsTest.cs b/Sources/Tests/UnitTests/Calculus/DerivativeGapsTest.cs index c61c0f86d..8bcb0ad02 100644 --- a/Sources/Tests/UnitTests/Calculus/DerivativeGapsTest.cs +++ b/Sources/Tests/UnitTests/Calculus/DerivativeGapsTest.cs @@ -23,6 +23,30 @@ public sealed class DerivativeGapsTest public void SignumHasADerivative() => Assert.Equal("0 provided not x = 0".ToEntity(), "sgn(x)".ToEntity().Differentiate("x")); + // A conditional expression is differentiated where its condition holds, and under + // `x > 0` the roots of x are real, so the sign and the modulus of an expression in them + // are differentiated as on the real line. The antiderivative of a square written in + // x^(1/3) is `sgn(x^(1/3) + c) F(x) provided x > 0`, and could not be checked by + // differentiating it back while that sign's derivative was left as written. + [Theory] + [InlineData("sgn(x^(1/3) + 2) provided x > 0", "1", "0")] + [InlineData("abs(sqrt(x) - 1) provided x > 0", "4", "1/4")] + [InlineData("abs(sqrt(x) - 1) provided x > 0", "1/4", "-1")] + [InlineData("piecewise(abs(sqrt(x) - 1) provided x > 0, 0)", "1/4", "-1")] + [InlineData("max(sqrt(x), 1) provided x > 0", "4", "1/4")] + [InlineData("max(sqrt(x), 1) provided x > 0", "1/4", "0")] + public void UnderItsConditionARootIsReal(string expression, string at, string derivative) => + Assert.Equal(derivative.ToEntity().Evaled, expression.ToEntity().Differentiate("x").Substitute("x", at.ToEntity()).Evaled); + + // For x < 0 the roots are complex and nothing makes their sign flat. `x >= 0` does not + // help either: at 0 there is no interval of positive x around the point. + [Theory] + [InlineData("sgn(x^(1/3) + 2)")] + [InlineData("abs(sqrt(x) - 1)")] + [InlineData("sgn(x^(1/3) + 2) provided x >= 0")] + public void WithoutAStrictConditionARootIsNotReadAsReal(string expression) => + Assert.Contains("derivative(", expression.ToEntity().Differentiate("x").Stringize()); + // The antiderivative of abs(x) is sgn(x) * x^2 / 2. Differentiating it back used to // produce derivative(sgn(x), x) and stop, so the answer could not be checked or // used numerically.