Given's Rotation SVD example

?

Percentage Accurate: 79.5% → 91.0%
Time: 17.3s
Precision: binary64
Cost: 26884

?

\[10^{-150} < \left|x\right| \land \left|x\right| < 10^{+150}\]
\[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-\sqrt{p \cdot p}}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}\\ \end{array} \]
(FPCore (p x)
 :precision binary64
 (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))
(FPCore (p x)
 :precision binary64
 (if (<= (/ x (sqrt (+ (* p (* 4.0 p)) (* x x)))) -1.0)
   (/ (- (sqrt (* p p))) x)
   (sqrt (fma (/ 0.5 (hypot x (* p 2.0))) x 0.5))))
double code(double p, double x) {
	return sqrt((0.5 * (1.0 + (x / sqrt((((4.0 * p) * p) + (x * x)))))));
}
double code(double p, double x) {
	double tmp;
	if ((x / sqrt(((p * (4.0 * p)) + (x * x)))) <= -1.0) {
		tmp = -sqrt((p * p)) / x;
	} else {
		tmp = sqrt(fma((0.5 / hypot(x, (p * 2.0))), x, 0.5));
	}
	return tmp;
}
function code(p, x)
	return sqrt(Float64(0.5 * Float64(1.0 + Float64(x / sqrt(Float64(Float64(Float64(4.0 * p) * p) + Float64(x * x)))))))
end
function code(p, x)
	tmp = 0.0
	if (Float64(x / sqrt(Float64(Float64(p * Float64(4.0 * p)) + Float64(x * x)))) <= -1.0)
		tmp = Float64(Float64(-sqrt(Float64(p * p))) / x);
	else
		tmp = sqrt(fma(Float64(0.5 / hypot(x, Float64(p * 2.0))), x, 0.5));
	end
	return tmp
