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
19 changes: 19 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,25 @@ A secant above the bar is still declined: `sec(x)^2 e^(-3 i x)` is rational in `
imaginary rate, which nothing answers yet. Rubi's 4.7.2: 237 to 249 of 290, no row lost, 16
timeouts to 14.

### A power of a constant multiple of the radicand `arcosh` holds is written over that radicand

`arcosh(a x)^2/sqrt(1 - a^2 x^2)` and `x (a + b arcosh(c x))/(d - c^2 d x^2)^3` ran out of time.
`arcosh(c x)` is `ln(c x + sqrt(c^2 x^2 - 1))`, and the substitution `u = arcosh(c x)` wants the
root its derivative carries, `sqrt(c^2 x^2 - 1)`; beside it the integrand holds a constant multiple
of that radicand, `d - c^2 d x^2 = -d (c^2 x^2 - 1)`, which nothing read as one. A whole power of
the multiple is now the product of the powers, and a root `(lambda M)^(k/2)` is `K^k M^(k/2)` with
`K = sqrt(lambda M)/sqrt(M)` -- plus or minus `i`, or `sqrt(lambda)`, constant on every interval
where it is defined -- which stands in front of the answer the way Rubi writes it. The answer
is checked where the integrand is real as well as where it is not: `arcosh(2 x)^2/sqrt(1 - 4 x^2)`
is real on `(-1/2, 1/2)`, where `arcosh(2x)` is `i arccos(2x)`.

| Input | Was (2.5.0) | Now |
|---|---|---|
| `"arcosh(a*x)^2/sqrt(1-a^2*x^2)".Integrate("x")` | left unevaluated | `arcosh(a x)^3/(3a)` over `sqrt(1 - a^2 x^2)/sqrt(a^2 x^2 - 1)` |
| `"x*(a+b*arcosh(c*x))/(d-c^2*d*x^2)^3".Integrate("x")` | left unevaluated | in `arcosh(c x)` and powers of `c^2 x^2 - 1` |

Rubi's 7.2.4 and 7.2.5, all 277 problems that count: 127 to 255, no row lost, 121 timeouts to 18.

### `binomial(n, k)` is a function

**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14954,6 +14954,171 @@ static bool Through(Entity node, Entity.Variable x)
}
}

/// <summary>
/// A power of a constant multiple of a radicand that a logarithm holds under a root,
/// written over that radicand: <c>acosh(c x)</c> is <c>ln(c x + sqrt(c^2 x^2 - 1))</c>,
/// and beside it <c>d - c^2 d x^2</c> is <c>-d (c^2 x^2 - 1)</c>. A whole power is the
/// product of the powers; a root <c>(lambda M)^(k/2)</c> is <c>K^k M^(k/2)</c> with
/// <c>K = sqrt(lambda M)/sqrt(M)</c>, which is constant wherever it is defined -- plus or
/// minus <c>i</c>, or plus or minus <c>sqrt(lambda)</c> -- so it stands in front of the
/// answer the way Rubi writes it.
/// </summary>
/// <remarks>
/// The substitution <c>u = acosh(c x)</c> needs the root its derivative carries,
/// <c>sqrt(c^2 x^2 - 1)</c>; written over <c>1 - c^2 x^2</c> the integrand is the same
/// function times <c>i</c> and nothing reads it: <c>acosh(a x)^2/sqrt(1 - a^2 x^2)</c> ran
/// out of time where <c>acosh(a x)^2/sqrt(a^2 x^2 - 1)</c> takes a third of a second, and
/// <c>x (a + b acosh(c x))/(d - c^2 d x^2)^3</c> -- a whole power, no root at all -- the same.
/// <c>K</c> is carried through the integration as a symbol: it is constant, so an
/// antiderivative in it is one in <c>x</c> once it is written out, whatever the branches.
/// https://github.com/asc-community/AngouriMath/issues/718
/// </remarks>
internal static Entity? SolveByWritingAConstantMultipleOfARadicandOverIt(Entity expr, Entity.Variable x, bool integrateByParts)
{
// At the question asked only. The inverse cosine is in the integrand as asked, and a
// round of parts differentiates it away, so what is asked below holds no logarithm
// this rule needs -- but the substitutions below write logarithms of their own, and
// there the rule found a genuine multiple, rewrote it and asked a whole search again
// that was going to decline anyway: `atan(c x/sqrt(a - c^2 x^2))^2/sqrt(d - c^2 d x^2/a)`,
// with no logarithm in it as asked, took thirty-one seconds to decline where it took
// seven, twelve of them in each of two such re-asks.
if (!Integration.AnsweringTheQuestionAsked)
return null;
// The radicands a logarithm holds under a square root.
var references = new List<Entity>();
foreach (var logarithm in expr.Nodes.OfType<Logf>())
foreach (var node in logarithm.Nodes)
if (node is Powf(var radicand, Number.Rational { ERational: var half }) && half.Denominator.Equals(EInteger.FromInt32(2))
&& radicand.ContainsNode(x) && TreeAnalyzer.TryGetPolynomial(radicand, x, out var read) && read.Keys.Max() >= EInteger.One
&& !references.Contains(radicand))
references.Add(radicand);
if (references.Count == 0)
return null;
var multiple = Variable.CreateUnique(expr, "k_rad");
Entity? written = null, root = null, reference = null;
var rewritten = expr.Replace(node =>
{
if (node is not Powf(var @base, Number.Rational exponent) || !@base.ContainsNode(x) || references.Contains(@base))
return node;
foreach (var candidate in references)
{
if (TryReadAsAConstantMultiple(@base, candidate, x) is not { } lambda)
continue;
if (exponent is Number.Integer)
return MathS.Pow(lambda, exponent) * MathS.Pow(candidate, exponent);
if (!exponent.ERational.Denominator.Equals(EInteger.FromInt32(2)))
return node;
// Only one such root per integrand: K is `sqrt(base)/sqrt(candidate)`, and two
// different bases would each want their own.
if (written is not null && (written != @base || reference != candidate))
return node;
(written, reference) = (@base, candidate);
var numeratorOfExponent = Number.Integer.Create(exponent.ERational.Numerator);
return MathS.Pow(multiple, numeratorOfExponent) * MathS.Pow(candidate, exponent);
}
return node;
});
if (rewritten == expr)
return null;
var simplified = rewritten.InnerSimplified;
if (simplified == expr || simplified == expr.InnerSimplified)
return null;
if (Integration.ComputeAsTheSameQuestion(simplified, x, integrateByParts) is not { } answer)
return null;
if (written is not null && reference is not null)
{
root = MathS.Sqrt(written) / MathS.Sqrt(reference);
answer = answer.Substitute(multiple, root);
}
return answer.ContainsNode(multiple) ? null : answer;
}

/// <summary>
/// Whether <paramref name="expr"/>/<paramref name="reference"/> takes the same value at two
/// points, every other symbol pinned to a fixed value -- a necessary condition for a
/// constant ratio, and a cheap one. Where either side cannot be evaluated the question is
/// left to the symbolic check.
/// </summary>
private static bool HasTheSameRatioAtTwoPoints(Entity expr, Entity reference, Entity.Variable x)
{
var pinned = new Dictionary<Variable, Entity>();
var values = new[] { 1.37, 0.61, 2.23, 1.91, 0.83, 1.13 };
var index = 0;
foreach (var symbol in expr.Vars.Concat(reference.Vars).Distinct())
if (symbol != x)
pinned[symbol] = values[index++ % values.Length];
Number.Complex? RatioAt(double at)
{
var top = expr.Substitute(x, at);
var bottom = reference.Substitute(x, at);
foreach (var pair in pinned)
{
top = top.Substitute(pair.Key, pair.Value);
bottom = bottom.Substitute(pair.Key, pair.Value);
}
try
{
return top.EvalNumerical() is Number.Complex a && bottom.EvalNumerical() is Number.Complex b && !b.IsZero
? (Number.Complex)(a / b) : null;
}
catch (AngouriMath.Core.Exceptions.CannotEvalException) { return null; }
}
if (RatioAt(0.37) is not { } first || RatioAt(1.71) is not { } second)
return true;
var difference = (first - second).Abs() as Number.Real;
var scale = System.Math.Max(1.0, (double)((Number.Real)first.Abs()).EDecimal.ToDouble());
return difference is null || (double)difference.EDecimal.ToDouble() <= 1e-9 * scale;
}

/// <summary>
/// <paramref name="expr"/> as a constant times <paramref name="reference"/>, both
/// polynomials in <paramref name="x"/> of the same degree: the ratio of the leading
/// coefficients, where every other coefficient agrees with it once simplified.
/// </summary>
private static Entity? TryReadAsAConstantMultiple(Entity expr, Entity reference, Entity.Variable x)
{
// Read as written, or simplified first: the normalisation makes a power of a
// polynomial with a symbolic leading coefficient monic and writes the constant term as
// `d^0 (-1)^(-1) c^(-2)`, which the polynomial reader does not take -- and that is the
// spelling `(d - c^2 d x^2)^3` below the bar arrives in. Bare, because `d^0` simplifies
// to `1 provided not d = 0`, and a condition is not a polynomial either.
if (!TreeAnalyzer.TryGetPolynomial(expr, x, out var mine)
&& !TreeAnalyzer.TryGetPolynomial(Functions.PartialFractions.Bare(expr.InnerSimplified), x, out mine))
return null;
if (!TreeAnalyzer.TryGetPolynomial(reference, x, out var theirs)
|| mine.Count != theirs.Count || mine.Values.Any(c => c.ContainsNode(x)) || theirs.Values.Any(c => c.ContainsNode(x)))
return null;
var top = theirs.Keys.Max()!;
if (!mine.TryGetValue(top, out var leading) || theirs[top].Evaled is Number.Complex { IsZero: true })
return null;
// A constant multiple has the same ratio everywhere, so two points settle most pairs
// before anything is simplified: the check below simplifies symbolic differences, and
// asked of every power in a by-parts remainder of `atan(c x/sqrt(a - c^2 x^2))^2/
// sqrt(d - c^2 d x^2/a)` -- two hundred of them -- it took twenty-five seconds.
if (!HasTheSameRatioAtTwoPoints(expr, reference, x))
return null;
// Simplified where it holds symbols: the ratio of the leading coefficients of
// `d - c^2 d x^2` and `c^2 x^2 - 1` is `-c^2 d/c^2` to the inner simplification, and
// written into a whole power that way it cost the integrand its time -- `-d` is
// what the rules below want to see.
var lambda = (leading / theirs[top]).InnerSimplified;
if (lambda.Vars.Any())
lambda = Functions.PartialFractions.Bare(lambda.Simplify());
if (lambda.Evaled is Number.Complex { IsZero: true } || lambda == Number.Integer.One)
return null;
foreach (var pair in theirs)
{
if (!mine.TryGetValue(pair.Key, out var own))
return null;
var difference = (own - lambda * pair.Value).InnerSimplified;
if (difference.Evaled is Number.Complex { IsZero: true })
continue;
if (!difference.Vars.Any() || Functions.PartialFractions.Bare(difference.Simplify()).Evaled is not Number.Complex { IsZero: true })
return null;
}
return lambda;
}

