_multiplyComplex, imaginary part

Percentage Accurate: 99.1% → 99.1%
Time: 1.1s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[x.re \cdot y.im + x.im \cdot y.re \]
(FPCore (x.re x.im y.re y.im)
  :precision binary64
  (+ (* x.re y.im) (* x.im y.re)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
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(x_46re, x_46im, y_46re, y_46im)
use fmin_fmax_functions
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    code = (x_46re * y_46im) + (x_46im * y_46re)
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return (x_46_re * y_46_im) + (x_46_im * y_46_re)
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(Float64(x_46_re * y_46_im) + Float64(x_46_im * y_46_re))
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = (x_46_re * y_46_im) + (x_46_im * y_46_re);
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(N[(x$46$re * y$46$im), $MachinePrecision] + N[(x$46$im * y$46$re), $MachinePrecision]), $MachinePrecision]
x.re \cdot y.im + x.im \cdot y.re

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 3 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: 99.1% accurate, 1.0× speedup?

\[x.re \cdot y.im + x.im \cdot y.re \]
(FPCore (x.re x.im y.re y.im)
  :precision binary64
  (+ (* x.re y.im) (* x.im y.re)))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
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(x_46re, x_46im, y_46re, y_46im)
use fmin_fmax_functions
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    code = (x_46re * y_46im) + (x_46im * y_46re)
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	return (x_46_re * y_46_im) + (x_46_im * y_46_re);
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	return (x_46_re * y_46_im) + (x_46_im * y_46_re)
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	return Float64(Float64(x_46_re * y_46_im) + Float64(x_46_im * y_46_re))
end
function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = (x_46_re * y_46_im) + (x_46_im * y_46_re);
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(N[(x$46$re * y$46$im), $MachinePrecision] + N[(x$46$im * y$46$re), $MachinePrecision]), $MachinePrecision]
x.re \cdot y.im + x.im \cdot y.re

Alternative 1: 75.9% accurate, 0.5× speedup?

\[\begin{array}{l} \mathbf{if}\;x.im \cdot y.re \leq -135000000000000001299567467802132480:\\ \;\;\;\;x.im \cdot y.re\\ \mathbf{elif}\;x.im \cdot y.re \leq 1680000000000000006915647556027450635070041971932968113865158897434624:\\ \;\;\;\;x.re \cdot y.im\\ \mathbf{else}:\\ \;\;\;\;x.im \cdot y.re\\ \end{array} \]
(FPCore (x.re x.im y.re y.im)
  :precision binary64
  (if (<= (* x.im y.re) -135000000000000001299567467802132480)
  (* x.im y.re)
  (if (<=
       (* x.im y.re)
       1680000000000000006915647556027450635070041971932968113865158897434624)
    (* x.re y.im)
    (* x.im y.re))))
double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if ((x_46_im * y_46_re) <= -1.35e+35) {
		tmp = x_46_im * y_46_re;
	} else if ((x_46_im * y_46_re) <= 1.68e+69) {
		tmp = x_46_re * y_46_im;
	} else {
		tmp = x_46_im * y_46_re;
	}
	return tmp;
}
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(x_46re, x_46im, y_46re, y_46im)
use fmin_fmax_functions
    real(8), intent (in) :: x_46re
    real(8), intent (in) :: x_46im
    real(8), intent (in) :: y_46re
    real(8), intent (in) :: y_46im
    real(8) :: tmp
    if ((x_46im * y_46re) <= (-1.35d+35)) then
        tmp = x_46im * y_46re
    else if ((x_46im * y_46re) <= 1.68d+69) then
        tmp = x_46re * y_46im
    else
        tmp = x_46im * y_46re
    end if
    code = tmp
end function
public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
	double tmp;
	if ((x_46_im * y_46_re) <= -1.35e+35) {
		tmp = x_46_im * y_46_re;
	} else if ((x_46_im * y_46_re) <= 1.68e+69) {
		tmp = x_46_re * y_46_im;
	} else {
		tmp = x_46_im * y_46_re;
	}
	return tmp;
}
def code(x_46_re, x_46_im, y_46_re, y_46_im):
	tmp = 0
	if (x_46_im * y_46_re) <= -1.35e+35:
		tmp = x_46_im * y_46_re
	elif (x_46_im * y_46_re) <= 1.68e+69:
		tmp = x_46_re * y_46_im
	else:
		tmp = x_46_im * y_46_re
	return tmp
function code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0
	if (Float64(x_46_im * y_46_re) <= -1.35e+35)
		tmp = Float64(x_46_im * y_46_re);
	elseif (Float64(x_46_im * y_46_re) <= 1.68e+69)
		tmp = Float64(x_46_re * y_46_im);
	else
		tmp = Float64(x_46_im * y_46_re);
	end
	return tmp
end
function tmp_2 = code(x_46_re, x_46_im, y_46_re, y_46_im)
	tmp = 0.0;
	if ((x_46_im * y_46_re) <= -1.35e+35)
		tmp = x_46_im * y_46_re;
	elseif ((x_46_im * y_46_re) <= 1.68e+69)
		tmp = x_46_re * y_46_im;
	else
		tmp = x_46_im * y_46_re;
	end
	tmp_2 = tmp;
