mlpack
load_vec_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_DATA_LOAD_VEC_IMPL_HPP
14 #define MLPACK_CORE_DATA_LOAD_VEC_IMPL_HPP
15 
16 // In case it hasn't already been included.
17 #include "load.hpp"
18 
19 namespace mlpack {
20 namespace data {
21 
22 // Load column vector.
23 template<typename eT>
24 bool Load(const std::string& filename,
25  arma::Col<eT>& vec,
26  const bool fatal)
27 {
28  // First load into auxiliary matrix.
29  arma::Mat<eT> tmp;
30  bool success = Load(filename, tmp, fatal, false);
31  if (!success)
32  {
33  vec.clear();
34  return false;
35  }
36 
37  // Now check the size to see that it is a vector, and return a vector.
38  if (tmp.n_cols > 1)
39  {
40  if (tmp.n_rows > 1)
41  {
42  // Problem: invalid size!
43  if (fatal)
44  {
45  Log::Fatal << "Matrix in file '" << filename << "' is not a vector, but"
46  << " instead has size " << tmp.n_rows << "x" << tmp.n_cols << "!"
47  << std::endl;
48  }
49  else
50  {
51  Log::Warn << "Matrix in file '" << filename << "' is not a vector, but "
52  << "instead has size " << tmp.n_rows << "x" << tmp.n_cols << "!"
53  << std::endl;
54  }
55 
56  vec.clear();
57  return false;
58  }
59  else
60  {
66  arma::access::rw(tmp.n_rows) = tmp.n_cols;
67  arma::access::rw(tmp.n_cols) = 1;
68 
73  *((arma::Mat<eT>*) &vec) = std::move(tmp);
74  return true;
75  }
76  }
77  else
78  {
79  // It's loaded as a column vector. We can call the move constructor
80  // directly.
81  *((arma::Mat<eT>*) &vec) = std::move(tmp);
82  return true;
83  }
84 }
85 
86 // Load row vector.
87 template<typename eT>
88 bool Load(const std::string& filename,
89  arma::Row<eT>& rowvec,
90  const bool fatal)
91 {
92  arma::Mat<eT> tmp;
93  bool success = Load(filename, tmp, fatal, false);
94  if (!success)
95  {
96  rowvec.clear();
97  return false;
98  }
99 
100  if (tmp.n_rows > 1)
101  {
102  if (tmp.n_cols > 1)
103  {
104  // Problem: invalid size!
105  if (fatal)
106  {
107  Log::Fatal << "Matrix in file '" << filename << "' is not a vector, but"
108  << " instead has size " << tmp.n_rows << "x" << tmp.n_cols << "!"
109  << std::endl;
110  }
111  else
112  {
113  Log::Warn << "Matrix in file '" << filename << "' is not a vector, but "
114  << "instead has size " << tmp.n_rows << "x" << tmp.n_cols << "!"
115  << std::endl;
116  }
117 
118  rowvec.clear();
119  return false;
120  }
121  else
122  {
128  arma::access::rw(tmp.n_cols) = tmp.n_rows;
129  arma::access::rw(tmp.n_rows) = 1;
130 
135  *((arma::Mat<eT>*) &rowvec) = std::move(tmp);
136  return true;
137  }
138  }
139  else
140  {
141  // It's loaded as a row vector. We can call the move constructor directly.
142  *((arma::Mat<eT>*) &rowvec) = std::move(tmp);
143  return true;
144  }
145 }
146 
147 } // namespace data
148 } // namespace mlpack
149 
150 #endif
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
bool Load(const std::string &filename, arma::Mat< eT > &matrix, const bool fatal=false, const bool transpose=true, const arma::file_type inputLoadType=arma::auto_detect)
Loads a matrix from file, guessing the filetype from the extension.
Definition: load_impl.hpp:89