version  0.0.1
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:
mspass::utility::dvector

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
 
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}

◆ 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 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}

◆ ~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); }

◆ get_address()

double * mspass::utility::dmatrix::get_address ( size_t  r,
size_t  c 
) const
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}

◆ 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}

◆ operator()() [2/2]

double & mspass::utility::dmatrix::operator() ( size_t  r,
size_t  c 
)
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}

◆ operator*()

dmatrix mspass::utility::dmatrix::operator* ( double  s) const
noexcept
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

◆ 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
Ais 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}

◆ 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}

◆ 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}

◆ rows()

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

Return number of rows in this matrix.

215{ return (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}

◆ 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}

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 (x1.columns() != b.rows())
144 throw dmatrix_size_error(x1.rows(), x1.columns(), b.rows(),
145 b.rows() * b.columns());
146 dmatrix prod(x1.rows(), b.columns());
147 for (i = 0; i < x1.rows(); i++)
148 for (j = 0; j < b.columns(); j++) {
149 double *x1ptr, *bptr;
150 x1ptr = const_cast<dmatrix &>(x1).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(x1.columns(), x1ptr, x1.rows(), bptr, 1);
156 }
157 return prod;
158}

◆ 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
sA
160 {
161 size_t i;
162 dmatrix tempmat(zx.rows(), zx.columns());
163 size_t lenary = zx.rows() * zx.columns();
164 double *zptr, *dptr;
165 zptr = const_cast<dmatrix &>(zx).get_address(0, 0);
166 dptr = tempmat.get_address(0, 0);
167 for (i = 0; i < lenary; ++i) {
168 (*dptr) = x * (*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(x1.columns(), x1.rows());
186 for (i = 0; i < x1.rows(); i++)
187 for (j = 0; j < x1.columns(); j++) {
188 temp(j, i) = x1(i, j);
189 }
190 return temp;
191}

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