OverSim
Mtx Class Reference

#include <yang.h>

Public Member Functions

 Mtx (int n, int m, double **)
 Mtx (int n, int m, double d=0)
 Mtx (const Mtx &)
 ~Mtx ()
Mtxoperator= (const Mtx &)
Mtxoperator+= (const Mtx &)
Mtxoperator-= (const Mtx &)
Vtr operator* (const Vtr &) const
double * operator[] (int i) const
Mtxoperator+ ()
Mtx operator+ (const Mtx &)
int rows () const
int cols () const
void getcol (int i, Vtr &vec) const
void setcol (int i, const Vtr &vec)
void clear ()
int transpose (Mtx &dest) const
Mtx operator* (const Mtx &) const
const double & operator() (int i, int j) const
double & operator() (int i, int j)
int QRdecomp (Mtx &Q, Mtx &R)
int QRdecomp_slow (Mtx &Q, Mtx &R)

Private Attributes

int nrows
int ncols
double ** ets

Friends

Mtx operator- (const Mtx &)
Mtx operator- (const Mtx &, const Mtx &)
Vtr operator* (const Vtr &, const Mtx &)
std::ostream & operator<< (std::ostream &, const Mtx &)

Detailed Description

Definition at line 48 of file yang.h.

Constructor & Destructor Documentation

Mtx::Mtx ( int  n,
int  m,
double **  dbp 
)

Definition at line 137 of file yang.cc.

{ // construct from double pointer
nrows = n;
ncols = m;
ets = new double* [nrows];
for (int i = 0; i < nrows; i++) {
ets[i] = new double [ncols];
for (int j = 0; j < ncols; j++) ets[i][j] = dbp[i][j];
}
}
Mtx::Mtx ( int  n,
int  m,
double  d = 0 
)

Definition at line 147 of file yang.cc.

{ // construct from a double
ets = new double* [nrows = n];
for (int i = 0; i< nrows; i++) {
ets[i] = new double [ncols = m];
for (int j = 0; j < ncols; j++) ets[i][j] = a;
}
}
Mtx::Mtx ( const Mtx mat)

Definition at line 155 of file yang.cc.

{ // copy constructor
ets = new double* [nrows = mat.nrows];
for (int i = 0; i< nrows; i++) {
ets[i] = new double [ncols = mat.ncols];
for (int j = 0; j < ncols; j++) ets[i][j] = mat[i][j];
}
}
Mtx::~Mtx ( )

Definition at line 163 of file yang.cc.

{ // destructor
for (int i = 0; i< nrows; i++) delete[] ets[i];
delete[] ets;
}

Member Function Documentation

void Mtx::clear ( )

Definition at line 251 of file yang.cc.

Referenced by QRdecomp(), and QRdecomp_slow().

{
for (int i = 0; i < nrows; i++)
for (int j = 0; j < ncols; j++)
ets[i][j] = 0;
}
int Mtx::cols ( ) const
inline

Definition at line 74 of file yang.h.

Referenced by operator<<(), and transpose().

{return ncols;}
void Mtx::getcol ( int  i,
Vtr vec 
) const

Definition at line 235 of file yang.cc.

{
if (vec.size() != nrows)
error("Mtx::getcol(): Bad vector size.");
for (int i = 0; i < nrows; i++)
vec[i] = ets[i][n];
}
const double& Mtx::operator() ( int  i,
int  j 
) const
inline

Definition at line 81 of file yang.h.

{return ets[i][j];}
double& Mtx::operator() ( int  i,
int  j 
)
inline

Definition at line 82 of file yang.h.

{return ets[i][j];}
Vtr Mtx::operator* ( const Vtr v) const

Definition at line 205 of file yang.cc.

