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
35 changes: 34 additions & 1 deletion src/TiledArray/tensor/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,45 @@ namespace detail {
class NDArrayPrinter {
public:
NDArrayPrinter(int width = 10, int precision = 6)
: width(width), precision(precision) {}
: width(width),
precision(precision),
truncate_(0.5 * std::pow(10., -precision)) {}

private:
int width = 10;
int precision = 10;

/// truncates (=sets to zero) small floating-point numbers
class FloatTruncate {
public:
/// truncates numbers smaller than @p threshold
FloatTruncate(double threshold) noexcept : threshold_{threshold} {}

[[nodiscard]] auto operator()(std::floating_point auto val) const noexcept {
return std::abs(val) < threshold_ ? decltype(val){0} : val;
}

template <typename T>
requires detail::is_complex_v<T> &&
std::floating_point<typename T::value_type>
[[nodiscard]] auto operator()(T const& val) const noexcept {
using std::imag;
using std::real;
return T{(*this)(real(val)), (*this)(imag(val))};
}

template <typename T>
requires(!(std::floating_point<T> || detail::is_complex_v<T>))
[[nodiscard]] auto operator()(T const& val) const noexcept {
return val;
}

private:
double threshold_;
};

FloatTruncate truncate_;

// Helper function to recursively print the array
template <typename T, typename Index = Range1::index1_type,
typename Char = char, typename CharTraits = std::char_traits<Char>>
Expand Down
4 changes: 2 additions & 2 deletions src/TiledArray/tensor/print.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ void NDArrayPrinter::printArray(const T* data, const std::size_t order,

for (size_t i = 0; i < extents[level]; ++i) {
if (level == order - 1) {
auto value = truncate_(data[offset + i * strides[level]]);
// At the deepest level, print the actual values
os << std::fixed << std::setprecision(precision) << std::setw(width) << std::setfill(Char(' '))
<< data[offset + i * strides[level]];
os << std::fixed << std::setprecision(precision) << std::setw(width) << std::setfill(Char(' ')) << value;
if (i < extents[level] - 1) {
os << ", ";
}
Expand Down
Loading