From 02878e6aa66e9942f28113407a85feb5b4914ce4 Mon Sep 17 00:00:00 2001 From: "Ryan M. Richard" Date: Mon, 16 Nov 2026 12:09:09 -0600 Subject: [PATCH] adds triplex support --- .../tensorwrapper/types/floating_point.hpp | 88 +++++++++++++++++-- cxx/src/tensorwrapper/buffer/contiguous.cpp | 2 +- .../buffer/detail_/hash_utilities.hpp | 7 ++ .../shape/detail_/smooth_alias.hpp | 2 +- .../shape/detail_/smooth_view_pimpl.hpp | 4 +- .../tensorwrapper/buffer/contiguous.cpp | 16 ++++ .../buffer/detail_/hash_utilities.cpp | 8 ++ .../generate/generate_eigen_system.cpp | 3 +- .../generate/generate_eigenvalues.cpp | 3 +- .../generate/random_orthogonal_matrix.cpp | 3 +- .../operations/approximately_equal.cpp | 4 +- .../tensorwrapper/shape/smooth_view.cpp | 11 +++ .../tensorwrapper/types/floating_point.cpp | 86 ++++++++++++++++++ 13 files changed, 224 insertions(+), 13 deletions(-) create mode 100644 tests/cxx/unit_tests/tensorwrapper/types/floating_point.cpp diff --git a/cxx/include/tensorwrapper/types/floating_point.hpp b/cxx/include/tensorwrapper/types/floating_point.hpp index 4bf825a8..ae8dcb5b 100644 --- a/cxx/include/tensorwrapper/types/floating_point.hpp +++ b/cxx/include/tensorwrapper/types/floating_point.hpp @@ -15,7 +15,9 @@ */ #pragma once +#include #include +#include #include #include #ifdef ENABLE_SIGMA @@ -50,9 +52,15 @@ using taylor_model_type = sigma::TaylorModel; using tmfloat = taylor_model_type; using tmdouble = taylor_model_type; +/// An interval with an independently stored nominal value (see sigma::Triplex) +template +using triplex_type = sigma::Triplex>; +using tpfloat = triplex_type; +using tpdouble = triplex_type; + using floating_point_types = std::tuple; + tafloat, tadouble, tmfloat, tmdouble, tpfloat, tpdouble>; template constexpr bool is_uncertain_v = @@ -74,10 +82,14 @@ template constexpr bool is_taylor_model_v = std::is_same_v || std::is_same_v; +template +constexpr bool is_triplex_v = + std::is_same_v || std::is_same_v; + template constexpr bool is_uq_type_v = is_uncertain_v || is_interval_v || is_affine_v || - is_thresholded_affine_v || is_taylor_model_v; + is_thresholded_affine_v || is_taylor_model_v || is_triplex_v; template T construct_uq_type(const U& center, const V& radius) { @@ -88,6 +100,11 @@ T construct_uq_type(const U& center, const V& radius) { } else if constexpr(is_affine_v || is_thresholded_affine_v || is_taylor_model_v) { return T(center - radius, center + radius); + } else if constexpr(is_triplex_v) { + // 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) { throw std::logic_error("UQ type not recognized in construct_uq_type."); } else { @@ -95,12 +112,53 @@ T construct_uq_type(const U& center, const V& 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 +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) { + return T(nominal, std::max(minus, plus)); + } else if constexpr(is_interval_v || is_affine_v || + is_thresholded_affine_v || is_taylor_model_v) { + return T(nominal - minus, nominal + plus); + } else if constexpr(is_triplex_v) { + return T(nominal - minus, nominal, nominal + plus); + } else if constexpr(is_uq_type_v) { + throw std::logic_error("UQ type not recognized in construct_uq_type."); + } else { + return T(nominal); + } +} + template auto uq_center(const T& value) { if constexpr(is_uncertain_v) { return value.mean(); } else if constexpr(is_interval_v) { return value.median(); + } else if constexpr(is_triplex_v) { + return value.nominal(); } else if constexpr(is_affine_v || is_thresholded_affine_v) { return value.center(); } else if constexpr(is_taylor_model_v) { @@ -116,7 +174,7 @@ template auto uq_upper(const T& value) { if constexpr(is_uncertain_v) { return value.mean() + value.sd(); - } else if constexpr(is_interval_v) { + } else if constexpr(is_interval_v || is_triplex_v) { return value.upper(); } else if constexpr(is_affine_v || is_thresholded_affine_v || is_taylor_model_v) { @@ -132,7 +190,7 @@ template auto uq_lower(const T& value) { if constexpr(is_uncertain_v) { return value.mean() - value.sd(); - } else if constexpr(is_interval_v) { + } else if constexpr(is_interval_v || is_triplex_v) { return value.lower(); } else if constexpr(is_affine_v || is_thresholded_affine_v || is_taylor_model_v) { @@ -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"); @@ -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 @@ -232,6 +294,10 @@ template using taylor_model_type = T; using tmfloat = float; using tmdouble = double; +template +using triplex_type = T; +using tpfloat = float; +using tpdouble = double; using floating_point_types = std::tuple; @@ -250,6 +316,9 @@ constexpr bool is_thresholded_affine_v = false; template constexpr bool is_taylor_model_v = false; +template +constexpr bool is_triplex_v = false; + template constexpr bool is_uq_type_v = false; @@ -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 +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 T uq_center(const T& value) { return value; diff --git a/cxx/src/tensorwrapper/buffer/contiguous.cpp b/cxx/src/tensorwrapper/buffer/contiguous.cpp index 329e43d7..3708d538 100644 --- a/cxx/src/tensorwrapper/buffer/contiguous.cpp +++ b/cxx/src/tensorwrapper/buffer/contiguous.cpp @@ -42,7 +42,7 @@ Contiguous::Contiguous(buffer_type buffer, shape_type shape) : my_base_type(std::make_unique(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( diff --git a/cxx/src/tensorwrapper/buffer/detail_/hash_utilities.hpp b/cxx/src/tensorwrapper/buffer/detail_/hash_utilities.hpp index 236bc304..6ec3e872 100644 --- a/cxx/src/tensorwrapper/buffer/detail_/hash_utilities.hpp +++ b/cxx/src/tensorwrapper/buffer/detail_/hash_utilities.hpp @@ -81,6 +81,13 @@ void hash_input(hash_type& seed, const types::interval_type& value) { hash_input(seed, value.upper()); } +template +void hash_input(hash_type& seed, const types::triplex_type& value) { + hash_input(seed, value.lower()); + hash_input(seed, value.nominal()); + hash_input(seed, value.upper()); +} + template void hash_input(hash_type& seed, const types::affine_type& value) { hash_input(seed, value.center()); diff --git a/cxx/src/tensorwrapper/shape/detail_/smooth_alias.hpp b/cxx/src/tensorwrapper/shape/detail_/smooth_alias.hpp index bd355325..4aed6e4c 100644 --- a/cxx/src/tensorwrapper/shape/detail_/smooth_alias.hpp +++ b/cxx/src/tensorwrapper/shape/detail_/smooth_alias.hpp @@ -65,7 +65,7 @@ class SmoothAlias : public SmoothViewPIMPL { /// 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(); } ///@} diff --git a/cxx/src/tensorwrapper/shape/detail_/smooth_view_pimpl.hpp b/cxx/src/tensorwrapper/shape/detail_/smooth_view_pimpl.hpp index a9d3566c..a384d521 100644 --- a/cxx/src/tensorwrapper/shape/detail_/smooth_view_pimpl.hpp +++ b/cxx/src/tensorwrapper/shape/detail_/smooth_view_pimpl.hpp @@ -55,7 +55,7 @@ class SmoothViewPIMPL : public tensorwrapper::detail_::PolymorphicBase< typename types::ClassTraits::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_(); } @@ -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; diff --git a/tests/cxx/unit_tests/tensorwrapper/buffer/contiguous.cpp b/tests/cxx/unit_tests/tensorwrapper/buffer/contiguous.cpp index e4f0004c..07ccd469 100644 --- a/tests/cxx/unit_tests/tensorwrapper/buffer/contiguous.cpp +++ b/tests/cxx/unit_tests/tensorwrapper/buffer/contiguous.cpp @@ -15,6 +15,7 @@ */ #include "../testing/testing.hpp" +#include #include #include @@ -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::max()) + 2; + + shape_type shape{2, big}; + Contiguous buf(std::vector(2 * big, 1.0), shape); + REQUIRE(buf.shape().extent(0) == 2); + REQUIRE(buf.shape().extent(1) == big); + REQUIRE(wtf::fp::float_cast(buf.get_elem({1, big - 1})) == 1.0); +} + TEST_CASE("make_contiguous(buffer, shape)") { using buffer::Contiguous; using tensor_type = Tensor; diff --git a/tests/cxx/unit_tests/tensorwrapper/buffer/detail_/hash_utilities.cpp b/tests/cxx/unit_tests/tensorwrapper/buffer/detail_/hash_utilities.cpp index 4fd4c334..94b9543e 100644 --- a/tests/cxx/unit_tests/tensorwrapper/buffer/detail_/hash_utilities.cpp +++ b/tests/cxx/unit_tests/tensorwrapper/buffer/detail_/hash_utilities.cpp @@ -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) { + 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) { throw std::runtime_error("UQ type not registered for hash_input"); } else { diff --git a/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigen_system.cpp b/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigen_system.cpp index 3e041aab..3e1e5005 100644 --- a/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigen_system.cpp +++ b/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigen_system.cpp @@ -33,7 +33,8 @@ template constexpr double eigen_system_tol = std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || - std::is_same_v || std::is_same_v ? + std::is_same_v || std::is_same_v || + std::is_same_v ? 1e-5 : 1e-12; } // namespace diff --git a/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigenvalues.cpp b/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigenvalues.cpp index 85ed270a..545cf34b 100644 --- a/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigenvalues.cpp +++ b/tests/cxx/unit_tests/tensorwrapper/generate/generate_eigenvalues.cpp @@ -35,7 +35,8 @@ template constexpr double log_spacing_tol = std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || - std::is_same_v || std::is_same_v ? + std::is_same_v || std::is_same_v || + std::is_same_v ? 5e-3 : 1e-9; } // namespace diff --git a/tests/cxx/unit_tests/tensorwrapper/generate/random_orthogonal_matrix.cpp b/tests/cxx/unit_tests/tensorwrapper/generate/random_orthogonal_matrix.cpp index 4bb2e46f..4c7560a2 100644 --- a/tests/cxx/unit_tests/tensorwrapper/generate/random_orthogonal_matrix.cpp +++ b/tests/cxx/unit_tests/tensorwrapper/generate/random_orthogonal_matrix.cpp @@ -53,7 +53,8 @@ TEMPLATE_LIST_TEST_CASE("random_orthogonal_matrix", "", std::is_same_v || std::is_same_v || std::is_same_v || - std::is_same_v ? + std::is_same_v || + std::is_same_v ? 1e-5 : 1e-12; REQUIRE(approximately_equal(product, ident, tol)); diff --git a/tests/cxx/unit_tests/tensorwrapper/operations/approximately_equal.cpp b/tests/cxx/unit_tests/tensorwrapper/operations/approximately_equal.cpp index 0a6b4d2e..0f7aa7ba 100644 --- a/tests/cxx/unit_tests/tensorwrapper/operations/approximately_equal.cpp +++ b/tests/cxx/unit_tests/tensorwrapper/operations/approximately_equal.cpp @@ -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) { + // Triplex bounds are intervals, so the same applies to them. + if constexpr(!types::is_interval_v && + !types::is_triplex_v) { pscalar2->set_elem({}, TestType{42.0} + value); pvector2->set_elem({0}, TestType{1.23} + value); Tensor scalar2(s0, std::move(pscalar2)); diff --git a/tests/cxx/unit_tests/tensorwrapper/shape/smooth_view.cpp b/tests/cxx/unit_tests/tensorwrapper/shape/smooth_view.cpp index 147e1769..365e430e 100644 --- a/tests/cxx/unit_tests/tensorwrapper/shape/smooth_view.cpp +++ b/tests/cxx/unit_tests/tensorwrapper/shape/smooth_view.cpp @@ -15,6 +15,7 @@ */ #include "../testing/testing.hpp" +#include #include #include #include @@ -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::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); diff --git a/tests/cxx/unit_tests/tensorwrapper/types/floating_point.cpp b/tests/cxx/unit_tests/tensorwrapper/types/floating_point.cpp new file mode 100644 index 00000000..cf85fa97 --- /dev/null +++ b/tests/cxx/unit_tests/tensorwrapper/types/floating_point.cpp @@ -0,0 +1,86 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../testing/testing.hpp" +#include +#include + +using namespace tensorwrapper; + +TEMPLATE_LIST_TEST_CASE("construct_uq_type(nominal, minus, plus)", "", + types::floating_point_types) { + using T = TestType; + + // nominal = 1, 0.25 below and 0.5 above, i.e., [0.75, 1.5] + const double nominal = 1.0, minus = 0.25, plus = 0.5; + const auto value = types::construct_uq_type(nominal, minus, plus); + + if constexpr(types::is_triplex_v) { + REQUIRE(value.lower() == 0.75); + REQUIRE(value.nominal() == 1.0); + REQUIRE(value.upper() == 1.5); + } else if constexpr(types::is_uncertain_v) { + // Uncertain can't be asymmetric, so it takes the larger deviation + REQUIRE(value.mean() == 1.0); + REQUIRE(value.sd() == 0.5); + } else if constexpr(types::is_uq_type_v) { + // The interval-like types keep the exact bounds + REQUIRE(types::uq_lower(value) == 0.75); + REQUIRE(types::uq_upper(value) == 1.5); + } else { + REQUIRE(value == T(1.0)); + } + + SECTION("negative deviations throw") { + REQUIRE_THROWS_AS(types::construct_uq_type(nominal, -0.1, plus), + std::domain_error); + REQUIRE_THROWS_AS(types::construct_uq_type(nominal, minus, -0.1), + std::domain_error); + } +} + +#ifdef ENABLE_SIGMA +TEST_CASE("triplex_type") { + using types::tpdouble; + using types::tpfloat; + + STATIC_REQUIRE(types::is_triplex_v); + STATIC_REQUIRE(types::is_triplex_v); + STATIC_REQUIRE(types::is_uq_type_v); + STATIC_REQUIRE_FALSE(types::is_interval_v); + STATIC_REQUIRE_FALSE(types::is_triplex_v); + STATIC_REQUIRE_FALSE(types::is_triplex_v); + + SECTION("construct_uq_type(center, radius) is symmetric") { + auto value = types::construct_uq_type(1.0, 0.5); + REQUIRE(value.lower() == 0.5); + REQUIRE(value.nominal() == 1.0); + REQUIRE(value.upper() == 1.5); + } + + SECTION("uq_center is the nominal value, not the median") { + tpdouble value(0.0, 0.25, 2.0); + REQUIRE(types::uq_center(value) == 0.25); + REQUIRE(types::uq_lower(value) == 0.0); + REQUIRE(types::uq_upper(value) == 2.0); + } + + SECTION("registered with WTF") { + auto kind = wtf::enums::from_string("tpdouble"); + REQUIRE(wtf::enums::to_string(kind) == "tpdouble"); + } +} +#endif