Logistic regression 2

Percentage Accurate: 99.5% → 99.5%
Time: 8.5s
Alternatives: 8
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \log \left(1 + e^{x}\right) - x \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
	return log((1.0 + exp(x))) - (x * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
	return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y):
	return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y)
	return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y))
end
function tmp = code(x, y)
	tmp = log((1.0 + exp(x))) - (x * y);
end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\log \left(1 + e^{x}\right) - x \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 8 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.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(1 + e^{x}\right) - x \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
	return log((1.0 + exp(x))) - (x * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
	return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y):
	return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y)
	return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y))
end
function tmp = code(x, y)
	tmp = log((1.0 + exp(x))) - (x * y);
end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\log \left(1 + e^{x}\right) - x \cdot y
\end{array}

Alternative 1: 99.5% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x, -y, \mathsf{log1p}\left(e^{x}\right)\right) \end{array} \]
(FPCore (x y) :precision binary64 (fma x (- y) (log1p (exp x))))
double code(double x, double y) {
	return fma(x, -y, log1p(exp(x)));
}
function code(x, y)
	return fma(x, Float64(-y), log1p(exp(x)))
end
code[x_, y_] := N[(x * (-y) + N[Log[1 + N[Exp[x], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x, -y, \mathsf{log1p}\left(e^{x}\right)\right)
\end{array}
Derivation
  1. Initial program 98.8%

    \[\log \left(1 + e^{x}\right) - x \cdot y \]
  2. Step-by-step derivation
    1. sub-neg98.8%

      \[\leadsto \color{blue}{\log \left(1 + e^{x}\right) + \left(-x \cdot y\right)} \]
    2. +-commutative98.8%

      \[\leadsto \color{blue}{\left(-x \cdot y\right) + \log \left(1 + e^{x}\right)} \]
    3. distribute-rgt-neg-in98.8%

      \[\leadsto \color{blue}{x \cdot \left(-y\right)} + \log \left(1 + e^{x}\right) \]
    4. fma-def98.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, -y, \log \left(1 + e^{x}\right)\right)} \]
    5. log1p-def99.2%

      \[\leadsto \mathsf{fma}\left(x, -y, \color{blue}{\mathsf{log1p}\left(e^{x}\right)}\right) \]
  3. Simplified99.2%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, -y, \mathsf{log1p}\left(e^{x}\right)\right)} \]
  4. Final simplification99.2%

    \[\leadsto \mathsf{fma}\left(x, -y, \mathsf{log1p}\left(e^{x}\right)\right) \]

Alternative 2: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \mathsf{log1p}\left(e^{x}\right) - x \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (- (log1p (exp x)) (* x y)))
double code(double x, double y) {
	return log1p(exp(x)) - (x * y);
}
public static double code(double x, double y) {
	return Math.log1p(Math.exp(x)) - (x * y);
}
def code(x, y):
	return math.log1p(math.exp(x)) - (x * y)
function code(x, y)
	return Float64(log1p(exp(x)) - Float64(x * y))
