math.log/1 on complex, real part

Percentage Accurate: 51.8% → 99.2%
Time: 1.5s
Alternatives: 1
Speedup: 0.6×

Specification

?
\[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
(FPCore (re im)
  :precision binary64
  (log (sqrt (+ (* re re) (* im im)))))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    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]
\log \left(\sqrt{re \cdot re + im \cdot im}\right)

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 1 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.8% accurate, 1.0× speedup?

\[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
(FPCore (re im)
  :precision binary64
  (log (sqrt (+ (* re re) (* im im)))))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    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]
\log \left(\sqrt{re \cdot re + im \cdot im}\right)

Alternative 1: 99.2% accurate, 0.6× speedup?

\[\log \left(\mathsf{max}\left(\left|re\right|, \left|im\right|\right)\right) \]
(FPCore (re im)
  :precision binary64
  (log (fmax (fabs re) (fabs im))))
double code(double re, double im) {
	return log(fmax(fabs(re), fabs(im)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(fmax(abs(re), abs(im)))
end function
public static double code(double re, double im) {
	return Math.log(fmax(Math.abs(re), Math.abs(im)));
}
def code(re, im):
	return math.log(fmax(math.fabs(re), math.fabs(im)))
function code(re, im)
	return log(fmax(abs(re), abs(im)))
end
function tmp = code(re, im)
	tmp = log(max(abs(re), abs(im)));
end
code[re_, im_] := N[Log[N[Max[N[Abs[re], $MachinePrecision], N[Abs[im], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\log \left(\mathsf{max}\left(\left|re\right|, \left|im\right|\right)\right)
Derivation
  1. Initial program 51.8%

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

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

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

      \[\leadsto -1 \cdot \log \left(\frac{1}{im}\right) \]
    3. lower-/.f6427.7%

      \[\leadsto -1 \cdot \log \left(\frac{1}{im}\right) \]
  4. Applied rewrites27.7%

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

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

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

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

      \[\leadsto \log \left(\frac{1}{\frac{1}{im}}\right) \]
    5. lift-/.f64N/A

      \[\leadsto \log \left(\frac{1}{\frac{1}{im}}\right) \]
    6. frac-2negN/A

      \[\leadsto \log \left(\frac{1}{\frac{\mathsf{neg}\left(1\right)}{\mathsf{neg}\left(im\right)}}\right) \]
    7. distribute-frac-negN/A

      \[\leadsto \log \left(\frac{1}{\mathsf{neg}\left(\frac{1}{\mathsf{neg}\left(im\right)}\right)}\right) \]
    8. distribute-neg-frac2N/A

      \[\leadsto \log \left(\frac{1}{\frac{1}{\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)}}\right) \]
    9. remove-double-divN/A

      \[\leadsto \log \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \]
    10. lower-log.f64N/A

      \[\leadsto \log \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(im\right)\right)\right)\right) \]
    11. mul-1-negN/A

      \[\leadsto \log \left(\mathsf{neg}\left(-1 \cdot im\right)\right) \]
    12. *-commutativeN/A

      \[\leadsto \log \left(\mathsf{neg}\left(im \cdot -1\right)\right) \]
    13. distribute-rgt-neg-outN/A

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

      \[\leadsto \log \left(im \cdot 1\right) \]
    15. *-rgt-identity27.7%

      \[\leadsto \log im \]
  6. Applied rewrites27.7%

    \[\leadsto \log im \]
  7. Add Preprocessing

Reproduce

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