math.log10 on complex, real part

Percentage Accurate: 50.9% → 98.9%
Time: 7.9s
Alternatives: 2
Speedup: 1.5×

Specification

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

\\
\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10}
\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: 50.9% accurate, 1.0× speedup?

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

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

Alternative 1: 98.9% accurate, 1.4× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ im_m = \left|im\right| \\ [re_m, im_m] = \mathsf{sort}([re_m, im_m])\\ \\ \frac{\log \left(im\_m + re\_m \cdot \left(re\_m \cdot \left(\frac{-0.125 \cdot \frac{re\_m \cdot \frac{re\_m}{im\_m}}{im\_m}}{im\_m} + \frac{0.5}{im\_m}\right)\right)\right)}{\log 10} \end{array} \]
re_m = (fabs.f64 re)
im_m = (fabs.f64 im)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
(FPCore (re_m im_m)
 :precision binary64
 (/
  (log
   (+
    im_m
    (*
     re_m
     (*
      re_m
      (+ (/ (* -0.125 (/ (* re_m (/ re_m im_m)) im_m)) im_m) (/ 0.5 im_m))))))
  (log 10.0)))
re_m = fabs(re);
im_m = fabs(im);
assert(re_m < im_m);
double code(double re_m, double im_m) {
	return log((im_m + (re_m * (re_m * (((-0.125 * ((re_m * (re_m / im_m)) / im_m)) / im_m) + (0.5 / im_m)))))) / log(10.0);
}
re_m = abs(re)
im_m = abs(im)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
real(8) function code(re_m, im_m)
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im_m
    code = log((im_m + (re_m * (re_m * ((((-0.125d0) * ((re_m * (re_m / im_m)) / im_m)) / im_m) + (0.5d0 / im_m)))))) / log(10.0d0)
end function
re_m = Math.abs(re);
im_m = Math.abs(im);
assert re_m < im_m;
public static double code(double re_m, double im_m) {
	return Math.log((im_m + (re_m * (re_m * (((-0.125 * ((re_m * (re_m / im_m)) / im_m)) / im_m) + (0.5 / im_m)))))) / Math.log(10.0);
}
re_m = math.fabs(re)
im_m = math.fabs(im)
[re_m, im_m] = sort([re_m, im_m])
def code(re_m, im_m):
	return math.log((im_m + (re_m * (re_m * (((-0.125 * ((re_m * (re_m / im_m)) / im_m)) / im_m) + (0.5 / im_m)))))) / math.log(10.0)
re_m = abs(re)
im_m = abs(im)
re_m, im_m = sort([re_m, im_m])
function code(re_m, im_m)
	return Float64(log(Float64(im_m + Float64(re_m * Float64(re_m * Float64(Float64(Float64(-0.125 * Float64(Float64(re_m * Float64(re_m / im_m)) / im_m)) / im_m) + Float64(0.5 / im_m)))))) / log(10.0))
end
re_m = abs(re);
im_m = abs(im);
re_m, im_m = num2cell(sort([re_m, im_m])){:}
function tmp = code(re_m, im_m)
	tmp = log((im_m + (re_m * (re_m * (((-0.125 * ((re_m * (re_m / im_m)) / im_m)) / im_m) + (0.5 / im_m)))))) / log(10.0);
