MsPASS C++ API  2.4.1.dev4+g92330b7a
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | Friends | List of all members
mspass::utility::dmatrix Class Reference

Lightweight, simple matrix object. More...

#include <dmatrix.h>

Inheritance diagram for mspass::utility::dmatrix:
Inheritance graph
[legend]

Public Member Functions

 dmatrix ()
 
 dmatrix (const size_t nr, const size_t nc)
 
 dmatrix (const dmatrix &other)
 
 ~dmatrix ()
 
double operator() (const size_t rowindex, const size_t colindex) const
 
double & operator() (size_t r, size_t c)
 
dmatrixoperator= (const dmatrix &other)
 
dmatrixoperator+= (const dmatrix &other)
 Add one matrix to another.
 
dmatrixoperator-= (const dmatrix &other)
 Subtract one matrix to another.
 
dmatrix operator+ (const dmatrix &other) const
 
dmatrix operator- (const dmatrix &other) const
 
dmatrix operator* (double s) const noexcept
 
double * get_address (size_t r, size_t c) const
 Get a pointer to the location of a matrix component.
 
size_t rows () const
 
size_t columns () const
 
std::vector< size_t > size () const
 Return a vector with 2 elements giving the size.
 
void zero ()
 

Protected Attributes

std::vector< double > ary
 
size_t length
 
size_t nrr
 
size_t ncc
 

Friends

class boost::serialization::access
 
dmatrix operator* (const dmatrix &A, const dmatrix &B)
 Procedure to multiply two matrices. This could be implemented with a dmatrix::operator but this was an existing procedure known to work that I didn't choose to mess with. Sizes must be compatible or an exception will be thrown.
 
dmatrix operator* (const double &s, const dmatrix &A) noexcept
 Scale a matrix by a constant.
 
dmatrix tr (const dmatrix &A) noexcept
 Transpose a matrix.
 
std::ostream & operator<< (std::ostream &os, dmatrix &A)
 Text output operator.
 

Detailed Description

Lightweight, simple matrix object.

This class defines a lightweight, simple double precision matrix. Provides basic matrix functionality. Note that elements of the matrix are stored internally in FORTRAN order but using C style indexing. That is, all indices begin at 0, not 1 and run to size - 1. Further, FORTRAN order means the elements are actually ordered in columns as in FORTRAN in a continuous, logical block of memory. This allow one to use the BLAS functions to access the elements of the matrix. As usual be warned this is useful for efficiency and speed, but completely circumvents the bounds checking used by methods in the object.

Author
Robert R and Gary L. Pavlis

Constructor & Destructor Documentation

◆ dmatrix() [1/3]

mspass::utility::dmatrix::dmatrix ( )

Default constructor. Produces a 1x1 matrix as a place holder.

8 {
9 nrr = 0;
10 ncc = 0;
11 length = 0;
12 ary.reserve(0);
13}
size_t nrr
Definition dmatrix.h:271
size_t ncc
Definition dmatrix.h:273
std::vector< double > ary
Definition dmatrix.h:267
size_t length
Definition dmatrix.h:269

References ary, length, ncc, and nrr.

◆ dmatrix() [2/3]

mspass::utility::dmatrix::dmatrix ( const size_t  nr,
const size_t  nc 
)

Basic constructor. Allocates space for nr x nc array and initializes to zeros.

Parameters
nrnumber of rows to allocate for this matrix.
ncnumber of columns to allocate for this matrix.
14 {
15 nrr = nr;
16 ncc = nc;
17 length = nr * nc;
18 if (length < 1) {
19 length = 1;
20 nrr = ncc = 0;
21 }
22 /* This std::vector method allocates space so zero method can just
23 * use indexing. */
24 ary.resize(length);
25 this->zero();
26}
void zero()
Definition dmatrix.cc:203

References ary, length, ncc, nrr, and zero().

◆ dmatrix() [3/3]

mspass::utility::dmatrix::dmatrix ( const dmatrix other)