end
code[x_, y_] := N[(N[Log[1 + N[Exp[x], $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{log1p}\left(e^{x}\right) - x \cdot y
\end{array}
Derivation
  1. Initial program 98.8%

    \[\log \left(1 + e^{x}\right) - x \cdot y \]
  2. Step-by-step derivation
    1. log1p-def99.2%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
  3. Simplified99.2%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
  4. Final simplification99.2%

    \[\leadsto \mathsf{log1p}\left(e^{x}\right) - x \cdot y \]

Alternative 3: 82.1% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -7.4 \cdot 10^{-53}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 1.75 \cdot 10^{-93}:\\ \;\;\;\;\log 2\\ \mathbf{elif}\;x \leq 5.8 \cdot 10^{-18} \lor \neg \left(x \leq 2.1 \cdot 10^{-9}\right):\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + x \cdot 0.5\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x -7.4e-53)
   (* x (- y))
   (if (<= x 1.75e-93)
     (log 2.0)
     (if (or (<= x 5.8e-18) (not (<= x 2.1e-9)))
       (* x (- 0.5 y))
       (+ (log 2.0) (* x 0.5))))))
double code(double x, double y) {
	double tmp;
	if (x <= -7.4e-53) {
		tmp = x * -y;
	} else if (x <= 1.75e-93) {
		tmp = log(2.0);
	} else if ((x <= 5.8e-18) || !(x <= 2.1e-9)) {
		tmp = x * (0.5 - y);
	} else {
		tmp = log(2.0) + (x * 0.5);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= (-7.4d-53)) then
        tmp = x * -y
    else if (x <= 1.75d-93) then
        tmp = log(2.0d0)
    else if ((x <= 5.8d-18) .or. (.not. (x <= 2.1d-9))) then
        tmp = x * (0.5d0 - y)
    else
        tmp = log(2.0d0) + (x * 0.5d0)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (x <= -7.4e-53) {
		tmp = x * -y;
	} else if (x <= 1.75e-93) {
		tmp = Math.log(2.0);
	} else if ((x <= 5.8e-18) || !(x <= 2.1e-9)) {
		tmp = x * (0.5 - y);
	} else {
		tmp = Math.log(2.0) + (x * 0.5);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if x <= -7.4e-53:
		tmp = x * -y
	elif x <= 1.75e-93:
		tmp = math.log(2.0)
	elif (x <= 5.8e-18) or not (x <= 2.1e-9):
		tmp = x * (0.5 - y)
	else:
		tmp = math.log(2.0) + (x * 0.5)
	return tmp
function code(x, y)
	tmp = 0.0
	if (x <= -7.4e-53)
		tmp = Float64(x * Float64(-y));
	elseif (x <= 1.75e-93)
		tmp = log(2.0);
	elseif ((x <= 5.8e-18) || !(x <= 2.1e-9))
		tmp = Float64(x * Float64(0.5 - y));
	else
		tmp = Float64(log(2.0) + Float64(x * 0.5));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= -7.4e-53)
		tmp = x * -y;
	elseif (x <= 1.75e-93)
		tmp = log(2.0);
	elseif ((x <= 5.8e-18) || ~((x <= 2.1e-9)))
		tmp = x * (0.5 - y);
	else
		tmp = log(2.0) + (x * 0.5);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[x, -7.4e-53], N[(x * (-y)), $MachinePrecision], If[LessEqual[x, 1.75e-93], N[Log[2.0], $MachinePrecision], If[Or[LessEqual[x, 5.8e-18], N[Not[LessEqual[x, 2.1e-9]], $MachinePrecision]], N[(x * N[(0.5 - y), $MachinePrecision]), $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.4 \cdot 10^{-53}:\\
\;\;\;\;x \cdot \left(-y\right)\\

\mathbf{elif}\;x \leq 1.75 \cdot 10^{-93}:\\
\;\;\;\;\log 2\\

\mathbf{elif}\;x \leq 5.8 \cdot 10^{-18} \lor \neg \left(x \leq 2.1 \cdot 10^{-9}\right):\\
\;\;\;\;x \cdot \left(0.5 - y\right)\\

\mathbf{else}:\\
\;\;\;\;\log 2 + x \cdot 0.5\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if x < -7.39999999999999965e-53

    1. Initial program 98.9%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around inf 91.6%

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
    5. Step-by-step derivation
      1. associate-*r*91.6%

        \[\leadsto \color{blue}{\left(-1 \cdot y\right) \cdot x} \]
      2. *-commutative91.6%

        \[\leadsto \color{blue}{x \cdot \left(-1 \cdot y\right)} \]
      3. mul-1-neg91.6%

        \[\leadsto x \cdot \color{blue}{\left(-y\right)} \]
    6. Simplified91.6%

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

    if -7.39999999999999965e-53 < x < 1.75e-93

    1. Initial program 100.0%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{\log 2} - x \cdot y \]
    5. Taylor expanded in x around 0 80.2%

      \[\leadsto \color{blue}{\log 2} \]

    if 1.75e-93 < x < 5.8e-18 or 2.10000000000000019e-9 < x

    1. Initial program 94.4%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def94.4%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified94.4%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around 0 95.4%

      \[\leadsto \color{blue}{\left(0.5 - y\right) \cdot x + \log 2} \]
    5. Taylor expanded in x around inf 78.4%

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

    if 5.8e-18 < x < 2.10000000000000019e-9

    1. Initial program 98.7%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def99.7%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified99.7%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around 0 99.7%

      \[\leadsto \color{blue}{\left(0.5 - y\right) \cdot x + \log 2} \]
    5. Taylor expanded in y around 0 79.8%

      \[\leadsto \color{blue}{0.5 \cdot x + \log 2} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification84.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -7.4 \cdot 10^{-53}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 1.75 \cdot 10^{-93}:\\ \;\;\;\;\log 2\\ \mathbf{elif}\;x \leq 5.8 \cdot 10^{-18} \lor \neg \left(x \leq 2.1 \cdot 10^{-9}\right):\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + x \cdot 0.5\\ \end{array} \]

Alternative 4: 99.2% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1.4:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right) + \log 2\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x -1.4) (* x (- y)) (+ (* x (- 0.5 y)) (log 2.0))))
double code(double x, double y) {
	double tmp;
	if (x <= -1.4) {
		tmp = x * -y;
	} else {
		tmp = (x * (0.5 - y)) + log(2.0);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= (-1.4d0)) then
        tmp = x * -y
    else
        tmp = (x * (0.5d0 - y)) + log(2.0d0)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (x <= -1.4) {
		tmp = x * -y;
	} else {
		tmp = (x * (0.5 - y)) + Math.log(2.0);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if x <= -1.4:
		tmp = x * -y
	else:
		tmp = (x * (0.5 - y)) + math.log(2.0)
	return tmp
function code(x, y)
	tmp = 0.0
	if (x <= -1.4)
		tmp = Float64(x * Float64(-y));
	else
		tmp = Float64(Float64(x * Float64(0.5 - y)) + log(2.0));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= -1.4)
		tmp = x * -y;
	else
		tmp = (x * (0.5 - y)) + log(2.0);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[x, -1.4], N[(x * (-y)), $MachinePrecision], N[(N[(x * N[(0.5 - y), $MachinePrecision]), $MachinePrecision] + N[Log[2.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.4:\\
\;\;\;\;x \cdot \left(-y\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(0.5 - y\right) + \log 2\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.3999999999999999

    1. Initial program 98.8%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around inf 97.6%

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
    5. Step-by-step derivation
      1. associate-*r*97.6%

        \[\leadsto \color{blue}{\left(-1 \cdot y\right) \cdot x} \]
      2. *-commutative97.6%

        \[\leadsto \color{blue}{x \cdot \left(-1 \cdot y\right)} \]
      3. mul-1-neg97.6%

        \[\leadsto x \cdot \color{blue}{\left(-y\right)} \]
    6. Simplified97.6%

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

    if -1.3999999999999999 < x

    1. Initial program 98.8%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def98.9%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified98.9%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around 0 98.8%

      \[\leadsto \color{blue}{\left(0.5 - y\right) \cdot x + \log 2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.4:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right) + \log 2\\ \end{array} \]

Alternative 5: 98.8% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -120:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 - x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x -120.0) (* x (- y)) (- (log 2.0) (* x y))))
double code(double x, double y) {
	double tmp;
	if (x <= -120.0) {
		tmp = x * -y;
	} else {
		tmp = log(2.0) - (x * y);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= (-120.0d0)) then
        tmp = x * -y
    else
        tmp = log(2.0d0) - (x * y)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (x <= -120.0) {
		tmp = x * -y;
	} else {
		tmp = Math.log(2.0) - (x * y);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if x <= -120.0:
		tmp = x * -y
	else:
		tmp = math.log(2.0) - (x * y)
	return tmp
function code(x, y)
	tmp = 0.0
	if (x <= -120.0)
		tmp = Float64(x * Float64(-y));
	else
		tmp = Float64(log(2.0) - Float64(x * y));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= -120.0)
		tmp = x * -y;
	else
		tmp = log(2.0) - (x * y);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[x, -120.0], N[(x * (-y)), $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -120:\\
\;\;\;\;x \cdot \left(-y\right)\\

\mathbf{else}:\\
\;\;\;\;\log 2 - x \cdot y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -120

    1. Initial program 98.8%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around inf 98.8%

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
    5. Step-by-step derivation
      1. associate-*r*98.8%

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

        \[\leadsto \color{blue}{x \cdot \left(-1 \cdot y\right)} \]
      3. mul-1-neg98.8%

        \[\leadsto x \cdot \color{blue}{\left(-y\right)} \]
    6. Simplified98.8%

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

    if -120 < x

    1. Initial program 98.8%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def98.9%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified98.9%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around 0 97.1%

      \[\leadsto \color{blue}{\log 2} - x \cdot y \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -120:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 - x \cdot y\\ \end{array} \]

Alternative 6: 82.2% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{-54}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 4.1 \cdot 10^{-94}:\\ \;\;\;\;\log 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x -1e-54) (* x (- y)) (if (<= x 4.1e-94) (log 2.0) (* x (- 0.5 y)))))
double code(double x, double y) {
	double tmp;
	if (x <= -1e-54) {
		tmp = x * -y;
	} else if (x <= 4.1e-94) {
		tmp = log(2.0);
	} else {
		tmp = x * (0.5 - y);
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= (-1d-54)) then
        tmp = x * -y
    else if (x <= 4.1d-94) then
        tmp = log(2.0d0)
    else
        tmp = x * (0.5d0 - y)
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (x <= -1e-54) {
		tmp = x * -y;
	} else if (x <= 4.1e-94) {
		tmp = Math.log(2.0);
	} else {
		tmp = x * (0.5 - y);
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if x <= -1e-54:
		tmp = x * -y
	elif x <= 4.1e-94:
		tmp = math.log(2.0)
	else:
		tmp = x * (0.5 - y)
	return tmp
function code(x, y)
	tmp = 0.0
	if (x <= -1e-54)
		tmp = Float64(x * Float64(-y));
	elseif (x <= 4.1e-94)
		tmp = log(2.0);
	else
		tmp = Float64(x * Float64(0.5 - y));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= -1e-54)
		tmp = x * -y;
	elseif (x <= 4.1e-94)
		tmp = log(2.0);
	else
		tmp = x * (0.5 - y);
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[x, -1e-54], N[(x * (-y)), $MachinePrecision], If[LessEqual[x, 4.1e-94], N[Log[2.0], $MachinePrecision], N[(x * N[(0.5 - y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-54}:\\
\;\;\;\;x \cdot \left(-y\right)\\

\mathbf{elif}\;x \leq 4.1 \cdot 10^{-94}:\\
\;\;\;\;\log 2\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(0.5 - y\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -1e-54

    1. Initial program 98.9%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around inf 91.6%

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
    5. Step-by-step derivation
      1. associate-*r*91.6%

        \[\leadsto \color{blue}{\left(-1 \cdot y\right) \cdot x} \]
      2. *-commutative91.6%

        \[\leadsto \color{blue}{x \cdot \left(-1 \cdot y\right)} \]
      3. mul-1-neg91.6%

        \[\leadsto x \cdot \color{blue}{\left(-y\right)} \]
    6. Simplified91.6%

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

    if -1e-54 < x < 4.10000000000000001e-94

    1. Initial program 100.0%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def100.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{\log 2} - x \cdot y \]
    5. Taylor expanded in x around 0 80.2%

      \[\leadsto \color{blue}{\log 2} \]

    if 4.10000000000000001e-94 < x

    1. Initial program 95.0%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Step-by-step derivation
      1. log1p-def95.2%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Simplified95.2%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    4. Taylor expanded in x around 0 96.0%

      \[\leadsto \color{blue}{\left(0.5 - y\right) \cdot x + \log 2} \]
    5. Taylor expanded in x around inf 68.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{-54}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 4.1 \cdot 10^{-94}:\\ \;\;\;\;\log 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]

Alternative 7: 51.9% accurate, 51.8× speedup?

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

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

    \[\log \left(1 + e^{x}\right) - x \cdot y \]
  2. Step-by-step derivation
    1. log1p-def99.2%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
  3. Simplified99.2%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
  4. Taylor expanded in x around inf 52.6%

    \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
  5. Step-by-step derivation
    1. associate-*r*52.6%

      \[\leadsto \color{blue}{\left(-1 \cdot y\right) \cdot x} \]
    2. *-commutative52.6%

      \[\leadsto \color{blue}{x \cdot \left(-1 \cdot y\right)} \]
    3. mul-1-neg52.6%

      \[\leadsto x \cdot \color{blue}{\left(-y\right)} \]
  6. Simplified52.6%

    \[\leadsto \color{blue}{x \cdot \left(-y\right)} \]
  7. Final simplification52.6%

    \[\leadsto x \cdot \left(-y\right) \]

Alternative 8: 3.6% accurate, 69.0× speedup?

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

\\
x \cdot 0.5
\end{array}
Derivation
  1. Initial program 98.8%

    \[\log \left(1 + e^{x}\right) - x \cdot y \]
  2. Step-by-step derivation
    1. log1p-def99.2%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
  3. Simplified99.2%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
  4. Taylor expanded in x around 0 82.7%

    \[\leadsto \color{blue}{\left(0.5 - y\right) \cdot x + \log 2} \]
  5. Taylor expanded in y around 0 47.1%

    \[\leadsto \color{blue}{0.5 \cdot x + \log 2} \]
  6. Taylor expanded in x around inf 3.7%

    \[\leadsto \color{blue}{0.5 \cdot x} \]
  7. Step-by-step derivation
    1. *-commutative3.7%

      \[\leadsto \color{blue}{x \cdot 0.5} \]
  8. Simplified3.7%

    \[\leadsto \color{blue}{x \cdot 0.5} \]
  9. Final simplification3.7%

    \[\leadsto x \cdot 0.5 \]

Developer target: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 0:\\ \;\;\;\;\log \left(1 + e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + e^{-x}\right) - \left(-x\right) \cdot \left(1 - y\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x 0.0)
   (- (log (+ 1.0 (exp x))) (* x y))
   (- (log (+ 1.0 (exp (- x)))) (* (- x) (- 1.0 y)))))
double code(double x, double y) {
	double tmp;
	if (x <= 0.0) {
		tmp = log((1.0 + exp(x))) - (x * y);
	} else {
		tmp = log((1.0 + exp(-x))) - (-x * (1.0 - y));
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= 0.0d0) then
        tmp = log((1.0d0 + exp(x))) - (x * y)
    else
        tmp = log((1.0d0 + exp(-x))) - (-x * (1.0d0 - y))
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (x <= 0.0) {
		tmp = Math.log((1.0 + Math.exp(x))) - (x * y);
	} else {
		tmp = Math.log((1.0 + Math.exp(-x))) - (-x * (1.0 - y));
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if x <= 0.0:
		tmp = math.log((1.0 + math.exp(x))) - (x * y)
	else:
		tmp = math.log((1.0 + math.exp(-x))) - (-x * (1.0 - y))
	return tmp
function code(x, y)
	tmp = 0.0
	if (x <= 0.0)
		tmp = Float64(log(Float64(1.0 + exp(x))) - Float64(x * y));
	else
		tmp = Float64(log(Float64(1.0 + exp(Float64(-x)))) - Float64(Float64(-x) * Float64(1.0 - y)));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= 0.0)
		tmp = log((1.0 + exp(x))) - (x * y);
	else
		tmp = log((1.0 + exp(-x))) - (-x * (1.0 - y));
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[x, 0.0], N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], N[(N[Log[N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[((-x) * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 0:\\
\;\;\;\;\log \left(1 + e^{x}\right) - x \cdot y\\

\mathbf{else}:\\
\;\;\;\;\log \left(1 + e^{-x}\right) - \left(-x\right) \cdot \left(1 - y\right)\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2023181 
(FPCore (x y)
  :name "Logistic regression 2"
  :precision binary64

  :herbie-target
  (if (<= x 0.0) (- (log (+ 1.0 (exp x))) (* x y)) (- (log (+ 1.0 (exp (- x)))) (* (- x) (- 1.0 y))))

  (- (log (+ 1.0 (exp x))) (* x y)))