math.log/1 on complex, real part

Percentage Accurate: 51.7% → 100.0%
Time: 2.0s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \log \left(\sqrt{re \cdot re + im \cdot im}\right) \end{array} \]
(FPCore (re im) :precision binary64 (log (sqrt (+ (* re re) (* im im)))))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im))));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(sqrt(((re * re) + (im * im))))
end function
public static double code(double re, double im) {
	return Math.log(Math.sqrt(((re * re) + (im * im))));
}
def code(re, im):
	return math.log(math.sqrt(((re * re) + (im * im))))
function code(re, im)
	return log(sqrt(Float64(Float64(re * re) + Float64(im * im))))
end
function tmp = code(re, im)
	tmp = log(sqrt(((re * re) + (im * im))));
end
code[re_, im_] := N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\sqrt{re \cdot re + im \cdot im}\right)
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 3 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 51.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(\sqrt{re \cdot re + im \cdot im}\right) \end{array} \]
(FPCore (re im) :precision binary64 (log (sqrt (+ (* re re) (* im im)))))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im))));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(sqrt(((re * re) + (im * im))))
end function
public static double code(double re, double im) {
	return Math.log(Math.sqrt(((re * re) + (im * im))));
}
def code(re, im):
	return math.log(math.sqrt(((re * re) + (im * im))))
function code(re, im)
	return log(sqrt(Float64(Float64(re * re) + Float64(im * im))))
end
function tmp = code(re, im)
	tmp = log(sqrt(((re * re) + (im * im))));
end
code[re_, im_] := N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\sqrt{re \cdot re + im \cdot im}\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(\mathsf{hypot}\left(re, im\right)\right) \end{array} \]
(FPCore (re im) :precision binary64 (log (hypot re im)))
double code(double re, double im) {
	return log(hypot(re, im));
}
public static double code(double re, double im) {
	return Math.log(Math.hypot(re, im));
}
def code(re, im):
	return math.log(math.hypot(re, im))
function code(re, im)
	return log(hypot(re, im))
end
function tmp = code(re, im)
	tmp = log(hypot(re, im));
end
code[re_, im_] := N[Log[N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\mathsf{hypot}\left(re, im\right)\right)
\end{array}
Derivation
  1. Initial program 51.9%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. expm1-log1p-u51.9%

      \[\leadsto \log \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt{re \cdot re + im \cdot im}\right)\right)\right)} \]
    2. expm1-udef32.4%

      \[\leadsto \log \color{blue}{\left(e^{\mathsf{log1p}\left(\sqrt{re \cdot re + im \cdot im}\right)} - 1\right)} \]
    3. log1p-udef32.4%

      \[\leadsto \log \left(e^{\color{blue}{\log \left(1 + \sqrt{re \cdot re + im \cdot im}\right)}} - 1\right) \]
    4. add-exp-log32.4%

      \[\leadsto \log \left(\color{blue}{\left(1 + \sqrt{re \cdot re + im \cdot im}\right)} - 1\right) \]
    5. hypot-def74.0%

      \[\leadsto \log \left(\left(1 + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right) - 1\right) \]
  3. Applied egg-rr74.0%

    \[\leadsto \log \color{blue}{\left(\left(1 + \mathsf{hypot}\left(re, im\right)\right) - 1\right)} \]
  4. Step-by-step derivation
    1. +-commutative74.0%

      \[\leadsto \log \left(\color{blue}{\left(\mathsf{hypot}\left(re, im\right) + 1\right)} - 1\right) \]
    2. associate--l+100.0%

      \[\leadsto \log \color{blue}{\left(\mathsf{hypot}\left(re, im\right) + \left(1 - 1\right)\right)} \]
    3. metadata-eval100.0%

      \[\leadsto \log \left(\mathsf{hypot}\left(re, im\right) + \color{blue}{0}\right) \]
    4. +-rgt-identity100.0%

      \[\leadsto \log \color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)} \]
  5. Simplified100.0%

    \[\leadsto \log \color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)} \]
  6. Final simplification100.0%

    \[\leadsto \log \left(\mathsf{hypot}\left(re, im\right)\right) \]

Alternative 2: 43.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq -3.5 \cdot 10^{-157}:\\ \;\;\;\;\log \left(-re\right)\\ \mathbf{else}:\\ \;\;\;\;\log im\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (if (<= re -3.5e-157) (log (- re)) (log im)))
double code(double re, double im) {
	double tmp;
	if (re <= -3.5e-157) {
		tmp = log(-re);
	} else {
		tmp = log(im);
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: tmp
    if (re <= (-3.5d-157)) then
        tmp = log(-re)
    else
        tmp = log(im)
    end if
    code = tmp
end function
public static double code(double re, double im) {
	double tmp;
	if (re <= -3.5e-157) {
		tmp = Math.log(-re);
	} else {
		tmp = Math.log(im);
	}
	return tmp;
}
def code(re, im):
	tmp = 0
	if re <= -3.5e-157:
		tmp = math.log(-re)
	else:
		tmp = math.log(im)
	return tmp
function code(re, im)
	tmp = 0.0
	if (re <= -3.5e-157)
		tmp = log(Float64(-re));
	else
		tmp = log(im);
	end
	return tmp
end
function tmp_2 = code(re, im)
	tmp = 0.0;
	if (re <= -3.5e-157)
		tmp = log(-re);
	else
		tmp = log(im);
	end
	tmp_2 = tmp;
end
code[re_, im_] := If[LessEqual[re, -3.5e-157], N[Log[(-re)], $MachinePrecision], N[Log[im], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.5 \cdot 10^{-157}:\\
\;\;\;\;\log \left(-re\right)\\

\mathbf{else}:\\
\;\;\;\;\log im\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < -3.5000000000000002e-157

    1. Initial program 55.0%

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
    2. Taylor expanded in re around -inf 70.9%

      \[\leadsto \log \color{blue}{\left(-1 \cdot re\right)} \]
    3. Step-by-step derivation
      1. mul-1-neg70.9%

        \[\leadsto \log \color{blue}{\left(-re\right)} \]
    4. Simplified70.9%

      \[\leadsto \log \color{blue}{\left(-re\right)} \]

    if -3.5000000000000002e-157 < re

    1. Initial program 50.1%

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
    2. Taylor expanded in re around 0 39.4%

      \[\leadsto \log \color{blue}{im} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification51.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq -3.5 \cdot 10^{-157}:\\ \;\;\;\;\log \left(-re\right)\\ \mathbf{else}:\\ \;\;\;\;\log im\\ \end{array} \]

Alternative 3: 27.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \log im \end{array} \]
(FPCore (re im) :precision binary64 (log im))
double code(double re, double im) {
	return log(im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(im)
end function
public static double code(double re, double im) {
	return Math.log(im);
}
def code(re, im):
	return math.log(im)
function code(re, im)
	return log(im)
end
function tmp = code(re, im)
	tmp = log(im);
end
code[re_, im_] := N[Log[im], $MachinePrecision]
\begin{array}{l}

\\
\log im
\end{array}
Derivation
  1. Initial program 51.9%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Taylor expanded in re around 0 31.5%

    \[\leadsto \log \color{blue}{im} \]
  3. Final simplification31.5%

    \[\leadsto \log im \]

Reproduce

?
herbie shell --seed 2023174 
(FPCore (re im)
  :name "math.log/1 on complex, real part"
  :precision binary64
  (log (sqrt (+ (* re re) (* im im)))))