Skip to content

Commit

Permalink
Cleanups for mfexp and added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnoel committed Nov 25, 2015
1 parent 0131145 commit 31508ee
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 121 deletions.
101 changes: 41 additions & 60 deletions src/linad99/mfexp.cpp
@@ -1,70 +1,51 @@
/*
* $Id$
*
* Author: David Fournier
* Copyright (c) 2008-2012 Regents of the University of California
*/
/**
\file
Robust exponential functions for variable scalar objects.
*/
#include <fvar.hpp>
\file Robust exponential functions for variable scalar objects.
/**
Robust exponential function for variable argument > 60 or < -60.
Prevents overflow and underflow for arguments outside of the domain
of exp().
(Note: \f$e^{60} > 10^{26}\f$.)
\param x dvariable exponent.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
x > 60 & e^{60}\frac{(1+2(x-60))}{1+x-60}\\
x < 60 & e^{-60}\frac{(1-x-60)}{1+2(-x-60)}\\
{\rm else} & e^x
\end{array}\right.\f$
\ingroup misc
*/
dvariable mfexp(const prevariable& x)
{
double b=60;
if (x<=b && x>=-b)
{
return exp(x);
}
else if (x>b)
{
return exp(b)*(1.+2.*(x-b))/(1.+x-b);
}
else
{
return exp(-b)*(1.-x-b)/(1.+2.*(-x-b));
}
}
Author: David Fournier
Copyright (c) 2008-2015 Regents of the University of California
*/
#include <fvar.hpp>

/**
Robust exponential function for variable argument with user
specified domain bound.
Prevents overflow and underflow for arguments outside of the domain
\param x dvariable exponent.
\param b double user specified function domain bound.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
Robust exponential function for variable argument with user
specified domain bound.
Prevents overflow and underflow for arguments outside of the domain
\param x dvariable exponent.
\param b double user specified function domain bound.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
x > b & e^b\frac{(1+2(x-b))}{1+x-b}\\
x < b & e^{-b}\frac{(1-x-b)}{1+2(-x-b)}\\
{\rm else} & e^x
\end{array}\right.\f$
\ingroup misc
*/
\end{array}\right.\f$
\ingroup misc
*/
dvariable mfexp(const prevariable& x, double b)
{
if (x > b)
{
return exp(b)*(1.+2.*(x-b))/(1.+x-b);
}
else if (x < -b)
{
if (x<=b && x>=-b)
{
return exp(x);
}
else if (x>b)
{
return exp(b)*(1.+2.*(x-b))/(1.+x-b);
}
else
{
return exp(-b)*(1.-x-b)/(1.+2.*(-x-b));
}
return exp(-b)*(1.-x-b)/(1.+2.*(-x-b));
}
return exp(x);
}
/**
Robust exponential function for variable argument > 60 or < -60.
Prevents overflow and underflow for arguments outside of the domain
of exp().
(Note: \f$e^{60} > 10^{26}\f$.)
\param x dvariable exponent.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
x > 60 & e^{60}\frac{(1+2(x-60))}{1+x-60}\\
x < 60 & e^{-60}\frac{(1-x-60)}{1+2(-x-60)}\\
{\rm else} & e^x
\end{array}\right.\f$
\ingroup misc
*/
dvariable mfexp(const prevariable& x)
{
double b = 60;
return mfexp(x, b);
}
104 changes: 43 additions & 61 deletions src/linad99/mfexpcon.cpp
@@ -1,70 +1,52 @@
/*
* $Id$
*
* Author: David Fournier
* Copyright (c) 2008-2012 Regents of the University of California
*/
/**
* \file
Robust exponential functions for constant scalar objects.
*/
#include <fvar.hpp>
\file Robust exponential functions for constant scalar objects.
/**
Robust exponential function for constant argument > 60 or < -60.
Prevents overflow and underflow for arguments outside of the domain
of exp().
(Note: \f$e^{60} > 10^{26}\f$.)
\param x exponent.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
x > 60 & e^{60}\frac{(1+2(x-60))}{1+x-60}\\
x < 60 & e^{-60}\frac{(1-x-60)}{1+2(-x-60)}\\
{\rm else} & e^x
\end{array}\right.\f$
\ingroup misc
*/
double mfexp(double x)
{
double b = 60;
if (x<=b && x>=-b)
{
return exp(x);
}
else if (x>b)
{
return exp(b)*(1.+2.*(x-b))/(1.+x-b);
}
else
{
return exp(-b)*(1.-x-b)/(1.+2.*(-x-b));
}
}
Author: David Fournier
Copyright (c) 2008-2015 Regents of the University of California
*/
#include <fvar.hpp>

