MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RollingMatrix.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2016 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include <vector>
11 using namespace std;
12 #include "RollingMatrix.h"
13 #include <cassert>
14 // #include <iostream>
15 
17  : nrows_(0), ncolumns_(0), currentStartRow_(0)
18 {;}
19 
20 
22 {;}
23 
25 {
26  nrows_ = other.nrows_;
27  ncolumns_ = other.ncolumns_;
29  rows_ = other.rows_;
30  return *this;
31 }
32 
33 
34 void RollingMatrix::resize( unsigned int nrows, unsigned int ncolumns )
35 {
36  rows_.resize( nrows );
37  nrows_ = nrows;
38  ncolumns_ = ncolumns;
39  for ( unsigned int i = 0; i < nrows; ++i ) {
40  rows_[i].resize( ncolumns, 0.0 );
41  }
42  currentStartRow_ = 0;
43 }
44 
45 double RollingMatrix::get( unsigned int row, unsigned int column ) const
46 {
47  unsigned int index = (row + currentStartRow_ ) % nrows_;
48  return rows_[index][column];
49 }
50 
51 void RollingMatrix::sumIntoEntry( double input, unsigned int row, unsigned int column )
52 {
53  unsigned int index = (row + currentStartRow_ ) % nrows_;
54  SparseVector& sv = rows_[index];
55  sv[column] += input;
56 }
57 
58 void RollingMatrix::sumIntoRow( const vector< double >& input, unsigned int row )
59 {
60  unsigned int index = (row + currentStartRow_) % nrows_;
61  SparseVector& sv = rows_[index];
62 
63  for (unsigned int i = 0; i < input.size(); ++i )
64  sv[i] += input[i];
65 }
66 
67 
68 double RollingMatrix::dotProduct( const vector< double >& input,
69  unsigned int row, unsigned int startColumn ) const
70 {
72  unsigned int index = (row + currentStartRow_) % nrows_;
73  const SparseVector& sv = rows_[index];
74  unsigned int i2 = input.size()/2;
75  unsigned int istart = (startColumn >= i2) ? 0 : i2-startColumn;
76  unsigned int colstart = (startColumn <= i2) ? 0 : startColumn - i2;
77  unsigned int iend = (sv.size()-startColumn > i2 ) ? input.size() :
78  i2 - startColumn + sv.size();
79 
80  // if ( iend >= istart ) cout << startColumn << i2 << istart << iend << colstart << "\n";
81  double ret = 0;
82  for (unsigned int i = istart, j = 0; i < iend; ++i, ++j )
83  ret += sv[j + colstart] * input[i];
84 
85  /*
86  double ret = 0;
87  if ( input.size() + startColumn <= sv.size() ) {
88  for (unsigned int i = 0; i < input.size(); ++i )
89  ret += sv[i + startColumn] * input[i];
90  } else if ( sv.size() > startColumn ) {
91  unsigned int end = sv.size() - startColumn;
92  for (unsigned int i = 0; i < end; ++i )
93  ret += sv[i + startColumn] * input[i];
94  }
95  */
96  return ret;
97 }
98 
99 void RollingMatrix::correl( vector< double >& ret,
100  const vector< double >& input, unsigned int row) const
101 
102 {
103  if ( ret.size() < ncolumns_ )
104  ret.resize( ncolumns_, 0.0 );
105  for ( unsigned int i = 0; i < ncolumns_; ++i ) {
106  ret[i] += dotProduct( input, row, i );
107  }
108 }
109 
110 void RollingMatrix::zeroOutRow( unsigned int row )
111 {
112  unsigned int index = (row + currentStartRow_) % nrows_;
113  rows_[index].assign( rows_[index].size(), 0.0 );
114 }
115 
117 {
118  if ( currentStartRow_ == 0 )
119  currentStartRow_ = nrows_ - 1;
120  else
122  zeroOutRow( 0 );
123 }
vector< SparseVector > rows_
Definition: RollingMatrix.h:58
unsigned int nrows_
Definition: RollingMatrix.h:54
void resize(unsigned int numRows, unsigned int numColumns)
double dotProduct(const vector< double > &input, unsigned int row, unsigned int startColumn) const
void sumIntoRow(const vector< double > &input, unsigned int row)
unsigned int currentStartRow_
Definition: RollingMatrix.h:56
unsigned int ncolumns_
Definition: RollingMatrix.h:55
void zeroOutRow(unsigned int row)
void sumIntoEntry(double input, unsigned int row, unsigned int column)
void correl(vector< double > &ret, const vector< double > &input, unsigned int row) const
RollingMatrix & operator=(const RollingMatrix &other)
double get(unsigned int row, unsigned int column) const
vector< double > SparseVector
Definition: RollingMatrix.h:14