math.square on complex, real part

Percentage Accurate: 93.7% → 100.0%
Time: 1.4s
Alternatives: 4
Speedup: 1.2×

Specification

?
\[re \cdot re - im \cdot im \]
(FPCore re_sqr (re im)
  :precision binary64
  (- (* re re) (* im im)))
double re_sqr(double re, double im) {
	return (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 re_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
	return (re * re) - (im * im);
}
def re_sqr(re, im):
	return (re * re) - (im * im)
function re_sqr(re, im)
	return Float64(Float64(re * re) - Float64(im * im))
end
function tmp = re_sqr(re, im)
	tmp = (re * re) - (im * im);
end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
re \cdot re - im \cdot im

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

\[re \cdot re - im \cdot im \]
(FPCore re_sqr (re im)
  :precision binary64
  (- (* re re) (* im im)))
double re_sqr(double re, double im) {
	return (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 re_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    re_sqr = (re * re) - (im * im)
end function
public static double re_sqr(double re, double im) {
	return (re * re) - (im * im);
}
def re_sqr(re, im):
	return (re * re) - (im * im)
function re_sqr(re, im)
	return Float64(Float64(re * re) - Float64(im * im))
end
function tmp = re_sqr(re, im)
	tmp = (re * re) - (im * im);
end
re$95$sqr[re_, im_] := N[(N[(re * re), $MachinePrecision] - N[(im * im), $MachinePrecision]), $MachinePrecision]
re \cdot re - im \cdot im

Alternative 1: 100.0% accurate, 1.2× speedup?

\[\left(re - im\right) \cdot \left(im + re\right) \]
(FPCore re_sqr (re im)
  :precision binary64
  (* (- re im) (+ im re)))
double re_sqr(double re, double im) {
	return (re - im) * (im + 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 re_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    re_sqr = (re - im) * (im + re)
end function
public static double re_sqr(double re, double im) {
	return (re - im) * (im + re);
}
def re_sqr(re, im):
	return (re - im) * (im + re)
function re_sqr(re, im)
	return Float64(Float64(re - im) * Float64(im + re))
end
function tmp = re_sqr(re, im)
	tmp = (re - im) * (im + re);
end
re$95$sqr[re_, im_] := N[(N[(re - im), $MachinePrecision] * N[(im + re), $MachinePrecision]), $MachinePrecision]
\left(re - im\right) \cdot \left(im + re\right)
Derivation
  1. Initial program 93.7%

    \[re \cdot re - im \cdot im \]
  2. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{re \cdot re - im \cdot im} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{re \cdot re} - im \cdot im \]
    3. lift-*.f64N/A

      \[\leadsto re \cdot re - \color{blue}{im \cdot im} \]
    4. difference-of-squaresN/A

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
    5. *-commutativeN/A

      \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(re + im\right)} \]
    6. lower-*.f64N/A

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

      \[\leadsto \color{blue}{\left(re - im\right)} \cdot \left(re + im\right) \]
    8. +-commutativeN/A

      \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
    9. lower-+.f64100.0%

      \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(im + re\right)} \]
  4. Add Preprocessing

Alternative 2: 60.5% accurate, 0.9× speedup?

\[\left(\left|re\right| - \left|im\right|\right) \cdot \left|im\right| \]
(FPCore re_sqr (re im)
  :precision binary64
  (* (- (fabs re) (fabs im)) (fabs im)))
