Linear.Matrix:fromQuaternion from linear-1.19.1.3, A

Percentage Accurate: 95.0% → 100.0%
Time: 1.0s
Alternatives: 3
Speedup: 1.4×

Specification

?
\[2 \cdot \left(x \cdot x - x \cdot y\right) \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (* 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)
use fmin_fmax_functions
    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]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	(2) * ((x * x) - (x * y))
END code
2 \cdot \left(x \cdot x - x \cdot y\right)

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 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: 95.0% accurate, 1.0× speedup?

\[2 \cdot \left(x \cdot x - x \cdot y\right) \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (* 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)
use fmin_fmax_functions
    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]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	(2) * ((x * x) - (x * y))
END code
2 \cdot \left(x \cdot x - x \cdot y\right)

Alternative 1: 100.0% accurate, 1.4× speedup?

\[\left(x + x\right) \cdot \left(x - y\right) \]
(FPCore (x y)
  :precision binary64
  :pre TRUE
  (* (+ x x) (- x y)))
double code(double x, double y) {
	return (x + x) * (x - y);
}
real(8) function code(x, y)
use fmin_fmax_functions
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + x) * (x - y)
end function
public static double code(double x, double y) {
	return (x + x) * (x - y);
}
def code(x, y):
	return (x + x) * (x - y)
function code(x, y)
	return Float64(Float64(x + x) * Float64(x - y))
end
function tmp = code(x, y)
	tmp = (x + x) * (x - y);
end
code[x_, y_] := N[(N[(x + x), $MachinePrecision] * N[(x - y), $MachinePrecision]), $MachinePrecision]
f(x, y):
	x in [-inf, +inf],
	y in [-inf, +inf]
code: THEORY
BEGIN
f(x, y: real): real =
	(x + x) * (x - y)
END code
\left(x + x\right) \cdot \left(x - y\right)
Derivation
  1. Initial program 95.0%

    \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
  2. Step-by-step derivation
    1. Applied rewrites100.0%

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

    Alternative 2: 57.0% accurate, 1.8× speedup?

    \[\left(-2 \cdot x\right) \cdot y \]
    (FPCore (x y)
      :precision binary64
      :pre TRUE
      (* (* -2.0 x) y))
    double code(double x, double y) {
    	return (-2.0 * x) * y;
    }
    
    real(8) function code(x, y)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        code = ((-2.0d0) * x) * y
    end function
    
    public static double code(double x, double y) {
    	return (-2.0 * x) * y;
    }
    
    def code(x, y):
    	return (-2.0 * x) * y
    
    function code(x, y)
    	return Float64(Float64(-2.0 * x) * y)
    end
    
    function tmp = code(x, y)
    	tmp = (-2.0 * x) * y;
    end
    
    code[x_, y_] := N[(N[(-2.0 * x), $MachinePrecision] * y), $MachinePrecision]
    
    f(x, y):
    	x in [-inf, +inf],
    	y in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y: real): real =
    	((-2) * x) * y
    END code
    \left(-2 \cdot x\right) \cdot y
    
    Derivation
    1. Initial program 95.0%

      \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
    2. Taylor expanded in x around 0

      \[\leadsto -2 \cdot \left(x \cdot y\right) \]
    3. Step-by-step derivation
      1. Applied rewrites56.9%

        \[\leadsto -2 \cdot \left(x \cdot y\right) \]
      2. Step-by-step derivation
        1. Applied rewrites57.0%

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

        Alternative 3: 56.9% accurate, 1.8× speedup?

        \[-2 \cdot \left(x \cdot y\right) \]
        (FPCore (x y)
          :precision binary64
          :pre TRUE
          (* -2.0 (* x y)))
        double code(double x, double y) {
        	return -2.0 * (x * y);
        }
        
        real(8) function code(x, y)
        use fmin_fmax_functions
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            code = (-2.0d0) * (x * y)
        end function
        
        public static double code(double x, double y) {
        	return -2.0 * (x * y);
        }
        
        def code(x, y):
        	return -2.0 * (x * y)
        
        function code(x, y)
        	return Float64(-2.0 * Float64(x * y))
        end
        
        function tmp = code(x, y)
        	tmp = -2.0 * (x * y);
        end
        
        code[x_, y_] := N[(-2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision]
        
        f(x, y):
        	x in [-inf, +inf],
        	y in [-inf, +inf]
        code: THEORY
        BEGIN
        f(x, y: real): real =
        	(-2) * (x * y)
        END code
        -2 \cdot \left(x \cdot y\right)
        
        Derivation
        1. Initial program 95.0%

          \[2 \cdot \left(x \cdot x - x \cdot y\right) \]
        2. Taylor expanded in x around 0

          \[\leadsto -2 \cdot \left(x \cdot y\right) \]
        3. Step-by-step derivation
          1. Applied rewrites56.9%

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

          Reproduce

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