Linear.Matrix:fromQuaternion from linear-1.19.1.3, A

Percentage Accurate: 95.0% → 100.0%
Time: 5.1s
Alternatives: 5
Speedup: 1.3×

Specification

?
\[\begin{array}{l} \\ 2 \cdot \left(x \cdot x - x \cdot y\right) \end{array} \]
(FPCore (x y) :precision binary64 (* 2.0 (- (* x x) (* x y))))
double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 2.0d0 * ((x * x) - (x * y))
end function
public static double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
def code(x, y):
	return 2.0 * ((x * x) - (x * y))
function code(x, y)
	return Float64(2.0 * Float64(Float64(x * x) - Float64(x * y)))
end
function tmp = code(x, y)
	tmp = 2.0 * ((x * x) - (x * y));
end
code[x_, y_] := N[(2.0 * N[(N[(x * x), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
2 \cdot \left(x \cdot x - x \cdot y\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 5 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 95.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 2 \cdot \left(x \cdot x - x \cdot y\right) \end{array} \]
(FPCore (x y) :precision binary64 (* 2.0 (- (* x x) (* x y))))
double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 2.0d0 * ((x * x) - (x * y))
end function
public static double code(double x, double y) {
	return 2.0 * ((x * x) - (x * y));
}
def code(x, y):
	return 2.0 * ((x * x) - (x * y))
function code(x, y)
	return Float64(2.0 * Float64(Float64(x * x) - Float64(x * y)))
end
function tmp = code(x, y)
	tmp = 2.0 * ((x * x) - (x * y));
end
code[x_, y_] := N[(2.0 * N[(N[(x * x), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
2 \cdot \left(x \cdot x - x \cdot y\right)
\end{array}

Alternative 1: 100.0% accurate, 1.3× speedup?

\[\begin{array}{l} \\ x \cdot \left(2 \cdot \left(x - y\right)\right) \end{array} \]
(FPCore (x y) :precision binary64 (* x (* 2.0 (- x y))))
double code(double x, double y) {
	return x * (2.0 * (x - y));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x * (2.0d0 * (x - y))
end function
public static double code(double x, double y) {
	return x * (2.0 * (x - y));
}
def code(x, y):
	return x * (2.0 * (x - y))
function code(x, y)
	return Float64(x * Float64(2.0 * Float64(x - y)))
end
function tmp = code(x, y)
	tmp = x * (2.0 * (x - y));
end
code[x_, y_] := N[(x * N[(2.0 * N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(2 \cdot \left(x - y\right)\right)
\end{array}
Derivation
  1. Initial program 96.1%

    \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
  2. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \left(x \cdot x - x \cdot y\right) \cdot \color{blue}{2} \]
    2. distribute-lft-out--N/A

      \[\leadsto \left(x \cdot \left(x - y\right)\right) \cdot 2 \]
    3. associate-*l*N/A

      \[\leadsto x \cdot \color{blue}{\left(\left(x - y\right) \cdot 2\right)} \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(\left(x - y\right) \cdot 2\right)}\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(x, \left(2 \cdot \color{blue}{\left(x - y\right)}\right)\right) \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(2, \color{blue}{\left(x - y\right)}\right)\right) \]
    7. --lowering--.f64100.0%

      \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(2, \mathsf{\_.f64}\left(x, \color{blue}{y}\right)\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{x \cdot \left(2 \cdot \left(x - y\right)\right)} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 2: 82.8% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot \left(y \cdot -2\right)\\ \mathbf{if}\;y \leq -5.1 \cdot 10^{+32}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;y \leq 6.8 \cdot 10^{-73}:\\ \;\;\;\;x \cdot \left(x \cdot 2\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* x (* y -2.0))))
   (if (<= y -5.1e+32) t_0 (if (<= y 6.8e-73) (* x (* x 2.0)) t_0))))
double code(double x, double y) {
	double t_0 = x * (y * -2.0);
	double tmp;
	if (y <= -5.1e+32) {
		tmp = t_0;
	} else if (y <= 6.8e-73) {
		tmp = x * (x * 2.0);
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: t_0
    real(8) :: tmp
    t_0 = x * (y * (-2.0d0))
    if (y <= (-5.1d+32)) then
        tmp = t_0
    else if (y <= 6.8d-73) then
        tmp = x * (x * 2.0d0)
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double t_0 = x * (y * -2.0);
	double tmp;
	if (y <= -5.1e+32) {
		tmp = t_0;
	} else if (y <= 6.8e-73) {
		tmp = x * (x * 2.0);
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, y):
	t_0 = x * (y * -2.0)
	tmp = 0
	if y <= -5.1e+32:
		tmp = t_0
	elif y <= 6.8e-73:
		tmp = x * (x * 2.0)
	else:
		tmp = t_0
	return tmp
function code(x, y)
	t_0 = Float64(x * Float64(y * -2.0))
	tmp = 0.0
	if (y <= -5.1e+32)
		tmp = t_0;
	elseif (y <= 6.8e-73)
		tmp = Float64(x * Float64(x * 2.0));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, y)
	t_0 = x * (y * -2.0);
	tmp = 0.0;
	if (y <= -5.1e+32)
		tmp = t_0;
	elseif (y <= 6.8e-73)
		tmp = x * (x * 2.0);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := Block[{t$95$0 = N[(x * N[(y * -2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -5.1e+32], t$95$0, If[LessEqual[y, 6.8e-73], N[(x * N[(x * 2.0), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := x \cdot \left(y \cdot -2\right)\\
\mathbf{if}\;y \leq -5.1 \cdot 10^{+32}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;y \leq 6.8 \cdot 10^{-73}:\\
\;\;\;\;x \cdot \left(x \cdot 2\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -5.10000000000000004e32 or 6.80000000000000042e-73 < y

    1. Initial program 92.9%

      \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
    2. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \left(x \cdot x - x \cdot y\right) \cdot \color{blue}{2} \]
      2. distribute-lft-out--N/A

        \[\leadsto \left(x \cdot \left(x - y\right)\right) \cdot 2 \]
      3. associate-*l*N/A

        \[\leadsto x \cdot \color{blue}{\left(\left(x - y\right) \cdot 2\right)} \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(\left(x - y\right) \cdot 2\right)}\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(x, \left(2 \cdot \color{blue}{\left(x - y\right)}\right)\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(2, \color{blue}{\left(x - y\right)}\right)\right) \]
      7. --lowering--.f64100.0%

        \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(2, \mathsf{\_.f64}\left(x, \color{blue}{y}\right)\right)\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x \cdot \left(2 \cdot \left(x - y\right)\right)} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0

      \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(-2 \cdot y\right)}\right) \]
    6. Step-by-step derivation
      1. *-lowering-*.f6481.3%

        \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(-2, \color{blue}{y}\right)\right) \]
    7. Simplified81.3%

      \[\leadsto x \cdot \color{blue}{\left(-2 \cdot y\right)} \]

    if -5.10000000000000004e32 < y < 6.80000000000000042e-73

    1. Initial program 100.0%

      \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{2 \cdot {x}^{2}} \]
    4. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto 2 \cdot \left(x \cdot \color{blue}{x}\right) \]
      2. associate-*r*N/A

        \[\leadsto \left(2 \cdot x\right) \cdot \color{blue}{x} \]
      3. *-commutativeN/A

        \[\leadsto x \cdot \color{blue}{\left(2 \cdot x\right)} \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(2 \cdot x\right)}\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(x, \left(x \cdot \color{blue}{2}\right)\right) \]
      6. *-lowering-*.f6490.0%

        \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(x, \color{blue}{2}\right)\right) \]
    5. Simplified90.0%

      \[\leadsto \color{blue}{x \cdot \left(x \cdot 2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification85.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -5.1 \cdot 10^{+32}:\\ \;\;\;\;x \cdot \left(y \cdot -2\right)\\ \mathbf{elif}\;y \leq 6.8 \cdot 10^{-73}:\\ \;\;\;\;x \cdot \left(x \cdot 2\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(y \cdot -2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 56.5% accurate, 1.8× speedup?

\[\begin{array}{l} \\ x \cdot \left(y \cdot -2\right) \end{array} \]
(FPCore (x y) :precision binary64 (* x (* y -2.0)))
double code(double x, double y) {
	return x * (y * -2.0);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x * (y * (-2.0d0))
end function
public static double code(double x, double y) {
	return x * (y * -2.0);
}
def code(x, y):
	return x * (y * -2.0)
function code(x, y)
	return Float64(x * Float64(y * -2.0))
end
function tmp = code(x, y)
	tmp = x * (y * -2.0);
end
code[x_, y_] := N[(x * N[(y * -2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(y \cdot -2\right)
\end{array}
Derivation
  1. Initial program 96.1%

    \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
  2. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \left(x \cdot x - x \cdot y\right) \cdot \color{blue}{2} \]
    2. distribute-lft-out--N/A

      \[\leadsto \left(x \cdot \left(x - y\right)\right) \cdot 2 \]
    3. associate-*l*N/A

      \[\leadsto x \cdot \color{blue}{\left(\left(x - y\right) \cdot 2\right)} \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(\left(x - y\right) \cdot 2\right)}\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(x, \left(2 \cdot \color{blue}{\left(x - y\right)}\right)\right) \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(2, \color{blue}{\left(x - y\right)}\right)\right) \]
    7. --lowering--.f64100.0%

      \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(2, \mathsf{\_.f64}\left(x, \color{blue}{y}\right)\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{x \cdot \left(2 \cdot \left(x - y\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in x around 0

    \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(-2 \cdot y\right)}\right) \]
  6. Step-by-step derivation
    1. *-lowering-*.f6456.1%

      \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(-2, \color{blue}{y}\right)\right) \]
  7. Simplified56.1%

    \[\leadsto x \cdot \color{blue}{\left(-2 \cdot y\right)} \]
  8. Final simplification56.1%

    \[\leadsto x \cdot \left(y \cdot -2\right) \]
  9. Add Preprocessing

Alternative 4: 4.3% accurate, 3.0× speedup?

\[\begin{array}{l} \\ x \cdot 2 \end{array} \]
(FPCore (x y) :precision binary64 (* x 2.0))
double code(double x, double y) {
	return x * 2.0;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x * 2.0d0
end function
public static double code(double x, double y) {
	return x * 2.0;
}
def code(x, y):
	return x * 2.0
function code(x, y)
	return Float64(x * 2.0)
end
function tmp = code(x, y)
	tmp = x * 2.0;
end
code[x_, y_] := N[(x * 2.0), $MachinePrecision]
\begin{array}{l}

\\
x \cdot 2
\end{array}
Derivation
  1. Initial program 96.1%

    \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{2 \cdot {x}^{2}} \]
  4. Step-by-step derivation
    1. unpow2N/A

      \[\leadsto 2 \cdot \left(x \cdot \color{blue}{x}\right) \]
    2. associate-*r*N/A

      \[\leadsto \left(2 \cdot x\right) \cdot \color{blue}{x} \]
    3. *-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left(2 \cdot x\right)} \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(2 \cdot x\right)}\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(x, \left(x \cdot \color{blue}{2}\right)\right) \]
    6. *-lowering-*.f6454.3%

      \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(x, \color{blue}{2}\right)\right) \]
  5. Simplified54.3%

    \[\leadsto \color{blue}{x \cdot \left(x \cdot 2\right)} \]
  6. Applied egg-rr4.5%

    \[\leadsto x \cdot \color{blue}{2} \]
  7. Add Preprocessing

Alternative 5: 3.8% accurate, 9.0× speedup?

\[\begin{array}{l} \\ 2 \end{array} \]
(FPCore (x y) :precision binary64 2.0)
double code(double x, double y) {
	return 2.0;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = 2.0d0
end function
public static double code(double x, double y) {
	return 2.0;
}
def code(x, y):
	return 2.0
function code(x, y)
	return 2.0
end
function tmp = code(x, y)
	tmp = 2.0;
end
code[x_, y_] := 2.0
\begin{array}{l}

\\
2
\end{array}
Derivation
  1. Initial program 96.1%

    \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{2 \cdot {x}^{2}} \]
  4. Step-by-step derivation
    1. unpow2N/A

      \[\leadsto 2 \cdot \left(x \cdot \color{blue}{x}\right) \]
    2. associate-*r*N/A

      \[\leadsto \left(2 \cdot x\right) \cdot \color{blue}{x} \]
    3. *-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left(2 \cdot x\right)} \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(x, \color{blue}{\left(2 \cdot x\right)}\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(x, \left(x \cdot \color{blue}{2}\right)\right) \]
    6. *-lowering-*.f6454.3%

      \[\leadsto \mathsf{*.f64}\left(x, \mathsf{*.f64}\left(x, \color{blue}{2}\right)\right) \]
  5. Simplified54.3%

    \[\leadsto \color{blue}{x \cdot \left(x \cdot 2\right)} \]
  6. Applied egg-rr3.9%

    \[\leadsto \color{blue}{2} \]
  7. Add Preprocessing

Developer Target 1: 100.0% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \left(x \cdot 2\right) \cdot \left(x - y\right) \end{array} \]
(FPCore (x y) :precision binary64 (* (* x 2.0) (- x y)))
double code(double x, double y) {
	return (x * 2.0) * (x - y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x * 2.0d0) * (x - y)
end function
public static double code(double x, double y) {
	return (x * 2.0) * (x - y);
}
def code(x, y):
	return (x * 2.0) * (x - y)
function code(x, y)
	return Float64(Float64(x * 2.0) * Float64(x - y))
end
function tmp = code(x, y)
	tmp = (x * 2.0) * (x - y);
end
code[x_, y_] := N[(N[(x * 2.0), $MachinePrecision] * N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(x \cdot 2\right) \cdot \left(x - y\right)
\end{array}

Reproduce

?
herbie shell --seed 2024139 
(FPCore (x y)
  :name "Linear.Matrix:fromQuaternion from linear-1.19.1.3, A"
  :precision binary64

  :alt
  (! :herbie-platform default (* (* x 2) (- x y)))

  (* 2.0 (- (* x x) (* x y))))