end
code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := If[LessEqual[N[(x$46$im * y$46$re), $MachinePrecision], -135000000000000001299567467802132480], N[(x$46$im * y$46$re), $MachinePrecision], If[LessEqual[N[(x$46$im * y$46$re), $MachinePrecision], 1680000000000000006915647556027450635070041971932968113865158897434624], N[(x$46$re * y$46$im), $MachinePrecision], N[(x$46$im * y$46$re), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;x.im \cdot y.re \leq -135000000000000001299567467802132480:\\
\;\;\;\;x.im \cdot y.re\\

\mathbf{elif}\;x.im \cdot y.re \leq 1680000000000000006915647556027450635070041971932968113865158897434624:\\
\;\;\;\;x.re \cdot y.im\\

\mathbf{else}:\\
\;\;\;\;x.im \cdot y.re\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x.im y.re) < -1.35e35 or 1.68e69 < (*.f64 x.im y.re)

    1. Initial program 99.1%

      \[x.re \cdot y.im + x.im \cdot y.re \]
    2. Taylor expanded in x.re around 0

      \[\leadsto \color{blue}{x.im \cdot y.re} \]
    3. Step-by-step derivation
      1. lower-*.f6451.8%

        \[\leadsto x.im \cdot \color{blue}{y.re} \]
    4. Applied rewrites51.8%

      \[\leadsto \color{blue}{x.im \cdot y.re} \]

    if -1.35e35 < (*.f64 x.im y.re) < 1.68e69

    1. Initial program 99.1%

      \[x.re \cdot y.im + x.im \cdot y.re \]
    2. Taylor expanded in x.re around inf

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

        \[\leadsto x.re \cdot \color{blue}{\left(y.im + \frac{x.im \cdot y.re}{x.re}\right)} \]
      2. lower-+.f64N/A

        \[\leadsto x.re \cdot \left(y.im + \color{blue}{\frac{x.im \cdot y.re}{x.re}}\right) \]
      3. lower-/.f64N/A

        \[\leadsto x.re \cdot \left(y.im + \frac{x.im \cdot y.re}{\color{blue}{x.re}}\right) \]
      4. lower-*.f6491.7%

        \[\leadsto x.re \cdot \left(y.im + \frac{x.im \cdot y.re}{x.re}\right) \]
    4. Applied rewrites91.7%

      \[\leadsto \color{blue}{x.re \cdot \left(y.im + \frac{x.im \cdot y.re}{x.re}\right)} \]
    5. Taylor expanded in x.re around inf

      \[\leadsto x.re \cdot y.im \]
    6. Step-by-step derivation
      1. Applied rewrites52.0%

        \[\leadsto x.re \cdot y.im \]
    7. Recombined 2 regimes into one program.
    8. Add Preprocessing

    Alternative 2: 51.8% accurate, 2.3× speedup?

    \[x.im \cdot y.re \]
    (FPCore (x.re x.im y.re y.im)
      :precision binary64
      (* x.im y.re))
    double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
    	return x_46_im * y_46_re;
    }
    
    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(x_46re, x_46im, y_46re, y_46im)
    use fmin_fmax_functions
        real(8), intent (in) :: x_46re
        real(8), intent (in) :: x_46im
        real(8), intent (in) :: y_46re
        real(8), intent (in) :: y_46im
        code = x_46im * y_46re
    end function
    
    public static double code(double x_46_re, double x_46_im, double y_46_re, double y_46_im) {
    	return x_46_im * y_46_re;
    }
    
    def code(x_46_re, x_46_im, y_46_re, y_46_im):
    	return x_46_im * y_46_re
    
    function code(x_46_re, x_46_im, y_46_re, y_46_im)
    	return Float64(x_46_im * y_46_re)
    end
    
    function tmp = code(x_46_re, x_46_im, y_46_re, y_46_im)
    	tmp = x_46_im * y_46_re;
    end
    
    code[x$46$re_, x$46$im_, y$46$re_, y$46$im_] := N[(x$46$im * y$46$re), $MachinePrecision]
    
    x.im \cdot y.re
    
    Derivation
    1. Initial program 99.1%

      \[x.re \cdot y.im + x.im \cdot y.re \]
    2. Taylor expanded in x.re around 0

      \[\leadsto \color{blue}{x.im \cdot y.re} \]
    3. Step-by-step derivation
      1. lower-*.f6451.8%

        \[\leadsto x.im \cdot \color{blue}{y.re} \]
    4. Applied rewrites51.8%

      \[\leadsto \color{blue}{x.im \cdot y.re} \]
    5. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2025285 -o generate:evaluate
    (FPCore (x.re x.im y.re y.im)
      :name "_multiplyComplex, imaginary part"
      :precision binary64
      (+ (* x.re y.im) (* x.im y.re)))