Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 83 additions & 5 deletions cxx/include/tensorwrapper/types/floating_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

#pragma once
#include <algorithm>
#include <cmath>
#include <stdexcept>
#include <tuple>
#include <wtf/wtf.hpp>
#ifdef ENABLE_SIGMA
Expand Down Expand Up @@ -50,9 +52,15 @@ using taylor_model_type = sigma::TaylorModel<T>;
using tmfloat = taylor_model_type<float>;
using tmdouble = taylor_model_type<double>;

/// An interval with an independently stored nominal value (see sigma::Triplex)
template<typename T>
using triplex_type = sigma::Triplex<sigma::Interval<T>>;
using tpfloat = triplex_type<float>;
using tpdouble = triplex_type<double>;

using floating_point_types =
std::tuple<float, double, ufloat, udouble, ifloat, idouble, afloat, adouble,
tafloat, tadouble, tmfloat, tmdouble>;
tafloat, tadouble, tmfloat, tmdouble, tpfloat, tpdouble>;

template<typename T>
constexpr bool is_uncertain_v =
Expand All @@ -74,10 +82,14 @@ template<typename T>
constexpr bool is_taylor_model_v =
std::is_same_v<T, tmfloat> || std::is_same_v<T, tmdouble>;

template<typename T>
constexpr bool is_triplex_v =
std::is_same_v<T, tpfloat> || std::is_same_v<T, tpdouble>;

template<typename T>
constexpr bool is_uq_type_v =
is_uncertain_v<T> || is_interval_v<T> || is_affine_v<T> ||
is_thresholded_affine_v<T> || is_taylor_model_v<T>;
is_thresholded_affine_v<T> || is_taylor_model_v<T> || is_triplex_v<T>;

template<typename T, typename U, typename V>
T construct_uq_type(const U& center, const V& radius) {
Expand All @@ -88,19 +100,65 @@ T construct_uq_type(const U& center, const V& radius) {
} else if constexpr(is_affine_v<T> || is_thresholded_affine_v<T> ||
is_taylor_model_v<T>) {
return T(center - radius, center + radius);
} else if constexpr(is_triplex_v<T>) {
// Unlike the bounds of an interval, those of a triplex must bracket
// the nominal value, so a radius is treated as a magnitude
const auto r = radius < V(0) ? -radius : radius;
return T(center - r, center, center + r);
} else if constexpr(is_uq_type_v<T>) {
throw std::logic_error("UQ type not recognized in construct_uq_type.");
} else {
return T(center + radius);
}
}

/** @brief Constructs a UQ value from a nominal value and signed deviations.
*
* The value lies in [nominal - minus, nominal + plus]. Types which can store
* that exactly, i.e., triplex, do so. The others store the tightest
* representation they can which contains it: the interval-like types store
* the bounds, which drops the nominal value, and Uncertain stores @p nominal
* as its mean and the larger of @p minus and @p plus as its standard
* deviation. Non-UQ types store @p nominal.
*
* @tparam T The type to construct.
*
* @param[in] nominal The nominal value, i.e., the best estimate.
* @param[in] minus The deviation below @p nominal. Must be non-negative.
* @param[in] plus The deviation above @p nominal. Must be non-negative.
*
* @return The constructed value.
*
* @throw std::domain_error if @p minus or @p plus is negative. Strong throw
* guarantee.
*/
template<typename T, typename U, typename V, typename W>
T construct_uq_type(const U& nominal, const V& minus, const W& plus) {
if(minus < V(0) || plus < W(0))
throw std::domain_error(
"construct_uq_type: deviations must be non-negative");
if constexpr(is_uncertain_v<T>) {
return T(nominal, std::max<U>(minus, plus));
} else if constexpr(is_interval_v<T> || is_affine_v<T> ||
is_thresholded_affine_v<T> || is_taylor_model_v<T>) {
return T(nominal - minus, nominal + plus);
} else if constexpr(is_triplex_v<T>) {
return T(nominal - minus, nominal, nominal + plus);
} else if constexpr(is_uq_type_v<T>) {
throw std::logic_error("UQ type not recognized in construct_uq_type.");
} else {
return T(nominal);
}
}

