5 #ifndef ROADRUNNER_MATRIX3D_H
6 #define ROADRUNNER_MATRIX3D_H
16 template<
typename IndexType,
typename DataType>
18 using Matrix3DInitializer = std::initializer_list<std::initializer_list<std::initializer_list<DataType>>>;
30 index_(std::vector<IndexType>(
numZ)),
32 for (
int i = 0; i <
numZ; i++) {
37 Matrix3D(std::initializer_list<IndexType> idx, Matrix3DInitializer data)
38 : index_(idx.begin(), idx.end()), data_(data.begin(), data.end()) {
39 if (index_.size() != data.size()) {
40 throw std::logic_error(
"The size of index != size of 3D data in Matrix3D initializer list. ");
44 void insert(IndexType idx, Matrix <DataType> mat) {
46 auto it = std::find(index_.begin(), index_.end(), idx);
47 if (it != index_.end()) {
48 auto pos = std::distance(index_.begin(), it);
52 if (!index_.empty()) {
56 if (mat.numRows() !=
numRows()) {
57 std::ostringstream err;
58 err <<
"Cannot insert a matrix with " << mat.numRows();
59 err <<
" rows into a Matrix3D that has " <<
numRows() <<
"rows";
60 throw std::invalid_argument(err.str());
62 if (mat.numCols() !=
numCols()) {
63 std::ostringstream err;
64 err <<
"Cannot insert a matrix with " << mat.numCols();
65 err <<
" rows into a Matrix3D that has " <<
numCols() <<
"rows";
66 throw std::invalid_argument(err.str());
69 index_.push_back(idx);
73 void pushBack(IndexType idx, Matrix <DataType> mat) {
75 std::ostringstream err;
76 err <<
"Number of rows and columns in mat are invalid for this Matrix3D (";
78 throw std::invalid_argument(err.str());
80 index_.push_back(idx);
90 std::ostringstream err;
91 err <<
"requested idx " << idx <<
" from a Matrix3D with " <<
numZ() <<
" elements";
92 throw std::invalid_argument(err.str());
107 std::ostringstream err;
108 err <<
"requested kth index " << k <<
" from a Matrix3D with " <<
numZ()
109 <<
" elements in the depth direction";
110 throw std::invalid_argument(err.str());
133 std::vector<DataType>
slice(
int k,
int j) {
135 std::ostringstream err;
136 err <<
"requested kth index " << k <<
" from a Matrix3D with " <<
numZ()
137 <<
" elements in the depth direction";
138 throw std::invalid_argument(err.str());
141 std::ostringstream err;
142 err <<
"requested jth index " << j <<
" from a Matrix3D with " <<
numRows()
143 <<
" elements in the height (y) direction";
144 throw std::invalid_argument(err.str());
146 auto submatrix = data_[k].getValues();
159 DataType
slice(
int k,
int j,
int i) {
161 std::ostringstream err;
162 err <<
"requested kth index " << k <<
" from a Matrix3D with " <<
numZ()
163 <<
" elements in the depth direction";
164 throw std::invalid_argument(err.str());
167 std::ostringstream err;
168 err <<
"requested jth index " << j <<
" from a Matrix3D with " <<
numRows()
169 <<
" elements in the width (x) direction";
170 throw std::invalid_argument(err.str());
173 std::ostringstream err;
174 err <<
"requested ith index " << i <<
" from a Matrix3D with " <<
numCols()
175 <<
" elements in the hight (y) direction";
176 throw std::invalid_argument(err.str());
178 std::vector<std::vector<DataType>> submatrix = data_[k].getValues();
180 return submatrix[j][i];
190 if (std::find(index_.begin(), index_.end(), idx) == index_.end()) {
191 std::ostringstream err;
192 err <<
"Index \"" << idx <<
"\" has been requested but is not present in this Matrix3D.";
193 throw std::invalid_argument(err.str());
196 int pos = std::distance(index_.begin(), std::find(index_.begin(), index_.end(), idx));
210 throw std::invalid_argument(
"k is too big");
213 throw std::invalid_argument(
"wrong dimensions");
227 return data_[0].numRows();
237 return data_[0].numCols();
247 return index_.size();
254 rowNames_ = rowNames;
255 for (
int i = 0; i <
numZ(); i++) {
256 data_[i].setRowNames(rowNames);
264 colNames_ = colNames;
265 for (
int i = 0; i <
numZ(); i++) {
266 data_[i].setColNames(colNames);
274 return slice(0).rowNames;
281 return slice(0).colNames;
292 for (
int i = 0; i <
numZ(); i++) {
293 if (index_[i] != other.index_[i]) {
297 if (data_[i] != other.data_[i]) {
309 return !(*
this == other);
320 for (
int i = 0; i <
numZ(); i++) {
321 if ((index_[i] - other.index_[i]) > tol) {
333 template<
typename IndexType_,
typename DataType_>
343 for (
int k=0; k<
numZ(); k++){
356 for (
int k=0; k<
numZ(); k++){
369 for (
int k=0; k<
numZ(); k++){
382 for (
int k=0; k<
numZ(); k++){
389 std::vector<IndexType> index_;
390 std::vector<Matrix < DataType>> data_;
391 std::vector<std::string> rowNames_{};
392 std::vector<std::string> colNames_{};
395 template<
typename IndexType_,
typename DataType_>
396 std::ostream &operator<<(std::ostream &os, Matrix3D<IndexType_, DataType_> &matrix3D) {
397 for (
int i = 0; i < matrix3D.numZ(); i++) {
398 os <<
"\t\t" << matrix3D.index_[i] << std::endl;
399 os << matrix3D[i] << std::endl;
A basic local 3D version of the Matrix class, based on initializer_list.
Definition: Matrix3D.h:17
int numCols()
get number of columns in this 3D matrix
Definition: Matrix3D.h:234
rr::Matrix< DataType > & slice(int k)
1D Matrix slicer.
Definition: Matrix3D.h:105
rr::Matrix3D< DataType, IndexType > & colSliceByName(const std::vector< std::string > &rowNames)
slice a Matrix3D by colnames.
Definition: Matrix3D.h:120
DataType slice(int k, int j, int i)
3D Matrix slicer.
Definition: Matrix3D.h:159
std::vector< DataType > slice(int k, int j)
2D Matrix slicer.
Definition: Matrix3D.h:133
const std::vector< IndexType > & getIndex() const
getter for the index data field of the Matrix3D.
Definition: Matrix3D.h:204
void deleteCol(const std::string &which)
delete the col indexed by
Definition: Matrix3D.h:380
std::vector< std::string > getColNames()
return the column names for this Matrix3D
Definition: Matrix3D.h:280
void setColNames(const std::vector< std::string > &colNames)
set col names for each of the z matrices
Definition: Matrix3D.h:263
int numRows()
get number of rows in this 3D matrix
Definition: Matrix3D.h:224
Matrix3D()=default
default construct a 3D matrix
bool operator!=(Matrix3D< IndexType, DataType > &other)
inequality operator
Definition: Matrix3D.h:308
std::vector< std::string > getRowNames()
return the row names for this Matrix3D
Definition: Matrix3D.h:273
int numZ()
get number of matrices in this 3D matrix
Definition: Matrix3D.h:244
bool almostEquals(Matrix3D< double, double > &other, double tol)
equality operator for double IndexType and DataType types only
Definition: Matrix3D.h:315
bool operator==(Matrix3D< IndexType, DataType > &other)
equality operator
Definition: Matrix3D.h:287
Matrix3D(int numRows, int numCols, int numZ)
construct an empty 3D matrix with numRows x numCols x numZ dimensions.
Definition: Matrix3D.h:29
void setRowNames(const std::vector< std::string > &rowNames)
set row names for each of the z matrices
Definition: Matrix3D.h:253
Matrix< DataType > & operator[](int idx)
Indexer to slice a Matrix3D and index value and data at idx.
Definition: Matrix3D.h:88
void deleteRow(const std::string &which)
delete the row indexed by
Definition: Matrix3D.h:354
void deleteCol(const int &which)
delete the col indexed by
Definition: Matrix3D.h:367
Matrix< DataType > & getItem(IndexType idx)
slicing operator that uses the user provided index.
Definition: Matrix3D.h:188
void deleteRow(const int &which)
delete the row indexed by
Definition: Matrix3D.h:341
A basic local matrix class, based on the libstruct version.
Definition: Matrix.h:18
void deleteCol(int which)
delete col
Definition: Matrix.h:364
void deleteRow(int which)
delete row
Definition: Matrix.h:314