sábado, 29 de noviembre de 2025

Calculus: A Little Story of Unification

One man’s trick may be another man’s method, and there may be more to an apparent trick than first meets the eye. 
— Todd Trimble

In my somewhat crazy obsession with proving/deriving or generalizing everything from the half–angle formulas, one day I sat down to try to generalize my own generalization of the Newton–Mollweide formula. In the process, I found an expression that factored neatly in terms of sines and cosines of half-angles (aha!). Playing with that expression I derived an unusual trigonometric formula for the roots of quadratic polynomials. In a few days I realized that this formula was just one member of a whole family of trigonometric formulas for the roots of quadratic polynomials (very wild formulas, by the way).

If we have several formulas that all produce the same two roots, then we can combine them to generate expressions that are numerically identical, right? That is how Theorems 1–2 in this draft arose. From these two theorems I was able to derive five transformations that achieve:

Unification. They unify the use of complex exponentials with half-angle tangents substitutions, as well as hyperbolic parametrizations and Euler substitutions (1 and 2; the third seems to have been added by someone other than Euler). In Section 6 I show how Euler substitutions 1 and 2 are recovered (up to trivial reparametrizations) by Transformations 2 and 5, respectively.

Automatic sign handling. You do not have to worry about signs depending on the domain, since the branch-wise back-substitution formula automatically takes care of them for you.

Usefulness for CAS. They allow one to solve integrals built from
$$\tan\!\left(\tfrac12\sec^{-1}/\csc^{-1}(\dots)\right),$$
without much difficulty (most CAS systems fail here). Please have a look at the results of this benchmark against Mathematica (MMA). Using a branch-wise back-substitution, the USM (that is what I call this method) beat MMA in speed in $67/100$ cases with an average speed-up of $\times 34$. It produced only $5$ monstrous antiderivatives versus $24$ from MMA: the maximum byte count of USM was $21{,}616$ versus $150{,}360$ for MMA. In another mini-benchmark (Example 19), the byte count of MMA was above $600{,}000$ (the antiderivative takes $21$ pages!) while for USM it did not exceed $5000$ (and the antiderivative fits in half a page).

As an illustration, consider the following integral (Example 5 in the draft):

$$\int \sqrt{\frac{x+1}{x+3}}\,dx \qquad (x \ge -1).$$

Notice that

$$\frac{x+1}{x+3} = \frac{x + b - a}{x + b + a}$$

with $a = 1$, $b = 2$. Apply Transform 2 (upper sign for $x \ge -1$):

$$\sqrt{\frac{x+1}{x+3}} = \frac{1 - t}{1 + t}, \qquad dx = \frac{t^2 - 1}{2t^2}\,dt, \qquad t = x + 2 - \sqrt{x^2 + 4x + 3}.$$

Thus

$$\int \sqrt{\frac{x+1}{x+3}}\,dx = \int \frac{1 - t}{1 + t} \cdot \frac{t^2 - 1}{2t^2}\,dt = -\frac12 \int \Bigl(1 - 2t^{-1} + t^{-2}\Bigr)\,dt = \ln|t| + \frac12\bigl(t^{-1} - t\bigr) + C,$$

hence

$$\int \sqrt{\frac{x+1}{x+3}}\,dx = \ln\!\bigl(x+2-\sqrt{x^2+4x+3}\bigr) + \sqrt{x^2+4x+3} + C.$$

It is instructive to contrast the algebraic economy of USM with standard approaches for this integrand. The classical rationalization $u = \sqrt{\frac{x+1}{x+3}}$ yields the rational form $\int \frac{4u^2}{(u^2-1)^2}\,du$, which typically necessitates a rather cumbersome partial fraction decomposition. Trying to bypass this with a second substitution introduces its own friction: The hyperbolic choice $u = \coth z$ leads to a fairly manageable integration of $\cosh^2 z$, but the back-substitution is algebraically tedious, requiring double-angle expansions and inverse hyperbolic identities to revert to $(x)$. The trigonometric choice $u = \sec \theta$ leads to the laborious integral $\int \csc^3 \theta\,d\theta$, which usually involves recursive integration by parts or a reduction formula that almost nobody remembers. Crucially, both traditional paths impose a distinct second layer of substitution ($x \to u \to z$ or $\theta$), whereas USM Transform 2 structurally cancels the denominator in a single step, collapsing the integrand immediately to the elementary expression $1 - 2t^{-1} + t^{-2}$.

