Skip to content

Replace boost::isfinite, etc. with std:: versions #1010

Closed

Description

@bob-carpenter

Description

We should move functionality from boost to std libraries wherever possible. There are a bunch of runtime numerical tests like isfinite and isnan that are supported in std now and we should move to those versions.

  1. Figure out which functions are now supported in std.

  2. Change the Stan code to use the std versions.

Example

Line 18 of prim/err/check_finite.hpp:

 if (!(boost::math::isfinite)(value_of_rec(y)))

Also, we can get rid of a lot of the type promotion in Boost and just use auto as the return type. For example, stan/math/prim/mat/fun/add.hpp could just be:

#ifndef STAN_MATH_PRIM_MAT_FUN_ADD_HPP
#define STAN_MATH_PRIM_MAT_FUN_ADD_HPP

#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/err/check_matching_dims.hpp>

namespace stan {
namespace math {

/**
 * Return the sum of the specified matrices.  The two matrices
 * must have the same dimensions.
 * @tparam T1 Scalar type of first matrix.
 * @tparam T2 Scalar type of second matrix.
 * @tparam R Row type of matrices.
 * @tparam C Column type of matrices.
 * @param m1 First matrix.
 * @param m2 Second matrix.
 * @return Sum of the matrices.
 * @throw std::invalid_argument if m1 and m2 do not have the same
 * dimensions.
 */
template <typename T1, typename T2, int R, int C>
inline auto
add(const Eigen::Matrix<T1, R, C>& m1, const Eigen::Matrix<T2, R, C>& m2) {
  check_matching_dims("add", "m1", m1, "m2", m2);
  return (m1 + m2).eval();
}

/**
 * Return the sum of the specified matrix and specified scalar.
 *
 * @tparam T1 Scalar type of matrix.
 * @tparam T2 Type of scalar.
 * @param m Matrix.
 * @param c Scalar.
 * @return The matrix plus the scalar.
 */
template <typename T1, typename T2, int R, int C>
inline auto
add(const Eigen::Matrix<T1, R, C>& m, const T2& c) {
  return (m.array() + c).matrix().eval();
}

/**
 * Return the sum of the specified scalar and specified matrix.
 *
 * @tparam T1 Type of scalar.
 * @tparam T2 Scalar type of matrix.
 * @param c Scalar.
 * @param m Matrix.
 * @return The scalar plus the matrix.
 */
template <typename T1, typename T2, int R, int C>
inline auto
add(const T1& c, const Eigen::Matrix<T2, R, C>& m) {
  return add(m, c);
}

}  // namespace math
}  // namespace stan
#endif

Current Math Version

v2.18.0

Activity

  1. added this to the v3.0.0 milestone on Aug 28, 2018
  2. rok-cesnovar commented on Aug 28, 2018

    @rok-cesnovar
    Member

    Here is quick list of all boost::math functions used in Stan Math that have their std equivalent since C++11:

  3. andrjohns commented on Dec 9, 2018

    @andrjohns
    Collaborator

    Updating this issue with the goal of breaking the pulls into more manageable chunks. There are 3 categories of changes to be made (i.e. 3 different pulls):

    Boost functions with no required input checks:

    • asinh
    • cbrt
    • erf
    • erfc
    • expm1
    • isinf
    • isnan
    • round
    • trunc

    Boost functions that require input checks (to maintain current throwing behaviour):

    • acosh
      • Requires throw for x < 1
      • Requires windows-specific handling of inf input
    • atanh
      • Requires throw if x not in [-1,1]
    • lgamma
      • Change to return inf for zero input (currently returns nan)
    • log1p
      • Requires throw for x < -1
    • tgamma
      • Requires throw for x = 0, or x is negative integer

    Hand-coded functions that can be replaced

    • exp2
    • fdim
    • fma
    • fmax
    • fmin
    • isfinite
    • isgreater
    • isgreaterequal
    • isless
    • islessequal
    • hypot
    • ldexp
    • log2
  4. mcol commented on Dec 23, 2019

    @mcol
    Member

    Curiously enough, despite the issue title none of the PRs referenced here have actually removed the uses of boost::math::isfinite! :)

    Overall, the following still need to be done

    • fpclassify
    • isfinite
    • isinf (only in tests)
    • isnan (only in tests)
    • isnormal (only in tests)
  5. reopened this on Dec 23, 2019
  6. modified the milestones: 3.0.0, 3.0.0++ on Jan 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions