math.log/1 on complex, real part

Percentage Accurate: 51.7% → 99.7%
Time: 2.4s
Alternatives: 2
Speedup: 2.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 2 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: 99.7% accurate, 1.9× speedup?

\[\begin{array}{l} re = |re|\\ im = |im|\\ [re, im] = \mathsf{sort}([re, im])\\ \\ \log im + \frac{0.5 \cdot re}{im} \cdot \frac{re}{im} \end{array} \]
NOTE: re should be positive before calling this function
NOTE: im should be positive before calling this function
NOTE: re and im should be sorted in increasing order before calling this function.
(FPCore (re im)
 :precision binary64
 (+ (log im) (* (/ (* 0.5 re) im) (/ re im))))
re = abs(re);
im = abs(im);
assert(re < im);
double code(double re, double im) {
	return log(im) + (((0.5 * re) / im) * (re / im));
}
NOTE: re should be positive before calling this function
NOTE: im should be positive before calling this function
NOTE: re and im should be sorted in increasing order before calling this function.
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(im) + (((0.5d0 * re) / im) * (re / im))
end function
re = Math.abs(re);
im = Math.abs(im);
assert re < im;
public static double code(double re, double im) {
	return Math.log(im) + (((0.5 * re) / im) * (re / im));
}
re = abs(re)
im = abs(im)
[re, im] = sort([re, im])
def code(re, im):
	return math.log(im) + (((0.5 * re) / im) * (re / im))
re = abs(re)
im = abs(im)
re, im = sort([re, im])
function code(re, im)
	return Float64(log(im) + Float64(Float64(Float64(0.5 * re) / im) * Float64(re / im)))
end
re = abs(re)
im = abs(im)
re, im = num2cell(sort([re, im])){:}
function tmp = code(re, im)
	tmp = log(im) + (((0.5 * re) / im) * (re / im));
end
NOTE: re should be positive before calling this function
NOTE: im should be positive before calling this function
NOTE: re and im should be sorted in increasing order before calling this function.
code[re_, im_] := N[(N[Log[im], $MachinePrecision] + N[(N[(N[(0.5 * re), $MachinePrecision] / im), $MachinePrecision] * N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
re = |re|\\
im = |im|\\
[re, im] = \mathsf{sort}([re, im])\\
\\
\log im + \frac{0.5 \cdot re}{im} \cdot \frac{re}{im}
\end{array}
Derivation
  1. Initial program 59.0%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. hypot-def100.0%

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

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Taylor expanded in re around 0 19.2%

    \[\leadsto \color{blue}{\log im + 0.5 \cdot \frac{{re}^{2}}{{im}^{2}}} \]
  5. Step-by-step derivation
    1. associate-*r/19.2%

      \[\leadsto \log im + \color{blue}{\frac{0.5 \cdot {re}^{2}}{{im}^{2}}} \]
    2. unpow219.2%

      \[\leadsto \log im + \frac{0.5 \cdot \color{blue}{\left(re \cdot re\right)}}{{im}^{2}} \]
    3. unpow219.2%

      \[\leadsto \log im + \frac{0.5 \cdot \left(re \cdot re\right)}{\color{blue}{im \cdot im}} \]
  6. Simplified19.2%

    \[\leadsto \color{blue}{\log im + \frac{0.5 \cdot \left(re \cdot re\right)}{im \cdot im}} \]
  7. Step-by-step derivation
    1. associate-*r*19.2%

      \[\leadsto \log im + \frac{\color{blue}{\left(0.5 \cdot re\right) \cdot re}}{im \cdot im} \]
    2. times-frac22.0%

      \[\leadsto \log im + \color{blue}{\frac{0.5 \cdot re}{im} \cdot \frac{re}{im}} \]
  8. Applied egg-rr22.0%

    \[\leadsto \log im + \color{blue}{\frac{0.5 \cdot re}{im} \cdot \frac{re}{im}} \]
  9. Final simplification22.0%

    \[\leadsto \log im + \frac{0.5 \cdot re}{im} \cdot \frac{re}{im} \]

Alternative 2: 99.3% accurate, 2.0× speedup?

\[\begin{array}{l} re = |re|\\ im = |im|\\ [re, im] = \mathsf{sort}([re, im])\\ \\ \log im \end{array} \]
NOTE: re should be positive before calling this function
NOTE: im should be positive before calling this function
NOTE: re and im should be sorted in increasing order before calling this function.
(FPCore (re im) :precision binary64 (log im))
re = abs(re);
im = abs(im);
assert(re < im);
double code(double re, double im) {
	return log(im);
}
NOTE: re should be positive before calling this function
NOTE: im should be positive before calling this function
NOTE: re and im should be sorted in increasing order before calling this function.
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(im)
end function
re = Math.abs(re);
im = Math.abs(im);
assert re < im;
public static double code(double re, double im) {
	return Math.log(im);
}
re = abs(re)
im = abs(im)
[re, im] = sort([re, im])
def code(re, im):
	return math.log(im)
re = abs(re)
im = abs(im)
re, im = sort([re, im])
function code(re, im)
	return log(im)
end
re = abs(re)
im = abs(im)
re, im = num2cell(sort([re, im])){:}
function tmp = code(re, im)
	tmp = log(im);
end
NOTE: re should be positive before calling this function
NOTE: im should be positive before calling this function
NOTE: re and im should be sorted in increasing order before calling this function.
code[re_, im_] := N[Log[im], $MachinePrecision]
\begin{array}{l}
re = |re|\\
im = |im|\\
[re, im] = \mathsf{sort}([re, im])\\
\\
\log im
\end{array}
Derivation
  1. Initial program 59.0%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. hypot-def100.0%

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

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Taylor expanded in re around 0 23.8%

    \[\leadsto \color{blue}{\log im} \]
  5. Final simplification23.8%

    \[\leadsto \log im \]

Reproduce

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