Relation to $y = \frac12\left(t + t^{-1}\right)$ and $t = x \pm \frac1x$

Now observe that setting

$$y = \frac12\left(t + t^{-1}\right)$$

has an effect equivalent to what we did previously using USM.

The starting integral is

$$\int \sqrt{\frac{x+1}{x+3}}\,dx.$$

In the general setup, with parameters $a > 0$ and real $(b)$, the normalized variable is defined by $y = \frac{x + b}{a}$. For this specific example we have $a = 1$ and $b = 2$, so the normalization is simply $y = x + 2$. If we now apply the substitution $y = \frac12\left(t + \frac1t\right)$, we obtain the same rational form immediately:

$$\begin{aligned} y &= \frac{t^2 + 1}{2t} \quad\implies\quad dx = dy = \frac12\left(1 - \frac1{t^2}\right)\,dt = \frac{t^2 - 1}{2t^2}\,dt, \\[10pt] \sqrt{\frac{x+1}{x+3}} &= \sqrt{\frac{y-1}{y+1}} = \sqrt{\frac{\frac{t^2 - 2t + 1}{2t}} {\frac{t^2 + 2t + 1}{2t}}} = \sqrt{\frac{(t-1)^2}{(t+1)^2}} = \frac{|t-1|}{t+1} = \frac{1 - t}{1 + t} \quad\text{(for } x \ge -1\text{)}, \\[10pt] \int \sqrt{\frac{x+1}{x+3}}\,dx &= \int \underbrace{\frac{1 - t}{1 + t}}_{\text{Radical}} \cdot \underbrace{\frac{t^2 - 1}{2t^2}\,dt}_{\text{Jacobian}}. \end{aligned}$$

More generally, for expressions of the form

$$\sqrt{\frac{x + b - a}{x + b + a}},$$

defining $y = \dfrac{x+b}{a}$ gives

$$\frac{x + b - a}{x + b + a} = \frac{ay - a}{ay + a} = \frac{y-1}{y+1},$$

so the same pattern repeats in the general case. Solving integrands of the type $\sqrt{\frac{x+p}{x+q}}$, where $p$ and $q$ are real numbers, via the substitution $y = \frac12\left(t + \frac1t\right)$ is quite unusual (to my surprise). In the Math StackExchange community, you can find several threads (see here and here for examples) where integrators take significantly more convoluted routes for such integrals, rarely using this substitution.

The natural question is: What is the relation between the substitution $y = \tfrac12(t + t^{-1})$ and the substitution $t = x \pm \frac1x$ (which integrators often use (see here) when dealing with pseudo-elliptic integrals such as $\int \frac{x^2-1}{(x^2+1)\sqrt{x^4+1}}\,dx$)?

Relation between $y = \tfrac12(t + t^{-1})$ and $t = x \pm \tfrac1x$

Both substitutions are, in essence, two presentations of the same underlying rational transformation, just written with different variable names and possibly rescaled.

Begin with

$$y = \frac12\left(t + \frac1t\right).$$

Multiply by $(2t)$:

$$2yt = t^2 + 1 \quad\Longrightarrow\quad t^2 - 2yt + 1 = 0.$$

Seeing this as a quadratic in $(t)$, we get

$$t = \frac{2y \pm \sqrt{(2y)^2 - 4}}{2} = y \pm \sqrt{y^2 - 1}.$$

So the inverse of our substitution is

$$t = y \pm \sqrt{y^2 - 1}.$$

Now consider the substitution commonly used for pseudo-elliptic integrals:

$$t = x \pm \frac1x.$$

Take, for concreteness, the plus sign:

$$t = x + \frac1x.$$

Multiply both sides by $(x)$:

$$tx = x^2 + 1 \quad\Longrightarrow\quad x^2 - tx + 1 = 0.$$

Viewed as a quadratic in $(x)$, we obtain

$$x = \frac{t \pm \sqrt{t^2 - 4}}{2}.$$

Now compare this with the inverse of your substitution $y = \frac12(t + t^{-1})$, namely

$$t = y \pm \sqrt{y^2 - 1}.$$

If we perform a simple rescaling

$$y = \frac{t}{2},$$

then

$$x = \frac{t \pm \sqrt{t^2 - 4}}{2} = \frac{2y \pm \sqrt{4(y^2 - 1)}}{2} = y \pm \sqrt{y^2 - 1}.$$

But this last expression is exactly the same functional form as the inverse of our substitution. The only difference is which symbol we call the “input” and which we call the “output,” plus that harmless factor of $2$.

So, up to the linear rescaling $(y = t/2)$ and a relabeling of variables, the equations

$$y = \frac12\left(t + \frac1t\right) \quad\text{and}\quad t = x + \frac1x$$

describe the same algebraic relation between two variables and its inverse.

In particular:

Our substitution uses

$$t \longmapsto y = \frac12\left(t + \frac1t\right).$$

The pseudo-elliptic substitution can be seen as

$$x \longmapsto t = x + \frac1x,$$

and when you solve for $x$ in terms of $t$, you get the same square-root structure as when you solve for $t$ in terms of $y$.

We can construct our own pseudo-elliptic integrals. Assume $t>0$ for simplicity. Notice that 

$$\int \sqrt{x-\sqrt{x^2-1}}\,dx \overset{x=t-\frac1t}{=} \int \frac{t^2+1}{\sqrt{t^5-t^3+t^3\sqrt{t^4-3t^2+1}}} \, dt$$

We can return to the original integrand by doing

$$\int \frac{t^2+1}{\sqrt{t^5-t^3+t^3\sqrt{t^4-3t^2+1}}} \, dt \overset{x=t-\frac1t}{=}  \int \sqrt{x-\sqrt{x^2-1}}\,dx.$$

Similarly,

$$ \int \frac{1}{x\sqrt{x^{2}-2}}\,dx\overset{x = t+\frac1t}{=} \int \frac{t^{2}-1}{(t^{2}+1)\sqrt{t^{4}+1}}\,dt$$

or

$$\int \sqrt{\frac{x+b-a}{x+b+a}}\,dx\;\overset{x=\frac12\left(t-\frac1t\right)}{=}\;\int \sqrt{\frac{t^2 - 1 + 2t(b-a)}{t^2 - 1 + 2t(b+a)}} \cdot \frac{t^2+1}{2t^2}\,dt.$$

Finally, I opened this story with a comment from Todd Trimble that I found on Todd and Vishal's blog: “...One man’s trick may be another man’s method, and there may be more to an apparent trick than first meets the eye.” Let me be so bold as to claim that the method Todd is talking about is very likely USM, and that man is me. Thank you for reading.

lunes, 24 de noviembre de 2025

Benchmarking USM Transform #3 vs. Mathematica’s Integrate - Part 2

In these tables we benchmark the Unified Substitution Method (USM) change of variables that this arXiv draft calls Transformation 3 (this is the “Transform 1” used in the Mathematica code: see this notebook): a half-angle substitution that converts integrals built from tan(½ csc⁻¹((x+b)/a)) and tan(½ sec⁻¹((x+b)/a)) into a rational integrand in a new variable t. The general transformation formula is

$$\int f\!\left[x,\,\tan\left(\tfrac12\csc^{-1}\left(\frac{x+b}{a}\right)\right),\,
\tan\left(\tfrac12\sec^{-1}\left(\frac{x+b}{a}\right)\right)\right]\,dx=
\int f\!\left(a\,\frac{t^{2}+1}{2t} - b,\, t,\, \frac{1-t}{1+t}
\right)\, a\,\frac{t^{2}-1}{2t^{2}}\,dt.\tag{1}$$


