-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patharma_io.hpp
69 lines (59 loc) · 1.75 KB
/
arma_io.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2018-2023, University of Bremen, M. Farzalipour Tabriz
// Copyrights licensed under the 2-Clause BSD License.
// See the accompanying LICENSE.txt file for terms.
#pragma once
#include <armadillo>
#include <iomanip>
#include <iostream>
// single-line output for vec
template <typename T>
std::ostream &operator<<(std::ostream &o, const arma::Row<T> &vec) {
for (arma::uword elem = 0; elem < vec.n_elem; ++elem) {
o << vec(elem);
if (elem != vec.n_elem - 1) {
o << " ";
}
}
return o;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const arma::subview<T> &vec) {
for (arma::uword elem = 0; elem < vec.n_elem; ++elem) {
o << vec(elem);
if (elem != vec.n_elem - 1) {
o << " ";
}
}
return o;
}
// single-line output for mat
template <typename T>
std::ostream &operator<<(std::ostream &o, const arma::Mat<T> &mat) {
mat.each_row([&o](const arma::Row<T> &row) { o << row << "; "; });
return o;
}
template <typename T>
std::istream &operator>>(std::istream &o, arma::Row<T> &vec) {
vec.for_each([&o](T &elem) { o >> elem; });
return o;
}
template <typename T>
std::istream &operator>>(std::istream &o, arma::subview_row<T> vec) {
vec.for_each([&o](T &elem) { o >> elem; });
return o;
}
template <typename T>
std::istream &operator>>(std::istream &o, arma::Cube<T> &c) {
c.for_each([&o](T &elem) { o >> elem; });
return o;
}
template <typename T>
std::istream &operator>>(std::istream &o, arma::Mat<T> &m) {
m.for_each([&o](T &elem) { o >> elem; });
return o;
}
template <typename T> std::string to_string(T input) {
std::ostringstream output;
output << std::setprecision(15) << input;
return output.str();
}