/**
Robust exponential function for constant argument with user
specified domain bound.
Prevents overflow and underflow for arguments outside of the domain
\param x exponent.
\param b ouble user specified function domain bound.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
Robust exponential function for constant argument with user
specified domain bound.
Prevents overflow and underflow for arguments outside of the domain
\param x exponent.
\param b ouble user specified function domain bound.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
x > b & e^b\frac{(1+2(x-b))}{1+x-b}\\
x < b & e^{-b}\frac{(1-x-b)}{1+2(-x-b)}\\
{\rm else} & e^x
\end{array}\right.\f$
\ingroup misc
*/
double mfexp(double x,double b)
\end{array}\right.\f$
\ingroup misc
*/
double mfexp(double x, double b)
{
if (x > b)
{
return exp(b)*(1.+2.*(x-b))/(1.+x-b);
}
else if (x < -b)
{
if (x<=b && x>=-b)
{
return exp(x);
}
else if (x>b)
{
return exp(b)*(1.+2.*(x-b))/(1.+x-b);
}
else
{
return exp(-b)*(1.-x-b)/(1.+2.*(-x-b));
}
return exp(-b)*(1.-x-b)/(1.+2.*(-x-b));
}
return exp(x);
}
/**
Robust exponential function for constant argument > 60 or < -60.
Prevents overflow and underflow for arguments outside of the domain
of exp().
(Note: \f$e^{60} > 10^{26}\f$.)
\param x exponent.
\return \f$\left\{\begin{array} {r@{\quad:\quad}l}
x > 60 & e^{60}\frac{(1+2(x-60))}{1+x-60}\\
x < 60 & e^{-60}\frac{(1-x-60)}{1+2(-x-60)}\\
{\rm else} & e^x
\end{array}\right.\f$
\ingroup misc
*/
double mfexp(double x)
{
double b = 60;
return mfexp(x, b);
}

11 changes: 11 additions & 0 deletions tests/gtests/test_dvariable.cpp
Expand Up @@ -102,6 +102,7 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_TRUE(result > ::exp(60));
}
{
double input = -100;
Expand All @@ -111,6 +112,7 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_TRUE(result < ::exp(-60));
}
{
double input = 60;
Expand All @@ -120,6 +122,7 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_DOUBLE_EQ(result, ::exp(60));
}
{
double input = -60;
Expand All @@ -129,6 +132,7 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_DOUBLE_EQ(result, ::exp(-60));
}
{
double input = 61;
Expand All @@ -138,6 +142,8 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_TRUE(result > ::exp(60));
ASSERT_TRUE(result < ::exp(61));
}
{
double input = -61;
Expand All @@ -147,6 +153,8 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_TRUE(result < ::exp(-60));
ASSERT_TRUE(result > ::exp(-61));
}
{
double input = 10;
Expand All @@ -156,6 +164,7 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_DOUBLE_EQ(result, ::exp(10));
}
{
double input = -10;
Expand All @@ -165,6 +174,7 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_DOUBLE_EQ(result, ::exp(-10));
}
{
double input = 0;
Expand All @@ -174,5 +184,6 @@ TEST_F(test_dvariable, mfexp)
dvariable vresult = mfexp(vinput);

ASSERT_DOUBLE_EQ(result, value(vresult));
ASSERT_DOUBLE_EQ(result, ::exp(0));
}
}

0 comments on commit 31508ee

Please sign in to comment.