5 #ifndef ROADRUNNER_MATRIX_H
6 #define ROADRUNNER_MATRIX_H
8 #include "rr-libstruct/lsMatrix.h"
18 class Matrix :
public ls::Matrix<T> {
20 using ls::Matrix<T>::Matrix;
21 using ls::Matrix<T>::numCols;
22 using ls::Matrix<T>::numRows;
23 using ls::Matrix<T>::operator();
24 using ls::Matrix<T>::operator[];
25 using ls::Matrix<T>::resize;
26 using ls::Matrix<T>::getArray;
27 using ls::Matrix<T>::getValues;
28 using ls::Matrix<T>::setColNames;
29 using ls::Matrix<T>::setRowNames;
30 using ls::Matrix<T>::rowNames;
31 using ls::Matrix<T>::colNames;
32 using ls::Matrix<T>::swapCols;
33 using ls::Matrix<T>::swapRows;
39 explicit Matrix<T>(ls::Matrix<T> &matrix);
45 explicit Matrix<T>(ls::Matrix<T> *matrix);
108 void deleteRow(
const std::string& which);
130 void deleteCol(
const std::string& which);
139 resize(matrix.numRows(), matrix.numCols());
140 for (
int i = 0; i < numRows(); i++) {
141 for (
int j = 0; j < numCols(); j++) {
142 operator()(i, j) = matrix(i, j);
146 colNames = std::vector<std::string>(matrix.getColNames().begin(), matrix.getColNames().end());
147 rowNames = std::vector<std::string>(matrix.getRowNames().begin(), matrix.getRowNames().end());
162 if (empty() && other.
empty()) {
165 if (numRows() != other.numRows()) {
168 if (numCols() != other.numCols()) {
173 for (
int i = 0; i < numRows(); i++) {
174 for (
int j = 0; j < numCols(); j++) {
175 if (this->
operator()(i, j) != other(i, j)) {
189 return !(*
this == other);
200 if (empty() && other.
empty()) {
203 if (numRows() != other.numRows()) {
206 if (numCols() != other.numCols()) {
210 for (
int i = 0; i < numRows(); i++) {
211 for (
int j = 0; j < numCols(); j++) {
212 if ((this->
operator()(i, j) - other(i, j)) > tolerance) {
223 ls::Matrix<T> mat = ls::mult(*
this, matrix);
233 if (getArray() ==
nullptr)
244 if (rowNames.empty()) {
254 std::vector<std::string> rowLabels = rowNames;
258 bool swapped =
false;
259 for (
int i = 0; i < rowLabels.size() - 1; i++) {
260 for (
int j = 0; j < rowLabels.size() - 1; j++) {
261 if (rowLabels[j] > rowLabels[j + 1]) {
263 std::swap(rowLabels[j], rowLabels[j + 1]);
272 setRowNames(rowLabels);
281 if (colNames.empty()) {
291 std::vector<std::string> colLabels = colNames;
295 bool swapped =
false;
296 for (
int i = 0; i < colLabels.size() - 1; i++) {
297 for (
int j = 0; j < colLabels.size() - 1; j++) {
298 if (colLabels[j] > colLabels[j + 1]) {
300 std::swap(colLabels[j], colLabels[j + 1]);
309 setColNames(colLabels);
315 if (which > numRows() - 1) {
316 throw std::invalid_argument(
"Cannot delete row " + std::to_string(which));
319 auto *arr = getArray();
321 int originalSize = this->_Rows * this->_Cols;
325 rowNames.erase(rowNames.begin() + which);
327 if (which == this->_Rows) {
341 int firstIdx = which * this->_Cols;
342 int secondIdx = which * this->_Cols + this->_Cols;
343 while (secondIdx != originalSize) {
347 std::swap(arr[firstIdx], arr[secondIdx]);
355 auto it = std::find(rowNames.begin(), rowNames.end(), which);
356 if (it != rowNames.end()){
358 int idx = std::distance(rowNames.begin(), it);
365 if (which > numCols() - 1) {
366 throw std::invalid_argument(
"Cannot delete col " + std::to_string(which));
369 auto *arr = getArray();
371 int originalSize = this->_Rows * this->_Cols;
377 int idx = which + ((this->_Rows - 1) * this->_Cols);
379 while (idx >= which) {
384 if (idx == originalSize - 1) {
386 idx -= this->_Rows + (this->_Cols - this->_Rows);
390 for (
int i = idx; i < originalSize - 1; i++) {
391 std::swap(arr[i], arr[i + 1]);
395 idx -= this->_Rows + (this->_Cols - this->_Rows);
400 colNames.erase(colNames.begin() + which);
406 auto it = std::find(colNames.begin(), colNames.end(), which);
407 if (it != colNames.end()){
409 int idx = std::distance(colNames.begin(), it);
A basic local matrix class, based on the libstruct version.
Definition: Matrix.h:18
void deleteCol(int which)
delete col
Definition: Matrix.h:364
bool operator==(Matrix< T > &other)
Element-wise equality operator to compare a Matrix<T> with another Matrix<T>
Definition: Matrix.h:160
bool empty()
indicator method for empty Matrix.
Definition: Matrix.h:232
bool operator!=(Matrix< T > &other)
inequality operators
Definition: Matrix.h:188
Matrix(ls::Matrix< T > &matrix)
Constructor for creating a Matrix<T> from a ls::Matrix<T>
Definition: Matrix.h:138
bool almostEquals(Matrix< double > &other, const double &tolerance)
Element-wise comparison between this Matrix<double> with another.
Definition: Matrix.h:198
void deleteRow(int which)
delete row
Definition: Matrix.h:314
void sortRowsByLabels()
Reorder Matrix rows such that row names are in alphabetical order.
Definition: Matrix.h:243
Matrix< T > mult(Matrix< T > matrix)
matrix multiplication operator
Definition: Matrix.h:222
void sortColsByLabels()
Reorder Matrix cols such that col names are in alphabetical order.
Definition: Matrix.h:280