Commit d299c720a6c7ee6e8015162886ce924d375831bd

Authored by David Mayerich
1 parent ff4ee63d

added row and column functions

Showing 1 changed file with 23 additions and 0 deletions   Show diff stats
stim/math/matrix.h
... ... @@ -245,6 +245,14 @@ public:
245 245 return a - b;
246 246 }
247 247  
  248 + /// Sum all elements in the matrix
  249 + T sum() const {
  250 + size_t N = R * C; //calculate the number of elements in the matrix
  251 + T s = (T)0; //allocate a register to store the sum
  252 + for (size_t n = 0; n < N; n++) s += M[n]; //perform the summation
  253 + return s;
  254 + }
  255 +
248 256 /// Sort rows of the matrix by the specified indices
249 257 matrix<T> sort_rows(size_t* idx) const {
250 258 matrix<T> result(C, R); //create the output matrix
... ... @@ -267,6 +275,21 @@ public:
267 275 return result;
268 276 }
269 277  
  278 + /// Return the column specified by index i
  279 + matrix<T> col(size_t i) {
  280 + matrix<T> c(R, 1); //create a single column matrix
  281 + memcpy(c.data(), &data()[R*i], C * sizeof(T)); //copy the column
  282 + return c;
  283 + }
  284 +
  285 + /// Return the row specified by index i
  286 + matrix<T> row(size_t i) {
  287 + matrix<T> r(1, C); //create a single row matrix
  288 + for (size_t c = 0; c < C; c++)
  289 + r(0, c) = at(i, c);
  290 + return r;
  291 + }
  292 +
270 293 std::string toStr() const {
271 294 std::stringstream ss;
272 295  
... ...