version  0.0.1
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
dmatrix.h
1#ifndef _DMATRIX_H_
2#define _DMATRIX_H_
3#include <iostream>
4#include <sstream>
5#include <string>
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 "mspass/utility/MsPASSError.h"
11#include <boost/archive/binary_iarchive.hpp>
12#include <boost/archive/binary_oarchive.hpp>
13#include <boost/serialization/vector.hpp>
14namespace mspass {
15namespace utility {
16//==================================================================
26public:
34 dmatrix_index_error(const size_t nrmax, const size_t ncmax, const size_t ir,
35 const size_t ic) {
36 row = ir;
37 column = ic;
38 nrr = nrmax;
39 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
50private:
51 size_t row, column;
52 size_t nrr, ncc;
53};
61public:
69 dmatrix_size_error(const size_t nr1, const size_t nc1, const size_t nr2,
70 const size_t nc2) {
71 nrow1 = nr1;
72 ncol1 = nc1;
73 nrow2 = nr2;
74 ncol2 = nc2;
75 std::ostringstream oss;
76 oss << "dmatrix class: size mismatch error in binary operator"
77 << std::endl
78 << "matrix on left is " << nrow1 << "X" << ncol1
79 << " while matrix on right is " << nrow2 << "X" << ncol2 << std::endl;
80 message = oss.str();
81 badness = ErrorSeverity::Invalid;
82 };
83 /* necessary baggage for some compilers - empty destructor */
84 ~dmatrix_size_error() throw() {};
85
86private:
87 size_t nrow1, ncol1, nrow2, ncol2;
88};
104class dmatrix {
105public:
107 dmatrix();
114 dmatrix(const size_t nr, const size_t nc);
116 dmatrix(const dmatrix &other);
118 ~dmatrix();
128 double operator()(const size_t rowindex, const size_t colindex) const;
129 double &operator()(size_t r, size_t c);
131 dmatrix &operator=(const dmatrix &other);
142 dmatrix &operator+=(const dmatrix &other);
153 dmatrix &operator-=(const dmatrix &other);
164 dmatrix operator+(const dmatrix &other) const;
175 dmatrix operator-(const dmatrix &other) const;
176 // friend class dvector;
190 friend dmatrix operator*(const dmatrix &A, const dmatrix &B);
192 // Scale a matrix by a constant. X=c*A where c is a constant.
194
203 friend dmatrix operator*(const double &s, const dmatrix &A) noexcept;
204 dmatrix operator*(double s) const noexcept;
213 friend dmatrix tr(const dmatrix &A) noexcept;
214 /* \brief Get a pointer to the location of a matrix component.
215
216 Although a sharp knife it is useful at times to get a raw pointer to
217 the data in a dmatrix. A common one is using the BLAS to do vector
218 operations for speed. Users of this method must note that the data
219 for a dmatrix is stored as a single rowXcolumn std::vector container.
220 The matrix is stored in Fortran order (column1, column2, ...).
221 The contiguous memory guarantee of std::vector allows vector operations
222 with the BLAS to work by rows or columns.
223
224 \param r is the row index of the desired address
225 \param c is the column index of the desired memory address.
226 \return pointer to component at row r and column c.
227 \exception dmatrix_size_error will be throw if r or c are outside
228 matrix dimensions.
229 */
230 double *get_address(size_t r, size_t c) const;
238 friend std::ostream &operator<<(std::ostream &os, dmatrix &A);
240 size_t rows() const;
242 size_t columns() const;
248 std::vector<size_t> size() const;
250 void zero();
251
252protected:
253 std::vector<double> ary; // initial size of container 0
254 size_t length;
255 size_t nrr, ncc;
256
257private:
258 friend class boost::serialization::access;
259 template <class Archive>
260 void serialize(Archive &ar, const unsigned int version) {
261 ar & nrr & ncc & length;
262 ar & ary;
263 }
264};
273class dvector : public dmatrix {
274public:
276 dvector() : dmatrix() {};
278 dvector(size_t nrv) : dmatrix(nrv, 1) {};
280 dvector(const dvector &other);
282 dvector &operator=(const dvector &other);
284 double &operator()(size_t rowindex);
295 friend dvector operator*(const dmatrix &A, const dvector &x);
296};
297
298} // namespace utility
299} // end namespace mspass
300#endif
Base class for error object thrown by MsPASS library routines.
Definition MsPASSError.h:38
std::string message
Definition MsPASSError.h:108
ErrorSeverity badness
Definition MsPASSError.h:110
special convenience class for matrix indexing errors.
Definition dmatrix.h:25
dmatrix_index_error(const size_t nrmax, const size_t ncmax, const size_t ir, const size_t ic)
Definition dmatrix.h:34
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:104
dmatrix & operator=(const dmatrix &other)
Definition dmatrix.cc:93
size_t rows() const
Definition dmatrix.cc:215
dmatrix & operator-=(const dmatrix &other)
Subtract one matrix to another.
Definition dmatrix.cc:112
dmatrix & operator+=(const dmatrix &other)
Add one matrix to another.
Definition dmatrix.cc:103
friend std::ostream & operator<<(std::ostream &os, dmatrix &A)
Text output operator.
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:140
std::vector< size_t > size() const
Return a vector with 2 elements giving the size.
Definition dmatrix.cc:207
dmatrix operator+(const dmatrix &other) const
Definition dmatrix.cc:121
void zero()
Definition dmatrix.cc:203
size_t columns() const
Definition dmatrix.cc:216
dmatrix operator-(const dmatrix &other) const
Definition dmatrix.cc:130
friend dmatrix tr(const dmatrix &A) noexcept
Transpose a matrix.
Definition dmatrix.cc:183
double operator()(const size_t rowindex, const size_t colindex) const
Definition dmatrix.cc:39
dmatrix()
Definition dmatrix.cc:8
~dmatrix()
Definition dmatrix.cc:35
A vector compatible with dmatrix objects.
Definition dmatrix.h:273
dvector & operator=(const dvector &other)
Definition dmatrix.cc:219
dvector()
Definition dmatrix.h:276
friend dvector operator*(const dmatrix &A, const dvector &x)
Definition dmatrix.cc:239
double & operator()(size_t rowindex)
Definition dmatrix.cc:234
dvector(size_t nrv)
Definition dmatrix.h:278