end
code[p_, x_] := N[Sqrt[N[(0.5 * N[(1.0 + N[(x / N[Sqrt[N[(N[(N[(4.0 * p), $MachinePrecision] * p), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[p_, x_] := If[LessEqual[N[(x / N[Sqrt[N[(N[(p * N[(4.0 * p), $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -1.0], N[((-N[Sqrt[N[(p * p), $MachinePrecision]], $MachinePrecision]) / x), $MachinePrecision], N[Sqrt[N[(N[(0.5 / N[Sqrt[x ^ 2 + N[(p * 2.0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * x + 0.5), $MachinePrecision]], $MachinePrecision]]
\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)}
\begin{array}{l}
\mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\
\;\;\;\;\frac{-\sqrt{p \cdot p}}{x}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}\\


\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 8 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Target

Original79.5%
Target79.5%
Herbie91.0%
\[\sqrt{0.5 + \frac{\mathsf{copysign}\left(0.5, x\right)}{\mathsf{hypot}\left(1, \frac{2 \cdot p}{x}\right)}} \]

Derivation?

  1. Split input into 2 regimes
  2. if (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x)))) < -1

    1. Initial program 15.8%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Taylor expanded in x around -inf 60.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x}} \]
    3. Applied egg-rr64.9%

      \[\leadsto -1 \cdot \frac{\color{blue}{\sqrt{p \cdot p}}}{x} \]
      Step-by-step derivation

      [Start]60.4%

      \[ -1 \cdot \frac{\sqrt{2} \cdot \left(\sqrt{0.5} \cdot p\right)}{x} \]

      associate-*r* [=>]60.1%

      \[ -1 \cdot \frac{\color{blue}{\left(\sqrt{2} \cdot \sqrt{0.5}\right) \cdot p}}{x} \]

      sqrt-unprod [=>]61.1%

      \[ -1 \cdot \frac{\color{blue}{\sqrt{2 \cdot 0.5}} \cdot p}{x} \]

      metadata-eval [=>]61.1%

      \[ -1 \cdot \frac{\sqrt{\color{blue}{1}} \cdot p}{x} \]

      metadata-eval [=>]61.1%

      \[ -1 \cdot \frac{\color{blue}{1} \cdot p}{x} \]

      *-un-lft-identity [<=]61.1%

      \[ -1 \cdot \frac{\color{blue}{p}}{x} \]

      add-sqr-sqrt [=>]54.3%

      \[ -1 \cdot \frac{\color{blue}{\sqrt{p} \cdot \sqrt{p}}}{x} \]

      sqrt-unprod [=>]64.9%

      \[ -1 \cdot \frac{\color{blue}{\sqrt{p \cdot p}}}{x} \]

    if -1 < (/.f64 x (sqrt.f64 (+.f64 (*.f64 (*.f64 4 p) p) (*.f64 x x))))

    1. Initial program 100.0%

      \[\sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]
    2. Applied egg-rr100.0%

      \[\leadsto \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x}\right)} \]
      Step-by-step derivation

      [Start]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{x}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}\right)} \]

      clear-num [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\frac{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}}{x}}}\right)} \]

      associate-/r/ [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \color{blue}{\frac{1}{\sqrt{\left(4 \cdot p\right) \cdot p + x \cdot x}} \cdot x}\right)} \]

      +-commutative [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\sqrt{\color{blue}{x \cdot x + \left(4 \cdot p\right) \cdot p}}} \cdot x\right)} \]

      add-sqr-sqrt [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\sqrt{x \cdot x + \color{blue}{\sqrt{\left(4 \cdot p\right) \cdot p} \cdot \sqrt{\left(4 \cdot p\right) \cdot p}}}} \cdot x\right)} \]

      hypot-def [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\color{blue}{\mathsf{hypot}\left(x, \sqrt{\left(4 \cdot p\right) \cdot p}\right)}} \cdot x\right)} \]

      associate-*l* [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, \sqrt{\color{blue}{4 \cdot \left(p \cdot p\right)}}\right)} \cdot x\right)} \]

      sqrt-prod [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, \color{blue}{\sqrt{4} \cdot \sqrt{p \cdot p}}\right)} \cdot x\right)} \]

      metadata-eval [=>]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, \color{blue}{2} \cdot \sqrt{p \cdot p}\right)} \cdot x\right)} \]

      sqrt-unprod [<=]50.5%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{\left(\sqrt{p} \cdot \sqrt{p}\right)}\right)} \cdot x\right)} \]

      add-sqr-sqrt [<=]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot \color{blue}{p}\right)} \cdot x\right)} \]
    3. Applied egg-rr98.9%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1} \]
      Step-by-step derivation

      [Start]100.0%

      \[ \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)} \]

      expm1-log1p-u [=>]99.0%

      \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}\right)\right)} \]

      expm1-udef [=>]98.9%

      \[ \color{blue}{e^{\mathsf{log1p}\left(\sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}\right)} - 1} \]

      distribute-lft-in [=>]98.9%

      \[ e^{\mathsf{log1p}\left(\sqrt{\color{blue}{0.5 \cdot 1 + 0.5 \cdot \left(\frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}}\right)} - 1 \]

      metadata-eval [=>]98.9%

      \[ e^{\mathsf{log1p}\left(\sqrt{\color{blue}{0.5} + 0.5 \cdot \left(\frac{1}{\mathsf{hypot}\left(x, 2 \cdot p\right)} \cdot x\right)}\right)} - 1 \]

      associate-*l/ [=>]98.9%

      \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \color{blue}{\frac{1 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}}\right)} - 1 \]

      *-un-lft-identity [<=]98.9%

      \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + 0.5 \cdot \frac{\color{blue}{x}}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1 \]

      associate-*r/ [=>]98.9%

      \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + \color{blue}{\frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}}\right)} - 1 \]
    4. Simplified100.0%

      \[\leadsto \color{blue}{\sqrt{0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}}}} \]
      Step-by-step derivation

      [Start]98.9%

      \[ e^{\mathsf{log1p}\left(\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)} - 1 \]

      expm1-def [=>]99.0%

      \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}\right)\right)} \]

      expm1-log1p [=>]100.0%

      \[ \color{blue}{\sqrt{0.5 + \frac{0.5 \cdot x}{\mathsf{hypot}\left(x, 2 \cdot p\right)}}} \]

      associate-/l* [=>]100.0%

      \[ \sqrt{0.5 + \color{blue}{\frac{0.5}{\frac{\mathsf{hypot}\left(x, 2 \cdot p\right)}{x}}}} \]

      *-commutative [=>]100.0%

      \[ \sqrt{0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, \color{blue}{p \cdot 2}\right)}{x}}} \]
    5. Applied egg-rr100.0%

      \[\leadsto \sqrt{\color{blue}{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}} \]
      Step-by-step derivation

      [Start]100.0%

      \[ \sqrt{0.5 + \frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}}} \]

      +-commutative [=>]100.0%

      \[ \sqrt{\color{blue}{\frac{0.5}{\frac{\mathsf{hypot}\left(x, p \cdot 2\right)}{x}} + 0.5}} \]

      associate-/r/ [=>]100.0%

      \[ \sqrt{\color{blue}{\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)} \cdot x} + 0.5} \]

      fma-def [=>]100.0%

      \[ \sqrt{\color{blue}{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-\sqrt{p \cdot p}}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy91.0%
Cost26884
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-\sqrt{p \cdot p}}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}, x, 0.5\right)}\\ \end{array} \]
Alternative 2
Accuracy91.0%
Cost20612
\[\begin{array}{l} \mathbf{if}\;\frac{x}{\sqrt{p \cdot \left(4 \cdot p\right) + x \cdot x}} \leq -1:\\ \;\;\;\;\frac{-\sqrt{p \cdot p}}{x}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 + x \cdot \frac{0.5}{\mathsf{hypot}\left(x, p \cdot 2\right)}}\\ \end{array} \]
Alternative 3
Accuracy80.3%
Cost13836
\[\begin{array}{l} t_0 := \frac{-\sqrt{p \cdot p}}{x}\\ \mathbf{if}\;x \leq -9.2 \cdot 10^{+101}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -8.5 \cdot 10^{+90}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq -6.5 \cdot 10^{+53}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5 \cdot \left(1 + \frac{x}{\mathsf{hypot}\left(p \cdot 2, x\right)}\right)}\\ \end{array} \]
Alternative 4
Accuracy68.3%
Cost7388
\[\begin{array}{l} \mathbf{if}\;p \leq -7 \cdot 10^{-5}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;p \leq -2.1 \cdot 10^{-21}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq -2.5 \cdot 10^{-73}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq -3.4 \cdot 10^{-233}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{elif}\;p \leq 3.3 \cdot 10^{-189}:\\ \;\;\;\;1\\ \mathbf{elif}\;p \leq 2.45 \cdot 10^{-120}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;p \leq 4.6 \cdot 10^{-75}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5}\\ \end{array} \]
Alternative 5
Accuracy64.3%
Cost7180
\[\begin{array}{l} t_0 := \frac{-\sqrt{p \cdot p}}{x}\\ \mathbf{if}\;x \leq -9.2 \cdot 10^{+101}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -8.5 \cdot 10^{+90}:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{elif}\;x \leq -7.6 \cdot 10^{+53}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 40000:\\ \;\;\;\;\sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 6
Accuracy44.8%
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -6 \cdot 10^{-75}:\\ \;\;\;\;\frac{-p}{x}\\ \mathbf{elif}\;x \leq -6.5 \cdot 10^{-157}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 7
Accuracy44.5%
Cost324
\[\begin{array}{l} \mathbf{if}\;x \leq -5.8 \cdot 10^{-162}:\\ \;\;\;\;\frac{p}{x}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 8
Accuracy36.2%
Cost64
\[1 \]

Reproduce?

herbie shell --seed 2023263 
(FPCore (p x)
  :name "Given's Rotation SVD example"
  :precision binary64
  :pre (and (< 1e-150 (fabs x)) (< (fabs x) 1e+150))

  :herbie-target
  (sqrt (+ 0.5 (/ (copysign 0.5 x) (hypot 1.0 (/ (* 2.0 p) x)))))

  (sqrt (* 0.5 (+ 1.0 (/ x (sqrt (+ (* (* 4.0 p) p) (* x x))))))))