MsPASS C++ API  2.4.3.dev1+g61b06b96
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 <utility>
7#include <vector>
8/* Either text or binary can be specified here, but we use binary
9 * to emphasize this class is normally serialized binary for
10 * speed*/
11#include "mspass/utility/MsPASSError.h"
12#include <boost/archive/binary_iarchive.hpp>
13#include <boost/archive/binary_oarchive.hpp>
14#include <boost/serialization/vector.hpp>
15namespace mspass {
16namespace utility {
17//==================================================================
27public:
35 dmatrix_index_error(const size_t nrmax, const size_t ncmax, const size_t ir,
36 const size_t ic) {
37 row = ir;
38 column = ic;
39 nrr = nrmax;
40 ncc = ncmax;
41 std::ostringstream oss;
42 oss << "dmatrix object: indexing error" << std::endl
43 << "Matrix index (" << row << "," << column
44 << ")is outside range = " << nrr << "," << ncc << std::endl;
45 message = oss.str();
46 badness = ErrorSeverity::Invalid;
47 };
48 /* necessary baggage for some compilers - empty destructor */
49 ~dmatrix_index_error() throw() {};
50
51private:
52 size_t row, column;
53 size_t nrr, ncc;
54};
62public:
70 dmatrix_size_error(const size_t nr1, const size_t nc1, const size_t nr2,
71 const size_t nc2) {
72 nrow1 = nr1;
73 ncol1 = nc1;
74 nrow2 = nr2;
75 ncol2 = nc2;
76 std::ostringstream oss;
77 oss << "dmatrix class: size mismatch error in binary operator"
78 << std::endl
79 << "matrix on left is " << nrow1 << "X" << ncol1
80 << " while matrix on right is " << nrow2 << "X" << ncol2 << std::endl;
81 message = oss.str();
82 badness = ErrorSeverity::Invalid;
83 };
84 /* necessary baggage for some compilers - empty destructor */
85 ~dmatrix_size_error() throw() {};
86
87private:
88 size_t nrow1, ncol1, nrow2, ncol2;
89};
105class dmatrix {
106public:
108 dmatrix();
115 dmatrix(const size_t nr, const size_t nc);
117 dmatrix(const dmatrix &other);
119 ~dmatrix();
129 double operator()(const size_t rowindex, const size_t colindex) const;
137 double &operator()(size_t r, size_t c);
139 dmatrix &operator=(const dmatrix &other);
141 void swap(dmatrix &other) noexcept {
142 ary.swap(other.ary);
143 std::swap(length, other.length);
144 std::swap(nrr, other.nrr);
145 std::swap(ncc, other.ncc);
146 }
148 bool storage_is_consistent() const noexcept {
149 if (ary.size() != length)
150 return false;
151 if (nrr == 0 || ncc == 0)
152 return nrr == 0 && ncc == 0 && length == 0;
153 return length / nrr == ncc && length % nrr == 0;
154 }
165 dmatrix &operator+=(const dmatrix &other);
176 dmatrix &operator-=(const dmatrix &other);
187 dmatrix operator+(const dmatrix &other) const;
198 dmatrix operator-(const dmatrix &other) const;
199 // friend class dvector;
213 friend dmatrix operator*(const dmatrix &A, const dmatrix &B);
215 // Scale a matrix by a constant. X=c*A where c is a constant.
217
226 friend dmatrix operator*(const double &s, const dmatrix &A) noexcept;
233 dmatrix operator*(double s) const noexcept;
242 friend dmatrix tr(const dmatrix &A) noexcept;
259 double *get_address(size_t r, size_t c) const;
267 friend std::ostream &operator<<(std::ostream &os, dmatrix &A);
269 size_t rows() const;
271 size_t columns() const;
277 std::vector<size_t> size() const;
279 void zero();
280
281protected:
283 std::vector<double> ary; // initial size of container 0
285 size_t length;
287 size_t nrr;
289 size_t ncc;
290
291private:
292 friend class boost::serialization::access;
293 template <class Archive>
294 void serialize(Archive &ar, const unsigned int version) {
295 ar & nrr & ncc & length;
296 ar & ary;
297 }
298};
307class dvector : public dmatrix {
308public:
310 dvector() : dmatrix() {};
312 dvector(size_t nrv) : dmatrix(nrv, 1) {};
314 dvector(const dvector &other);
316 dvector &operator=(const dvector &other);
318 double &operator()(size_t rowindex);
329 friend dvector operator*(const dmatrix &A, const dvector &x);
330};
331
332} // namespace utility
333} // end namespace mspass
334#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:26
dmatrix_index_error(const size_t nrmax, const size_t ncmax, const size_t ir, const size_t ic)
Definition dmatrix.h:35
Convenience class for dmatrix use errors.
Definition dmatrix.h:61
dmatrix_size_error(const size_t nr1, const size_t nc1, const size_t nr2, const size_t nc2)
Definition dmatrix.h:70
Lightweight, simple matrix object.
Definition dmatrix.h:105
dmatrix & operator=(const dmatrix &other)
Definition dmatrix.cc:93
bool storage_is_consistent() const noexcept
Definition dmatrix.h:148
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:287
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:289
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
void swap(dmatrix &other) noexcept
Definition dmatrix.h:141
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:283
size_t length
Definition dmatrix.h:285
A vector compatible with dmatrix objects.
Definition dmatrix.h:307
dvector & operator=(const dvector &other)
Definition dmatrix.cc:219
dvector()
Definition dmatrix.h:310
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:312