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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| namespace lnpbqc{ template<typename DataType> struct matrixAtom{ int row,col; DataType ele; bool operator==(const matrixAtom<DataType>&others){ return row==others.row&&col==others.col&&ele==others.ele; } }; template<typename DataType> class matrix{ private: vector<matrixAtom<DataType> > _data; int _matrixRowNum; int _matrixColNum; public: int row()const{return _matrixRowNum;} int col()const{return _matrixColNum;} void row(int r){_matrixRowNum=r;} void col(int c){_matrixColNum=c;} vector<int> shape()const{ vector<int> v; v.push_back(row()); v.push_back(col()); return v; } vector<matrixAtom<DataType> > data()const{return _data;} matrix() : _matrixRowNum(0), _matrixColNum(0) { std::cout<<"需要设置row和col"<<std::endl; } matrix(int rows, int cols) : _matrixRowNum(rows), _matrixColNum(cols) {}
matrix(const vector<vector<DataType> > &inArray){ _matrixRowNum = inArray.size(); _matrixColNum = _matrixRowNum > 0 ? inArray[0].size() : 0; for (int i = 0; i < _matrixRowNum; ++i) { for (int j = 0; j < _matrixColNum; ++j) { if (inArray[i][j] != 0) { _data.push_back({i, j, inArray[i][j]}); } } } } matrix(const matrix<DataType>& other) { _matrixRowNum = other.row(); _matrixColNum = other.col(); _data = other.data(); } matrix<DataType>& operator=(const matrix<DataType>& other) { if (this != &other) { _matrixRowNum = other.row(); _matrixColNum = other.col(); _data = other.data(); } return *this; } ~matrix(){} DataType get(int row, int col) const { for (const auto& atom : _data) { if (atom.row == row && atom.col == col) return atom.ele; } return DataType(0); } void set(int row, int col, DataType value) { if(row>=_matrixRowNum||col>=_matrixColNum)throw "超出范围"; bool found = false; for (auto& atom : _data) { if (atom.row == row && atom.col == col) { found = true; if (value == 0) { _data.erase(std::find(_data.begin(), _data.end(), atom)); } else { atom.ele = value; } return; } } if (!found&&value != 0) { _data.push_back({row, col, value}); } } matrix<DataType> TransposeMatrix()const{ matrix<DataType> result(_matrixColNum, _matrixRowNum); for (const auto& atom : _data) { result.set(atom.col, atom.row, atom.ele); } return result; } void FastTransposeMatrix(){ } matrix<DataType> AddMatrix(const matrix<DataType> &otherMatrix){ if (_matrixRowNum != otherMatrix.row() || _matrixColNum != otherMatrix.col()){ throw "矩阵形状不匹配"; } matrix<DataType> result(_matrixRowNum, _matrixColNum); for (const auto& atom : _data) { result.set(atom.row, atom.col, atom.ele); } for (const auto& atom : otherMatrix.data()) { result.set(atom.row, atom.col, result.get(atom.row, atom.col) + atom.ele); } return result; } matrix<DataType> operator+(const matrix<DataType>& otherMatrix){ return this->AddMatrix(otherMatrix); } matrix<DataType> SubMatrix(const matrix<DataType> &otherMatrix){ if (_matrixRowNum != otherMatrix.row() || _matrixColNum != otherMatrix.col()){ throw "矩阵形状不匹配"; } matrix<DataType> result(_matrixRowNum, _matrixColNum); for (const auto& atom : _data) { result.set(atom.row, atom.col, atom.ele); } for (const auto& atom : otherMatrix.data()) { result.set(atom.row, atom.col, result.get(atom.row, atom.col) - atom.ele); } return result; } matrix<DataType> operator-(const matrix<DataType>& otherMatrix){ return this->SubMatrix(otherMatrix); } matrix<DataType> MultiMatrix(const matrix<DataType> &otherMatrix){ if (_matrixColNum != otherMatrix.row()){ throw "矩阵形状不匹配"; } matrix<DataType> result(_matrixRowNum, otherMatrix.col()); for (const auto& atomA : _data) { for (const auto& atomB : otherMatrix.data()) { if (atomA.col == atomB.row) { DataType newValue = result.get(atomA.row, atomB.col) + atomA.ele * atomB.ele; result.set(atomA.row, atomB.col, newValue); } } } return result; } matrix<DataType> operator*(const matrix<DataType> & other){ return this->MultiMatrix(other); } matrix<DataType> inverse() const { return matrix<DataType>(); } vector<vector<DataType> > TransformTo2DArray(){ vector<vector<DataType>> result(_matrixRowNum, vector<DataType>(_matrixColNum, 0)); for (const auto& atom : _data) { result[atom.row][atom.col] = atom.ele; } return result; } void PrintMatrix(){ std::cout <<"Address::"<<this<< "\nMatrix Rows: " << _matrixRowNum << ", Columns: " << _matrixColNum << std::endl; for (int i = 0; i < _matrixRowNum; i++) { for (int j = 0; j < _matrixColNum; j++) { std::cout << get(i, j) << "\t"; } std::cout << std::endl; } } }; }
|