1 /* 2 * Copyright © 2004-2011 Ondra Kamenik 3 * Copyright © 2019 Dynare Team 4 * 5 * This file is part of Dynare. 6 * 7 * Dynare is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * Dynare is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with Dynare. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 // Matrix interface. 22 23 /* Here we make an interface to 2-dimensional matrix defined in the Sylvester 24 module. That abstraction provides an interface to BLAS. The main purpose of 25 this file is to only make its subclass in order to keep the tensor library 26 and Sylvester module independent. So here is mainly renaming of methods. 27 28 Similarly as in the Sylvester module we declare two classes TwoDMatrix and 29 ConstTwoDMatrix. The only purpose of the latter is to allow submatrix 30 construction from const reference arguments. */ 31 32 #ifndef TWOD_MATRIX_H 33 #define TWOD_MATRIX_H 34 35 #include "GeneralMatrix.hh" 36 37 #include <matio.h> 38 39 #include <string> 40 #include <utility> 41 42 class TwoDMatrix; 43 44 class ConstTwoDMatrix : public ConstGeneralMatrix 45 { 46 public: ConstTwoDMatrix(int m,int n,ConstVector d)47 ConstTwoDMatrix(int m, int n, ConstVector d) 48 : ConstGeneralMatrix(std::move(d), m, n) 49 { 50 } 51 ConstTwoDMatrix(const ConstTwoDMatrix &m) = default; 52 ConstTwoDMatrix(ConstTwoDMatrix &&m) = default; 53 54 // Implicit conversion from TwoDMatrix is ok, since it's cheap 55 ConstTwoDMatrix(const TwoDMatrix &m); 56 57 // Constructors creating a submatrix of consecutive columns 58 ConstTwoDMatrix(const TwoDMatrix &m, int first_col, int num); 59 ConstTwoDMatrix(const ConstTwoDMatrix &m, int first_col, int num); 60 61 // Constructors creating a submatrix of consecutive rows 62 ConstTwoDMatrix(int first_row, int num, const TwoDMatrix &m); 63 ConstTwoDMatrix(int first_row, int num, const ConstTwoDMatrix &m); 64 ConstTwoDMatrix(const ConstTwoDMatrix & m,int first_row,int first_col,int rows,int cols)65 ConstTwoDMatrix(const ConstTwoDMatrix &m, int first_row, int first_col, int rows, int cols) 66 : ConstGeneralMatrix(m, first_row, first_col, rows, cols) 67 { 68 } 69 #if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) ConstTwoDMatrix(const mxArray * p)70 explicit ConstTwoDMatrix(const mxArray *p) : ConstGeneralMatrix(p) 71 { 72 } 73 #endif 74 ~ConstTwoDMatrix() override = default; 75 76 ConstTwoDMatrix &operator=(const ConstTwoDMatrix &v) = delete; 77 ConstTwoDMatrix &operator=(ConstTwoDMatrix &&v) = delete; 78 79 void writeMat(mat_t *fd, const std::string &vname) const; 80 }; 81 82 class TwoDMatrix : public GeneralMatrix 83 { 84 public: 85 TwoDMatrix(const TwoDMatrix &m) = default; 86 TwoDMatrix(TwoDMatrix &&m) = default; TwoDMatrix(int r,int c)87 TwoDMatrix(int r, int c) 88 : GeneralMatrix(r, c) 89 { 90 } TwoDMatrix(int r,int c,Vector d)91 TwoDMatrix(int r, int c, Vector d) 92 : GeneralMatrix(std::move(d), r, c) 93 { 94 } TwoDMatrix(const GeneralMatrix & m)95 TwoDMatrix(const GeneralMatrix &m) 96 : GeneralMatrix(m) 97 { 98 } 99 // We don't want implict conversion from ConstGeneralMatrix, since it's expensive TwoDMatrix(const ConstGeneralMatrix & m)100 explicit TwoDMatrix(const ConstGeneralMatrix &m) 101 : GeneralMatrix(m) 102 { 103 } 104 template<class T> TwoDMatrix(const TransposedMatrix<T> & m)105 explicit TwoDMatrix(const TransposedMatrix<T> &m) 106 : GeneralMatrix(m) 107 { 108 } 109 // Select only some columns (with data copy) TwoDMatrix(const TwoDMatrix & m,int first_col,int num)110 TwoDMatrix(const TwoDMatrix &m, int first_col, int num) 111 : GeneralMatrix(m, 0, first_col, m.nrows(), num) 112 { 113 } 114 // Select only some columns (with data sharing) TwoDMatrix(TwoDMatrix & m,int first_col,int num)115 TwoDMatrix(TwoDMatrix &m, int first_col, int num) 116 : GeneralMatrix(m, 0, first_col, m.nrows(), num) 117 { 118 } 119 // Select only some rows (with data copy) TwoDMatrix(int first_row,int num,const TwoDMatrix & m)120 TwoDMatrix(int first_row, int num, const TwoDMatrix &m) 121 : GeneralMatrix(m, first_row, 0, num, m.ncols()) 122 { 123 } 124 // Select only some rows (with data sharing) TwoDMatrix(int first_row,int num,TwoDMatrix & m)125 TwoDMatrix(int first_row, int num, TwoDMatrix &m) 126 : GeneralMatrix(m, first_row, 0, num, m.ncols()) 127 { 128 } 129 // Select a submatrix (with data sharing) TwoDMatrix(TwoDMatrix & m,int first_row,int first_col,int rows,int cols)130 TwoDMatrix(TwoDMatrix &m, int first_row, int first_col, int rows, int cols) 131 : GeneralMatrix(m, first_row, first_col, rows, cols) 132 { 133 } 134 // Select a submatrix (with data copy) TwoDMatrix(const TwoDMatrix & m,int first_row,int first_col,int rows,int cols)135 TwoDMatrix(const TwoDMatrix &m, int first_row, int first_col, int rows, int cols) 136 : GeneralMatrix(m, first_row, first_col, rows, cols) 137 { 138 } 139 140 #if defined(MATLAB_MEX_FILE) || defined(OCTAVE_MEX_FILE) TwoDMatrix(mxArray * p)141 explicit TwoDMatrix(mxArray *p) : GeneralMatrix(p) 142 { 143 } 144 #endif 145 ~TwoDMatrix() override = default; 146 147 TwoDMatrix &operator=(const TwoDMatrix &m) = default; 148 TwoDMatrix &operator=(TwoDMatrix &&m) = default; 149 TwoDMatrix &operator=(const ConstTwoDMatrix &m); 150 151 // TwoDMatrix row methods declarations 152 void copyRow(int from, int to); 153 void copyRow(const ConstTwoDMatrix &m, int from, int to); 154 void copyRow(const TwoDMatrix & m,int from,int to)155 copyRow(const TwoDMatrix &m, int from, int to) 156 { 157 copyRow(ConstTwoDMatrix(m), from, to); 158 } 159 void addRow(const ConstTwoDMatrix & m,int from,int to)160 addRow(const ConstTwoDMatrix &m, int from, int to) 161 { 162 addRow(1.0, m, from, to); 163 } 164 void addRow(const TwoDMatrix & m,int from,int to)165 addRow(const TwoDMatrix &m, int from, int to) 166 { 167 addRow(1.0, ConstTwoDMatrix(m), from, to); 168 } 169 void addRow(double d, const ConstTwoDMatrix &m, int from, int to); 170 void addRow(double d,const TwoDMatrix & m,int from,int to)171 addRow(double d, const TwoDMatrix &m, int from, int to) 172 { 173 addRow(d, ConstTwoDMatrix(m), from, to); 174 } 175 176 // TwoDMatrix column methods declarations 177 void copyColumn(int from, int to); 178 void copyColumn(const ConstTwoDMatrix &m, int from, int to); 179 void copyColumn(const TwoDMatrix & m,int from,int to)180 copyColumn(const TwoDMatrix &m, int from, int to) 181 { 182 copyColumn(ConstTwoDMatrix(m), from, to); 183 } 184 void addColumn(const ConstTwoDMatrix & m,int from,int to)185 addColumn(const ConstTwoDMatrix &m, int from, int to) 186 { 187 addColumn(1.0, ConstTwoDMatrix(m), from, to); 188 } 189 void addColumn(const TwoDMatrix & m,int from,int to)190 addColumn(const TwoDMatrix &m, int from, int to) 191 { 192 addColumn(1.0, ConstTwoDMatrix(m), from, to); 193 } 194 void addColumn(double d, const ConstTwoDMatrix &m, int from, int to); 195 void addColumn(double d,const TwoDMatrix & m,int from,int to)196 addColumn(double d, const TwoDMatrix &m, int from, int to) 197 { 198 addColumn(d, ConstTwoDMatrix(m), from, to); 199 } 200 201 // Saves the matrix to a text file 202 void save(const std::string &fname) const; 203 204 void writeMat(mat_t * fd,const std::string & vname) const205 writeMat(mat_t *fd, const std::string &vname) const 206 { 207 ConstTwoDMatrix(*this).writeMat(fd, vname); 208 } 209 }; 210 211 #endif 212