MsPASS C++ API  2.4.1.dev4+g92330b7a
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;
136 double &operator()(size_t r, size_t c);
138 dmatrix &operator=(const dmatrix &other);
149 dmatrix &operator+=(const dmatrix &other);
160 dmatrix &operator-=(const dmatrix &other);
171 dmatrix operator+(const dmatrix &other) const;
182 dmatrix operator-(const dmatrix &other) const;
183 // friend class dvector;
197 friend dmatrix operator*(const dmatrix &A, const dmatrix &B);
199 // Scale a matrix by a constant. X=c*A where c is a constant.
201
210 friend dmatrix operator*(const double &s, const dmatrix &A) noexcept;
217 dmatrix operator*(double s) const noexcept;
226 friend dmatrix tr(const dmatrix &A) noexcept;
243 double *get_address(size_t r, size_t c) const;
251 friend std::ostream &operator<<(std::ostream &os, dmatrix &A);
253 size_t rows() const;
255 size_t columns() const;
261 std::vector<size_t> size() const;
263 void zero();
264
265protected:
267 std::vector<double> ary; // initial size of container 0
269 size_t length;
271 size_t nrr;
273 size_t ncc;
274
275private:
276 friend class boost::serialization::access;
277 template <class Archive>
278 void serialize(Archive &ar, const unsigned int version) {
279 ar & nrr & ncc & length;
280 ar & ary;
281 }
282};
291class dvector : public dmatrix {
292public:
294 dvector() : dmatrix() {};
296 dvector(size_t nrv) : dmatrix(nrv, 1) {};
298 dvector(const dvector &other);
300 dvector &operator=(const dvector &other);
302 double &operator()(size_t rowindex);
313 friend dvector operator*(const dmatrix &A, const dvector &x);
314};
315
316} // namespace utility
317} // end namespace mspass
318#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.
size_t nrr
Definition dmatrix.h:271
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 ncc
Definition dmatrix.h:273
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
double * get_address(size_t r, size_t c) const
Get a pointer to the location of a matrix component.
Definition dmatrix.cc:72
dmatrix()
Definition dmatrix.cc:8
~dmatrix()
Definition dmatrix.cc:35
std::vector< double > ary
Definition dmatrix.h:267
size_t length
Definition dmatrix.h:269
A vector compatible with dmatrix objects.
Definition dmatrix.h:291
dvector & operator=(const dvector &other)
Definition dmatrix.cc:219
dvector()
Definition dmatrix.h:294
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:296