martes, 19 de agosto de 2025

A generalization of the law of cotangents

Introduction (classical law of cotangents)
In trigonometry, the law of cotangents is a relationship among the side lengths of a triangle and the cotangents of the halves of its angles.
For a triangle with side lengths \(a',b',c'\) opposite the vertices \(A,B,C\) respectively, let
\[s=\frac{a'+b'+c'}{2}\quad\text{and}\quad r=\text{inradius}.\]
If the angles at \(A,B,C\) are \(\alpha',\beta,\gamma\), then
\[\boxed{\;\frac{\cot(\alpha'/2)}{s-a'}=\frac{\cot(\beta/2)}{s-b'}=\frac{\cot(\gamma/2)}{s-c'}=\frac{1}{r}\; }.\]

In this note, we generalize the law of cotangents to cyclic quadrilaterals.

Setup (cyclic quadrilateral)
Let \(ABCD\) be a cyclic quadrilateral with side lengths
\[|AB|=a,\quad |BC|=b,\quad |CD|=c,\quad |DA|=d,\qquad s=\frac{a+b+c+d}{2}.\]
Set \(\alpha=\angle BAD\), \(\beta=\angle ABC\), \(\gamma=\angle BCD\), \(\varphi=\angle CDA\) (see Figure 1). Let \(\Delta\) denote the area of \(ABCD\).

Figure 1. A cyclic quadrilateral $ABCD$.

Lemma (Half–angle formulas). For \(\alpha=\angle BAD\),
\[\boxed{\;\sin^2\frac{\alpha}{2}=\frac{(s-a)(s-d)}{ad+bc},
\qquad\cos^2\frac{\alpha}{2}=\frac{(s-b)(s-c)}{ad+bc}
\;}.\]

Proof. Let \(\gamma=\angle BCD\). Since \(ABCD\) is cyclic, \(\alpha+\gamma=\pi\). Applying the Law of Cosines in triangles \(ABD\) and \(BCD\) and using \(\cos(\pi-\alpha)=-\cos\alpha\),
\[a^2+d^2-2ad\cos\alpha=b^2+c^2-2bc\cos(\pi-\alpha)=b^2+c^2+2bc\cos\alpha,\]
hence
\[\cos\alpha=\frac{a^2+d^2-b^2-c^2}{2(ad+bc)}.\]

For \(\cos^2(\alpha/2)\):
\[\begin{aligned}\cos^2\frac{\alpha}{2}&=\frac{1+\cos\alpha}{2}
=\frac{2(ad+bc)+a^2+d^2-b^2-c^2}{4(ad+bc)}\\[2pt]&=\frac{(a+d)^2-(b-c)^2}{4(ad+bc)}=\frac{(a+d-b+c)(a+d+b-c)}{4(ad+bc)}\\[2pt]&=\frac{\bigl((a+b+c+d)-2b\bigr)\bigl((a+b+c+d)-2c\bigr)}{4(ad+bc)}\\[2pt]&=\frac{(s-b)(s-c)}{ad+bc}.\end{aligned}\]

For \(\sin^2(\alpha/2)\):
\[\begin{aligned}\sin^2\frac{\alpha}{2}&=\frac{1-\cos\alpha}{2}
=\frac{2(ad+bc)-(a^2+d^2-b^2-c^2)}{4(ad+bc)}\\[2pt]&=\frac{(b+c)^2-(a-d)^2}{4(ad+bc)}=\frac{(b+c-a+d)(b+c+a-d)}{4(ad+bc)}\\[2pt]&=\frac{\bigl((a+b+c+d)-2a\bigr)\bigl((a+b+c+d)-2d\bigr)}{4(ad+bc)}\\[2pt]&=\frac{(s-a)(s-d)}{ad+bc}.\end{aligned}\]
This proves the two identities for \(\alpha\). \(\square\)

Theorem (Generalized law of cotangents for cyclic quadrilaterals)
As a consequence of the half–angle formulas,
\[\boxed{\;\frac{\cot(\alpha/2)}{(s-b)(s-c)}\;=\;\frac{\cot(\beta/2)}{(s-c)(s-d)}=\frac{\cot(\gamma/2)}{(s-d)(s-a)}=\frac{\cot(\varphi/2)}{(s-a)(s-b)}=\frac{1}{\Delta}\; }.\]

Proof. From the lemma for \(\alpha\),
\[\cot^2\frac{\alpha}{2}=\frac{\cos^2(\alpha/2)}{\sin^2(\alpha/2)}
=\frac{(s-b)(s-c)}{(s-a)(s-d)},\]
hence
\[\frac{\cot(\alpha/2)}{(s-b)(s-c)}=\frac{1}{\sqrt{(s-a)(s-b)(s-c)(s-d)}}.\]
By Brahmagupta’s formula, \(\displaystyle \Delta=\sqrt{(s-a)(s-b)(s-c)(s-d)}\), so \(\displaystyle \frac{\cot(\alpha/2)}{(s-b)(s-c)}=\frac{1}{\Delta}\). Cyclic relabeling of \(a,b,c,d\) and \(\alpha,\beta,\gamma,\varphi\) yields the other three equalities. \(\square\)

Reduction to the classical law of cotangents (triangle case \(d=0\))
Let \(d=0\). Then \(D\) coalesces with \(A\) and \(ABCD\) degenerates to the triangle \(ABC\) with semiperimeter \(s=\tfrac{a+b+c}{2}\) and area \(\Delta\) (see Figure 2). Taking \(\alpha\) as the angle formed by \(\overline{AB}\) and the limiting tangent at \(A\), the tangent–chord theorem and the limiting relation \(\alpha+\gamma=\pi\) give \(\alpha=\pi-\gamma\), hence \(\cot(\alpha/2)=\tan(\gamma/2)\).

From the generalized theorem, with \(d=0\),
\[\frac{\cot(\beta/2)}{(s-c)s}=\frac{1}{\Delta},\qquad\frac{\cot(\gamma/2)}{(s-a)s}=\frac{1}{\Delta}.
\tag{1}\]
Figure 2. Since $d=0$, $D$ coalesces with $A$.

Let \(r\) be the inradius of \(\triangle ABC\). Since \(\Delta=rs\) and (by Heron) \(\Delta^2=s(s-a)(s-b)(s-c)\), we have
\[r^2s^2=s(s-a)(s-b)(s-c)\quad\Longrightarrow\quad
r^2=\frac{(s-a)(s-b)(s-c)}{s}. \tag{2}\]
Using \((1)\) and \(\Delta=rs\),
\[\frac{\cot(\beta/2)}{s-c}=\frac{1}{r},\qquad\frac{\cot(\gamma/2)}{s-a}=\frac{1}{r}. \tag{3}\]

To obtain the relation at \(A\), set \(\alpha'=\angle BAC\).
Since \(\alpha'=\pi-(\beta+\gamma)\),
\[\cot\frac{\alpha'}{2}=\tan\!\left(\frac{\beta+\gamma}{2}\right)=\frac{\tan(\beta/2)+\tan(\gamma/2)}{1-\tan(\beta/2)\tan(\gamma/2)}.\]
From \((3)\), \(\tan(\beta/2)=\dfrac{r}{s-c}\) and \(\tan(\gamma/2)=\dfrac{r}{s-a}\). Therefore,
\[\begin{aligned}\cot\frac{\alpha'}{2}&=\frac{r\!\left(\frac{1}{s-c}+\frac{1}{s-a}\right)}{1-\dfrac{r^2}{(s-a)(s-c)}}=\frac{r\,\dfrac{2s-(a+c)}{(s-a)(s-c)}}{1-\dfrac{r^2}{(s-a)(s-c)}}\\[4pt]&=\frac{r\,\dfrac{b}{(s-a)(s-c)}}{\dfrac{b}{s}}
\quad\text{(since \(2s=a+b+c\) and by \((2)\))}\\[4pt]
&=\frac{rs}{(s-a)(s-c)}=\frac{rs}{\dfrac{r^2s}{\,s-b\,}}
=\frac{s-b}{r}.\end{aligned}\]
Hence
\[\frac{\cot(\alpha'/2)}{s-b}=\frac{\cot(\beta/2)}{s-c}=\frac{\cot(\gamma/2)}{s-a}=\frac{1}{r}.\]
Relabeling \(a':=|BC|\), \(b':=|CA|\), \(c':=|AB|\) gives
\[\boxed{\;\frac{\cot(\alpha'/2)}{s-a'}=\frac{\cot(\beta/2)}{s-b'}=\frac{\cot(\gamma/2)}{s-c'}=\frac{1}{r}\;},\]
which is precisely the classical law of cotangents for \(\triangle ABC\).

lunes, 26 de mayo de 2025

USM Transform #3 vs. Mathematica Integrate

Warning: The 19-example table is an eye-catching demo, but it is not large or diverse enough to qualify as a “representative” benchmark in the usual software-testing sense. It convincingly illustrates Transform 3’s strengths on its target family—integrals built from $\tan\bigl(\tfrac12\operatorname{arcsec/arccsc}(\cdots)\bigr)$—yet it is too small for reliable statistics and too narrow to say much about performance outside that niche.

USM Transform 3 is described in the draft paper “A Unified Substitution Method for Integration”.

All raw timings and expression statistics were taken verbatim from the diagnostic printouts in USMvsMMAfinal.pdf and USMvsMMAFinal2.pdf . 

How to read the table

  • MMA time – wall-clock seconds for Integrate[…] on the original integrand (labeled Direct time in the notebook).

  • USM timesingle figure obtained by adding:

    • parameter-extraction time +

    • conversion ( x → t ) time +

    • USM-upper-branch integration time +

    • USM-lower-branch integration time

  • LeafCnt / ByteCnt – Mathematica’s LeafCount and ByteCount of the returned antiderivative.

  • Any USM entry that beats Mathematica (smaller value) is red bold-faced.

$$\begin{array}{r|r|r|r|r|r|r}\# & \text{MMA time (s)} & \text{USM time (s)} & \text{MMA LeafCnt} & \text{USM LeafCnt (lo--up)} & \text{MMA ByteCnt} & \text{USM ByteCnt (lo--up)}\\\hline1 & 0.0031 & 0.0283 & 311 & \mathbf{\color{red}{92\text{--}96}} & 9920 & \mathbf{\color{red}{2840\text{--}2984}}\\2 & 1.1700 & \mathbf{\color{red}{0.0026}} & 295 & \mathbf{\color{red}{76\text{--}84}} & 9208 & \mathbf{\color{red}{2256\text{--}2544}}\\3 & 6.1800 & \mathbf{\color{red}{0.0025}} & 8962 & \mathbf{\color{red}{64\text{--}72}} & 283576 & \mathbf{\color{red}{1928\text{--}2216}}\\4 & 0.2580 & \mathbf{\color{red}{0.0018}} & 85 & \mathbf{\color{red}{66\text{--}74}} & 2768 & \mathbf{\color{red}{1960\text{--}2248}}\\5 & 0.0113 & \mathbf{\color{red}{0.0098}} & 23 & 32\text{--}36 & 680 & 984\text{--}1128\\6 & 0.0268 & \mathbf{\color{red}{0.0225}} & 40 & 62\text{--}66 & 1248 & 1888\text{--}2032\\7 & 0.2130 & \mathbf{\color{red}{0.0015}} & 57 & \mathbf{\color{red}{47\text{--}53}} & 1808 & \mathbf{\color{red}{1416\text{--}1632}}\\8 & 19.2000& \mathbf{\color{red}{8.1960}} & 325 & \mathbf{\color{red}{250\text{--}278}} & 9984 & \mathbf{\color{red}{7368\text{--}8376}}\\9 & 2.1300 & \mathbf{\color{red}{0.0262}} & 193 & \mathbf{\color{red}{63\text{--}71}} & 6216 & \mathbf{\color{red}{1960\text{--}2248}}\\10 & 4.5200 & \mathbf{\color{red}{0.0274}} & 151 & \mathbf{\color{red}{90\text{--}102}} & 4720 & \mathbf{\color{red}{2736\text{--}3168}}\\11 & 0.0120 & \mathbf{\color{red}{0.0015}} & 58 & \mathbf{\color{red}{42\text{--}46}} & 1776 & \mathbf{\color{red}{1288\text{--}1432}}\\12 & 0.0146 & \mathbf{\color{red}{0.0016}} & 158 & \mathbf{\color{red}{49\text{--}53}} & 4880 & \mathbf{\color{red}{1440\text{--}1584}}\\13 & 0.0122 & \mathbf{\color{red}{0.0112}} & 39 & 44\text{--}48 & 1240 & 1360\text{--}1504\\14 & 0.2250 & \mathbf{\color{red}{0.0023}} & 106 & \mathbf{\color{red}{62\text{--}68}} & 3312 & \mathbf{\color{red}{1888\text{--}2104}}\\15 & 0.0154 & \mathbf{\color{red}{0.0018}} & 47 & \mathbf{\color{red}{43\text{--}47}} & 1448 & \mathbf{\color{red}{1312\text{--}1456}}\\16 & 6.0500 & \mathbf{\color{red}{0.0357}} & 456 & \mathbf{\color{red}{332\text{--}360}} & 13976 & \mathbf{\color{red}{9960\text{--}10968}}\\17 & 0.0068 & 0.0220 & 308 & \mathbf{\color{red}{85\text{--}93}} & 9720 & \mathbf{\color{red}{2608\text{--}2896}}\\18 & 1.0500 & \mathbf{\color{red}{0.0041}} & 524 & \mathbf{\color{red}{109\text{--}117}} & 16272 & \mathbf{\color{red}{3232\text{--}3520}}\\19 & 9.3900 & \mathbf{\color{red}{0.0933}} & 19148 & \mathbf{\color{red}{129\text{--}139}} & 600840 & \mathbf{\color{red}{3880\text{--}4240}}\\\end{array}$$

Interpreting the numbers

  • LeafCount (Lower--Upper) counts the total nodes in Mathematica’s expression tree; ByteCount (Lower--Upper) measures the memory footprint in bytes. Both are crude but serviceable proxies for “formula complexity”. Lower values signal more compact, and usually more readable, antiderivatives.

  • USM total time is purposely holistic: the tiny setup overhead (parameter extraction + conversion) is included together with the actual integration of both branches, so a reader sees the real end-to-end cost of calling the transform.

  • Why two branches? Transform 3 generates an “upper” and a “lower” substitution dictated by the radical’s sign conventions; the faster of the two could in principle be chosen automatically, but here both were timed and summed for fairness.

Average speed-up

$$\begin{array}{r|r|r|r}\# & \text{MMA time (s)} & \text{USM time (s)} & \text{Speed-up} \\\hline1 & 0.0031 & 0.0283 & 0.11 \\2 & 1.1700 & \mathbf{\color{red}{0.0026}} & 450 \\3 & 6.1800 & \mathbf{\color{red}{0.0025}} & 2472 \\4 & 0.2580 & \mathbf{\color{red}{0.0018}} & 143 \\5 & 0.0113 & \mathbf{\color{red}{0.0098}} & 1.15 \\6 & 0.0268 & \mathbf{\color{red}{0.0225}} & 1.19 \\7 & 0.2130 & \mathbf{\color{red}{0.0015}} & 142 \\8 & 19.2000& \mathbf{\color{red}{8.1960}} & 2.34 \\9 & 2.1300 & \mathbf{\color{red}{0.0262}} & 81.3 \\10 & 4.5200 & \mathbf{\color{red}{0.0274}} & 165 \\11 & 0.0120 & \mathbf{\color{red}{0.0015}} & 8.0 \\12 & 0.0146 & \mathbf{\color{red}{0.0016}} & 9.13 \\13 & 0.0122 & \mathbf{\color{red}{0.0112}} & 1.09 \\14 & 0.2250 & \mathbf{\color{red}{0.0023}} & 97.8 \\15 & 0.0154 & \mathbf{\color{red}{0.0018}} & 8.56 \\16 & 6.0500 & \mathbf{\color{red}{0.0357}} & 170 \\17 & 0.0068 & 0.0220 & 0.31 \\18 & 1.0500 & \mathbf{\color{red}{0.0041}} & 256 \\19 & 9.3900 & \mathbf{\color{red}{0.0933}} & 101 \\\end{array}$$

Using the arithmetic mean of the 19 speed-up values


$$\text{speed-up}_i \;=\;\frac{\text{MMA time}_i}{\text{USM time}_i},$$

the average speed-up is


$$\boxed{\displaystyle \overline{\text{speed-up}}\;=\;216.3\ (\text{approximately})}.$$

Take-aways

  • Speed advantage – USM outruns built-in Integrate on 17 / 19 examples, sometimes spectacularly (Ex 3 is > 2000× faster). Only the Ex 1 and Ex 17 favour MMA on raw time. However, for these two examples USM gave much simpler antiderivatives.

  • Compact answers – In 16 / 19 cases the USM result is much smaller (LeafCount & ByteCount). The gains are eye-catching for the hardest problems: Ex 3 shrinks from 8 962 leaves to 64--72, and Ex 19 from 19 148 to 129--139.

  • Overhead is negligible – Parameter extraction and x ⁣ ⁣tx\!\to\!t conversion stay in the micro-second realm; almost all of the USM total is the symbolic integration itself, yet seldom exceeds a few hundredths of a second.

Bottom line: On this mixed radical-inverse-trig mini-benchmark, the USM (Transform 3) consistently yields faster and simpler antiderivatives than Mathematica’s built-in integrator.