Skip to content

Commit b13dbf4

Browse files
An exponential times a trigonometric at the same frequency no longer divides by zero, and a phase is expanded (#1472)
`x e^(-i x) cos(x)` was answered NaN at every point of its domain. The closed form `int e^(ax)(c cos(bx) + d sin(bx))` divides by `a^2 + b^2`, and both places that use it guard the resonance `a = ±i b` -- where the product is a sum of two exponentials -- by asking whether `a^2 + b^2` *evaluates* to a number. With a symbolic frequency it does not: `(-i f)^2 + f^2` is `-f^2 + f^2`, which `InnerSimplified` does not collect. The guard passed, the formula divided by a zero it could not see, and the answer was NaN -- a wrong answer, not a decline. Both now test the resonance symbolically. On that footing, two capabilities. A phase in the trigonometric's argument is expanded by the angle-sum identity where an exponential stands beside it, since the closed rule reads one frequency and no phase -- an exponential's own offset is a constant factor, a trigonometric's is not. And `A + i A tan(z)` below the bar is written as `A e^(i z)/cos(z)`, `A + i A cot(z)` as `i A e^(-i z)/sin(z)`, exactly, since `cos z + i sin z` is `e^(i z)`: beside a polynomial that is the shape the closed rule answers, where the imaginary unit in the coefficient is read by nothing else. Below the bar only -- above it the tangent's own rules answer in the tangent and more shortly, and the rewrite made one such row two and a half times slower before it was restricted. Family 4 of the Rubi suite: 284 -> 286 of 422, 0 wrong, and eight rows that produced nothing now produce answers; family 6 377/417 and the 1774-problem suite 1707 unchanged. Suite 12641 passed; allocation gate passed on all 19 gated benchmarks. Every new row verified as a complex function, the answer's derivative against the integrand at five points. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 3b9036e commit b13dbf4

5 files changed

Lines changed: 234 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,33 @@ quotients that nothing below reads. Rubi's 6.3.2 and 6.5.3
11311131
| `"sech(a+2*ln(c/x^(1/2)))^3".Integrate("x")` | left unevaluated | the antiderivative |
11321132
| `"x*tanh(a+2*ln(x))^2".Integrate("x")` | left unevaluated | the antiderivative |
11331133

1134+
### An exponential times a trigonometric at the same frequency no longer divides by zero, and a phase is expanded
1135+
1136+
`x e^(-i x) cos(x)` was answered `NaN` at every point of its domain. The closed form
1137+
`int e^(a x)(c cos(b x) + d sin(b x)) = e^(a x)(...)/(a^2 + b^2)` is guarded against the resonance
1138+
`a = ±i b`, where the product is a sum of two exponentials and the formula divides by zero -- but
1139+
the guard tested whether `a^2 + b^2` *evaluates* to a number, and with a symbolic frequency
1140+
`(-i f)^2 + f^2` is `-f^2 + f^2`, which `InnerSimplified` does not collect. So the guard passed,
1141+
the formula divided by a zero it could not see, and the answer was `NaN`: a wrong answer, not a
1142+
decline. Both places that test it -- the table pattern and the polynomial route -- see the
1143+
resonance symbolically now.
1144+
1145+
With that fixed, two capabilities: a **phase** in the trigonometric's argument is expanded by the
1146+
angle-sum identity where an exponential stands beside it (`cos(f x + p)` is
1147+
`cos(p) cos(f x) - sin(p) sin(f x)`; the rule reads one frequency and no phase, an exponential's
1148+
own offset being a constant factor where a trigonometric's is not), and **`A + i A tan(z)` below
1149+
the bar** is written as `A e^(i z)/cos(z)`, with `A + i A cot(z)` as `i A e^(-i z)/sin(z)` --
1150+
exactly, since `cos z + i sin z` is `e^(i z)`. Beside a polynomial that is the shape the closed
1151+
rule answers, where the imaginary unit in the coefficient is read by nothing else. Rubi's 4.3.10,
1152+
4.4.1 and 4.4.10 ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
1153+
1154+
| Input | Was (2.5.0) | Now |
1155+
|---|---|---|
1156+
| `"x*e^(-i*x)*cos(x)".Integrate("x")` | left unevaluated | `x^2/4 + e^(-2 i x)(1 + 2 i x)/8` up to the form -- where `master` between the releases answered `NaN` |
1157+
| `"x*e^(2*x)*cos(3*x+1)".Integrate("x")` | left unevaluated | the antiderivative |
1158+
| `"x/(2+2*i*tan(x))".Integrate("x")` | left unevaluated | the antiderivative |
1159+
| `"(c+d*x)/(a+i*a*tan(pe+f*x))".Integrate("x")` | left unevaluated | the antiderivative |
1160+
11341161
### `binomial(n, k)` is a function
11351162

11361163
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3359,7 +3359,10 @@ void Add(int k, bool isSine, Entity coefficient)
33593359
var exponential = MathS.Pow(MathS.e, rate * x);
33603360
var cosine = MathS.Cos(frequency * x);
33613361
var sine = MathS.Sin(frequency * x);
3362-
if (scale == 0 || scale.Evaled is Number.Complex { IsZero: true })
3362+
// Symbolically as well as numerically: with a symbolic frequency `f` the rate `-i f`
3363+
// leaves `-f^2 + f^2`, which `InnerSimplified` does not collect.
3364+
if (scale == 0 || scale.Evaled is Number.Complex { IsZero: true }
3365+
|| scale.Vars.Any() && Functions.PartialFractions.Bare(scale.Simplify()).Evaled is Number.Complex { IsZero: true })
33633366
{
33643367
// Resonant: by Euler, `c cos(bx) + d sin(bx)` is
33653368
// `(c - i d) e^(i b x)/2 + (c + i d) e^(-i b x)/2`, and each term beside
@@ -14479,6 +14482,134 @@ static bool IsAWholeNumberOfSteps(ERational difference)
1447914482
return (inFront * antiderivative - frontSlope * integralOfAntiderivative).InnerSimplified;
1448014483
}
1448114484

14485+
/// <summary>
14486+
/// A sine or cosine of a linear form with a phase, beside an exponential, expanded by the
14487+
/// angle-sum identity so that both are of the same bare multiple of the variable:
14488+
/// <c>cos(f x + p)</c> is <c>cos(p) cos(f x) - sin(p) sin(f x)</c>. The closed rule for a
14489+
/// polynomial times an exponential times a trigonometric reads one frequency and no
14490+
/// phase -- an exponential's own offset is a constant factor, a trigonometric's is not --
14491+
/// and a phase is what every row of Rubi's 4.3.10 and 4.4.10 carries once
14492+
/// <c>A + i A tan(pe + f x)</c> is written as an exponential over a cosine.
14493+
/// </summary>
14494+
/// <remarks>
14495+
/// Only beside an exponential of the variable, which is the shape that rule answers: the
14496+
/// expansion doubles the terms of every sine and cosine it touches, and the sum split
14497+
/// would then answer `sin(a + b x)` as two integrals where one closed rule answers it as
14498+
/// one. Once: the expanded form has no phase left.
14499+
/// https://github.com/asc-community/AngouriMath/issues/718
14500+
/// </remarks>
14501+
internal static Entity? SolveByExpandingATrigonometricPhaseBesideAnExponential(Entity expr, Entity.Variable x, bool integrateByParts)
14502+
{
14503+
if (!Integration.AnsweringTheQuestionAsked)
14504+
return null;
14505+
// An exponential of the variable among the factors, and a phase to expand.
14506+
if (!expr.Nodes.Any(node => node is Powf(var @base, var exponent) && !@base.ContainsNode(x) && exponent.ContainsNode(x)))
14507+
return null;
14508+
var expanded = expr.Replace(node =>
14509+
{
14510+
Entity argument;
14511+
bool isSine;
14512+
switch (node)
14513+
{
14514+
case Sinf(var inner): (argument, isSine) = (inner, true); break;
14515+
case Cosf(var inner): (argument, isSine) = (inner, false); break;
14516+
default: return node;
14517+
}
14518+
if (!argument.ContainsNode(x) || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out var offset)
14519+
|| TreeAnalyzer.IsZero(slope) || offset.ContainsNode(x) || TreeAnalyzer.IsZero(offset)
14520+
|| offset.Evaled is Number.Integer(0))
14521+
return node;
14522+
var bare = (slope * x).InnerSimplified;
14523+
return isSine
14524+
? MathS.Sin(offset) * MathS.Cos(bare) + MathS.Cos(offset) * MathS.Sin(bare)
14525+
: MathS.Cos(offset) * MathS.Cos(bare) - MathS.Sin(offset) * MathS.Sin(bare);
14526+
});
14527+
if (expanded == expr)
14528+
return null;
14529+
return Integration.ComputeAsAQuestionOfItsOwn(expanded.Expand().InnerSimplified, x, integrateByParts);
14530+
}
14531+
14532+
/// <summary>
14533+
/// <c>A + i A tan(z)</c> is <c>A e^(i z)/cos(z)</c>, and <c>A + i A cot(z)</c> is
14534+
/// <c>i A e^(-i z)/sin(z)</c> -- exactly, wherever the tangent is defined, since
14535+
/// <c>cos(z) + i sin(z)</c> is <c>e^(i z)</c>. A power of one of them below the bar is
14536+
/// then a power of the cosine times an exponential, and beside a polynomial that is a
14537+
/// shape the closed rules answer: <c>(c + d x)^2/(a + i a tan(pe + f x))^2</c> is a
14538+
/// search past the budget as written and a second once rewritten.
14539+
/// </summary>
14540+
/// <remarks>
14541+
/// The four signs are the four identities: <c>A - i A tan(z)</c> is
14542+
/// <c>A e^(-i z)/cos(z)</c> and <c>A - i A cot(z)</c> is <c>-i A e^(i z)/sin(z)</c>.
14543+
/// Rubi's 4.3.10 and 4.4.10, where every such row carries the imaginary unit in the
14544+
/// coefficient and nothing else reads it. The same question, not one of its own: what is
14545+
/// handed on is the integrand in another spelling.
14546+
/// https://github.com/asc-community/AngouriMath/issues/718
14547+
/// </remarks>
14548+
internal static Entity? SolveByWritingAnImaginaryTangentAsAnExponential(Entity expr, Entity.Variable x, bool integrateByParts)
14549+
{
14550+
// Below the bar only: `(A + i A tan(z))^3` above it is a polynomial in the tangent,
14551+
// which the rules for those answer in the tangent and more shortly, where the
14552+
// reciprocal is what nothing reads.
14553+
var (above, below) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(expr));
14554+
if (!below.ContainsNode(x))
14555+
return null;
14556+
var rewritten = below.Replace(node =>
14557+
{
14558+
if (node is not Sumf and not Minusf || !node.ContainsNode(x))
14559+
return node;
14560+
Entity constant = Number.Integer.Zero;
14561+
Entity? coefficient = null;
14562+
Entity? argument = null;
14563+
var isTangent = false;
14564+
foreach (var term in Sumf.LinearChildren(node))
14565+
{
14566+
if (!term.ContainsNode(x))
14567+
{
14568+
constant += term;
14569+
continue;
14570+
}
14571+
if (coefficient is not null)
14572+
return node;
14573+
Entity factors = Number.Integer.One;
14574+
foreach (var factor in Mulf.LinearChildren(term))
14575+
switch (factor)
14576+
{
14577+
case Tanf(var inner) when argument is null:
14578+
(argument, isTangent) = (inner, true);
14579+
break;
14580+
case Cotanf(var inner) when argument is null:
14581+
(argument, isTangent) = (inner, false);
14582+
break;
14583+
default:
14584+
if (factor.ContainsNode(x))
14585+
return node;
14586+
factors *= factor;
14587+
break;
14588+
}
14589+
if (argument is null)
14590+
return node;
14591+
coefficient = factors;
14592+
}
14593+
if (coefficient is null || argument is null || constant == Number.Integer.Zero)
14594+
return node;
14595+
// The ratio is the imaginary unit one way or the other, and nothing else.
14596+
// Bare: `i a/a` simplifies to `i provided not a = 0`, and a condition is not a
14597+
// number to compare against.
14598+
var ratio = Functions.PartialFractions.Bare((coefficient / constant).InnerSimplified);
14599+
if (ratio.Evaled is not Number.Complex)
14600+
ratio = Functions.PartialFractions.Bare(ratio.Simplify());
14601+
var plus = ratio.Evaled == MathS.i.Evaled;
14602+
if (!plus && ratio.Evaled != (-MathS.i).Evaled)
14603+
return node;
14604+
var exponential = MathS.Pow(MathS.e, ((plus == isTangent ? MathS.i : -MathS.i) * argument).InnerSimplified);
14605+
// A + i A tan(z) is A e^(iz)/cos(z); A + i A cot(z) is i A e^(-iz)/sin(z).
14606+
return isTangent
14607+
? constant * exponential / MathS.Cos(argument)
14608+
: (plus ? MathS.i : -MathS.i) * constant * exponential / MathS.Sin(argument);
14609+
});
14610+
return rewritten == below ? null : Integration.ComputeAsTheSameQuestion((above / rewritten).InnerSimplified, x, integrateByParts);
14611+
}
14612+
1448214613
/// <summary>
1448314614
/// Bioche's first two rules for the hyperbolic functions: a rational function of
1448414615
/// <c>sinh(y)</c> and <c>cosh(y)</c> that is odd in the hyperbolic sine is a rational

