Diagrams.Solve.Polynomial:cubForm from diagrams-solve-0.1, F

Percentage Accurate: 99.6% → 99.6%
Time: 869.0ms
Alternatives: 2
Speedup: 1.0×

Specification

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

\\
\left(x \cdot 27\right) \cdot y
\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 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: 99.6% accurate, 1.0× speedup?

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

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

Alternative 1: 99.6% accurate, 1.0× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y] = \mathsf{sort}([x_m, y])\\ \\ x\_s \cdot \left(\left(x\_m \cdot 27\right) \cdot y\right) \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
NOTE: x_m and y should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y) :precision binary64 (* x_s (* (* x_m 27.0) y)))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
assert(x_m < y);
double code(double x_s, double x_m, double y) {
	return x_s * ((x_m * 27.0) * y);
}
x\_m = abs(x)
x\_s = copysign(1.0d0, x)
NOTE: x_m and y should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    code = x_s * ((x_m * 27.0d0) * y)
end function
x\_m = Math.abs(x);
x\_s = Math.copySign(1.0, x);
assert x_m < y;
public static double code(double x_s, double x_m, double y) {
	return x_s * ((x_m * 27.0) * y);
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
[x_m, y] = sort([x_m, y])
def code(x_s, x_m, y):
	return x_s * ((x_m * 27.0) * y)
x\_m = abs(x)
x\_s = copysign(1.0, x)
x_m, y = sort([x_m, y])
function code(x_s, x_m, y)
	return Float64(x_s * Float64(Float64(x_m * 27.0) * y))
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
x_m, y = num2cell(sort([x_m, y])){:}
function tmp = code(x_s, x_m, y)
	tmp = x_s * ((x_m * 27.0) * y);
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m and y should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_] := N[(x$95$s * N[(N[(x$95$m * 27.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y] = \mathsf{sort}([x_m, y])\\
\\
x\_s \cdot \left(\left(x\_m \cdot 27\right) \cdot y\right)
\end{array}
Derivation
  1. Initial program 99.3%

    \[\left(x \cdot 27\right) \cdot y \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y] = \mathsf{sort}([x_m, y])\\ \\ x\_s \cdot \left(x\_m \cdot \left(27 \cdot y\right)\right) \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
NOTE: x_m and y should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y) :precision binary64 (* x_s (* x_m (* 27.0 y))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
assert(x_m < y);
double code(double x_s, double x_m, double y) {
	return x_s * (x_m * (27.0 * y));
}
x\_m = abs(x)
x\_s = copysign(1.0d0, x)
NOTE: x_m and y should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    code = x_s * (x_m * (27.0d0 * y))
end function
x\_m = Math.abs(x);
x\_s = Math.copySign(1.0, x);
assert x_m < y;
public static double code(double x_s, double x_m, double y) {
	return x_s * (x_m * (27.0 * y));
}
x\_m = math.fabs(x)
x\_s = math.copysign(1.0, x)
[x_m, y] = sort([x_m, y])
def code(x_s, x_m, y):
	return x_s * (x_m * (27.0 * y))
x\_m = abs(x)
x\_s = copysign(1.0, x)
x_m, y = sort([x_m, y])
function code(x_s, x_m, y)
	return Float64(x_s * Float64(x_m * Float64(27.0 * y)))
end
x\_m = abs(x);
x\_s = sign(x) * abs(1.0);
x_m, y = num2cell(sort([x_m, y])){:}
function tmp = code(x_s, x_m, y)
	tmp = x_s * (x_m * (27.0 * y));
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m and y should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_] := N[(x$95$s * N[(x$95$m * N[(27.0 * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y] = \mathsf{sort}([x_m, y])\\
\\
x\_s \cdot \left(x\_m \cdot \left(27 \cdot y\right)\right)
\end{array}
Derivation
  1. Initial program 99.3%

    \[\left(x \cdot 27\right) \cdot y \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. associate-*l*N/A

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

      \[\leadsto \color{blue}{\left(27 \cdot y\right) \cdot x} \]
    3. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(27 \cdot y\right) \cdot x} \]
    4. lower-*.f6499.7

      \[\leadsto \color{blue}{\left(27 \cdot y\right)} \cdot x \]
  4. Applied egg-rr99.7%

    \[\leadsto \color{blue}{\left(27 \cdot y\right) \cdot x} \]
  5. Final simplification99.7%

    \[\leadsto x \cdot \left(27 \cdot y\right) \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2024212 
(FPCore (x y)
  :name "Diagrams.Solve.Polynomial:cubForm  from diagrams-solve-0.1, F"
  :precision binary64
  (* (* x 27.0) y))