version  0.0.1
Defines the C++ API for MsPASS
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. More...
 
dmatrixoperator-= (const dmatrix &other)
 Subtract one matrix to another. More...
 
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. More...
 
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. More...
 
dmatrix operator* (const double &s, const dmatrix &A) noexcept
 Scale a matrix by a constant. More...
 
dmatrix tr (const dmatrix &A) noexcept
 Transpose a matrix. More...
 
std::ostream & operator<< (std::ostream &os, dmatrix &A)
 Text output operator. More...
 

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.

9 {
10  nrr=0;
11  ncc=0;
12  length=0;
13  ary.reserve(0);
14 }

◆ 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.
16 {
17  nrr=nr;
18  ncc=nc;
19  length=nr*nc;
20  if(length<1)
21  {
22  length=1;
23  nrr=ncc=0;
24  }
25  /* This std::vector method allocates space so zero method can just
26  * use indexing. */
27  ary.resize(length);
28  this->zero();
29 }
void zero()
Definition: dmatrix.cc:215

◆ dmatrix() [3/3]

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

Standard copy constructor.

32  {
33  nrr=other.nrr;
34  ncc=other.ncc;
35  length=other.length;
36  ary=other.ary;
37  }

◆ ~dmatrix()

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

Destructor - releases any matrix memory.

40 {
41 //if(ary!=NULL) delete [] ary;
42 }

Member Function Documentation

◆ columns()

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

Return number of columns in this matrix.

233 {
234  return(ncc);
235 }

◆ operator()()

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
45 {
46  int out_of_range=0;
47  if (rowindex>=nrr) out_of_range=1;
48  if (rowindex<0) out_of_range=1;
49  if (colindex>=ncc) out_of_range=1;
50  if (colindex<0) out_of_range=1;
51  if (out_of_range)
52  throw dmatrix_index_error(nrr,ncc,rowindex,colindex);
53  double result=ary[rowindex+(nrr)*(colindex)];
54  return result;
55 }

◆ 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.
122 {
123  try{
124  dmatrix result(*this);
125  result += other;
126  return result;
127  }catch(...){throw;};
128 }
dmatrix()
Definition: dmatrix.cc:8

◆ 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.
102 {
103  size_t i;
104  if ((nrr!=other.nrr)||(length!=other.length))
105  throw dmatrix_size_error(nrr, ncc, other.nrr, other.length);
106  for(i=0;i<length;i++)
107  ary[i]+=other.ary[i];
108  return *this;
109 }

◆ 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(...){throw;};
136 }

◆ 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

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

◆ rows()

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

Return number of rows in this matrix.

229 {
230  return(nrr);
231 }

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

220 {
221  vector<size_t> sz;
222  sz.push_back(nrr);
223  sz.push_back(ncc);
224  return(sz);
225 }

◆ zero()

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

Initialize a matrix to all zeros.

216 {
217  for(size_t i=0;i<length;++i) ary[i]=0.0;
218 }

Friends And Related Function 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(),
145  b.rows(), 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  {
150  double *x1ptr,*bptr;
151  x1ptr=const_cast<dmatrix&>(x1).get_address(i,0);
152  bptr=const_cast<dmatrix&>(b).get_address(0,j);
153  /* This temporary seems necessary */
154  double *dptr;
155  dptr=prod.get_address(i,j);
156  *dptr=ddot(x1.columns(),x1ptr,x1.rows(),bptr,1);
157  }
158  return prod;
159 }

◆ 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
162 {
163  size_t i;
164  dmatrix tempmat(zx.rows(),zx.columns());
165  size_t lenary=zx.rows()*zx.columns();
166  double *zptr,*dptr;
167  zptr=const_cast<dmatrix&>(zx).get_address(0,0);
168  dptr=tempmat.get_address(0,0);
169  for(i=0;i<lenary;++i)
170  {
171  (*dptr)=x*(*zptr);
172  ++dptr;
173  ++zptr;
174  }
175  return tempmat;
176 }

◆ 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
191 {
192  size_t i,j;
193  dmatrix temp(x1.columns(),x1.rows());
194  for(i=0; i<x1.rows(); i++)
195  for(j=0; j<x1.columns();j++)
196  {
197  temp(j,i)=x1(i,j);
198  }
199  return temp;
200 }

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