Standard copy constructor.

28 {
29 nrr = other.nrr;
30 ncc = other.ncc;
31 length = other.length;
32 ary = other.ary;
33}

References ary, length, ncc, and nrr.

◆ ~dmatrix()

mspass::utility::dmatrix::~dmatrix ( )

Destructor - releases any matrix memory.

35 {
36 // if(ary!=NULL) delete [] ary;
37}

Member Function Documentation

◆ columns()

size_t mspass::utility::dmatrix::columns ( ) const

Return number of columns in this matrix.

216{ return (ncc); }

References ncc.

◆ get_address()

double * mspass::utility::dmatrix::get_address ( size_t  r,
size_t  c 
) const

Get a pointer to the location of a matrix component.

Although a sharp knife it is useful at times to get a raw pointer to the data in a dmatrix. A common one is using the BLAS to do vector operations for speed. Users of this method must note that the data for a dmatrix is stored as a single rowXcolumn std::vector container. The matrix is stored in Fortran order (column1, column2, ...). The contiguous memory guarantee of std::vector allows vector operations with the BLAS to work by rows or columns.

Parameters
ris the row index of the desired address
cis the column index of the desired memory address.
Returns
pointer to component at row r and column c.
Exceptions
dmatrix_size_errorwill be throw if r or c are outside matrix dimensions.
72 {
73 double *ptr;
74 int out_of_range = 0;
75 if (rowindex >= nrr)
76 out_of_range = 1;
77 if (rowindex < 0)
78 out_of_range = 1;
79 if (colindex >= ncc)
80 out_of_range = 1;
81 if (colindex < 0)
82 out_of_range = 1;
83 if (out_of_range)
84 throw dmatrix_index_error(nrr, ncc, rowindex, colindex);
85 /* This is somewhat contradictory to the const qualifier of the method
86 but not really. const in that position implies the method itself
87 does not alter the object, but a pointer that is not const is
88 always something that allows data to be changed*/
89 ptr = const_cast<double *>(&(ary[rowindex + (nrr) * (colindex)]));
90 return (ptr);
91}

References ary, ncc, and nrr.

◆ operator()() [1/2]

double mspass::utility::dmatrix::operator() ( const size_t  rowindex,
const size_t  colindex 
) const

Indexing operator to fetch an array element.

Can also be used to set an element as a left hand side (e.g. A(2,4)=2.0;).

Parameters
rowindexrow to fetch
colindexcolumn to fetch.
Returns
value of matrix element at position (rowindex,colindex)
Exceptions
dmatrix_index_erroris thrown if request is out of range
39 {
40 int out_of_range = 0;
41 if (rowindex >= nrr)
42 out_of_range = 1;
43 if (rowindex < 0)
44 out_of_range = 1;
45 if (colindex >= ncc)
46 out_of_range = 1;
47 if (colindex < 0)
48 out_of_range = 1;
49 if (out_of_range)
50 throw dmatrix_index_error(nrr, ncc, rowindex, colindex);
51 double result = ary[rowindex + (nrr) * (colindex)];
52 return result;
53}

References ary, ncc, and nrr.

◆ operator()() [2/2]

double & mspass::utility::dmatrix::operator() ( size_t  r,
size_t  c 
)

Mutable indexing operator to fetch or modify an array element.

Parameters
rrow index to fetch.
ccolumn index to fetch.
Returns
reference to matrix element at position (r,c).
Exceptions
dmatrix_index_erroris thrown if request is out of range.
54 {
55 int out_of_range = 0;
56 if (rowindex >= nrr)
57 out_of_range = 1;
58 if (rowindex < 0)
59 out_of_range = 1;
60 if (colindex >= ncc)
61 out_of_range = 1;
62 if (colindex < 0)
63 out_of_range = 1;
64 if (out_of_range)
65 throw dmatrix_index_error(nrr, ncc, rowindex, colindex);
66 return (ary[rowindex + (nrr) * (colindex)]);
67}

References ary, ncc, and nrr.

◆ operator*()

dmatrix mspass::utility::dmatrix::operator* ( double  s) const
noexcept

Scale this matrix by a constant.

Multiplies every element in this matrix by s and returns the scaled copy.

Parameters
sscaling factor.
Returns
this matrix multiplied by s.
175 {
176 double *ptr;
177 dmatrix result(*this);
178 ptr = result.get_address(0, 0);
179 dscal(length, x, ptr, 1);
180 return result;
181}
dmatrix()
Definition dmatrix.cc:8

References get_address().

◆ operator+()

dmatrix mspass::utility::dmatrix::operator+ ( const dmatrix other) const

Operator to add two matrices.

This operator is similar to += but is the operator used in constructs like X=A+B. Like += other and this must be the same size or an exception will be thrown.

Parameters
othermatrix to be added
Exceptions
throwsa dmatrix_size_error if other and this are not the same size.
121 {
122 try {
123 dmatrix result(*this);
124 result += other;
125 return result;
126 } catch (...) {
127 throw;
128 };
129}

◆ operator+=()

dmatrix & mspass::utility::dmatrix::operator+= ( const dmatrix other)

Add one matrix to another.

Matrix addition is a standard operation but demands the two matrices to be added are the same size. Hence, an exception will happen if you use this operator with a size mismatch.

Parameters
otheris the matrix to be added to this.
Exceptions
throwsa dmatrix_size_error if other and this are not the same size.
103 {
104 size_t i;
105 if ((nrr != other.nrr) || (length != other.length))
106 throw dmatrix_size_error(nrr, ncc, other.nrr, other.length);
107 for (i = 0; i < length; i++)
108 ary[i] += other.ary[i];
109 return *this;
110}

References ary, length, ncc, and nrr.

◆ operator-()

dmatrix mspass::utility::dmatrix::operator- ( const dmatrix other) const

Operator to add two matrices.

This operator is similar to -= but is the operator used in constructs like X=A-B. Like -= other and this must be the same size or an exception will be thrown.

Parameters
othermatrix to be added
Exceptions
throwsa dmatrix_size_error if other and this are not the same size.
130 {
131 try {
132 dmatrix result(*this);
133 result -= other;
134 return result;
135 } catch (...) {
136 throw;
137 };
138}

◆ operator-=()

dmatrix & mspass::utility::dmatrix::operator-= ( const dmatrix other)

Subtract one matrix to another.

Matrix subtraction is a standard operation but demands the two matrices to be added are the same size. Hence, an exception will happen if you use this operator with a size mismatch.

Parameters
otheris the matrix to be subracted from to this.
Exceptions
throwsa dmatrix_size_error if other and this are not the same size.
112 {
113 size_t i;
114 if ((nrr != other.nrr) || (length != other.length))
115 throw dmatrix_size_error(nrr, ncc, other.nrr, other.length);
116 for (i = 0; i < length; i++)
117 ary[i] -= other.ary[i];
118 return *this;
119}

References ary, length, ncc, and nrr.

◆ operator=()

dmatrix & mspass::utility::dmatrix::operator= ( const dmatrix other)

Standard assignment operator

93 {
94 if (&other != this) {
95 ncc = other.ncc;
96 nrr = other.nrr;
97 length = other.length;
98 ary = other.ary;
99 }
100 return *this;
101}

References ary, length, ncc, and nrr.

◆ rows()

size_t mspass::utility::dmatrix::rows ( ) const

Return number of rows in this matrix.

215{ return (nrr); }

References nrr.

◆ size()

vector< size_t > mspass::utility::dmatrix::size ( ) const

Return a vector with 2 elements giving the size.

This function returns an std::vector with 2 elements with size information. first component is rows, second is columns. This simulates the matlab size function.

207 {
208 vector<size_t> sz;
209 sz.push_back(nrr);
210 sz.push_back(ncc);
211 return (sz);
212}