end
re_m = N[Abs[re], $MachinePrecision]
im_m = N[Abs[im], $MachinePrecision]
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
code[re$95$m_, im$95$m_] := N[(N[Log[N[(im$95$m + N[(re$95$m * N[(re$95$m * N[(N[(N[(-0.125 * N[(N[(re$95$m * N[(re$95$m / im$95$m), $MachinePrecision]), $MachinePrecision] / im$95$m), $MachinePrecision]), $MachinePrecision] / im$95$m), $MachinePrecision] + N[(0.5 / im$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[Log[10.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
re_m = \left|re\right|
\\
im_m = \left|im\right|
\\
[re_m, im_m] = \mathsf{sort}([re_m, im_m])\\
\\
\frac{\log \left(im\_m + re\_m \cdot \left(re\_m \cdot \left(\frac{-0.125 \cdot \frac{re\_m \cdot \frac{re\_m}{im\_m}}{im\_m}}{im\_m} + \frac{0.5}{im\_m}\right)\right)\right)}{\log 10}
\end{array}
Derivation
  1. Initial program 50.7%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
  2. Step-by-step derivation
    1. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\log \left(\sqrt{re \cdot re + im \cdot im}\right), \color{blue}{\log 10}\right) \]
    2. log-lowering-log.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right), \log \color{blue}{10}\right) \]
    3. hypot-defineN/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right), \log 10\right) \]
    4. hypot-lowering-hypot.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right), \log 10\right) \]
    5. log-lowering-log.f6499.1%

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
  3. Simplified99.1%

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

    \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\color{blue}{\left(im + {re}^{2} \cdot \left(\frac{-1}{8} \cdot \frac{{re}^{2}}{{im}^{3}} + \frac{1}{2} \cdot \frac{1}{im}\right)\right)}\right), \mathsf{log.f64}\left(10\right)\right) \]
  6. Step-by-step derivation
    1. +-lowering-+.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left({re}^{2} \cdot \left(\frac{-1}{8} \cdot \frac{{re}^{2}}{{im}^{3}} + \frac{1}{2} \cdot \frac{1}{im}\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    2. unpow2N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(\left(re \cdot re\right) \cdot \left(\frac{-1}{8} \cdot \frac{{re}^{2}}{{im}^{3}} + \frac{1}{2} \cdot \frac{1}{im}\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    3. associate-*l*N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(re \cdot \left(re \cdot \left(\frac{-1}{8} \cdot \frac{{re}^{2}}{{im}^{3}} + \frac{1}{2} \cdot \frac{1}{im}\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(re, \left(re \cdot \left(\frac{-1}{8} \cdot \frac{{re}^{2}}{{im}^{3}} + \frac{1}{2} \cdot \frac{1}{im}\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    5. *-lowering-*.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(re, \left(\frac{-1}{8} \cdot \frac{{re}^{2}}{{im}^{3}} + \frac{1}{2} \cdot \frac{1}{im}\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    6. +-lowering-+.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(re, \mathsf{+.f64}\left(\left(\frac{-1}{8} \cdot \frac{{re}^{2}}{{im}^{3}}\right), \left(\frac{1}{2} \cdot \frac{1}{im}\right)\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
  7. Simplified22.3%

    \[\leadsto \frac{\log \color{blue}{\left(im + re \cdot \left(re \cdot \left(\frac{-0.125 \cdot \frac{\frac{re \cdot re}{im}}{im}}{im} + \frac{0.5}{im}\right)\right)\right)}}{\log 10} \]
  8. Step-by-step derivation
    1. associate-/l*N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(re, \mathsf{+.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{-1}{8}, \mathsf{/.f64}\left(\left(re \cdot \frac{re}{im}\right), im\right)\right), im\right), \mathsf{/.f64}\left(\frac{1}{2}, im\right)\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    2. *-commutativeN/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(re, \mathsf{+.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{-1}{8}, \mathsf{/.f64}\left(\left(\frac{re}{im} \cdot re\right), im\right)\right), im\right), \mathsf{/.f64}\left(\frac{1}{2}, im\right)\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(re, \mathsf{+.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{-1}{8}, \mathsf{/.f64}\left(\mathsf{*.f64}\left(\left(\frac{re}{im}\right), re\right), im\right)\right), im\right), \mathsf{/.f64}\left(\frac{1}{2}, im\right)\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
    4. /-lowering-/.f6423.3%

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(re, \mathsf{*.f64}\left(re, \mathsf{+.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{-1}{8}, \mathsf{/.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(re, im\right), re\right), im\right)\right), im\right), \mathsf{/.f64}\left(\frac{1}{2}, im\right)\right)\right)\right)\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
  9. Applied egg-rr23.3%

    \[\leadsto \frac{\log \left(im + re \cdot \left(re \cdot \left(\frac{-0.125 \cdot \frac{\color{blue}{\frac{re}{im} \cdot re}}{im}}{im} + \frac{0.5}{im}\right)\right)\right)}{\log 10} \]
  10. Final simplification23.3%

    \[\leadsto \frac{\log \left(im + re \cdot \left(re \cdot \left(\frac{-0.125 \cdot \frac{re \cdot \frac{re}{im}}{im}}{im} + \frac{0.5}{im}\right)\right)\right)}{\log 10} \]
  11. Add Preprocessing

Alternative 2: 98.4% accurate, 1.5× speedup?

\[\begin{array}{l} re_m = \left|re\right| \\ im_m = \left|im\right| \\ [re_m, im_m] = \mathsf{sort}([re_m, im_m])\\ \\ \frac{\log im\_m}{\log 10} \end{array} \]
re_m = (fabs.f64 re)
im_m = (fabs.f64 im)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
(FPCore (re_m im_m) :precision binary64 (/ (log im_m) (log 10.0)))
re_m = fabs(re);
im_m = fabs(im);
assert(re_m < im_m);
double code(double re_m, double im_m) {
	return log(im_m) / log(10.0);
}
re_m = abs(re)
im_m = abs(im)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
real(8) function code(re_m, im_m)
    real(8), intent (in) :: re_m
    real(8), intent (in) :: im_m
    code = log(im_m) / log(10.0d0)
end function
re_m = Math.abs(re);
im_m = Math.abs(im);
assert re_m < im_m;
public static double code(double re_m, double im_m) {
	return Math.log(im_m) / Math.log(10.0);
}
re_m = math.fabs(re)
im_m = math.fabs(im)
[re_m, im_m] = sort([re_m, im_m])
def code(re_m, im_m):
	return math.log(im_m) / math.log(10.0)
re_m = abs(re)
im_m = abs(im)
re_m, im_m = sort([re_m, im_m])
function code(re_m, im_m)
	return Float64(log(im_m) / log(10.0))
end
re_m = abs(re);
im_m = abs(im);
re_m, im_m = num2cell(sort([re_m, im_m])){:}
function tmp = code(re_m, im_m)
	tmp = log(im_m) / log(10.0);
end
re_m = N[Abs[re], $MachinePrecision]
im_m = N[Abs[im], $MachinePrecision]
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
code[re$95$m_, im$95$m_] := N[(N[Log[im$95$m], $MachinePrecision] / N[Log[10.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
re_m = \left|re\right|
\\
im_m = \left|im\right|
\\
[re_m, im_m] = \mathsf{sort}([re_m, im_m])\\
\\
\frac{\log im\_m}{\log 10}
\end{array}
Derivation
  1. Initial program 50.7%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
  2. Step-by-step derivation
    1. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\log \left(\sqrt{re \cdot re + im \cdot im}\right), \color{blue}{\log 10}\right) \]
    2. log-lowering-log.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right), \log \color{blue}{10}\right) \]
    3. hypot-defineN/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right), \log 10\right) \]
    4. hypot-lowering-hypot.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right), \log 10\right) \]
    5. log-lowering-log.f6499.1%

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right), \mathsf{log.f64}\left(10\right)\right) \]
  3. Simplified99.1%

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

    \[\leadsto \color{blue}{\frac{\log im}{\log 10}} \]
  6. Step-by-step derivation
    1. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\log im, \color{blue}{\log 10}\right) \]
    2. log-lowering-log.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(im\right), \log \color{blue}{10}\right) \]
    3. log-lowering-log.f6425.2%

      \[\leadsto \mathsf{/.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{log.f64}\left(10\right)\right) \]
  7. Simplified25.2%

    \[\leadsto \color{blue}{\frac{\log im}{\log 10}} \]
  8. Add Preprocessing

Reproduce

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