‎Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,19 @@ private static bool IsZeroOnceSimplified(Entity expr)
420420
/// exponential times a sine or cosine divides by: zero for a rate of <c>i</c> times
421421
/// the frequency, where the product is a sum of two exponentials instead.
422422
/// </summary>
423+
/// <remarks>
424+
/// Symbolically as well as numerically. With a symbolic frequency <c>f</c>, the rate
425+
/// <c>-i f</c> -- which is what <c>A + i A tan(f x)</c> leaves once written as
426+
/// <c>A e^(i f x)/cos(f x)</c> -- gives <c>-f^2 + f^2</c>, which
427+
/// <see cref="Entity.InnerSimplified"/> does not collect, so the evaluation is not a
428+
/// number and the guard passed: the answer then divided by a zero it could not see and
429+
/// was <c>NaN</c> at every point.
430+
/// </remarks>
423431
private static bool NotResonant(Entity rate, Entity frequency)
424-
=> (rate * rate + frequency * frequency).InnerSimplified.Evaled is not Entity.Number.Complex { IsZero: true };
432+
{
433+
var scale = (rate * rate + frequency * frequency).InnerSimplified;
434+
return scale.Evaled is not Entity.Number.Complex { IsZero: true } && !IsZeroOnceSimplified(scale);
435+
}
425436