{ // matrix-vector multiply
if (ncols != v.size())
error("Mtx::op*(Vtr): Error: Mat. and vec. sizes do not match.");
Vtr tm(nrows);
for (int i = 0; i < nrows; i++)
for (int j = 0; j < ncols; j++) tm[i] += ets[i][j]*v[j];
return tm;
}
Mtx Mtx::operator* ( const Mtx mat) const

Definition at line 287 of file yang.cc.

{
Mtx tmp(nrows, mat.ncols);
if (ncols != mat.nrows)
error("Mtx::op*=: Bad matrix sizes.");
for (int i = 0; i < nrows; i++)
for (int j = 0; j < mat.ncols; j++)
for (int k = 0; k < ncols; k++)
tmp(i,j) += ets[i][k] * mat.ets[k][j];
return tmp;
}
Mtx & Mtx::operator+ ( )

Definition at line 191 of file yang.cc.

{ // usage: mat1 = + mat2;
return *this;
}
Mtx Mtx::operator+ ( const Mtx mat)

Definition at line 199 of file yang.cc.

{ // usage: m = m1 + m2
Mtx sum = *this; // user-defined copy constructor
sum += mat; // is important here
return sum; // otherwise m1 would be changed
}
Mtx & Mtx::operator+= ( const Mtx mat)

Definition at line 177 of file yang.cc.

{ // add-assign
if (nrows != mat.nrows || ncols != mat.ncols) error("bad matrix sizes");
for (int i = 0; i < nrows; i++)
for (int j = 0; j < ncols; j++) ets[i][j] += mat[i][j];
return *this;
}
Mtx & Mtx::operator-= ( const Mtx mat)

Definition at line 184 of file yang.cc.

{ // subtract-assign
if (nrows != mat.nrows || ncols != mat.ncols) error("bad matrix sizes");
for (int i = 0; i < nrows; i++)
for (int j = 0; j < ncols; j++) ets[i][j] -= mat[i][j];
return *this;
}
Mtx & Mtx::operator= ( const Mtx mat)

Definition at line 168 of file yang.cc.

{ // copy assignment
if (this != &mat) {
if (nrows != mat.nrows || ncols != mat.ncols) error("bad matrix sizes");
for (int i = 0; i < nrows; i++)
for (int j = 0; j < ncols; j++) ets[i][j] = mat[i][j];
}
return *this;
}
double* Mtx::operator[] ( int  i) const
inline

Definition at line 63 of file yang.h.

{ return ets[i]; } // subscripting, row i
int Mtx::QRdecomp ( Mtx Q,
Mtx R 
)

Definition at line 302 of file yang.cc.

{
if ((Q.nrows != nrows) || (Q.ncols != ncols) ||
(R.nrows != ncols) || (R.ncols != ncols)) {
std::cerr << "Mtx::QRdecomp(): Error: Bad matrix size.";
return -1;
}
double** tmp;
double norm, dot;
// tmp = A transpose
int tmprows = ncols;
int tmpcols = nrows;
tmp = new double* [tmprows];
for (int i=0; i<tmprows; i++) {
tmp[i] = new double [tmpcols];
for (int j=0; j<tmpcols; j++)
tmp[i][j] = ets[j][i];
}
Mat_DP QT(tmprows,tmpcols);
R.clear();
for (int i=0; i<tmprows; i++) {
norm = 0;
for (int k=0; k<tmpcols; k++)
norm += tmp[i][k]*tmp[i][k];
norm = std::sqrt(norm);
R.ets[i][i] = norm;
for (int k=0; k<tmpcols; k++)
QT.ets[i][k] = tmp[i][k] / norm;
for (int j=i+1; j<tmprows; j++) {
dot = 0;
for (int k=0; k<tmpcols; k++)
dot += QT.ets[i][k]*tmp[j][k];
R.ets[i][j] = dot;
for (int k=0; k<tmpcols; k++)
tmp[j][k] = tmp[j][k] - dot*QT.ets[i][k];
}
}
QT.transpose(Q);
for (int i=0; i<tmprows; i++)
delete[] tmp[i];
return 0;
}
int Mtx::QRdecomp_slow ( Mtx Q,
Mtx R 
)