template<typename T>
auto uq_center(const T& value) {
if constexpr(is_uncertain_v<T>) {
return value.mean();
} else if constexpr(is_interval_v<T>) {
return value.median();
} else if constexpr(is_triplex_v<T>) {
return value.nominal();
} else if constexpr(is_affine_v<T> || is_thresholded_affine_v<T>) {
return value.center();
} else if constexpr(is_taylor_model_v<T>) {
Expand All @@ -116,7 +174,7 @@ template<typename T>
auto uq_upper(const T& value) {
if constexpr(is_uncertain_v<T>) {
return value.mean() + value.sd();
} else if constexpr(is_interval_v<T>) {
} else if constexpr(is_interval_v<T> || is_triplex_v<T>) {
return value.upper();
} else if constexpr(is_affine_v<T> || is_thresholded_affine_v<T> ||
is_taylor_model_v<T>) {
Expand All @@ -132,7 +190,7 @@ template<typename T>
auto uq_lower(const T& value) {
if constexpr(is_uncertain_v<T>) {
return value.mean() - value.sd();
} else if constexpr(is_interval_v<T>) {
} else if constexpr(is_interval_v<T> || is_triplex_v<T>) {
return value.lower();
} else if constexpr(is_affine_v<T> || is_thresholded_affine_v<T> ||
is_taylor_model_v<T>) {
Expand Down Expand Up @@ -197,7 +255,9 @@ T pow(T value, double pow) {
MACRO_IN(tensorwrapper::types::tafloat); \
MACRO_IN(tensorwrapper::types::tadouble); \
MACRO_IN(tensorwrapper::types::tmfloat); \
MACRO_IN(tensorwrapper::types::tmdouble)
MACRO_IN(tensorwrapper::types::tmdouble); \
MACRO_IN(tensorwrapper::types::tpfloat); \
MACRO_IN(tensorwrapper::types::tpdouble)
} // namespace tensorwrapper::types

WTF_REGISTER_FP_TYPE_AS(tensorwrapper::types::ufloat, "ufloat");
Expand All @@ -210,6 +270,8 @@ WTF_REGISTER_FP_TYPE_AS(tensorwrapper::types::tafloat, "tafloat");
WTF_REGISTER_FP_TYPE_AS(tensorwrapper::types::tadouble, "tadouble");
WTF_REGISTER_FP_TYPE_AS(tensorwrapper::types::tmfloat, "tmfloat");
WTF_REGISTER_FP_TYPE_AS(tensorwrapper::types::tmdouble, "tmdouble");
WTF_REGISTER_FP_TYPE_AS(tensorwrapper::types::tpfloat, "tpfloat");
WTF_REGISTER_FP_TYPE_AS(tensorwrapper::types::tpdouble, "tpdouble");

#else
template<typename T>
Expand All @@ -232,6 +294,10 @@ template<typename T>
using taylor_model_type = T;
using tmfloat = float;
using tmdouble = double;
template<typename T>
using triplex_type = T;
using tpfloat = float;
using tpdouble = double;

using floating_point_types = std::tuple<float, double>;

Expand All @@ -250,6 +316,9 @@ constexpr bool is_thresholded_affine_v = false;
template<typename T>
constexpr bool is_taylor_model_v = false;

template<typename T>
constexpr bool is_triplex_v = false;

template<typename T>
constexpr bool is_uq_type_v = false;

Expand All @@ -258,6 +327,15 @@ T construct_uq_type(const U& center, const V&) {
return T(center);
}

/// Without Sigma there are no UQ types, so only @p nominal is kept
template<typename T, typename U, typename V, typename W>
T construct_uq_type(const U& nominal, const V& minus, const W& plus) {
if(minus < V(0) || plus < W(0))
throw std::domain_error(
"construct_uq_type: deviations must be non-negative");
return T(nominal);
}

template<typename T>
T uq_center(const T& value) {
return value;
Expand Down
2 changes: 1 addition & 1 deletion cxx/src/tensorwrapper/buffer/contiguous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Contiguous::Contiguous(buffer_type buffer, shape_type shape) :
my_base_type(std::make_unique<layout::Physical>(shape)),
m_shape_(std::move(shape)),
m_buffer_() {
if(buffer.size() == shape.size()) {
if(buffer.size() == m_shape_.size()) {
m_buffer_ = std::move(buffer);
} else {
throw std::invalid_argument(
Expand Down
7 changes: 7 additions & 0 deletions cxx/src/tensorwrapper/buffer/detail_/hash_utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ void hash_input(hash_type& seed, const types::interval_type<T>& value) {
hash_input(seed, value.upper());
}

template<typename T>
void hash_input(hash_type& seed, const types::triplex_type<T>& value) {
hash_input(seed, value.lower());
hash_input(seed, value.nominal());
hash_input(seed, value.upper());
}

template<typename T>
void hash_input(hash_type& seed, const types::affine_type<T>& value) {
hash_input(seed, value.center());
Expand Down
2 changes: 1 addition & 1 deletion cxx/src/tensorwrapper/shape/detail_/smooth_alias.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SmoothAlias : public SmoothViewPIMPL<SmoothType> {

/// These just call shape's member function with the same name
///@{
rank_type extent_(size_type i) const override { return shape_().extent(i); }
size_type extent_(size_type i) const override { return shape_().extent(i); }
rank_type rank_() const noexcept override { return shape_().rank(); }
size_type size_() const noexcept override { return shape_().size(); }
///@}
Expand Down
4 changes: 2 additions & 2 deletions cxx/src/tensorwrapper/shape/detail_/smooth_view_pimpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SmoothViewPIMPL : public tensorwrapper::detail_::PolymorphicBase<
typename types::ClassTraits<parent_type>::const_pimpl_pointer;

/// Derived class implements by overriding extent_
rank_type extent(size_type i) const { return extent_(i); }
size_type extent(size_type i) const { return extent_(i); }

/// Derived class implements by overriding rank_
rank_type rank() const noexcept { return rank_(); }
Expand All @@ -68,7 +68,7 @@ class SmoothViewPIMPL : public tensorwrapper::detail_::PolymorphicBase<

protected:
/// Derived class should implement to be consistent with SmoothView::extent
virtual rank_type extent_(size_type i) const = 0;
virtual size_type extent_(size_type i) const = 0;

/// Derived class should implement to be consistent with SmoothView::rank
virtual rank_type rank_() const noexcept = 0;
Expand Down
16 changes: 16 additions & 0 deletions tests/cxx/unit_tests/tensorwrapper/buffer/contiguous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "../testing/testing.hpp"
#include <limits>
#include <tensorwrapper/buffer/contiguous.hpp>
#include <tensorwrapper/types/floating_point.hpp>

Expand Down Expand Up @@ -556,6 +557,21 @@ TEMPLATE_LIST_TEST_CASE("Contiguous", "", types::floating_point_types) {
}
}

TEST_CASE("Contiguous with an extent larger than rank_type can hold") {
// Regression test: the extents of shape() were returned as rank_type, so
// they wrapped, e.g., 65537 became 1
using buffer::Contiguous;
using shape_type = typename Contiguous::shape_type;
using rank_type = typename shape_type::rank_type;
const auto big = std::size_t(std::numeric_limits<rank_type>::max()) + 2;

shape_type shape{2, big};
Contiguous buf(std::vector<double>(2 * big, 1.0), shape);
REQUIRE(buf.shape().extent(0) == 2);
REQUIRE(buf.shape().extent(1) == big);
REQUIRE(wtf::fp::float_cast<double>(buf.get_elem({1, big - 1})) == 1.0);
}

TEST_CASE("make_contiguous(buffer, shape)") {
using buffer::Contiguous;
using tensor_type = Tensor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ TEMPLATE_LIST_TEST_CASE("hash_input", "", types::floating_point_types) {
boost::hash_combine(corr, value.remainder().lower());
boost::hash_combine(corr, value.remainder().upper());
REQUIRE(seed == corr);
} else if constexpr(types::is_triplex_v<TestType>) {
value_type value(0.5, 1.0, 2.0);
hash_input(seed, value);
hash_type corr{0};
boost::hash_combine(corr, value.lower());
boost::hash_combine(corr, value.nominal());
boost::hash_combine(corr, value.upper());
REQUIRE(seed == corr);
} else if constexpr(types::is_uq_type_v<TestType>) {
throw std::runtime_error("UQ type not registered for hash_input");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ template<typename T>
constexpr double eigen_system_tol =
std::is_same_v<T, float> || std::is_same_v<T, types::ufloat> ||
std::is_same_v<T, types::ifloat> || std::is_same_v<T, types::afloat> ||
std::is_same_v<T, types::tafloat> || std::is_same_v<T, types::tmfloat> ?
std::is_same_v<T, types::tafloat> || std::is_same_v<T, types::tmfloat> ||
std::is_same_v<T, types::tpfloat> ?
1e-5 :
1e-12;
} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ template<typename T>
constexpr double log_spacing_tol =
std::is_same_v<T, float> || std::is_same_v<T, types::ufloat> ||
std::is_same_v<T, types::ifloat> || std::is_same_v<T, types::afloat> ||
std::is_same_v<T, types::tafloat> || std::is_same_v<T, types::tmfloat> ?
std::is_same_v<T, types::tafloat> || std::is_same_v<T, types::tmfloat> ||
std::is_same_v<T, types::tpfloat> ?
5e-3 :
1e-9;
} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ TEMPLATE_LIST_TEST_CASE("random_orthogonal_matrix", "",
std::is_same_v<TestType, types::ifloat> ||
std::is_same_v<TestType, types::afloat> ||
std::is_same_v<TestType, types::tafloat> ||
std::is_same_v<TestType, types::tmfloat> ?
std::is_same_v<TestType, types::tmfloat> ||
std::is_same_v<TestType, types::tpfloat> ?
1e-5 :
1e-12;
REQUIRE(approximately_equal(product, ident, tol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ TEMPLATE_LIST_TEST_CASE("approximately_equal", "",
// the default tolerance. Note that approximately_equal should work
// for intervals and tolerances greater than machine epsilon; that is
// tested in the "Differ by less than provided tolerance" section below.
if constexpr(!types::is_interval_v<TestType>) {
// Triplex bounds are intervals, so the same applies to them.
if constexpr(!types::is_interval_v<TestType> &&
!types::is_triplex_v<TestType>) {
pscalar2->set_elem({}, TestType{42.0} + value);
pvector2->set_elem({0}, TestType{1.23} + value);
Tensor scalar2(s0, std::move(pscalar2));
Expand Down
11 changes: 11 additions & 0 deletions tests/cxx/unit_tests/tensorwrapper/shape/smooth_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "../testing/testing.hpp"
#include <limits>
#include <set>
#include <tensorwrapper/shape/smooth.hpp>
#include <tensorwrapper/shape/smooth_view.hpp>
Expand Down Expand Up @@ -83,6 +84,16 @@ TEMPLATE_LIST_TEST_CASE("SmoothView", "", types2test) {
REQUIRE(alias_vector.size() == size_type(3));
}

SECTION("extent larger than rank_type can hold") {
// Regression test: extents were returned as rank_type, so they wrapped
const auto big = size_type(std::numeric_limits<rank_type>::max()) + 2;
smooth_type big_shape{3, big};
view_type alias_big(big_shape);
REQUIRE(alias_big.extent(0) == size_type(3));
REQUIRE(alias_big.extent(1) == big);
REQUIRE(alias_big.size() == 3 * big);
}

SECTION("Utility methods") {
SECTION("swap") {
view_type scalar_copy(alias_scalar);
Expand Down
Loading
Loading