math.log10 on complex, real part

Percentage Accurate: 51.3% → 98.7%
Time: 11.2s
Alternatives: 5
Speedup: 1.1×

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 5 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.3% 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.7% accurate, 0.6× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ re_m = \left|re\right| \\ [re_m, im_m] = \mathsf{sort}([re_m, im_m])\\ \\ \left(\log 0.1 \cdot {\log 0.1}^{-2}\right) \cdot \left(-\log im\_m\right) \end{array} \]
im_m = (fabs.f64 im)
re_m = (fabs.f64 re)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
(FPCore (re_m im_m)
 :precision binary64
 (* (* (log 0.1) (pow (log 0.1) -2.0)) (- (log im_m))))
im_m = fabs(im);
re_m = fabs(re);
assert(re_m < im_m);
double code(double re_m, double im_m) {
	return (log(0.1) * pow(log(0.1), -2.0)) * -log(im_m);
}
im_m = abs(im)
re_m = abs(re)
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(0.1d0) * (log(0.1d0) ** (-2.0d0))) * -log(im_m)
end function
im_m = Math.abs(im);
re_m = Math.abs(re);
assert re_m < im_m;
public static double code(double re_m, double im_m) {
	return (Math.log(0.1) * Math.pow(Math.log(0.1), -2.0)) * -Math.log(im_m);
}
im_m = math.fabs(im)
re_m = math.fabs(re)
[re_m, im_m] = sort([re_m, im_m])
def code(re_m, im_m):
	return (math.log(0.1) * math.pow(math.log(0.1), -2.0)) * -math.log(im_m)
im_m = abs(im)
re_m = abs(re)
re_m, im_m = sort([re_m, im_m])
function code(re_m, im_m)
	return Float64(Float64(log(0.1) * (log(0.1) ^ -2.0)) * Float64(-log(im_m)))
end
im_m = abs(im);
re_m = abs(re);
re_m, im_m = num2cell(sort([re_m, im_m])){:}
function tmp = code(re_m, im_m)
	tmp = (log(0.1) * (log(0.1) ^ -2.0)) * -log(im_m);