For each integrand in the 10 datasets, we compare Transform 1 + back-substitution against Mathematica’s Integrate by timing both methods (total t-USM time, split into the y≥1 and y≤−1 branches, versus Integrate time) and by measuring the structural size of the resulting antiderivatives (LeafCnt and ByteCnt for USM and for Integrate). The integrands are systematically built from tan(½ arccsc(…)) and tan(½ arcsec(…)), with powers, factors of x, x², x³, and rational combinations such as 1/(1+tan²(…)) and products of the two half-angle tangents, so the benchmark probes exactly the niche Transform 1 is designed for, from simple to highly intricate cases. Overall, the data show that on many of the “hard” mixed cases USM is much faster (often by an order of magnitude) while producing antiderivatives of comparable or smaller complexity; on very simple, pattern-friendly integrands Integrate can be faster because USM has a fixed overhead; and across the full test family USM timings are more predictable, with the y≥1 / y≤−1 branch split adding only modest extra cost. In short, Transform 1 is a robust, domain-specific integrator for these arccsc/arcsec tan-half-angle families: it typically yields simpler or similar antiderivatives and large speedups on difficult examples, at the price of some overhead on the easy ones. 

Conclusions from the benchmark

1. On many “hard” mixed cases, Transform 1 is much faster than Integrate.

In datasets like 3, 5, 7, and 8 (the ones with products and rational combinations of both arccsc and arcsec half-angle tangents), Integrate often takes from a few tenths of a second up to several seconds, while t-USM usually stays in the tens to low hundreds of milliseconds.

In some individual examples we get around one order of magnitude speedup: Integrate is in the 1–10 second range while t-USM is still below about 0.2 seconds.

⇒ For structurally complicated expressions in this class, the Transform 1 route is clearly advantageous.

2. On simple or pattern-friendly cases, Integrate can be faster than Transform 1.

In datasets like 1, 4, 9, and 10, several examples have Integrate times of just a few milliseconds, while t-USM has a relatively fixed overhead (often between about 0.02 and 0.06 seconds).

In those cases, Mathematica recognizes a very simple pattern (such as standard tan or rational trig identities) and wins on raw speed.

⇒ Our transform has a nontrivial constant overhead. It shines when the problem is hard for Integrate, but cannot beat Mathematica’s near-instant pattern match on the easy ones.

3. Runtime variability vs. predictability

Integrate is highly variable: sometimes extremely fast, sometimes very slow, even for similar-looking integrands in the same dataset.

t-USM is more stable: most examples sit in a narrow time band, with far fewer extreme slowdowns.

⇒ Transform 1 gives more predictable performance over this whole integrand family, whereas Integrate is opportunistically very fast but with occasional expensive spikes.

4. Result size and complexity stay comparable and reasonable.

The leaf and byte counts show that:

USM antiderivatives are usually similar or somewhat larger in size compared to Integrate’s results, reflecting the mechanical tan-substitution and back-substitution.

There is no systematic explosion in size: the USM expressions stay in the same general range as the ones produced by Integrate.

⇒ From a “how big and messy is the final formula?” standpoint, Transform 1 is competitive and practical, even if it does not always find the most compact form that Integrate sometimes can.

5. The y >= 1 / y <= -1 split is reasonable and not a major cost.

The USM y>=1 and USM y<=-1 times are usually of the same order, with the y >= 1 branch often a bit slower but not dramatically.

Summing them to get t–USM total time roughly doubles the branch time, but that combined cost is still modest compared with the multi-second peaks seen in Integrate.

⇒ The branch-based back-substitution strategy works well in practice and does not dominate the runtime.

Some more specific details
  • USM total time was faster than Integrate in 82 cases.
  • USM produced a simpler antiderivative (smaller ByteCnt) than Integrate in exactly 50 cases.
  • “Monster” antiderivatives (ByteCnt >= 10,000) occurred 5 times for USM and 24 times for Integrate.
  • The largest ByteCnt observed for a USM antiderivative was 19,840, compared with 150,360 for Integrate.