Skip to content

Commit 71249a6

Browse files
Differentiate the sign and modulus of an expression with a logarithm where the logarithm is real (#2013)
TreeAnalyzer.GatherTheBasesOfRoots, which differentiation asks where an expression that is not real everywhere is real on an open set, read the bases of fractional powers only. A logarithm to a positive constant base is real where its argument is positive, and is now gathered the same way, so the derivatives of sgn and abs of an expression holding one become the real-line piecewise rather than being left unevaluated. The antiderivative of Rubi's 3.5:190, sqrt(ln(a x^n)^2)/x, is sgn(ln(a x^n)) ln(a x^n)^2/(2 n), exact by mpmath and continuous where the logarithm crosses zero, and could not be checked back: it is now verified (Unverified -> Solved, alone, at the same 0.01 GB). The answer itself is unchanged. Part of #233. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 98821ed commit 71249a6

3 files changed

Lines changed: 137 additions & 5 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5884,6 +5884,22 @@ below are one indeterminate. Rubi's 7.5.1 and 7.6.1
58845884
| `"x^2 * asech(c * x)".ToEntity().Integrate("x")` | `integral(...)`, the same | an antiderivative |
58855885
| `"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` |
58865886

5887+
### The sign and the modulus of an expression with a logarithm in it are differentiated where the logarithm is real
5888+
5889+
**Answers where there were none.** The derivative of `sgn(f)` and of `abs(f)` was left unevaluated where `f`
5890+
holds a logarithm, which is not real on the whole line. A logarithm to a positive constant base is real where
5891+
its argument is positive, and is read now as a root's base is (see the entry below): the real-line rule, `0`
5892+
and `sgn(f) f'`, where every such argument and base is positive, and the derivative left as written elsewhere.
5893+
The antiderivative of Rubi's 3.5 `sqrt(ln(a x^n)^2)/x`, `sgn(ln(a x^n)) ln(a x^n)^2/(2 n)`, which is unchanged,
5894+
could not be differentiated back anywhere
5895+
([#1186](https://github.com/asc-community/AngouriMath/issues/1186)).
5896+
5897+
| Input | Was (2.5.0) | Now |
5898+
|---|---|---|
5899+
| `"sgn(ln(x))".ToEntity().Differentiate("x")` | `derivative(sgn(ln(x)), x)` | `piecewise(0 provided (x > 0 and not ln(x) = 0), (derivative(sgn(ln(x)), x)) provided True)` |
5900+
| `"abs(ln(x))".ToEntity().Differentiate("x")` | `derivative(abs(ln(x)), x)` | `sgn(ln(x)) * 1 / x` where `x > 0` and `ln(x)` is not `0`, the derivative as written elsewhere |
5901+
| `"abs(ln(x) - 1)".ToEntity().Differentiate("x")` | `derivative(abs(ln(x) - 1), x)` | `sgn(ln(x) - 1) * 1 / x` where `x > 0` and `ln(x) - 1` is not `0`, the derivative as written elsewhere |
5902+
58875903
### The sign and the modulus of an expression in roots are differentiated where the roots are real
58885904

58895905
**Answers where there were none.** The derivative of `sgn(f)` and of `abs(f)` was left unevaluated

‎Sources/AngouriMath/Functions/TreeAnalyzer/RealValued.cs‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ internal static bool IsRealValued(Entity expr, Variable x)
7777
/// <summary>
7878
/// Where an expression that is not real everywhere is real on an open set: the
7979
/// conjunction of <c>b &gt; 0</c> over the bases <c>b</c> of its powers to a real
80-
/// exponent that is not whole, or <see langword="null"/> where it has no such power or
80+
/// exponent that is not whole and the arguments <c>b</c> of its logarithms to a positive
81+
/// constant base, or <see langword="null"/> where it has no such power or logarithm or
8182
/// a shape <see cref="IsRealValued"/> does not list. <c>sqrt(1 + x) + 1/2</c> is real
82-
/// where <c>1 + x &gt; 0</c>, and <c>x^(1/3) - 1</c> where <c>x &gt; 0</c>, which is all
83-
/// that is claimed: with the downcasting off a cube root of a negative is the principal
84-
/// one. Strict for the reason <see cref="IsRealValued"/> reads only a strict bound: a
85-
/// derivative needs the expression real on an interval around the point.
83+
/// where <c>1 + x &gt; 0</c>, <c>x^(1/3) - 1</c> where <c>x &gt; 0</c>, and
84+
/// <c>ln(2 x^3)</c> where <c>2 x^3 &gt; 0</c>, which is all that is claimed: with the
85+
/// downcasting off a cube root of a negative is the principal one. Strict for the reason
86+
/// <see cref="IsRealValued"/> reads only a strict bound: a derivative needs the
87+
/// expression real on an interval around the point.
8688
/// </summary>
8789
/// <remarks>
8890
/// Asked by differentiation where the sign or the modulus of such an expression is
@@ -134,6 +136,16 @@ private static bool GatherTheBasesOfRoots(Entity expr, Variable x, List<Entity>
134136
if (!bases.Contains(@base))
135137
bases.Add(@base);
136138
return true;
139+
// A logarithm to a positive constant base is real where its argument is positive,
140+
// and so is gathered as a root's base is: sgn(ln(a x^n)), which the antiderivative
141+
// of sqrt(ln(a x^n)^2)/x carries, is flat where a x^n > 0.
142+
case Logf(var logBase, var antilogarithm) when !logBase.ContainsNode(x)
143+
&& IsARealNumber(logBase.Evaled) && ((Complex)logBase.Evaled).RealPart.IsPositive:
144+
if (!GatherTheBasesOfRoots(antilogarithm, x, bases))
145+
return false;
146+
if (!bases.Contains(antilogarithm))
147+
bases.Add(antilogarithm);
148+
return true;
137149
case Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf
138150
or Arctanf or Arccotanf or Signumf or Erff or Erfcf or Erfif or Eif or Sif or Shif:
139151
return GatherTheBasesOfRoots(expr.DirectChildren[0], x, bases);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
using static AngouriMath.Entity;
12+
13+
namespace AngouriMath.Tests.Calculus
14+
{
15+
/// <summary>
16+
/// The sign and the modulus of an expression with a logarithm in it are differentiated as on
17+
/// the real line where the logarithm's argument is positive, as they are where the bases of
18+
/// its roots are, and left unevaluated elsewhere. The antiderivative of Rubi's
19+
/// <c>sqrt(ln(a x^n)^2)/x</c> carries <c>sgn(ln(a x^n))</c>, and could not be checked by
20+
/// differentiating it back at any point.
21+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1186">#1186</a>
22+
/// </summary>
23+
[Trait("Area", "Calculus")]
24+
public sealed class ASignOfALogarithmIsFlatWhereTheLogarithmIsRealTest
25+
{
26+
[Theory]
27+
[InlineData("sgn(ln(x))", "3", "0")]
28+
[InlineData("sgn(ln(x))", "1/2", "0")]
29+
[InlineData("sgn(ln(2 * x^3))", "1/4", "0")]
30+
[InlineData("sgn(ln(x) - 1/2)", "5", "0")]
31+
[InlineData("abs(ln(x))", "3", "1/3")]
32+
[InlineData("abs(ln(x))", "1/2", "-2")]
33+
[InlineData("abs(ln(sqrt(x) + 1))", "4", "1/12")]
34+
public void WhereTheArgumentIsPositiveTheRealLineRuleHolds(string expression, string at, string derivative)
35+
=> Assert.Equal(derivative.ToEntity().Evaled,
36+
expression.ToEntity().Differentiate("x").Substitute("x", at.ToEntity()).Evaled);
37+
38+
// To another positive base as well: |log_2(x)|' is -1/(x ln 2) below 1.
39+
[Fact]
40+
public void ToAnotherPositiveBase()
41+
{
42+
var got = (double)((Number.Complex)"abs(log(2, x))".ToEntity().Differentiate("x").Substitute("x", "1/4".ToEntity()).EvalNumerical()).RealPart;
43+
Assert.Equal(-4 / Math.Log(2), got, 12);
44+
}
45+
46+
// Where the argument is negative the logarithm is off the real line, and sgn(z) is
47+
// z / |z|, which turns with z: nothing is claimed there.
48+
[Theory]
49+
[InlineData("sgn(ln(x))", "-2")]
50+
[InlineData("abs(ln(x - 1))", "1/2")]
51+
[InlineData("sgn(ln(sqrt(x) - 1))", "1/4")]
52+
public void WhereTheArgumentIsNegativeNothingIsClaimed(string expression, string at)
53+
{
54+
var derivative = expression.ToEntity().Differentiate("x").Substitute("x", at.ToEntity()).Evaled;
55+
Assert.IsNotType<Number.Integer>(derivative);
56+
Assert.Contains("derivative(", derivative.Stringize());
57+
}
58+
59+
// A symbol is complex, and so is a base off the real line or one in x: those are left
60+
// as they were.
61+
[Theory]
62+
[InlineData("sgn(ln(a * x))")]
63+
[InlineData("abs(ln(x) * a)")]
64+
[InlineData("sgn(log(i, x))")]
65+
[InlineData("sgn(log(x, 2))")]
66+
public void WhatIsNotRealOnAnIntervalIsLeftUnevaluated(string expression)
67+
=> Assert.IsType<Derivativef>(expression.ToEntity().Differentiate("x"));
68+
69+
[Theory(Timeout = 30000)]
70+
[InlineData("derivative(sgn(ln(x)), x)")]
71+
[InlineData("derivative(abs(ln(x)), x)")]
72+
[InlineData("derivative(sgn(ln(x)) * x, x, 2)")]
73+
public void TheDerivativeNodeOfSuchASignTerminates(string expression)
74+
{
75+
var entity = expression.ToEntity();
76+
Assert.NotNull(entity.Simplify());
77+
Assert.NotNull(entity.Evaled);
78+
}
79+
80+
[Theory]
81+
[InlineData("sqrt(ln(a*x^n)^2)/x")]
82+
[InlineData("sqrt(ln(x)^2)/x")]
83+
[InlineData("abs(ln(a*x))/x")]
84+
public void AnAntiderivativeCarryingSuchASignDifferentiatesBack(string integrand)
85+
{
86+
var integral = integrand.ToEntity().Integrate("x");
87+
Assert.DoesNotContain("integral(", integral.Stringize());
88+
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("n", 2.3);
89+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
90+
var original = Pinned(integrand.ToEntity());
91+
var compared = 0;
92+
foreach (var at in new[] { 0.11, 0.31, 0.57, 0.83, 1.29, 1.77, 2.41, 5.7 })
93+
{
94+
var want = original.Substitute("x", at).EvalNumerical();
95+
var got = derivative.Substitute("x", at).EvalNumerical();
96+
compared++;
97+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
98+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
99+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
100+
}
101+
Assert.Equal(8, compared);
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)