end
im_m = N[Abs[im], $MachinePrecision]
re_m = N[Abs[re], $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[(N[Log[0.1], $MachinePrecision] * N[Power[N[Log[0.1], $MachinePrecision], -2.0], $MachinePrecision]), $MachinePrecision] * (-N[Log[im$95$m], $MachinePrecision])), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
re_m = \left|re\right|
\\
[re_m, im_m] = \mathsf{sort}([re_m, im_m])\\
\\
\left(\log 0.1 \cdot {\log 0.1}^{-2}\right) \cdot \left(-\log im\_m\right)
\end{array}
Derivation
  1. Initial program 49.2%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10} \]
  2. Add Preprocessing
  3. Taylor expanded in re around 0

    \[\leadsto \frac{\color{blue}{\log im}}{\log 10} \]
  4. Step-by-step derivation
    1. lower-log.f6498.4

      \[\leadsto \frac{\color{blue}{\log im}}{\log 10} \]
  5. Applied rewrites98.4%

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

      \[\leadsto \color{blue}{\frac{\log im}{\log 10}} \]
    2. clear-numN/A

      \[\leadsto \color{blue}{\frac{1}{\frac{\log 10}{\log im}}} \]
    3. frac-2negN/A

      \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{neg}\left(\log 10\right)}{\mathsf{neg}\left(\log im\right)}}} \]
    4. lift-log.f64N/A

      \[\leadsto \frac{1}{\frac{\mathsf{neg}\left(\color{blue}{\log 10}\right)}{\mathsf{neg}\left(\log im\right)}} \]
    5. neg-logN/A

      \[\leadsto \frac{1}{\frac{\color{blue}{\log \left(\frac{1}{10}\right)}}{\mathsf{neg}\left(\log im\right)}} \]
    6. metadata-evalN/A

      \[\leadsto \frac{1}{\frac{\log \color{blue}{\frac{1}{10}}}{\mathsf{neg}\left(\log im\right)}} \]
    7. lift-log.f64N/A

      \[\leadsto \frac{1}{\frac{\color{blue}{\log \frac{1}{10}}}{\mathsf{neg}\left(\log im\right)}} \]
    8. associate-/r/N/A

      \[\leadsto \color{blue}{\frac{1}{\log \frac{1}{10}} \cdot \left(\mathsf{neg}\left(\log im\right)\right)} \]
    9. lower-*.f64N/A

      \[\leadsto \color{blue}{\frac{1}{\log \frac{1}{10}} \cdot \left(\mathsf{neg}\left(\log im\right)\right)} \]
    10. frac-2negN/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(1\right)}{\mathsf{neg}\left(\log \frac{1}{10}\right)}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    11. metadata-evalN/A

      \[\leadsto \frac{\color{blue}{-1}}{\mathsf{neg}\left(\log \frac{1}{10}\right)} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    12. lift-log.f64N/A

      \[\leadsto \frac{-1}{\mathsf{neg}\left(\color{blue}{\log \frac{1}{10}}\right)} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    13. neg-logN/A

      \[\leadsto \frac{-1}{\color{blue}{\log \left(\frac{1}{\frac{1}{10}}\right)}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    14. metadata-evalN/A

      \[\leadsto \frac{-1}{\log \color{blue}{10}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    15. lift-log.f64N/A

      \[\leadsto \frac{-1}{\color{blue}{\log 10}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    16. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{-1}{\log 10}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    17. lower-neg.f6497.9

      \[\leadsto \frac{-1}{\log 10} \cdot \color{blue}{\left(-\log im\right)} \]
  7. Applied rewrites97.9%

    \[\leadsto \color{blue}{\frac{-1}{\log 10} \cdot \left(-\log im\right)} \]
  8. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{-1}{\log 10}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    2. frac-2negN/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(-1\right)}{\mathsf{neg}\left(\log 10\right)}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    3. metadata-evalN/A

      \[\leadsto \frac{\color{blue}{1}}{\mathsf{neg}\left(\log 10\right)} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    4. lift-log.f64N/A

      \[\leadsto \frac{1}{\mathsf{neg}\left(\color{blue}{\log 10}\right)} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    5. neg-logN/A

      \[\leadsto \frac{1}{\color{blue}{\log \left(\frac{1}{10}\right)}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    6. metadata-evalN/A

      \[\leadsto \frac{1}{\log \color{blue}{\frac{1}{10}}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    7. lift-log.f64N/A

      \[\leadsto \frac{1}{\color{blue}{\log \frac{1}{10}}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    8. inv-powN/A

      \[\leadsto \color{blue}{{\log \frac{1}{10}}^{-1}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    9. metadata-evalN/A

      \[\leadsto {\log \frac{1}{10}}^{\color{blue}{\left(-2 + 1\right)}} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    10. pow-plusN/A

      \[\leadsto \color{blue}{\left({\log \frac{1}{10}}^{-2} \cdot \log \frac{1}{10}\right)} \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    11. lift-pow.f64N/A

      \[\leadsto \left(\color{blue}{{\log \frac{1}{10}}^{-2}} \cdot \log \frac{1}{10}\right) \cdot \left(\mathsf{neg}\left(\log im\right)\right) \]
    12. lower-*.f6498.9

      \[\leadsto \color{blue}{\left({\log 0.1}^{-2} \cdot \log 0.1\right)} \cdot \left(-\log im\right) \]
  9. Applied rewrites98.9%

    \[\leadsto \color{blue}{\left({\log 0.1}^{-2} \cdot \log 0.1\right)} \cdot \left(-\log im\right) \]
  10. Final simplification98.9%

    \[\leadsto \left(\log 0.1 \cdot {\log 0.1}^{-2}\right) \cdot \left(-\log im\right) \]
  11. Add Preprocessing

Alternative 2: 98.6% accurate, 0.6× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ re_m = \left|re\right| \\ [re_m, im_m] = \mathsf{sort}([re_m, im_m])\\ \\ {\log 0.1}^{-2} \cdot \left(\log 0.1 \cdot \left(-\log im\_m\right)\right) \end{array} \]
im_m = (fabs.f64 im)
re_m = (fabs.f64 re)
NOTE: re_m and im_m should be sorted in increasing order before calling this function.
(FPCore (re_m im_m)
 :precision binary64
 (* (pow (log 0.1) -2.0) (* (log 0.1) (- (log im_m)))))
im_m = fabs(im);
re_m = fabs(re);
assert(re_m < im_m);
double code(double re_m, double im_m) {
	return pow(log(0.1), -2.0) * (log(0.1) * -log(im_m));
}
im_m = abs(im)
re_m = abs(re)
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(0.1d0) ** (-2.0d0)) * (log(0.1d0) * -log(im_m))
end function
im_m = Math.abs(im);
re_m = Math.abs(re);
assert re_m < im_m;
public static double code(double re_m, double im_m) {
	return Math.pow(Math.log(0.1), -2.0) * (Math.log(0.1) * -Math.log(im_m));
}
im_m = math.fabs(im)
re_m = math.fabs(re)
[re_m, im_m] = sort([re_m, im_m])
def code(re_m, im_m):
	return math.pow(math.log(0.1), -2.0) * (math.log(0.1) * -math.log(im_m))
im_m = abs(im)
re_m = abs(re)
re_m, im_m = sort([re_m, im_m])
function code(re_m, im_m)
	return Float64((log(0.1) ^ -2.0) * Float64(log(0.1) * Float64(-log(im_m))))
end
im_m = abs(im);
re_m = abs(re);
re_m, im_m = num2cell(sort([re_m, im_m])){:}
function tmp = code(re_m, im_m)
	tmp = (log(0.1) ^ -2.0) * (log(0.1) * -log(im_m));
end
im_m = N[Abs[im], $MachinePrecision]
re_m = N[Abs[re], $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[Power[N[Log[0.1], $MachinePrecision], -2.0], $MachinePrecision] * N[(N[Log[0.1], $MachinePrecision] * (-N[Log[im$95$m], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
re_m = \left|re\right|
\\
[re_m, im_m] = \mathsf{sort}([re_m, im_m])\\
\\
{\log 0.1}^{-2} \cdot \left(\log 0.1 \cdot \left(-\log im\_m\right)\right)
\end{array}
Derivation
  1. Initial program 51.3%

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

      \[\leadsto \color{blue}{\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}{\log 10}} \]
    2. frac-2negN/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\log \left(\sqrt{re \cdot re + im \cdot im}\right)\right)}{\mathsf{neg}\left(\log 10\right)}} \]
    3. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(\log \left(\sqrt{re \cdot re + im \cdot im}\right)\right)}{\mathsf{neg}\left(\log 10\right)}} \]
    4. lift-log.f64N/A

      \[\leadsto \frac{\mathsf{neg}\left(\color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}\right)}{\mathsf{neg}\left(\log 10\right)} \]
    5. lift-sqrt.f64N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \color{blue}{\left(\sqrt{re \cdot re + im \cdot im}\right)}\right)}{\mathsf{neg}\left(\log 10\right)} \]
    6. pow1/2N/A

      \[\leadsto \frac{\mathsf{neg}\left(\log \color{blue}{\left({\left(re \cdot re + im \cdot im\right)}^{\frac{1}{2}}\right)}\right)}{\mathsf{neg}\left(\log 10\right)} \]
    7. log-powN/A

      \[\leadsto \frac{\mathsf{neg}\left(\color{blue}{\frac{1}{2} \cdot \log \left(re \cdot re + im \cdot im\right)}\right)}{\mathsf{neg}\left(\log 10\right)} \]
    8. distribute-lft-neg-inN/A

      \[\leadsto \frac{\color{blue}{\left(\mathsf{neg}\left(\frac{1}{2}\right)\right) \cdot \log \left(re \cdot re + im \cdot im\right)}}{\mathsf{neg}\left(\log 10\right)} \]
    9. lower-*.f64N/A

      \[\leadsto \frac{\color{blue}{\left(\mathsf{neg}\left(\frac{1}{2}\right)\right) \cdot \log \left(re \cdot re + im \cdot im\right)}}{\mathsf{neg}\left(\log 10\right)} \]
    10. metadata-evalN/A

      \[\leadsto \frac{\color{blue}{\frac{-1}{2}} \cdot \log \left(re \cdot re + im \cdot im\right)}{\mathsf{neg}\left(\log 10\right)} \]
    11. lower-log.f64N/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \color{blue}{\log \left(re \cdot re + im \cdot im\right)}}{\mathsf{neg}\left(\log 10\right)} \]
    12. lift-+.f64N/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \log \color{blue}{\left(re \cdot re + im \cdot im\right)}}{\mathsf{neg}\left(\log 10\right)} \]
    13. +-commutativeN/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \log \color{blue}{\left(im \cdot im + re \cdot re\right)}}{\mathsf{neg}\left(\log 10\right)} \]
    14. lift-*.f64N/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \log \left(\color{blue}{im \cdot im} + re \cdot re\right)}{\mathsf{neg}\left(\log 10\right)} \]
    15. lower-fma.f64N/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \log \color{blue}{\left(\mathsf{fma}\left(im, im, re \cdot re\right)\right)}}{\mathsf{neg}\left(\log 10\right)} \]
    16. lift-log.f64N/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \log \left(\mathsf{fma}\left(im, im, re \cdot re\right)\right)}{\mathsf{neg}\left(\color{blue}{\log 10}\right)} \]
    17. neg-logN/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \log \left(\mathsf{fma}\left(im, im, re \cdot re\right)\right)}{\color{blue}{\log \left(\frac{1}{10}\right)}} \]
    18. lower-log.f64N/A

      \[\leadsto \frac{\frac{-1}{2} \cdot \log \left(\mathsf{fma}\left(im, im, re \cdot re\right)\right)}{\color{blue}{\log \left(\frac{1}{10}\right)}} \]
    19. metadata-eval51.3

      \[\leadsto \frac{-0.5 \cdot \log \left(\mathsf{fma}\left(im, im, re \cdot re\right)\right)}{\log \color{blue}{0.1}} \]
  4. Applied rewrites51.3%

    \[\leadsto \color{blue}{\frac{-0.5 \cdot \log \left(\mathsf{fma}\left(im, im, re \cdot re\right)\right)}{\log 0.1}} \]
  5. Applied rewrites51.5%

    \[\leadsto \color{blue}{\left(\log 0.1 \cdot \left(-0.5 \cdot \log \left(\mathsf{fma}\left(re, re, im \cdot im\right)\right)\right)\right) \cdot {\log 0.1}^{-2}} \]
  6. Taylor expanded in im around inf

    \[\leadsto \left(\log \frac{1}{10} \cdot \color{blue}{\log \left(\frac{1}{im}\right)}\right) \cdot {\log \frac{1}{10}}^{-2} \]
  7. Step-by-step derivation
    1. log-recN/A

      \[\leadsto \left(\log \frac{1}{10} \cdot \color{blue}{\left(\mathsf{neg}\left(\log im\right)\right)}\right) \cdot {\log \frac{1}{10}}^{-2} \]
    2. lower-neg.f64N/A

      \[\leadsto \left(\log \frac{1}{10} \cdot \color{blue}{\left(\mathsf{neg}\left(\log im\right)\right)}\right) \cdot {\log \frac{1}{10}}^{-2} \]
    3. lower-log.f6498.6

      \[\leadsto \left(\log 0.1 \cdot \left(-\color{blue}{\log im}\right)\right) \cdot {\log 0.1}^{-2} \]
  8. Applied rewrites98.6%

    \[\leadsto \left(\log 0.1 \cdot \color{blue}{\left(-\log im\right)}\right) \cdot {\log 0.1}^{-2} \]
  9. Final simplification98.6%

    \[\leadsto {\log 0.1}^{-2} \cdot \left(\log 0.1 \cdot \left(-\log im\right)\right) \]
  10. Add Preprocessing

Reproduce

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