Program Listing for File math.hpp

Return to documentation for file (rcppsw/math/math.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <cmath>
#include <utility>

#include "rcppsw/rcppsw.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace rcppsw::math {

/*******************************************************************************
 * Constant Definitions
 ******************************************************************************/
static constexpr double kDOUBLE_EPSILON = RCSW_DOUBLE_EPSILON;

/*******************************************************************************
 * Free-Functions
 ******************************************************************************/
static inline bool is_equal(double lhs,
                            double rhs,
                            double tol = kDOUBLE_EPSILON) {
  return std::fabs(lhs - rhs) < tol;
}

static inline bool is_multiple_of(double x,
                                  double y,
                                  double tol = kDOUBLE_EPSILON) {
  return std::fabs(std::remainder(x, y)) < tol;
}

namespace detail {

template <class T>
using length_func_decltype = decltype(std::declval<T>().length());

} /* namespace detail */

template<typename T,
         RCPPSW_SFINAE_DECLDEF(rmpl::is_detected<detail::length_func_decltype, T>::value)>
static inline double l2norm(const T& lhs, const T& rhs) {
  return (lhs - rhs).length();
}

template<typename T,
         RCPPSW_SFINAE_DECLDEF(rmpl::is_detected<detail::length_func_decltype, T>::value)>

static inline double l2norm_squared(const T& lhs, const T& rhs) {
  return (lhs - rhs).squared_length();
}

} /* namespace rcppsw::math */