Skip to content

Commit ef25f0f

Browse files
committed
NDArrayPrinter::FloatTruncate: rename + refactor
1 parent 6c1a674 commit ef25f0f

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

src/TiledArray/tensor/print.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,45 @@ namespace detail {
4040
class NDArrayPrinter {
4141
public:
4242
NDArrayPrinter(int width = 10, int precision = 6)
43-
: width(width), precision(precision) {}
43+
: width(width),
44+
precision(precision),
45+
truncate_(0.5 * std::pow(10., -precision)) {}
4446

4547
private:
4648
int width = 10;
4749
int precision = 10;
4850

51+
/// truncates (=sets to zero) small floating-point numbers
52+
class FloatTruncate {
53+
public:
54+
/// truncates numbers smaller than @p threshold
55+
FloatTruncate(double threshold) noexcept : threshold_{threshold} {}
56+
57+
[[nodiscard]] auto operator()(std::floating_point auto val) const noexcept {
58+
return std::abs(val) < threshold_ ? decltype(val){0} : val;
59+
}
60+
61+
template <typename T>
62+
requires detail::is_complex_v<T> &&
63+
std::floating_point<typename T::value_type>
64+
[[nodiscard]] auto operator()(T const& val) const noexcept {
65+
using std::imag;
66+
using std::real;
67+
return T{(*this)(real(val)), (*this)(imag(val))};
68+
}
69+
70+
template <typename T>
71+
requires(!(std::floating_point<T> || detail::is_complex_v<T>))
72+
[[nodiscard]] auto operator()(T const& val) const noexcept {
73+
return val;
74+
}
75+
76+
private:
77+
double threshold_;
78+
};
79+
80+
FloatTruncate truncate_;
81+
4982
// Helper function to recursively print the array
5083
template <typename T, typename Index = Range1::index1_type,
5184
typename Char = char, typename CharTraits = std::char_traits<Char>>

src/TiledArray/tensor/print.ipp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,6 @@ namespace TiledArray {
3333

3434
namespace detail {
3535

36-
class NumberRounder {
37-
public:
38-
NumberRounder(int precision) noexcept : precision_{precision} {}
39-
40-
[[nodiscard]] auto round(std::floating_point auto val) const noexcept {
41-
return std::abs(val) <
42-
(0.5 * std::pow(static_cast<decltype(val)>(10), -precision_))
43-
? 0
44-
: val;
45-
}
46-
47-
template <typename T>
48-
requires detail::is_complex_v<T> && std::floating_point<typename T::value_type>
49-
[[nodiscard]] auto round(T const& val) const noexcept {
50-
using std::imag;
51-
using std::real;
52-
return T{round(real(val)), round(imag(val))};
53-
}
54-
55-
template <typename T>
56-
requires (!(std::floating_point<T> || detail::is_complex_v<T>))
57-
[[nodiscard]] auto round(T const& val) const noexcept { return val; }
58-
59-
private:
60-
int precision_;
61-
};
62-
6336
// Class to print n-dimensional arrays in NumPy style but with curly braces
6437
template <typename T, typename Index, typename Char, typename CharTraits>
6538
void NDArrayPrinter::printArray(const T* data, const std::size_t order,
@@ -75,11 +48,9 @@ void NDArrayPrinter::printArray(const T* data, const std::size_t order,
7548
os << std::basic_string<Char, CharTraits>(extra_indentation, ' ');
7649
os << "{";
7750

78-
auto rounder = NumberRounder{precision};
79-
8051
for (size_t i = 0; i < extents[level]; ++i) {
8152
if (level == order - 1) {
82-
auto value = rounder.round(data[offset + i * strides[level]]);
53+
auto value = truncate_(data[offset + i * strides[level]]);
8354
// At the deepest level, print the actual values
8455
os << std::fixed << std::setprecision(precision) << std::setw(width) << std::setfill(Char(' ')) << value;
8556
if (i < extents[level] - 1) {

0 commit comments

Comments
 (0)