sparse_mat.h
Go to the documentation of this file.
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of EcoLab
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
12 #ifndef SPARSE_MAT_H
13 #define SPARSE_MAT_H
14 
15 #include <arrays.h>
16 
17 namespace ecolab
18 {
20  class sparse_mat
21  {
22  public:
23  /* row and column dimension of matrix (used if diag.size==0) */
24  unsigned rowsz, colsz;
25  /*diagonal and values of offdiagonal*/
26  array_ns::array<double> diag, val;
27  /*row and columns of offdiagonal elements*/
29  sparse_mat(int s=0, int o=0): rowsz(s), colsz(s), diag(s), val(o), row(o), col(o) {}
30  sparse_mat(const sparse_mat& x)
31  {
32  rowsz=x.rowsz; colsz=x.colsz;
33  diag=x.diag; val=x.val;
34  row=x.row; col=x.col;
35  }
36  /*matrix multiplication*/
37  template <class E> typename
39  operator*(const E& x)
40  {
42  assert(row.size()==col.size() && row.size()==val.size());
43  if (diag.size()>0)
44  {
45  assert(diag.size()==x.size());
46  r = diag*x;
47  }
48  else
49  r.resize(rowsz,0);
50  r[row]+=val*x[col];
51  return r;
52  }
53  /* not quite sure how to do this yet!
54  sparse_mat operator*(sparse_mat x)
55  {
56  */
57  /*assignment*/
58  sparse_mat& operator=(const sparse_mat& x)
59  {
60  rowsz=x.rowsz; colsz=x.colsz;
61  diag=x.diag; val=x.val;
62  row=x.row; col=x.col;
63  return *this;
64  }
65  /*return the submatrix bounded by #min# and #max#*/
66  sparse_mat submat(unsigned min, unsigned max);
67  /* submatrix extraction and insertion */
68  void insert(const sparse_mat& x, unsigned where, unsigned old_size);
69 
70  /* initialise sparse mat in a random configuration */
71  void init_rand(unsigned conn, double sigma);
72  };
73 
75  inline sparse_mat tr(const sparse_mat& x)
76  {
77  sparse_mat r=x;
78  std::swap(r.row,r.col);
79  std::swap(r.rowsz,r.colsz);
80  return r;
81  }
82 }
83 
84 #include "sparse_mat.cd"
85 #endif
sparse_mat tr(const sparse_mat &x)
transpose matrix
Definition: sparse_mat.h:75
sparse matrix class
Definition: sparse_mat.h:20
dynamic array class
Definition: arrays.h:157
size_t size() const
number of elements
Definition: arrays.h:1766
_OPENMP
Definition: accessor.h:16
void resize(size_t s)
resize array to s elements
Definition: arrays.h:1599