/// <summary>
/// A fractional or symbolic power of a monomial, distributed: <c>(c x^n)^b</c> is
/// <c>c^b x^(n b)</c> for a positive <c>c</c> and a <c>n</c> that is not whole.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
// And a fractional or symbolic power of a monomial: `(c x^n)^b` is `c^b x^(n b)`
// for a positive `c`, on the `x > 0` where a symbolic `n` leaves the integrand real.
if ((answer = IndefiniteIntegralSolver.SolveByDistributingAPowerOfAMonomial(expr, x, integrateByParts)) is { }) return answer;
// A power of a constant multiple of the radicand a logarithm holds under a root,
// written over that radicand: `acosh(c x)` beside `(d - c^2 d x^2)^(5/2)`.
if ((answer = IndefiniteIntegralSolver.SolveByWritingAConstantMultipleOfARadicandOverIt(expr, x, integrateByParts)) is { }) return answer;
// A power of the variable times a sine or cosine of a logarithm, where two rounds of
// parts close on the integrand: `int x^m sin(a + b ln(c x^n))` in closed form.
if ((answer = IndefiniteIntegralSolver.SolveAPowerTimesATrigonometricOfALogarithm(expr, x, integrateByParts)) is { }) return answer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
Expand Down Expand Up @@ -79,6 +79,24 @@ private static void DifferentiatesBack(string integrand, double[] points)
[InlineData("artanh(2*x)/(1 - 4*x^2)^(3/2)")]
public void TheTangent(string integrand) => DifferentiatesBack(integrand, InsideTheHalf);

/// <summary>
/// A power of a constant multiple of the radicand the inverse cosine holds, written over
/// that radicand: <c>arcosh(c x)</c> is <c>ln(c x + sqrt(c^2 x^2 - 1))</c>, and
/// <c>(d - c^2 d x^2)^(k/2)</c> beside it is <c>K^k (c^2 x^2 - 1)^(k/2)</c> for
/// <c>K = sqrt(d - c^2 d x^2)/sqrt(c^2 x^2 - 1)</c>, which is constant on each interval
/// where it is defined and stands in front of the answer; a whole power is the product
/// of the powers. These are answered now, not only right when answered: the first is
/// real on <c>(-1/2, 1/2)</c>, where <c>arcosh(2x)</c> is <c>i arccos(2x)</c> and its
/// square real, and is checked there. Rubi's 7.2.4 and 7.2.5, 130 rows.
/// </summary>
[Theory]
[InlineData("arcosh(2*x)^2/sqrt(1 - 4*x^2)", new[] { -0.45, -0.2, 0.1, 0.3, 0.45 })]
[InlineData("x*(5 - 20*x^2)^(3/2)*(3 + 2*arcosh(2*x))", new[] { -0.4, 0.1, 0.3, 0.7, 1.2 })]
[InlineData("(3 + 2*arcosh(2*x))/(5 - 20*x^2)^(5/2)", new[] { -0.4, 0.1, 0.3, 0.7, 1.2 })]
[InlineData("x*(3 + 2*arcosh(2*x))/(5 - 20*x^2)^3", new[] { -0.4, 0.1, 0.3, 0.7, 1.2 })]
public void AConstantMultipleOfTheRadicandIsWrittenOverIt(string integrand, double[] points)
=> DifferentiatesBack(integrand, points);

/// <summary>
/// A root of a *negative* multiple of the quadratic is not the multiple's root times
/// the quadratic's: <c>arcosh(a x)^2/sqrt(1 - a^2 x^2)</c> came back with <c>i a</c>
Expand Down
Loading