426437
/// <summary>
427438
/// Whether <paramref name="expr"/> is an exponential in <paramref name="x"/>, and

‎Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
501501
// An exponential of a multiple of a logarithm is a power of the argument, which is
502502
// how every inverse hyperbolic function under an exponential arrives.
503503
if ((answer = IndefiniteIntegralSolver.SolveByFoldingAnExponentialOfALogarithm(expr, x, integrateByParts)) is { }) return answer;
504+
// `A + i A tan(z)` is `A e^(i z)/cos(z)`, which beside a polynomial is a shape the
505+
// closed rules answer, where the imaginary unit in the coefficient is read by none.
506+
if ((answer = IndefiniteIntegralSolver.SolveByWritingAnImaginaryTangentAsAnExponential(expr, x, integrateByParts)) is { }) return answer;
504507
// A fractional power of a perfect square is the power of the modulus, sgn(P) P^(2r).
505508
if ((answer = IndefiniteIntegralSolver.SolveByTakingARootOfAPerfectSquare(expr, x, integrateByParts)) is { }) return answer;
506509
// x^(n - 1) g(x^n) with a symbolic n is g(u)/n under u = x^n.
@@ -637,6 +640,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
637640
// rules, because none of them reads the exponential and all of them would have to
638641
// decline it.
639642
if ((answer = IndefiniteIntegralSolver.SolveAPolynomialTimesAnExponentialAndATrigonometric(expr, x)) is { }) return answer;
643+
// And the same shape with a phase in the trigonometric's argument, expanded by the
644+
// angle-sum identity so that the rule above reads one frequency and no phase.
645+
if ((answer = IndefiniteIntegralSolver.SolveByExpandingATrigonometricPhaseBesideAnExponential(expr, x, integrateByParts)) is { }) return answer;
640646
// A half-integer power of `1 + sin` or `1 - cos` and their kin, closed by one
641647
// cancellation that `a^2 = b^2` allows. Before the reductions, which do not read a
642648
// fractional power at all.

