Stratax
Scientific computing containers and operations
Loading...
Searching...
No Matches
Print.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iterator>
4#include <ostream>
5#include <string>
6
11
12namespace stratax::container {
13
14namespace detail {
15
25template<typename T>
27 std::ostream& os,
28 const Tensor<T>& tensor,
29 std::size_t dim,
30 std::size_t offset,
31 std::size_t depth)
32{
33 const auto& shape = tensor.shape();
34 const auto& strides = tensor.strides();
35
36 os << "[";
37
38 if (dim == shape.rank() - 1)
39 {
40 for (std::size_t i = 0; i < shape(dim); ++i)
41 {
42 os << tensor(offset + i * strides(dim));
43
44 if (i + 1 != shape(dim))
45 os << ", ";
46 }
47 }
48 else
49 {
50 os << '\n';
51
52 for (std::size_t i = 0; i < shape(dim); ++i)
53 {
54 os << std::string((depth + 1) * 4, ' ');
56 os,
57 tensor,
58 dim + 1,
59 offset + i * strides(dim),
60 depth + 1);
61
62 if (i + 1 != shape(dim))
63 {
64 os << ",\n";
65 }
66 }
67
68 os << '\n';
69 os << std::string(depth * 4, ' ');
70 }
71
72 os << "]";
73}
74
75}
76
85template<typename T>
86std::ostream& operator<<(std::ostream& os, const Vector<T>& vector)
87{
88 os << "[";
89
90 const auto end = vector.end();
91 for (auto it = vector.begin(); it != end; ++it)
92 {
93 os << *it;
94
95 if (std::next(it) != end)
96 {
97 os << ", ";
98 }
99 }
100
101 os << "]";
102
103 return os;
104}
105
114template<typename T>
115std::ostream& operator<<(std::ostream& os, const Matrix<T>& matrix)
116{
117 os << "[\n";
118
119 for (std::size_t i = 0; i < matrix.rows(); ++i)
120 {
121 os << " [";
122
123 for (std::size_t j = 0; j < matrix.cols(); ++j)
124 {
125 os << matrix(i, j);
126
127 if (j + 1 != matrix.cols())
128 os << ", ";
129 }
130
131 os << "]";
132
133 if (i + 1 != matrix.rows())
134 os << '\n';
135 }
136
137 os << '\n' << "]";
138
139 return os;
140}
141
142template<typename T>
154std::ostream& operator<<(std::ostream& os, const Tensor<T>& tensor)
155{
156 if (tensor.shape().elements() == 0)
157 {
158 os << "[]";
159 return os;
160 }
161
162 detail::print_tensor_recursive(os, tensor, 0, 0, 0);
163
164 return os;
165}
166
167}
std::size_t offset(const stratax::core::Shape &shape, const stratax::core::Strides &strides, const IndexContainer &index)
Computes a flat storage offset from a shape, strides, and index container.
Definition Indexing.hpp:23
std::size_t rows() const noexcept
Returns the number of rows.
Definition Matrix.hpp:181
std::size_t cols() const noexcept
Returns the number of columns.
Definition Matrix.hpp:191
const core::Shape & shape() const noexcept
Returns the tensor shape.
Definition Tensor.hpp:144
const core::Strides & strides() const noexcept
Returns the tensor strides.
Definition Tensor.hpp:154
T * end() noexcept
Returns an iterator one past the last element.
Definition Vector.hpp:361
T * begin() noexcept
Returns an iterator to the first element.
Definition Vector.hpp:331
std::size_t elements() const
Returns the total number of elements described by the shape.
Definition Shape.hpp:138
void print_tensor_recursive(std::ostream &os, const Tensor< T > &tensor, std::size_t dim, std::size_t offset, std::size_t depth)
Recursively prints a tensor using nested bracket notation.
Definition Print.hpp:26
std::ostream & operator<<(std::ostream &os, const Vector< T > &vector)
Writes a vector in compact bracketed list form.
Definition Print.hpp:86