Diagrams.ThreeD.Transform:aboutY from diagrams-lib-1.3.0.3

Percentage Accurate: 99.8% → 99.8%
Time: 5.2s
Alternatives: 7
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ x \cdot \cos y + z \cdot \sin y \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* x (cos y)) (* z (sin y))))
double code(double x, double y, double z) {
	return (x * cos(y)) + (z * sin(y));
}
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, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x * cos(y)) + (z * sin(y))
end function
public static double code(double x, double y, double z) {
	return (x * Math.cos(y)) + (z * Math.sin(y));
}
def code(x, y, z):
	return (x * math.cos(y)) + (z * math.sin(y))
function code(x, y, z)
	return Float64(Float64(x * cos(y)) + Float64(z * sin(y)))
end
function tmp = code(x, y, z)
	tmp = (x * cos(y)) + (z * sin(y));
end
code[x_, y_, z_] := N[(N[(x * N[Cos[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Sin[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \cos y + z \cdot \sin y
\end{array}

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

\[\begin{array}{l} \\ x \cdot \cos y + z \cdot \sin y \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* x (cos y)) (* z (sin y))))
double code(double x, double y, double z) {
	return (x * cos(y)) + (z * sin(y));
}
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, y, z)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = (x * cos(y)) + (z * sin(y))
end function
public static double code(double x, double y, double z) {
	return (x * Math.cos(y)) + (z * Math.sin(y));
}
def code(x, y, z):
	return (x * math.cos(y)) + (z * math.sin(y))
function code(x, y, z)
	return Float64(Float64(x * cos(y)) + Float64(z * sin(y)))
end
function tmp = code(x, y, z)
	tmp = (x * cos(y)) + (z * sin(y));
end
code[x_, y_, z_] := N[(N[(x * N[Cos[y], $MachinePrecision]), $MachinePrecision] + N[(z * N[Sin[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \cos y + z \cdot \sin y
\end{array}

Alternative 1: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\cos y, x, \sin y \cdot z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma (cos y) x (* (sin y) z)))
double code(double x, double y, double z) {
	return fma(cos(y), x, (sin(y) * z));
}
function code(x, y, z)
	return fma(cos(y), x, Float64(sin(y) * z))
end
code[x_, y_, z_] := N[(N[Cos[y], $MachinePrecision] * x + N[(N[Sin[y], $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\cos y, x, \sin y \cdot z\right)
\end{array}
Derivation
  1. Initial program 99.8%

    \[x \cdot \cos y + z \cdot \sin y \]
  2. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{x \cdot \cos y + z \cdot \sin y} \]
    2. remove-double-negN/A

      \[\leadsto x \cdot \cos y + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(z \cdot \sin y\right)\right)\right)\right)} \]
    3. lift-*.f64N/A

      \[\leadsto x \cdot \cos y + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{z \cdot \sin y}\right)\right)\right)\right) \]
    4. *-commutativeN/A

      \[\leadsto x \cdot \cos y + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{\sin y \cdot z}\right)\right)\right)\right) \]
    5. distribute-rgt-neg-inN/A

      \[\leadsto x \cdot \cos y + \left(\mathsf{neg}\left(\color{blue}{\sin y \cdot \left(\mathsf{neg}\left(z\right)\right)}\right)\right) \]
    6. distribute-lft-neg-inN/A

      \[\leadsto x \cdot \cos y + \color{blue}{\left(\mathsf{neg}\left(\sin y\right)\right) \cdot \left(\mathsf{neg}\left(z\right)\right)} \]
    7. lift-sin.f64N/A

      \[\leadsto x \cdot \cos y + \left(\mathsf{neg}\left(\color{blue}{\sin y}\right)\right) \cdot \left(\mathsf{neg}\left(z\right)\right) \]
    8. sin-neg-revN/A

      \[\leadsto x \cdot \cos y + \color{blue}{\sin \left(\mathsf{neg}\left(y\right)\right)} \cdot \left(\mathsf{neg}\left(z\right)\right) \]
    9. distribute-rgt-neg-inN/A

      \[\leadsto x \cdot \cos y + \color{blue}{\left(\mathsf{neg}\left(\sin \left(\mathsf{neg}\left(y\right)\right) \cdot z\right)\right)} \]
    10. distribute-lft-neg-inN/A

      \[\leadsto x \cdot \cos y + \color{blue}{\left(\mathsf{neg}\left(\sin \left(\mathsf{neg}\left(y\right)\right)\right)\right) \cdot z} \]
    11. fp-cancel-sub-sign-invN/A

      \[\leadsto \color{blue}{x \cdot \cos y - \sin \left(\mathsf{neg}\left(y\right)\right) \cdot z} \]
    12. *-commutativeN/A

      \[\leadsto x \cdot \cos y - \color{blue}{z \cdot \sin \left(\mathsf{neg}\left(y\right)\right)} \]
    13. fp-cancel-sub-sign-invN/A

      \[\leadsto \color{blue}{x \cdot \cos y + \left(\mathsf{neg}\left(z\right)\right) \cdot \sin \left(\mathsf{neg}\left(y\right)\right)} \]
    14. lift-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \cos y} + \left(\mathsf{neg}\left(z\right)\right) \cdot \sin \left(\mathsf{neg}\left(y\right)\right) \]
    15. *-commutativeN/A

      \[\leadsto \color{blue}{\cos y \cdot x} + \left(\mathsf{neg}\left(z\right)\right) \cdot \sin \left(\mathsf{neg}\left(y\right)\right) \]
    16. sin-neg-revN/A

      \[\leadsto \cos y \cdot x + \left(\mathsf{neg}\left(z\right)\right) \cdot \color{blue}{\left(\mathsf{neg}\left(\sin y\right)\right)} \]
    17. lift-sin.f64N/A

      \[\leadsto \cos y \cdot x + \left(\mathsf{neg}\left(z\right)\right) \cdot \left(\mathsf{neg}\left(\color{blue}{\sin y}\right)\right) \]
    18. distribute-rgt-neg-inN/A

      \[\leadsto \cos y \cdot x + \color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(z\right)\right) \cdot \sin y\right)\right)} \]
    19. distribute-lft-neg-outN/A

      \[\leadsto \cos y \cdot x + \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(z \cdot \sin y\right)\right)}\right)\right) \]
    20. lift-*.f64N/A

      \[\leadsto \cos y \cdot x + \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{z \cdot \sin y}\right)\right)\right)\right) \]
    21. remove-double-negN/A

      \[\leadsto \cos y \cdot x + \color{blue}{z \cdot \sin y} \]
  3. Applied rewrites99.8%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos y, x, \sin y \cdot z\right)} \]
  4. Add Preprocessing