‎Sources/Tests/UnitTests/Calculus/PolynomialExponentialTrigTest.cs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,62 @@ public void TheSearchThisAvoidsOpening(string integrand)
205205
return;
206206
DifferentiatesBack(integrand);
207207
}
208+
209+
/// <summary>
210+
/// A phase in the trigonometric's argument, expanded by the angle-sum identity where an
211+
/// exponential stands beside it: the closed rule reads one frequency and no phase, an
212+
/// exponential's own offset being a constant factor where a trigonometric's is not.
213+
/// </summary>
214+
[Theory]
215+
[InlineData("x*e^(2*x)*cos(3*x + 1)")]
216+
[InlineData("x^2*e^x*sin(x + 2)")]
217+
[InlineData("e^(3*x)*cos(2*x + 1/2)*sin(2*x + 1/2)")]
218+
public void APhaseIsExpandedBesideAnExponential(string integrand) => DifferentiatesBack(integrand);
219+
220+
/// <summary>
221+
/// The resonant case, where the rate is the frequency times <c>i</c> and the closed
222+
/// form's <c>a^2 + b^2</c> is zero: the product is a sum of two exponentials instead.
223+
/// With a symbolic frequency <c>-f^2 + f^2</c> is not collected by
224+
/// <see cref="Entity.InnerSimplified"/>, so the guard read it as a nonzero number and the
225+
/// answer divided by it -- <c>NaN</c> at every point, for every row of Rubi's 4.3.10 that
226+
/// carries <c>a + i a tan(pe + f x)</c> below the bar.
227+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
228+
/// </summary>
229+
[Theory]
230+
[InlineData("x*e^(-i*f*x)*cos(f*x)", "f")]
231+
[InlineData("e^(i*f*x)*sin(f*x)", "f")]
232+
[InlineData("x^2*e^(-i*x)*cos(x)", null)]
233+
public void TheResonanceIsSeenSymbolically(string integrand, string? symbol)
234+
{
235+
var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0);
236+
Assert.DoesNotContain("NaN", integral.Stringize());
237+
Assert.DoesNotContain("integral(", integral.Stringize());
238+
Entity Pin(Entity e) => symbol is null ? e : e.Substitute(symbol, 1.3);
239+
var derivative = Pin(integral).Differentiate("x");
240+
var original = Pin(integrand.ToEntity());
241+
foreach (var at in Points)
242+
{
243+
var got = derivative.Substitute("x", at).EvalNumerical();
244+
var want = original.Substitute("x", at).EvalNumerical();
245+
Assert.False(got.IsNaN, $"the antiderivative of {integrand} differentiates to NaN at x = {at}");
246+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
247+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
248+
Assert.True(difference / scale < 1e-9,
249+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
250+
}
251+
}
252+
253+
/// <summary>
254+
/// <c>A + i A tan(z)</c> below the bar is <c>A e^(i z)/cos(z)</c>, and
255+
/// <c>A + i A cot(z)</c> is <c>i A e^(-i z)/sin(z)</c>: beside a polynomial that is the
256+
/// shape above, where the imaginary unit in the coefficient is read by nothing else.
257+
/// Rubi's 4.3.10 and 4.4.10. Above the bar the tangent's own rules answer it, and the
258+
/// rewrite leaves it alone.
259+
/// </summary>
260+
[Theory]
261+
[InlineData("x/(2 + 2*i*tan(x))")]
262+
[InlineData("x^2/(3 - 3*i*cot(x))")]
263+
[InlineData("(1 + 2*x)/(3 + 3*i*tan(1/2 + x))^2")]
264+
public void AnImaginaryTangentBelowTheBarIsAnExponential(string integrand) => DifferentiatesBack(integrand);
208265
}
209266
}

0 commit comments

Comments
 (0)