Definition at line 352 of file yang.cc.

{
if ((Q.nrows != nrows) || (Q.ncols != ncols) ||
(R.nrows != ncols) || (R.ncols != ncols)) {
std::cerr << "Mtx::QRdecomp(): Error: Bad matrix size.";
return -1;
}
double** tmp;
double norm, dot;
tmp = new double* [nrows];
for (int i=0; i<nrows; i++) {
tmp[i] = new double [ncols];
for (int j=0; j<ncols; j++)
tmp[i][j] = ets[i][j];
}
R.clear();
for (int i=0; i<ncols; i++) {
norm = 0;
for (int k=0; k<nrows; k++)
norm += tmp[k][i]*tmp[k][i];
norm = std::sqrt(norm);
R.ets[i][i] = norm;
for (int k=0; k<nrows; k++)
Q.ets[k][i] = tmp[k][i] / norm;
for (int j=i+1; j<ncols; j++) {
dot = 0;
for (int k=0; k<nrows; k++)
dot += Q.ets[k][i]*tmp[k][j];
R.ets[i][j] = dot;
for (int k=0; k<nrows; k++)
tmp[k][j] = tmp[k][j] - dot*Q.ets[k][i];
}
}
for (int i=0; i<nrows; i++)
delete[] tmp[i];
return 0;
}
int Mtx::rows ( ) const
inline

Definition at line 73 of file yang.h.

Referenced by operator<<(), and transpose().

{return nrows;}
void Mtx::setcol ( int  i,
const Vtr vec 
)

Definition at line 243 of file yang.cc.

{
if (vec.size() != nrows)
error("Mtx::getcol(): Bad vector size.");
for (int i = 0; i < nrows; i++)
ets[i][n] = vec[i];
}
int Mtx::transpose ( Mtx dest) const

Definition at line 258 of file yang.cc.

Referenced by QRdecomp().

{
if ((nrows != dest.cols()) || (ncols != dest.rows())) {
std::cerr << "Mtx::transpose(): Error: Matrix dim."
<< std::endl;
return -1;
}
for (int i=0; i<nrows; i++)
for (int j=0; j<ncols; j++)
dest(j, i) = ets[i][j];
return 0;
}

Friends And Related Function Documentation

Vtr operator* ( const Vtr v,
const Mtx mat 
)
friend

Definition at line 214 of file yang.cc.

{
if (v.lenth != mat.nrows)
error("op*(Vtr, Mtx): Error: Mat. and vec. size do no match.");
Vtr res(mat.ncols, 0.0);
for (int i=0; i<mat.ncols; i++)
for (int j=0; j<v.lenth; j++)
res[i] = v.ets[j] * mat.ets[j][i];
return res;
}
Mtx operator- ( const Mtx mat)
friend

Definition at line 195 of file yang.cc.

{ // usage: mat1 = - mat2;
return Mtx(mat.nrows,mat.ncols) - mat;
}
Mtx operator- ( const Mtx m1,
const Mtx m2 
)
friend

Definition at line 226 of file yang.cc.

{ // matrix subtract
if(m1.nrows !=m2.nrows || m1.ncols !=m2.ncols)
error("bad matrix sizes");
Mtx sum = m1;
sum -= m2;
return sum;
}
std::ostream& operator<< ( std::ostream &  s,
const Mtx mat 
)
friend

Definition at line 272 of file yang.cc.

{
for (int i = 0; i < mat.rows(); i++) {
s << "| ";
for(int j = 0; j < mat.cols(); j++) {
s.setf(std::ios_base::fixed, std::ios_base::floatfield);
s.precision(4);
s.width(8);
s << mat.ets[i][j];
}
s << " |" << std::endl;
}
return s;
}

Member Data Documentation

int Mtx::ncols
private
int Mtx::nrows
private

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