Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Sources/AngouriMath/Functions/Continuous/Differentiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// <inheritdoc/>
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.
/// <inheritdoc/>
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
Expand Down
40 changes: 37 additions & 3 deletions Sources/AngouriMath/Functions/TreeAnalyzer/RealValued.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Website: https://am.angouri.org.
//

using System;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;

Expand All @@ -14,9 +15,10 @@ internal static partial class TreeAnalyzer
{
/// <summary>
/// Whether the expression is real wherever it is defined, given that <paramref name="x"/>
/// 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 <see cref="Assuming"/> 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.
/// </summary>
/// <remarks>
/// Asked by the limit reader, for which a bound or an oscillation is a fact about a real
Expand Down Expand Up @@ -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);
Expand All @@ -64,6 +72,32 @@ internal static bool IsRealValued(Entity expr, Variable x)
}
}

/// <summary>
/// What is known to hold wherever the expression being read has a value, or
/// <see langword="null"/>: the condition of the <c>provided</c>, 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.
/// </summary>
[ThreadStatic] private static Entity? assumed;

/// <summary>
/// Runs <paramref name="work"/> with <paramref name="condition"/> known to hold, as well
/// as whatever was known already.
/// </summary>
internal static T Assuming<T>(Entity condition, Func<T> 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)
Expand Down
24 changes: 24 additions & 0 deletions Sources/Tests/UnitTests/Calculus/DerivativeGapsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading