From d299c720a6c7ee6e8015162886ce924d375831bd Mon Sep 17 00:00:00 2001 From: David Date: Tue, 17 Jan 2017 18:50:45 -0600 Subject: [PATCH] added row and column functions --- stim/math/matrix.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+), 0 deletions(-) diff --git a/stim/math/matrix.h b/stim/math/matrix.h index 21f63bb..2405539 100644 --- a/stim/math/matrix.h +++ b/stim/math/matrix.h @@ -245,6 +245,14 @@ public: return a - b; } + /// Sum all elements in the matrix + T sum() const { + size_t N = R * C; //calculate the number of elements in the matrix + T s = (T)0; //allocate a register to store the sum + for (size_t n = 0; n < N; n++) s += M[n]; //perform the summation + return s; + } + /// Sort rows of the matrix by the specified indices matrix sort_rows(size_t* idx) const { matrix result(C, R); //create the output matrix @@ -267,6 +275,21 @@ public: return result; } + /// Return the column specified by index i + matrix col(size_t i) { + matrix c(R, 1); //create a single column matrix + memcpy(c.data(), &data()[R*i], C * sizeof(T)); //copy the column + return c; + } + + /// Return the row specified by index i + matrix row(size_t i) { + matrix r(1, C); //create a single row matrix + for (size_t c = 0; c < C; c++) + r(0, c) = at(i, c); + return r; + } + std::string toStr() const { std::stringstream ss; -- libgit2 0.21.4