Not Enough Math
Lightweight zero-dependency C++ math library
Loading...
Searching...
No Matches
mat_io.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <sstream>
4
5#include "mat.hpp"
6
7namespace nem
8{
9 static constexpr const char* MATRIX_COLUMN_SEPARATOR = ", ";
10 static constexpr const char* MATRIX_ROW_SEPARATOR = "\n";
11 static constexpr const char* MATRIX_L_BRACE = "[";
12 static constexpr const char* MATRIX_R_BRACE = "]";
13
14 template<typename T, size_t C, size_t R>
15 std::string tostr(const mat<T, C, R>& matrix)
16 {
17 std::stringstream ss;
18 for (size_t r = 0; r < R; ++r)
19 {
20 ss << MATRIX_L_BRACE;
21 for (size_t c = 0; c < C; ++c)
22 {
23 ss << matrix.at(r, c) << (c < C - 1 ? MATRIX_COLUMN_SEPARATOR : "");
24 }
25 ss << MATRIX_R_BRACE;
27 }
28 return ss.str();
29 }
30
31 template<typename T, size_t C, size_t R>
32 std::ostream& operator<<(std::ostream& os, const mat<T, C, R>& matrix)
33 {
34 for (size_t r = 0; r < R; ++r)
35 {
36 os << MATRIX_L_BRACE;
37 for (size_t c = 0; c < C; ++c)
38 {
39 os << matrix.at(r, c) << (c < C - 1 ? MATRIX_COLUMN_SEPARATOR : "");
40 }
41 os << MATRIX_R_BRACE;
43 }
44 return os;
45 }
46
47 template<typename T, size_t C, size_t R>
48 std::istream& operator>>(std::istream& is, mat<T, C, R>& matrix)
49 {
50 for (size_t r = 0; r < R; ++r)
51 {
52 for (size_t c = 0; c < C; ++c)
53 {
54 is >> matrix.at(r, c);
55 }
56 }
57 return is;
58 }
59}
Definition config.hpp:16
static constexpr const char * MATRIX_R_BRACE
Definition mat_io.hpp:12
static constexpr const char * MATRIX_L_BRACE
Definition mat_io.hpp:11
std::istream & operator>>(std::istream &is, mat< T, C, R > &matrix)
Definition mat_io.hpp:48
static constexpr const char * MATRIX_ROW_SEPARATOR
Definition mat_io.hpp:10
std::ostream & operator<<(std::ostream &os, const mat< T, C, R > &matrix)
Definition mat_io.hpp:32
std::string tostr(const mat< T, C, R > &matrix)
Definition mat_io.hpp:15
static constexpr const char * MATRIX_COLUMN_SEPARATOR
Definition mat_io.hpp:9
constexpr ITEM_TYPE & at(size_t row, size_t col)
Definition mat.hpp:46