Alternative 2: 82.0% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -3.4 \cdot 10^{-68}:\\ \;\;\;\;\mathsf{fma}\left(\sin y, z, 1 \cdot x\right)\\ \mathbf{elif}\;z \leq 1.9 \cdot 10^{-51}:\\ \;\;\;\;\mathsf{fma}\left(y, z, \cos y \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot 1 + z \cdot \sin y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z -3.4e-68)
   (fma (sin y) z (* 1.0 x))
   (if (<= z 1.9e-51) (fma y z (* (cos y) x)) (+ (* x 1.0) (* z (sin y))))))
double code(double x, double y, double z) {
	double tmp;
	if (z <= -3.4e-68) {
		tmp = fma(sin(y), z, (1.0 * x));
	} else if (z <= 1.9e-51) {
		tmp = fma(y, z, (cos(y) * x));
	} else {
		tmp = (x * 1.0) + (z * sin(y));
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (z <= -3.4e-68)
		tmp = fma(sin(y), z, Float64(1.0 * x));
	elseif (z <= 1.9e-51)
		tmp = fma(y, z, Float64(cos(y) * x));
	else
		tmp = Float64(Float64(x * 1.0) + Float64(z * sin(y)));
	end
	return tmp
end
code[x_, y_, z_] := If[LessEqual[z, -3.4e-68], N[(N[Sin[y], $MachinePrecision] * z + N[(1.0 * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 1.9e-51], N[(y * z + N[(N[Cos[y], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision], N[(N[(x * 1.0), $MachinePrecision] + N[(z * N[Sin[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -3.4 \cdot 10^{-68}:\\
\;\;\;\;\mathsf{fma}\left(\sin y, z, 1 \cdot x\right)\\

\mathbf{elif}\;z \leq 1.9 \cdot 10^{-51}:\\
\;\;\;\;\mathsf{fma}\left(y, z, \cos y \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot 1 + z \cdot \sin y\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if z < -3.40000000000000018e-68

    1. Initial program 99.8%

      \[x \cdot \cos y + z \cdot \sin y \]
    2. Taylor expanded in y around 0

      \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
    3. Step-by-step derivation
      1. Applied rewrites76.7%

        \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
      2. Step-by-step derivation
        1. lift-+.f64N/A

          \[\leadsto \color{blue}{x \cdot 1 + z \cdot \sin y} \]
        2. +-commutativeN/A

          \[\leadsto \color{blue}{z \cdot \sin y + x \cdot 1} \]
        3. lift-*.f64N/A

          \[\leadsto \color{blue}{z \cdot \sin y} + x \cdot 1 \]
        4. *-commutativeN/A

          \[\leadsto \color{blue}{\sin y \cdot z} + x \cdot 1 \]
        5. lower-fma.f6476.7

          \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, x \cdot 1\right)} \]
        6. lift-*.f64N/A

          \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{x \cdot 1}\right) \]
        7. *-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{1 \cdot x}\right) \]
        8. lower-*.f6476.7

          \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{1 \cdot x}\right) \]
      3. Applied rewrites76.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, 1 \cdot x\right)} \]

      if -3.40000000000000018e-68 < z < 1.90000000000000001e-51

      1. Initial program 99.8%

        \[x \cdot \cos y + z \cdot \sin y \]
      2. Step-by-step derivation
        1. lift-+.f64N/A

          \[\leadsto \color{blue}{x \cdot \cos y + z \cdot \sin y} \]
        2. +-commutativeN/A

          \[\leadsto \color{blue}{z \cdot \sin y + x \cdot \cos y} \]
        3. lift-*.f64N/A

          \[\leadsto \color{blue}{z \cdot \sin y} + x \cdot \cos y \]
        4. *-commutativeN/A

          \[\leadsto \color{blue}{\sin y \cdot z} + x \cdot \cos y \]
        5. lower-fma.f6499.8

          \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, x \cdot \cos y\right)} \]
        6. lift-*.f64N/A

          \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{x \cdot \cos y}\right) \]
        7. *-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{\cos y \cdot x}\right) \]
        8. lower-*.f6499.8

          \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{\cos y \cdot x}\right) \]
      3. Applied rewrites99.8%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, \cos y \cdot x\right)} \]
      4. Taylor expanded in y around 0

        \[\leadsto \mathsf{fma}\left(\color{blue}{y}, z, \cos y \cdot x\right) \]
      5. Step-by-step derivation
        1. Applied rewrites65.0%

          \[\leadsto \mathsf{fma}\left(\color{blue}{y}, z, \cos y \cdot x\right) \]

        if 1.90000000000000001e-51 < z

        1. Initial program 99.8%

          \[x \cdot \cos y + z \cdot \sin y \]
        2. Taylor expanded in y around 0

          \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
        3. Step-by-step derivation
          1. Applied rewrites76.7%

            \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
        4. Recombined 3 regimes into one program.
        5. Add Preprocessing

        Alternative 3: 82.0% accurate, 1.5× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\sin y, z, 1 \cdot x\right)\\ \mathbf{if}\;z \leq -3.4 \cdot 10^{-68}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 1.9 \cdot 10^{-51}:\\ \;\;\;\;\mathsf{fma}\left(y, z, \cos y \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
        (FPCore (x y z)
         :precision binary64
         (let* ((t_0 (fma (sin y) z (* 1.0 x))))
           (if (<= z -3.4e-68) t_0 (if (<= z 1.9e-51) (fma y z (* (cos y) x)) t_0))))
        double code(double x, double y, double z) {
        	double t_0 = fma(sin(y), z, (1.0 * x));
        	double tmp;
        	if (z <= -3.4e-68) {
        		tmp = t_0;
        	} else if (z <= 1.9e-51) {
        		tmp = fma(y, z, (cos(y) * x));
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        function code(x, y, z)
        	t_0 = fma(sin(y), z, Float64(1.0 * x))
        	tmp = 0.0
        	if (z <= -3.4e-68)
        		tmp = t_0;
        	elseif (z <= 1.9e-51)
        		tmp = fma(y, z, Float64(cos(y) * x));
        	else
        		tmp = t_0;
        	end
        	return tmp
        end
        
        code[x_, y_, z_] := Block[{t$95$0 = N[(N[Sin[y], $MachinePrecision] * z + N[(1.0 * x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -3.4e-68], t$95$0, If[LessEqual[z, 1.9e-51], N[(y * z + N[(N[Cos[y], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision], t$95$0]]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        t_0 := \mathsf{fma}\left(\sin y, z, 1 \cdot x\right)\\
        \mathbf{if}\;z \leq -3.4 \cdot 10^{-68}:\\
        \;\;\;\;t\_0\\
        
        \mathbf{elif}\;z \leq 1.9 \cdot 10^{-51}:\\
        \;\;\;\;\mathsf{fma}\left(y, z, \cos y \cdot x\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_0\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if z < -3.40000000000000018e-68 or 1.90000000000000001e-51 < z

          1. Initial program 99.8%

            \[x \cdot \cos y + z \cdot \sin y \]
          2. Taylor expanded in y around 0

            \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
          3. Step-by-step derivation
            1. Applied rewrites76.7%

              \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
            2. Step-by-step derivation
              1. lift-+.f64N/A

                \[\leadsto \color{blue}{x \cdot 1 + z \cdot \sin y} \]
              2. +-commutativeN/A

                \[\leadsto \color{blue}{z \cdot \sin y + x \cdot 1} \]
              3. lift-*.f64N/A

                \[\leadsto \color{blue}{z \cdot \sin y} + x \cdot 1 \]
              4. *-commutativeN/A

                \[\leadsto \color{blue}{\sin y \cdot z} + x \cdot 1 \]
              5. lower-fma.f6476.7

                \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, x \cdot 1\right)} \]
              6. lift-*.f64N/A

                \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{x \cdot 1}\right) \]
              7. *-commutativeN/A

                \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{1 \cdot x}\right) \]
              8. lower-*.f6476.7

                \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{1 \cdot x}\right) \]
            3. Applied rewrites76.7%

              \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, 1 \cdot x\right)} \]

            if -3.40000000000000018e-68 < z < 1.90000000000000001e-51

            1. Initial program 99.8%

              \[x \cdot \cos y + z \cdot \sin y \]
            2. Step-by-step derivation
              1. lift-+.f64N/A

                \[\leadsto \color{blue}{x \cdot \cos y + z \cdot \sin y} \]
              2. +-commutativeN/A

                \[\leadsto \color{blue}{z \cdot \sin y + x \cdot \cos y} \]
              3. lift-*.f64N/A

                \[\leadsto \color{blue}{z \cdot \sin y} + x \cdot \cos y \]
              4. *-commutativeN/A

                \[\leadsto \color{blue}{\sin y \cdot z} + x \cdot \cos y \]
              5. lower-fma.f6499.8

                \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, x \cdot \cos y\right)} \]
              6. lift-*.f64N/A

                \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{x \cdot \cos y}\right) \]
              7. *-commutativeN/A

                \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{\cos y \cdot x}\right) \]
              8. lower-*.f6499.8

                \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{\cos y \cdot x}\right) \]
            3. Applied rewrites99.8%

              \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, \cos y \cdot x\right)} \]
            4. Taylor expanded in y around 0

              \[\leadsto \mathsf{fma}\left(\color{blue}{y}, z, \cos y \cdot x\right) \]
            5. Step-by-step derivation
              1. Applied rewrites65.0%

                \[\leadsto \mathsf{fma}\left(\color{blue}{y}, z, \cos y \cdot x\right) \]
            6. Recombined 2 regimes into one program.
            7. Add Preprocessing

            Alternative 4: 76.7% accurate, 1.8× speedup?

            \[\begin{array}{l} \\ \mathsf{fma}\left(\sin y, z, 1 \cdot x\right) \end{array} \]
            (FPCore (x y z) :precision binary64 (fma (sin y) z (* 1.0 x)))
            double code(double x, double y, double z) {
            	return fma(sin(y), z, (1.0 * x));
            }
            
            function code(x, y, z)
            	return fma(sin(y), z, Float64(1.0 * x))
            end
            
            code[x_, y_, z_] := N[(N[Sin[y], $MachinePrecision] * z + N[(1.0 * x), $MachinePrecision]), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \mathsf{fma}\left(\sin y, z, 1 \cdot x\right)
            \end{array}
            
            Derivation
            1. Initial program 99.8%

              \[x \cdot \cos y + z \cdot \sin y \]
            2. Taylor expanded in y around 0

              \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
            3. Step-by-step derivation
              1. Applied rewrites76.7%

                \[\leadsto x \cdot \color{blue}{1} + z \cdot \sin y \]
              2. Step-by-step derivation
                1. lift-+.f64N/A

                  \[\leadsto \color{blue}{x \cdot 1 + z \cdot \sin y} \]
                2. +-commutativeN/A

                  \[\leadsto \color{blue}{z \cdot \sin y + x \cdot 1} \]
                3. lift-*.f64N/A

                  \[\leadsto \color{blue}{z \cdot \sin y} + x \cdot 1 \]
                4. *-commutativeN/A

                  \[\leadsto \color{blue}{\sin y \cdot z} + x \cdot 1 \]
                5. lower-fma.f6476.7

                  \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, x \cdot 1\right)} \]
                6. lift-*.f64N/A

                  \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{x \cdot 1}\right) \]
                7. *-commutativeN/A

                  \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{1 \cdot x}\right) \]
                8. lower-*.f6476.7

                  \[\leadsto \mathsf{fma}\left(\sin y, z, \color{blue}{1 \cdot x}\right) \]
              3. Applied rewrites76.7%

                \[\leadsto \color{blue}{\mathsf{fma}\left(\sin y, z, 1 \cdot x\right)} \]
              4. Add Preprocessing

              Alternative 5: 74.6% accurate, 1.7× speedup?

              \[\begin{array}{l} \\ \begin{array}{l} t_0 := z \cdot \sin y\\ \mathbf{if}\;y \leq -0.19:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 1.85 \cdot 10^{-7}:\\ \;\;\;\;x + y \cdot \left(z + y \cdot \mathsf{fma}\left(-0.5, x, -0.16666666666666666 \cdot \left(y \cdot z\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
              (FPCore (x y z)
               :precision binary64
               (let* ((t_0 (* z (sin y))))
                 (if (<= y -0.19)
                   t_0
                   (if (<= y 1.85e-7)
                     (+ x (* y (+ z (* y (fma -0.5 x (* -0.16666666666666666 (* y z)))))))
                     t_0))))
              double code(double x, double y, double z) {
              	double t_0 = z * sin(y);
              	double tmp;
              	if (y <= -0.19) {
              		tmp = t_0;
              	} else if (y <= 1.85e-7) {
              		tmp = x + (y * (z + (y * fma(-0.5, x, (-0.16666666666666666 * (y * z))))));
              	} else {
              		tmp = t_0;
              	}
              	return tmp;
              }
              
              function code(x, y, z)
              	t_0 = Float64(z * sin(y))
              	tmp = 0.0
              	if (y <= -0.19)
              		tmp = t_0;
              	elseif (y <= 1.85e-7)
              		tmp = Float64(x + Float64(y * Float64(z + Float64(y * fma(-0.5, x, Float64(-0.16666666666666666 * Float64(y * z)))))));
              	else
              		tmp = t_0;
              	end
              	return tmp
              end
              
              code[x_, y_, z_] := Block[{t$95$0 = N[(z * N[Sin[y], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -0.19], t$95$0, If[LessEqual[y, 1.85e-7], N[(x + N[(y * N[(z + N[(y * N[(-0.5 * x + N[(-0.16666666666666666 * N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]
              
              \begin{array}{l}
              
              \\
              \begin{array}{l}
              t_0 := z \cdot \sin y\\
              \mathbf{if}\;y \leq -0.19:\\
              \;\;\;\;t\_0\\
              
              \mathbf{elif}\;y \leq 1.85 \cdot 10^{-7}:\\
              \;\;\;\;x + y \cdot \left(z + y \cdot \mathsf{fma}\left(-0.5, x, -0.16666666666666666 \cdot \left(y \cdot z\right)\right)\right)\\
              
              \mathbf{else}:\\
              \;\;\;\;t\_0\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if y < -0.19 or 1.85000000000000002e-7 < y

                1. Initial program 99.8%

                  \[x \cdot \cos y + z \cdot \sin y \]
                2. Taylor expanded in x around 0

                  \[\leadsto \color{blue}{z \cdot \sin y} \]
                3. Step-by-step derivation
                  1. lower-*.f64N/A

                    \[\leadsto z \cdot \color{blue}{\sin y} \]
                  2. lower-sin.f6440.6

                    \[\leadsto z \cdot \sin y \]
                4. Applied rewrites40.6%

                  \[\leadsto \color{blue}{z \cdot \sin y} \]

                if -0.19 < y < 1.85000000000000002e-7

                1. Initial program 99.8%

                  \[x \cdot \cos y + z \cdot \sin y \]
                2. Taylor expanded in y around 0

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

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

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

                    \[\leadsto x + y \cdot \left(z + \color{blue}{y \cdot \left(\frac{-1}{2} \cdot x + \frac{-1}{6} \cdot \left(y \cdot z\right)\right)}\right) \]
                  4. lower-*.f64N/A

                    \[\leadsto x + y \cdot \left(z + y \cdot \color{blue}{\left(\frac{-1}{2} \cdot x + \frac{-1}{6} \cdot \left(y \cdot z\right)\right)}\right) \]
                  5. lower-fma.f64N/A

                    \[\leadsto x + y \cdot \left(z + y \cdot \mathsf{fma}\left(\frac{-1}{2}, \color{blue}{x}, \frac{-1}{6} \cdot \left(y \cdot z\right)\right)\right) \]
                  6. lower-*.f64N/A

                    \[\leadsto x + y \cdot \left(z + y \cdot \mathsf{fma}\left(\frac{-1}{2}, x, \frac{-1}{6} \cdot \left(y \cdot z\right)\right)\right) \]
                  7. lower-*.f6451.2

                    \[\leadsto x + y \cdot \left(z + y \cdot \mathsf{fma}\left(-0.5, x, -0.16666666666666666 \cdot \left(y \cdot z\right)\right)\right) \]
                4. Applied rewrites51.2%

                  \[\leadsto \color{blue}{x + y \cdot \left(z + y \cdot \mathsf{fma}\left(-0.5, x, -0.16666666666666666 \cdot \left(y \cdot z\right)\right)\right)} \]
              3. Recombined 2 regimes into one program.
              4. Add Preprocessing

              Alternative 6: 52.3% accurate, 12.3× speedup?

              \[\begin{array}{l} \\ \mathsf{fma}\left(z, y, x\right) \end{array} \]
              (FPCore (x y z) :precision binary64 (fma z y x))
              double code(double x, double y, double z) {
              	return fma(z, y, x);
              }
              
              function code(x, y, z)
              	return fma(z, y, x)
              end
              
              code[x_, y_, z_] := N[(z * y + x), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              \mathsf{fma}\left(z, y, x\right)
              \end{array}
              
              Derivation
              1. Initial program 99.8%

                \[x \cdot \cos y + z \cdot \sin y \]
              2. Taylor expanded in y around 0

                \[\leadsto \color{blue}{x + y \cdot z} \]
              3. Step-by-step derivation
                1. lower-+.f64N/A

                  \[\leadsto x + \color{blue}{y \cdot z} \]
                2. lower-*.f6452.3

                  \[\leadsto x + y \cdot \color{blue}{z} \]
              4. Applied rewrites52.3%

                \[\leadsto \color{blue}{x + y \cdot z} \]
              5. Step-by-step derivation
                1. lift-+.f64N/A

                  \[\leadsto x + \color{blue}{y \cdot z} \]
                2. +-commutativeN/A

                  \[\leadsto y \cdot z + \color{blue}{x} \]
                3. lift-*.f64N/A

                  \[\leadsto y \cdot z + x \]
                4. *-commutativeN/A

                  \[\leadsto z \cdot y + x \]
                5. lower-fma.f6452.3

                  \[\leadsto \mathsf{fma}\left(z, \color{blue}{y}, x\right) \]
              6. Applied rewrites52.3%

                \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
              7. Add Preprocessing

              Alternative 7: 17.0% accurate, 18.5× speedup?

              \[\begin{array}{l} \\ y \cdot z \end{array} \]
              (FPCore (x y z) :precision binary64 (* y z))
              double code(double x, double y, double z) {
              	return y * z;
              }
              
              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, y, z)
              use fmin_fmax_functions
                  real(8), intent (in) :: x
                  real(8), intent (in) :: y
                  real(8), intent (in) :: z
                  code = y * z
              end function
              
              public static double code(double x, double y, double z) {
              	return y * z;
              }
              
              def code(x, y, z):
              	return y * z
              
              function code(x, y, z)
              	return Float64(y * z)
              end
              
              function tmp = code(x, y, z)
              	tmp = y * z;
              end
              
              code[x_, y_, z_] := N[(y * z), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              y \cdot z
              \end{array}
              
              Derivation
              1. Initial program 99.8%

                \[x \cdot \cos y + z \cdot \sin y \]
              2. Taylor expanded in y around 0

                \[\leadsto \color{blue}{x + y \cdot z} \]
              3. Step-by-step derivation
                1. lower-+.f64N/A

                  \[\leadsto x + \color{blue}{y \cdot z} \]
                2. lower-*.f6452.3

                  \[\leadsto x + y \cdot \color{blue}{z} \]
              4. Applied rewrites52.3%

                \[\leadsto \color{blue}{x + y \cdot z} \]
              5. Taylor expanded in x around 0

                \[\leadsto y \cdot \color{blue}{z} \]
              6. Step-by-step derivation
                1. lower-*.f6417.0

                  \[\leadsto y \cdot z \]
              7. Applied rewrites17.0%

                \[\leadsto y \cdot \color{blue}{z} \]
              8. Add Preprocessing

              Reproduce

              ?
              herbie shell --seed 2025151 
              (FPCore (x y z)
                :name "Diagrams.ThreeD.Transform:aboutY from diagrams-lib-1.3.0.3"
                :precision binary64
                (+ (* x (cos y)) (* z (sin y))))