version  0.0.1
Defines the C++ API for MsPASS
dmatrix.h
1 #ifndef _DMATRIX_H_
2 #define _DMATRIX_H_
3 #include <string>
4 #include <iostream>
5 #include <sstream>
6 #include <vector>
7 /* Either text or binary can be specified here, but we use binary
8  * to emphasize this class is normally serialized binary for
9  * speed*/
10 #include <boost/serialization/vector.hpp>
11 #include <boost/archive/binary_oarchive.hpp>
12 #include <boost/archive/binary_iarchive.hpp>
13 #include "mspass/utility/MsPASSError.h"
14 namespace mspass
15 {
16 namespace utility{
17 //==================================================================
27 {
28 public:
36  dmatrix_index_error(const size_t nrmax,
37  const size_t ncmax, const size_t ir, const size_t ic)
38  {
39  row = ir; column=ic; nrr=nrmax; ncc=ncmax;
40  std::ostringstream oss;
41  oss << "dmatrix object: indexing error"<<std::endl
42  << "Matrix index (" << row << "," << column
43  << ")is outside range = " << nrr << "," << ncc << std::endl;
44  message = oss.str();
45  badness = ErrorSeverity::Invalid;
46  };
47  /* necessary baggage for some compilers - empty destructor */
48  ~dmatrix_index_error() throw(){};
49 private:
50  size_t row,column;
51  size_t nrr, ncc;
52 };
60 {
61 public:
69  dmatrix_size_error (const size_t nr1, const size_t nc1,
70  const size_t nr2, const size_t nc2)
71  {
72  nrow1=nr1; ncol1=nc1;nrow2=nr2;ncol2=nc2;
73  std::ostringstream oss;
74  oss << "dmatrix class: size mismatch error in binary operator"<<std::endl
75  << "matrix on left is "<< nrow1 << "X" << ncol1
76  << " while matrix on right is "
77  << nrow2 << "X" << ncol2 << std::endl;
78  message = oss.str();
79  badness = ErrorSeverity::Invalid;
80  };
81  /* necessary baggage for some compilers - empty destructor */
82  ~dmatrix_size_error() throw(){};
83 private:
84  size_t nrow1, ncol1, nrow2, ncol2;
85 };
101 class dmatrix
102 {
103 public:
105  dmatrix();
112  dmatrix(const size_t nr, const size_t nc);
114  dmatrix(const dmatrix& other);
116  ~dmatrix();
126  double operator()(const size_t rowindex, const size_t colindex) const;
127  double& operator()(size_t r,size_t c);
129  dmatrix& operator=(const dmatrix& other);
139  dmatrix& operator+=(const dmatrix& other);
149  dmatrix& operator-=(const dmatrix& other);
159  dmatrix operator+(const dmatrix& other) const;
169  dmatrix operator-(const dmatrix& other) const;
170  //friend class dvector;
184  friend dmatrix operator*(const dmatrix& A, const dmatrix& B);
186 // Scale a matrix by a constant. X=c*A where c is a constant.
188 
197  friend dmatrix operator*(const double& s, const dmatrix& A) noexcept;
198  dmatrix operator* (double s) const noexcept;
207  friend dmatrix tr(const dmatrix& A) noexcept;
208  /* \brief Get a pointer to the location of a matrix component.
209 
210  Although a sharp knife it is useful at times to get a raw pointer to
211  the data in a dmatrix. A common one is using the BLAS to do vector
212  operations for speed. Users of this method must note that the data
213  for a dmatrix is stored as a single rowXcolumn std::vector container.
214  The matrix is stored in Fortran order (column1, column2, ...).
215  The contiguous memory guarantee of std::vector allows vector operations
216  with the BLAS to work by rows or columns.
217 
218  \param r is the row index of the desired address
219  \param c is the column index of the desired memory address.
220  \return pointer to component at row r and column c.
221  \exception dmatrix_size_error will be throw if r or c are outside
222  matrix dimensions.
223  */
224  double* get_address(size_t r, size_t c) const;
232  friend std::ostream& operator<<(std::ostream& os, dmatrix& A);
234  size_t rows() const;
236  size_t columns() const;
242  std::vector<size_t> size() const;
244  void zero();
245 protected:
246  std::vector<double> ary; // initial size of container 0
247  size_t length;
248  size_t nrr, ncc;
249 private:
250  friend class boost::serialization::access;
251  template<class Archive>void serialize(Archive & ar,
252  const unsigned int version)
253  {
254  ar & nrr & ncc & length;
255  ar & ary;
256  }
257 };
266 class dvector : public dmatrix
267 {
268 public:
272  dvector(size_t nrv) : dmatrix(nrv,1){};
274  dvector(const dvector& other);
276  dvector& operator=(const dvector& other);
278  double &operator()(size_t rowindex);
289  friend dvector operator*(const dmatrix &A, const dvector &x);
290 };
291 
292 } // end utility namespace
293 } // end namespace mspass
294 #endif
Base class for error object thrown by MsPASS library routines.
Definition: MsPASSError.h:40
std::string message
Definition: MsPASSError.h:109
ErrorSeverity badness
Definition: MsPASSError.h:116
special convenience class for matrix indexing errors.
Definition: dmatrix.h:27
dmatrix_index_error(const size_t nrmax, const size_t ncmax, const size_t ir, const size_t ic)
Definition: dmatrix.h:36
Convenience class for dmatrix use errors.
Definition: dmatrix.h:60
dmatrix_size_error(const size_t nr1, const size_t nc1, const size_t nr2, const size_t nc2)
Definition: dmatrix.h:69
Lightweight, simple matrix object.
Definition: dmatrix.h:102
dmatrix & operator=(const dmatrix &other)
Definition: dmatrix.cc:89
friend std::ostream & operator<<(std::ostream &os, dmatrix &A)
Text output operator.
size_t rows() const
Definition: dmatrix.cc:228
dmatrix & operator-=(const dmatrix &other)
Subtract one matrix to another.
Definition: dmatrix.cc:111
dmatrix & operator+=(const dmatrix &other)
Add one matrix to another.
Definition: dmatrix.cc:101
friend dmatrix operator*(const dmatrix &A, const dmatrix &B)
Procedure to multiply two matrices. This could be implemented with a dmatrix::operator but this was a...
Definition: dmatrix.cc:139
std::vector< size_t > size() const
Return a vector with 2 elements giving the size.
Definition: dmatrix.cc:219
dmatrix operator+(const dmatrix &other) const
Definition: dmatrix.cc:121
void zero()
Definition: dmatrix.cc:215
size_t columns() const
Definition: dmatrix.cc:232
dmatrix operator-(const dmatrix &other) const
Definition: dmatrix.cc:129
friend dmatrix tr(const dmatrix &A) noexcept
Transpose a matrix.
Definition: dmatrix.cc:190
double operator()(const size_t rowindex, const size_t colindex) const
Definition: dmatrix.cc:44
dmatrix()
Definition: dmatrix.cc:8
~dmatrix()
Definition: dmatrix.cc:39
A vector compatible with dmatrix objects.
Definition: dmatrix.h:267
dvector & operator=(const dvector &other)
Definition: dmatrix.cc:238
dvector()
Definition: dmatrix.h:270
friend dvector operator*(const dmatrix &A, const dvector &x)
Definition: dmatrix.cc:262
double & operator()(size_t rowindex)
Definition: dmatrix.cc:256
dvector(size_t nrv)
Definition: dmatrix.h:272