References ncc, and nrr.

◆ zero()

void mspass::utility::dmatrix::zero ( )

Initialize a matrix to all zeros.

203 {
204 for (size_t i = 0; i < length; ++i)
205 ary[i] = 0.0;
206}

References ary, and length.

Friends And Related Symbol Documentation

◆ operator* [1/2]

dmatrix operator* ( const dmatrix A,
const dmatrix B 
)
friend

Procedure to multiply two matrices. This could be implemented with a dmatrix::operator but this was an existing procedure known to work that I didn't choose to mess with. Sizes must be compatible or an exception will be thrown.

Parameters
Ais the left matrix for the multiply.
Bis the right matrix for the multiply.
Exceptions
dmatrix_size_errorwill be thrown if the columns in A are not equal to the rows in B.
Returns
A*B
140 {
141 size_t i, j;
142 /* The computed length in last arg to the error object is a relic*/
143 if (A.columns() != B.rows())
144 throw dmatrix_size_error(A.rows(), A.columns(), B.rows(),
145 B.rows() * B.columns());
146 dmatrix prod(A.rows(), B.columns());
147 for (i = 0; i < A.rows(); i++)
148 for (j = 0; j < B.columns(); j++) {
149 double *x1ptr, *bptr;
150 x1ptr = const_cast<dmatrix &>(A).get_address(i, 0);
151 bptr = const_cast<dmatrix &>(B).get_address(0, j);
152 /* This temporary seems necessary */
153 double *dptr;
154 dptr = prod.get_address(i, j);
155 *dptr = ddot(A.columns(), x1ptr, A.rows(), bptr, 1);
156 }
157 return prod;
158}
double * get_address(size_t r, size_t c) const
Get a pointer to the location of a matrix component.
Definition dmatrix.cc:72

◆ operator* [2/2]

dmatrix operator* ( const double &  s,
const dmatrix A 
)
friend

Scale a matrix by a constant.

This procedure will multiply all elements of a matrix by a constant. The linear algebra concept of scaling a matrix.

Parameters
sis the scaling factor
Ais the matrix to be scaled
Returns
s*A
160 {
161 size_t i;
162 dmatrix tempmat(A.rows(), A.columns());
163 size_t lenary = A.rows() * A.columns();
164 double *zptr, *dptr;
165 zptr = const_cast<dmatrix &>(A).get_address(0, 0);
166 dptr = tempmat.get_address(0, 0);
167 for (i = 0; i < lenary; ++i) {
168 (*dptr) = s * (*zptr);
169 ++dptr;
170 ++zptr;
171 }
172 return tempmat;
173}

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
dmatrix A 
)
friend

Text output operator.

Output is ascii data written in the matrix layout. Note this can create huge lines and a lot of output for a large matrix so use carefully.

Parameters
osis the std::ostream to contain data.
Ais the data to be written

◆ tr

dmatrix tr ( const dmatrix A)
friend

Transpose a matrix.

A standard matrix operation is to transpose a matrix (reversing rows and columns). This takes input A and returns A^T.

Parameters
A- matrix to transpose.
Returns
A transposed
183 {
184 size_t i, j;
185 dmatrix temp(A.columns(), A.rows());
186 for (i = 0; i < A.rows(); i++)
187 for (j = 0; j < A.columns(); j++) {
188 temp(j, i) = A(i, j);
189 }
190 return temp;
191}

Member Data Documentation

◆ ary

std::vector<double> mspass::utility::dmatrix::ary
protected

Contiguous Fortran-order storage for all matrix elements.

◆ length

size_t mspass::utility::dmatrix::length
protected

Number of scalar values in ary that represent this matrix.

◆ ncc

size_t mspass::utility::dmatrix::ncc
protected

Number of columns in this matrix.

◆ nrr

size_t mspass::utility::dmatrix::nrr
protected

Number of rows in this matrix.


The documentation for this class was generated from the following files: