math.log/2 on complex, real part

Percentage Accurate: 52.2% → 99.4%
Time: 13.1s
Alternatives: 2
Speedup: 2.0×

Specification

?
\[\begin{array}{l} \\ \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \end{array} \]
(FPCore (re im base)
 :precision binary64
 (/
  (+ (* (log (sqrt (+ (* re re) (* im im)))) (log base)) (* (atan2 im re) 0.0))
  (+ (* (log base) (log base)) (* 0.0 0.0))))
double code(double re, double im, double base) {
	return ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0d0)) / ((log(base) * log(base)) + (0.0d0 * 0.0d0))
end function
public static double code(double re, double im, double base) {
	return ((Math.log(Math.sqrt(((re * re) + (im * im)))) * Math.log(base)) + (Math.atan2(im, re) * 0.0)) / ((Math.log(base) * Math.log(base)) + (0.0 * 0.0));
}
def code(re, im, base):
	return ((math.log(math.sqrt(((re * re) + (im * im)))) * math.log(base)) + (math.atan2(im, re) * 0.0)) / ((math.log(base) * math.log(base)) + (0.0 * 0.0))
function code(re, im, base)
	return Float64(Float64(Float64(log(sqrt(Float64(Float64(re * re) + Float64(im * im)))) * log(base)) + Float64(atan(im, re) * 0.0)) / Float64(Float64(log(base) * log(base)) + Float64(0.0 * 0.0)))
end
function tmp = code(re, im, base)
	tmp = ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
end
code[re_, im_, base_] := N[(N[(N[(N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] + N[(N[ArcTan[im / re], $MachinePrecision] * 0.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Log[base], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] + N[(0.0 * 0.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0}
\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: 52.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \end{array} \]
(FPCore (re im base)
 :precision binary64
 (/
  (+ (* (log (sqrt (+ (* re re) (* im im)))) (log base)) (* (atan2 im re) 0.0))
  (+ (* (log base) (log base)) (* 0.0 0.0))))
double code(double re, double im, double base) {
	return ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0d0)) / ((log(base) * log(base)) + (0.0d0 * 0.0d0))
end function
public static double code(double re, double im, double base) {
	return ((Math.log(Math.sqrt(((re * re) + (im * im)))) * Math.log(base)) + (Math.atan2(im, re) * 0.0)) / ((Math.log(base) * Math.log(base)) + (0.0 * 0.0));
}
def code(re, im, base):
	return ((math.log(math.sqrt(((re * re) + (im * im)))) * math.log(base)) + (math.atan2(im, re) * 0.0)) / ((math.log(base) * math.log(base)) + (0.0 * 0.0))
function code(re, im, base)
	return Float64(Float64(Float64(log(sqrt(Float64(Float64(re * re) + Float64(im * im)))) * log(base)) + Float64(atan(im, re) * 0.0)) / Float64(Float64(log(base) * log(base)) + Float64(0.0 * 0.0)))
end
function tmp = code(re, im, base)
	tmp = ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
end
code[re_, im_, base_] := N[(N[(N[(N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] + N[(N[ArcTan[im / re], $MachinePrecision] * 0.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Log[base], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] + N[(0.0 * 0.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0}
\end{array}

Alternative 1: 99.4% accurate, 2.0× speedup?

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

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

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
  2. Step-by-step derivation
    1. mul0-rgt56.3%

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
    2. +-rgt-identity56.3%

      \[\leadsto \frac{\color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}}{\log base \cdot \log base + 0 \cdot 0} \]
    3. metadata-eval56.3%

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
    4. +-rgt-identity56.3%

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
    5. times-frac56.4%

      \[\leadsto \color{blue}{\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log base} \cdot \frac{\log base}{\log base}} \]
    6. *-inverses56.4%

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log base} \cdot \color{blue}{1} \]
    7. *-rgt-identity56.4%

      \[\leadsto \color{blue}{\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log base}} \]
    8. hypot-define99.5%

      \[\leadsto \frac{\log \color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)}}{\log base} \]
  3. Simplified99.5%

    \[\leadsto \color{blue}{\frac{\log \left(\mathsf{hypot}\left(re, im\right)\right)}{\log base}} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 2: 27.8% accurate, 3.1× speedup?

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

\\
\frac{\log im}{\log base}
\end{array}
Derivation
  1. Initial program 56.3%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
  2. Step-by-step derivation
    1. mul0-rgt56.3%

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
    2. +-rgt-identity56.3%

      \[\leadsto \frac{\color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}}{\log base \cdot \log base + 0 \cdot 0} \]
    3. associate-/l*56.3%

      \[\leadsto \color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \frac{\log base}{\log base \cdot \log base + 0 \cdot 0}} \]
    4. hypot-define99.2%

      \[\leadsto \log \color{blue}{\left(\mathsf{hypot}\left(re, im\right)\right)} \cdot \frac{\log base}{\log base \cdot \log base + 0 \cdot 0} \]
    5. metadata-eval99.2%

      \[\leadsto \log \left(\mathsf{hypot}\left(re, im\right)\right) \cdot \frac{\log base}{\log base \cdot \log base + \color{blue}{0}} \]
    6. +-rgt-identity99.2%

      \[\leadsto \log \left(\mathsf{hypot}\left(re, im\right)\right) \cdot \frac{\log base}{\color{blue}{\log base \cdot \log base}} \]
    7. associate-/r*99.4%

      \[\leadsto \log \left(\mathsf{hypot}\left(re, im\right)\right) \cdot \color{blue}{\frac{\frac{\log base}{\log base}}{\log base}} \]
    8. *-inverses99.4%

      \[\leadsto \log \left(\mathsf{hypot}\left(re, im\right)\right) \cdot \frac{\color{blue}{1}}{\log base} \]
  3. Simplified99.4%

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right) \cdot \frac{1}{\log base}} \]
  4. Add Preprocessing
  5. Taylor expanded in re around 0 31.8%

    \[\leadsto \color{blue}{\frac{\log im}{\log base}} \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2024131 
(FPCore (re im base)
  :name "math.log/2 on complex, real part"
  :precision binary64
  (/ (+ (* (log (sqrt (+ (* re re) (* im im)))) (log base)) (* (atan2 im re) 0.0)) (+ (* (log base) (log base)) (* 0.0 0.0))))