double re_sqr(double re, double im) {
	return (fabs(re) - fabs(im)) * 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 re_sqr(re, im)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    re_sqr = (abs(re) - abs(im)) * abs(im)
end function
public static double re_sqr(double re, double im) {
	return (Math.abs(re) - Math.abs(im)) * Math.abs(im);
}
def re_sqr(re, im):
	return (math.fabs(re) - math.fabs(im)) * math.fabs(im)
function re_sqr(re, im)
	return Float64(Float64(abs(re) - abs(im)) * abs(im))
end
function tmp = re_sqr(re, im)
	tmp = (abs(re) - abs(im)) * abs(im);
end
re$95$sqr[re_, im_] := N[(N[(N[Abs[re], $MachinePrecision] - N[Abs[im], $MachinePrecision]), $MachinePrecision] * N[Abs[im], $MachinePrecision]), $MachinePrecision]
\left(\left|re\right| - \left|im\right|\right) \cdot \left|im\right|
Derivation
  1. Initial program 93.7%

    \[re \cdot re - im \cdot im \]
  2. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{re \cdot re - im \cdot im} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{re \cdot re} - im \cdot im \]
    3. lift-*.f64N/A

      \[\leadsto re \cdot re - \color{blue}{im \cdot im} \]
    4. difference-of-squaresN/A

      \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
    5. *-commutativeN/A

      \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(re + im\right)} \]
    6. lower-*.f64N/A

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

      \[\leadsto \color{blue}{\left(re - im\right)} \cdot \left(re + im\right) \]
    8. +-commutativeN/A

      \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
    9. lower-+.f64100.0%

      \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(im + re\right)} \]
  4. Taylor expanded in re around 0

    \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
  5. Step-by-step derivation
    1. Applied rewrites56.6%

      \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
    2. Add Preprocessing

    Alternative 3: 57.7% accurate, 0.7× speedup?

    \[\begin{array}{l} \mathbf{if}\;\left|re\right| \leq 6.4 \cdot 10^{+218}:\\ \;\;\;\;\left(-\left|im\right|\right) \cdot \left|im\right|\\ \mathbf{else}:\\ \;\;\;\;\left|re\right| \cdot \left|im\right|\\ \end{array} \]
    (FPCore re_sqr (re im)
      :precision binary64
      (if (<= (fabs re) 6.4e+218)
      (* (- (fabs im)) (fabs im))
      (* (fabs re) (fabs im))))
    double re_sqr(double re, double im) {
    	double tmp;
    	if (fabs(re) <= 6.4e+218) {
    		tmp = -fabs(im) * fabs(im);
    	} else {
    		tmp = fabs(re) * fabs(im);
    	}
    	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 re_sqr(re, im)
    use fmin_fmax_functions
        real(8), intent (in) :: re
        real(8), intent (in) :: im
        real(8) :: tmp
        if (abs(re) <= 6.4d+218) then
            tmp = -abs(im) * abs(im)
        else
            tmp = abs(re) * abs(im)
        end if
        re_sqr = tmp
    end function
    
    public static double re_sqr(double re, double im) {
    	double tmp;
    	if (Math.abs(re) <= 6.4e+218) {
    		tmp = -Math.abs(im) * Math.abs(im);
    	} else {
    		tmp = Math.abs(re) * Math.abs(im);
    	}
    	return tmp;
    }
    
    def re_sqr(re, im):
    	tmp = 0
    	if math.fabs(re) <= 6.4e+218:
    		tmp = -math.fabs(im) * math.fabs(im)
    	else:
    		tmp = math.fabs(re) * math.fabs(im)
    	return tmp
    
    function re_sqr(re, im)
    	tmp = 0.0
    	if (abs(re) <= 6.4e+218)
    		tmp = Float64(Float64(-abs(im)) * abs(im));
    	else
    		tmp = Float64(abs(re) * abs(im));
    	end
    	return tmp
    end
    
    function tmp_2 = re_sqr(re, im)
    	tmp = 0.0;
    	if (abs(re) <= 6.4e+218)
    		tmp = -abs(im) * abs(im);
    	else
    		tmp = abs(re) * abs(im);
    	end
    	tmp_2 = tmp;
    end
    
    re$95$sqr[re_, im_] := If[LessEqual[N[Abs[re], $MachinePrecision], 6.4e+218], N[((-N[Abs[im], $MachinePrecision]) * N[Abs[im], $MachinePrecision]), $MachinePrecision], N[(N[Abs[re], $MachinePrecision] * N[Abs[im], $MachinePrecision]), $MachinePrecision]]
    
    \begin{array}{l}
    \mathbf{if}\;\left|re\right| \leq 6.4 \cdot 10^{+218}:\\
    \;\;\;\;\left(-\left|im\right|\right) \cdot \left|im\right|\\
    
    \mathbf{else}:\\
    \;\;\;\;\left|re\right| \cdot \left|im\right|\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if re < 6.3999999999999997e218

      1. Initial program 93.7%

        \[re \cdot re - im \cdot im \]
      2. Step-by-step derivation
        1. lift--.f64N/A

          \[\leadsto \color{blue}{re \cdot re - im \cdot im} \]
        2. lift-*.f64N/A

          \[\leadsto \color{blue}{re \cdot re} - im \cdot im \]
        3. lift-*.f64N/A

          \[\leadsto re \cdot re - \color{blue}{im \cdot im} \]
        4. difference-of-squaresN/A

          \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
        5. *-commutativeN/A

          \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(re + im\right)} \]
        6. lower-*.f64N/A

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

          \[\leadsto \color{blue}{\left(re - im\right)} \cdot \left(re + im\right) \]
        8. +-commutativeN/A

          \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
        9. lower-+.f64100.0%

          \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
      3. Applied rewrites100.0%

        \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(im + re\right)} \]
      4. Taylor expanded in re around 0

        \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
      5. Step-by-step derivation
        1. Applied rewrites56.6%

          \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
        2. Taylor expanded in re around 0

          \[\leadsto \color{blue}{\left(-1 \cdot im\right)} \cdot im \]
        3. Step-by-step derivation
          1. lower-*.f6453.3%

            \[\leadsto \left(-1 \cdot \color{blue}{im}\right) \cdot im \]
        4. Applied rewrites53.3%

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

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

            \[\leadsto \left(\mathsf{neg}\left(im\right)\right) \cdot im \]
          3. lower-neg.f6453.3%

            \[\leadsto \left(-im\right) \cdot im \]
        6. Applied rewrites53.3%

          \[\leadsto \left(-im\right) \cdot im \]

        if 6.3999999999999997e218 < re

        1. Initial program 93.7%

          \[re \cdot re - im \cdot im \]
        2. Step-by-step derivation
          1. lift--.f64N/A

            \[\leadsto \color{blue}{re \cdot re - im \cdot im} \]
          2. lift-*.f64N/A

            \[\leadsto \color{blue}{re \cdot re} - im \cdot im \]
          3. lift-*.f64N/A

            \[\leadsto re \cdot re - \color{blue}{im \cdot im} \]
          4. difference-of-squaresN/A

            \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
          5. *-commutativeN/A

            \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(re + im\right)} \]
          6. lower-*.f64N/A

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

            \[\leadsto \color{blue}{\left(re - im\right)} \cdot \left(re + im\right) \]
          8. +-commutativeN/A

            \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
          9. lower-+.f64100.0%

            \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
        3. Applied rewrites100.0%

          \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(im + re\right)} \]
        4. Taylor expanded in re around 0

          \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
        5. Step-by-step derivation
          1. Applied rewrites56.6%

            \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
          2. Taylor expanded in re around 0

            \[\leadsto \color{blue}{\left(-1 \cdot im\right)} \cdot im \]
          3. Step-by-step derivation
            1. lower-*.f6453.3%

              \[\leadsto \left(-1 \cdot \color{blue}{im}\right) \cdot im \]
          4. Applied rewrites53.3%

            \[\leadsto \color{blue}{\left(-1 \cdot im\right)} \cdot im \]
          5. Taylor expanded in re around inf

            \[\leadsto \color{blue}{re} \cdot im \]
          6. Step-by-step derivation
            1. Applied rewrites14.8%

              \[\leadsto \color{blue}{re} \cdot im \]
          7. Recombined 2 regimes into one program.
          8. Add Preprocessing

          Alternative 4: 15.1% accurate, 1.8× speedup?

          \[re \cdot \left|im\right| \]
          (FPCore re_sqr (re im)
            :precision binary64
            (* re (fabs im)))
          double re_sqr(double re, double im) {
          	return 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 re_sqr(re, im)
          use fmin_fmax_functions
              real(8), intent (in) :: re
              real(8), intent (in) :: im
              re_sqr = re * abs(im)
          end function
          
          public static double re_sqr(double re, double im) {
          	return re * Math.abs(im);
          }
          
          def re_sqr(re, im):
          	return re * math.fabs(im)
          
          function re_sqr(re, im)
          	return Float64(re * abs(im))
          end
          
          function tmp = re_sqr(re, im)
          	tmp = re * abs(im);
          end
          
          re$95$sqr[re_, im_] := N[(re * N[Abs[im], $MachinePrecision]), $MachinePrecision]
          
          re \cdot \left|im\right|
          
          Derivation
          1. Initial program 93.7%

            \[re \cdot re - im \cdot im \]
          2. Step-by-step derivation
            1. lift--.f64N/A

              \[\leadsto \color{blue}{re \cdot re - im \cdot im} \]
            2. lift-*.f64N/A

              \[\leadsto \color{blue}{re \cdot re} - im \cdot im \]
            3. lift-*.f64N/A

              \[\leadsto re \cdot re - \color{blue}{im \cdot im} \]
            4. difference-of-squaresN/A

              \[\leadsto \color{blue}{\left(re + im\right) \cdot \left(re - im\right)} \]
            5. *-commutativeN/A

              \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(re + im\right)} \]
            6. lower-*.f64N/A

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

              \[\leadsto \color{blue}{\left(re - im\right)} \cdot \left(re + im\right) \]
            8. +-commutativeN/A

              \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
            9. lower-+.f64100.0%

              \[\leadsto \left(re - im\right) \cdot \color{blue}{\left(im + re\right)} \]
          3. Applied rewrites100.0%

            \[\leadsto \color{blue}{\left(re - im\right) \cdot \left(im + re\right)} \]
          4. Taylor expanded in re around 0

            \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
          5. Step-by-step derivation
            1. Applied rewrites56.6%

              \[\leadsto \left(re - im\right) \cdot \color{blue}{im} \]
            2. Taylor expanded in re around 0

              \[\leadsto \color{blue}{\left(-1 \cdot im\right)} \cdot im \]
            3. Step-by-step derivation
              1. lower-*.f6453.3%

                \[\leadsto \left(-1 \cdot \color{blue}{im}\right) \cdot im \]
            4. Applied rewrites53.3%

              \[\leadsto \color{blue}{\left(-1 \cdot im\right)} \cdot im \]
            5. Taylor expanded in re around inf

              \[\leadsto \color{blue}{re} \cdot im \]
            6. Step-by-step derivation
              1. Applied rewrites14.8%

                \[\leadsto \color{blue}{re} \cdot im \]
              2. Add Preprocessing

              Reproduce

              ?
              herbie shell --seed 2025257 
              (FPCore re_sqr (re im)
                :name "math.square on complex, real part"
                :precision binary64
                (- (* re re) (* im im)))