FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2

Percentage Accurate: 45.0% → 98.5%
Time: 2.3s
Alternatives: 2
Speedup: 2.9×

Specification

?
\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (sqrt (+ (+ (* x x) (* y y)) (* z z))))
double code(double x, double y, double z) {
	return sqrt((((x * x) + (y * y)) + (z * z)));
}
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 = sqrt((((x * x) + (y * y)) + (z * z)))
end function
public static double code(double x, double y, double z) {
	return Math.sqrt((((x * x) + (y * y)) + (z * z)));
}
def code(x, y, z):
	return math.sqrt((((x * x) + (y * y)) + (z * z)))
function code(x, y, z)
	return sqrt(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)))
end
function tmp = code(x, y, z)
	tmp = sqrt((((x * x) + (y * y)) + (z * z)));
end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	sqrt((((x * x) + (y * y)) + (z * z)))
END code
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}

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

\[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (sqrt (+ (+ (* x x) (* y y)) (* z z))))
double code(double x, double y, double z) {
	return sqrt((((x * x) + (y * y)) + (z * z)));
}
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 = sqrt((((x * x) + (y * y)) + (z * z)))
end function
public static double code(double x, double y, double z) {
	return Math.sqrt((((x * x) + (y * y)) + (z * z)));
}
def code(x, y, z):
	return math.sqrt((((x * x) + (y * y)) + (z * z)))
function code(x, y, z)
	return sqrt(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)))
end
function tmp = code(x, y, z)
	tmp = sqrt((((x * x) + (y * y)) + (z * z)));
end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	sqrt((((x * x) + (y * y)) + (z * z)))
END code
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}

Alternative 1: 98.5% accurate, 1.7× speedup?

\[\mathsf{max}\left(\left|y\right|, \mathsf{max}\left(\left|x\right|, \left|z\right|\right)\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (fmax (fabs y) (fmax (fabs x) (fabs z))))
double code(double x, double y, double z) {
	return fmax(fabs(y), fmax(fabs(x), fabs(z)));
}
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 = fmax(abs(y), fmax(abs(x), abs(z)))
end function
public static double code(double x, double y, double z) {
	return fmax(Math.abs(y), fmax(Math.abs(x), Math.abs(z)));
}
def code(x, y, z):
	return fmax(math.fabs(y), fmax(math.fabs(x), math.fabs(z)))
function code(x, y, z)
	return fmax(abs(y), fmax(abs(x), abs(z)))
end
function tmp = code(x, y, z)
	tmp = max(abs(y), max(abs(x), abs(z)));
end
code[x_, y_, z_] := N[Max[N[Abs[y], $MachinePrecision], N[Max[N[Abs[x], $MachinePrecision], N[Abs[z], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	LET tmp_1 = IF ((abs(x)) > (abs(z))) THEN (abs(x)) ELSE (abs(z)) ENDIF IN
	LET tmp_2 = IF ((abs(x)) > (abs(z))) THEN (abs(x)) ELSE (abs(z)) ENDIF IN
	LET tmp = IF ((abs(y)) > tmp_1) THEN (abs(y)) ELSE tmp_2 ENDIF IN
	tmp
END code
\mathsf{max}\left(\left|y\right|, \mathsf{max}\left(\left|x\right|, \left|z\right|\right)\right)
Derivation
  1. Initial program 45.0%

    \[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \]
  2. Taylor expanded in x around -inf

    \[\leadsto -1 \cdot x \]
  3. Step-by-step derivation
    1. Applied rewrites18.9%

      \[\leadsto -1 \cdot x \]
    2. Step-by-step derivation
      1. Applied rewrites18.9%

        \[\leadsto -x \]
      2. Taylor expanded in undef-var around zero

        \[\leadsto -0 \]
      3. Step-by-step derivation
        1. Applied rewrites2.3%

          \[\leadsto -0 \]
        2. Taylor expanded in z around inf

          \[\leadsto z \]
        3. Step-by-step derivation
          1. Applied rewrites18.2%

            \[\leadsto z \]
          2. Add Preprocessing

          Alternative 2: 68.1% accurate, 2.9× speedup?

          \[\mathsf{max}\left(\left|x\right|, \left|y\right|\right) \]
          (FPCore (x y z)
            :precision binary64
            :pre TRUE
            (fmax (fabs x) (fabs y)))
          double code(double x, double y, double z) {
          	return fmax(fabs(x), fabs(y));
          }
          
          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 = fmax(abs(x), abs(y))
          end function
          
          public static double code(double x, double y, double z) {
          	return fmax(Math.abs(x), Math.abs(y));
          }
          
          def code(x, y, z):
          	return fmax(math.fabs(x), math.fabs(y))
          
          function code(x, y, z)
          	return fmax(abs(x), abs(y))
          end
          
          function tmp = code(x, y, z)
          	tmp = max(abs(x), abs(y));
          end
          
          code[x_, y_, z_] := N[Max[N[Abs[x], $MachinePrecision], N[Abs[y], $MachinePrecision]], $MachinePrecision]
          
          f(x, y, z):
          	x in [-inf, +inf],
          	y in [-inf, +inf],
          	z in [-inf, +inf]
          code: THEORY
          BEGIN
          f(x, y, z: real): real =
          	LET tmp = IF ((abs(x)) > (abs(y))) THEN (abs(x)) ELSE (abs(y)) ENDIF IN
          	tmp
          END code
          \mathsf{max}\left(\left|x\right|, \left|y\right|\right)
          
          Derivation
          1. Initial program 45.0%

            \[\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z} \]
          2. Taylor expanded in x around -inf

            \[\leadsto -1 \cdot x \]
          3. Step-by-step derivation
            1. Applied rewrites18.9%

              \[\leadsto -1 \cdot x \]
            2. Step-by-step derivation
              1. Applied rewrites18.9%

                \[\leadsto -x \]
              2. Taylor expanded in undef-var around zero

                \[\leadsto -0 \]
              3. Step-by-step derivation
                1. Applied rewrites2.3%

                  \[\leadsto -0 \]
                2. Taylor expanded in y around inf

                  \[\leadsto y \]
                3. Step-by-step derivation
                  1. Applied rewrites19.0%

                    \[\leadsto y \]
                  2. Add Preprocessing

                  Reproduce

                  ?
                  herbie shell --seed 2026092 
                  (FPCore (x y z)
                    :name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
                    :precision binary64
                    (sqrt (+ (+ (* x x) (* y y)) (* z z))))