Stratax
Scientific computing containers and operations
Toggle main menu visibility
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
7
#include <
stratax/containers/Matrix.hpp
>
8
#include <
stratax/containers/Tensor.hpp
>
9
#include <
stratax/containers/Vector.hpp
>
10
#include <
stratax/core/Shape.hpp
>
11
12
namespace
stratax::container
{
13
14
namespace
detail
{
15
25
template
<
typename
T>
26
void
print_tensor_recursive
(
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,
' '
);
55
print_tensor_recursive
(
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
85
template
<
typename
T>
86
std::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
114
template
<
typename
T>
115
std::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
142
template
<
typename
T>
154
std::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
}
offset
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
Matrix.hpp
Shape.hpp
Tensor.hpp
Vector.hpp
stratax::container::Matrix
Definition
Matrix.hpp:29
stratax::container::Matrix::rows
std::size_t rows() const noexcept
Returns the number of rows.
Definition
Matrix.hpp:181
stratax::container::Matrix::cols
std::size_t cols() const noexcept
Returns the number of columns.
Definition
Matrix.hpp:191
stratax::container::Tensor
Definition
Tensor.hpp:30
stratax::container::Tensor::shape
const core::Shape & shape() const noexcept
Returns the tensor shape.
Definition
Tensor.hpp:144
stratax::container::Tensor::strides
const core::Strides & strides() const noexcept
Returns the tensor strides.
Definition
Tensor.hpp:154
stratax::container::Vector
Definition
Vector.hpp:27
stratax::container::Vector::end
T * end() noexcept
Returns an iterator one past the last element.
Definition
Vector.hpp:361
stratax::container::Vector::begin
T * begin() noexcept
Returns an iterator to the first element.
Definition
Vector.hpp:331
stratax::core::Shape::elements
std::size_t elements() const
Returns the total number of elements described by the shape.
Definition
Shape.hpp:138
stratax::container::detail
Definition
Print.hpp:14
stratax::container::detail::print_tensor_recursive
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
stratax::container
Definition
Matrix.hpp:15
stratax::container::operator<<
std::ostream & operator<<(std::ostream &os, const Vector< T > &vector)
Writes a vector in compact bracketed list form.
Definition
Print.hpp:86
include
stratax
io
Print.hpp
Generated by
1.17.0