Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Public Types | Public Member Functions | Friends | List of all members
JMATH::JMatrixND Class Reference

N x N matrix. More...

#include <JMatrixND.hh>

Inheritance diagram for JMATH::JMatrixND:
JMATH::JMatrixND_t JMATH::JMath< JMatrixND > JLANG::JEquals< JFirst_t, JSecond_t > std::vector< std::vector< double > > JMATH::JMatrixNS JFIT::JMatrixNZ

Classes

struct  JMatrixHelper
 Auxiliary class for matrix decomposition. More...
 

Public Types

typedef std::vector
< std::vector< double > > 
matrix_type
 
typedef matrix_type::const_iterator const_row_type
 
typedef std::vector< double >
::const_iterator 
const_col_type
 
typedef matrix_type::iterator row_type
 
typedef std::vector< double >
::iterator 
col_type
 

Public Member Functions

 JMatrixND ()
 Default constructor. More...
 
 JMatrixND (const size_t size)
 Constructor. More...
 
void resize (const size_t size)
 Resize matrix. More...
 
double at (size_t row, size_t col) const
 Get matrix element. More...
 
double & at (size_t row, size_t col)
 Get matrix element. More...
 
double operator() (size_t row, size_t col) const
 Get matrix element. More...
 
double & operator() (size_t row, size_t col)
 Get matrix element. More...
 
JMatrixND & reset ()
 Set matrix to the null matrix. More...
 
JMatrixND & setIdentity ()
 Set to identity matrix. More...
 
JMatrixND & transpose ()
 Transpose. More...
 
JMatrixND & negate ()
 Negate matrix. More...
 
JMatrixND & add (const JMatrixND &A)
 Matrix addition. More...
 
JMatrixND & sub (const JMatrixND &A)
 Matrix subtraction. More...
 
JMatrixND & mul (const double factor)
 Scale matrix. More...
 
JMatrixND & div (const double factor)
 Scale matrix. More...
 
JMatrixND & mul (const JMatrixND &A, const JMatrixND &B)
 Matrix multiplication. More...
 
bool equals (const JMatrixND &A, const double eps=std::numeric_limits< double >::min()) const
 Equality. More...
 
bool isIdentity (const double eps=std::numeric_limits< double >::min()) const
 Test identity. More...
 
double getDeterminant () const
 Get determinant of matrix. More...
 
bool isPositiveDefinite () const
 Test positive definiteness. More...
 
const bool isPositiveSemiDefinite () const
 Test positive semi-definiteness. More...
 
JMatrixND & mul (const JNullType &object)
 Multiply with object. More...
 

Friends

JReader & operator>> (JReader &in, JMatrixND &matrix)
 Read matrix from input. More...
 
JWriter & operator<< (JWriter &out, const JMatrixND &matrix)
 Write matrix to output. More...
 
std::ostream & operator<< (std::ostream &out, const JMatrixND &A)
 Print ASCII formatted output. More...
 

Detailed Description

N x N matrix.

Definition at line 63 of file JMatrixND.hh.

Member Typedef Documentation

Definition at line 44 of file JMatrixND.hh.

typedef matrix_type::const_iterator JMATH::JMatrixND_t::const_row_type
inherited

Definition at line 46 of file JMatrixND.hh.

typedef std::vector<double>::const_iterator JMATH::JMatrixND_t::const_col_type
inherited

Definition at line 47 of file JMatrixND.hh.

typedef matrix_type::iterator JMATH::JMatrixND_t::row_type
inherited

Definition at line 49 of file JMatrixND.hh.

Definition at line 50 of file JMatrixND.hh.

Constructor & Destructor Documentation

JMATH::JMatrixND::JMatrixND ( )
inline

Default constructor.

Definition at line 76 of file JMatrixND.hh.

77  {}
JMATH::JMatrixND::JMatrixND ( const size_t  size)
inline

Constructor.

Parameters
sizedimension

Definition at line 85 of file JMatrixND.hh.

86  {
87  resize(size);
88  reset();
89  }
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:98
JMatrixND & reset()
Set matrix to the null matrix.
Definition: JMatrixND.hh:170

Member Function Documentation

void JMATH::JMatrixND::resize ( const size_t  size)
inline

Resize matrix.

Note that this method resets the matrix.

Parameters
sizedimension

Definition at line 98 of file JMatrixND.hh.

99  {
100  matrix_type::resize(size);
101 
102  for (row_type row = this->begin(); row != this->end(); ++row) {
103 
104  row->resize(size);
105 
106  for (col_type col = row->begin(); col != row->end(); ++col) {
107  *col = 0.0;
108  }
109  }
110  }
std::vector< double >::iterator col_type
Definition: JMatrixND.hh:50
matrix_type::iterator row_type
Definition: JMatrixND.hh:49
double JMATH::JMatrixND::at ( size_t  row,
size_t  col 
) const
inline

Get matrix element.

Parameters
rowrow number
colcolumn number
Returns
matrix element at (row,col)

Definition at line 120 of file JMatrixND.hh.

121  {
122  return (*this)[row][col];
123  }
double& JMATH::JMatrixND::at ( size_t  row,
size_t  col 
)
inline

Get matrix element.

Parameters
rowrow number
colcolumn number
Returns
matrix element at (row,col)

Definition at line 133 of file JMatrixND.hh.

134  {
135  return (*this)[row][col];
136  }
double JMATH::JMatrixND::operator() ( size_t  row,
size_t  col 
) const
inline

Get matrix element.

Parameters
rowrow number
colcolumn number
Returns
matrix element at (row,col)

Definition at line 146 of file JMatrixND.hh.

147  {
148  return at(row, col);
149  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
double& JMATH::JMatrixND::operator() ( size_t  row,
size_t  col 
)
inline

Get matrix element.

Parameters
rowrow number
colcolumn number
Returns
matrix element at (row,col)

Definition at line 159 of file JMatrixND.hh.

160  {
161  return at(row, col);
162  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
JMatrixND& JMATH::JMatrixND::reset ( )
inline

Set matrix to the null matrix.

Returns
this matrix

Definition at line 170 of file JMatrixND.hh.

171  {
172  for (row_type row = this->begin(); row != this->end(); ++row) {
173  for (col_type col = row->begin(); col != row->end(); ++col) {
174  *col = 0.0;
175  }
176  }
177 
178  return *this;
179  }
std::vector< double >::iterator col_type
Definition: JMatrixND.hh:50
matrix_type::iterator row_type
Definition: JMatrixND.hh:49
JMatrixND& JMATH::JMatrixND::setIdentity ( )
inline

Set to identity matrix.

Returns
this matrix

Definition at line 187 of file JMatrixND.hh.

188  {
189  for (size_t i = 0; i != this->size(); ++i) {
190 
191  at(i,i) = 1.0;
192 
193  for (size_t j = 0; j != i; ++j) {
194  at(i,j) = at(j,i) = 0.0;
195  }
196  }
197 
198  return *this;
199  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
JMatrixND& JMATH::JMatrixND::transpose ( )
inline

Transpose.

Returns
this matrix

Definition at line 207 of file JMatrixND.hh.

208  {
209  for (size_t i = 0; i != this->size(); ++i) {
210  for (size_t j = 0; j != i; ++j) {
211  std::swap(at(i,j), at(j,i));
212  }
213  }
214 
215  return *this;
216  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
JMatrixND& JMATH::JMatrixND::negate ( )
inline

Negate matrix.

Returns
this matrix

Definition at line 224 of file JMatrixND.hh.

225  {
226  for (row_type row = this->begin(); row != this->end(); ++row) {
227  for (col_type col = row->begin(); col != row->end(); ++col) {
228  *col = -*col;
229  }
230  }
231 
232  return *this;
233  }
std::vector< double >::iterator col_type
Definition: JMatrixND.hh:50
matrix_type::iterator row_type
Definition: JMatrixND.hh:49
JMatrixND& JMATH::JMatrixND::add ( const JMatrixND &  A)
inline

Matrix addition.

Parameters
Amatrix
Returns
this matrix

Definition at line 242 of file JMatrixND.hh.

243  {
244  if (this->size() == A.size()) {
245 
246  for (size_t i = 0; i != this->size(); ++i) {
247  for (size_t j = 0; j != this->size(); ++j) {
248  at(i,j) += A.at(i,j);
249  }
250  }
251 
252  } else {
253  THROW(JException, "JMatrixND::add() inconsistent matrix dimensions " << this->size() << ' ' << A.size());
254  }
255 
256  return *this;
257  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
JMatrixND& JMATH::JMatrixND::sub ( const JMatrixND &  A)
inline

Matrix subtraction.

Parameters
Amatrix
Returns
this matrix

Definition at line 266 of file JMatrixND.hh.

267  {
268  if (this->size() == A.size()) {
269 
270  for (size_t i = 0; i != this->size(); ++i) {
271  for (size_t j = 0; j != this->size(); ++j) {
272  at(i,j) -= A.at(i,j);
273  }
274  }
275 
276  } else {
277  THROW(JException, "JMatrixND::sub() inconsistent matrix dimensions " << this->size() << ' ' << A.size());
278  }
279 
280  return *this;
281  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
JMatrixND& JMATH::JMatrixND::mul ( const double  factor)
inline

Scale matrix.

Parameters
factorfactor
Returns
this matrix

Definition at line 290 of file JMatrixND.hh.

291  {
292  for (row_type row = this->begin(); row != this->end(); ++row) {
293  for (col_type col = row->begin(); col != row->end(); ++col) {
294  *col *= factor;
295  }
296  }
297 
298  return *this;
299  }
std::vector< double >::iterator col_type
Definition: JMatrixND.hh:50
matrix_type::iterator row_type
Definition: JMatrixND.hh:49
JMatrixND& JMATH::JMatrixND::div ( const double  factor)
inline

Scale matrix.

Parameters
factorfactor
Returns
this matrix

Definition at line 308 of file JMatrixND.hh.

309  {
310  for (row_type row = this->begin(); row != this->end(); ++row) {
311  for (col_type col = row->begin(); col != row->end(); ++col) {
312  *col /= factor;
313  }
314  }
315 
316  return *this;
317  }
std::vector< double >::iterator col_type
Definition: JMatrixND.hh:50
matrix_type::iterator row_type
Definition: JMatrixND.hh:49
JMatrixND& JMATH::JMatrixND::mul ( const JMatrixND &  A,
const JMatrixND &  B 
)
inline

Matrix multiplication.

Parameters
Amatrix
Bmatrix
Returns
this matrix

Definition at line 327 of file JMatrixND.hh.

329  {
330  if (A.size() == B.size()) {
331 
332  this->resize(A.size());
333 
334  for (size_t i = 0; i != A.size(); ++i) {
335  for (size_t j = 0; j != A.size(); ++j) {
336 
337  this->at(i,j) = 0.0;
338 
339  for (size_t k = 0; k != A.size(); ++k) {
340  this->at(i,j) += A[i][k] * B[k][j];
341  }
342  }
343  }
344 
345  } else {
346 
347  THROW(JException, "JMatrixND::mul() inconsistent matrix dimensions " << A.size() << ' ' << B.size());
348  }
349 
350  return *this;
351  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:98
bool JMATH::JMatrixND::equals ( const JMatrixND &  A,
const double  eps = std::numeric_limits<double>::min() 
) const
inline

Equality.

Parameters
Amatrix
epsnumerical precision
Returns
true if matrices identical; else false

Definition at line 361 of file JMatrixND.hh.

363  {
364  if (this->size() == A.size()) {
365 
366  for (const_row_type row1 = this->begin(), row2 = A.begin(); row1 != this->end(); ++row1, ++row2) {
367  for (const_col_type col1 = row1->begin(), col2 = row2->begin(); col1 != row1->end(); ++col1, ++col2) {
368  if (fabs(*col1 - *col2) > eps) {
369  return false;
370  }
371  }
372  }
373 
374  return true;
375 
376  } else {
377  THROW(JException, "JMatrixND::equals() inconsistent matrix dimensions " << this->size() << ' ' << A.size());
378  }
379  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
matrix_type::const_iterator const_row_type
Definition: JMatrixND.hh:46
std::vector< double >::const_iterator const_col_type
Definition: JMatrixND.hh:47
bool JMATH::JMatrixND::isIdentity ( const double  eps = std::numeric_limits<double>::min()) const
inline

Test identity.

Parameters
epsnumerical precision
Returns
true if identity matrix; else false

Definition at line 388 of file JMatrixND.hh.

389  {
390  for (size_t i = 0; i != this->size(); ++i) {
391 
392  if (fabs(1.0 - at(i,i)) > eps) {
393  return false;
394  };
395 
396  for (size_t j = 0; j != i; ++j) {
397  if (fabs(at(i,j)) > eps || fabs(at(j,i)) > eps) {
398  return false;
399  };
400  }
401  }
402 
403  return true;
404  }
double at(size_t row, size_t col) const
Get matrix element.
Definition: JMatrixND.hh:120
double JMATH::JMatrixND::getDeterminant ( ) const
inline

Get determinant of matrix.

Returns
determinant of matrix

Definition at line 412 of file JMatrixND.hh.

413  {
414  JMatrixHelper& A = JMatrixHelper::getInstance();
415 
416  A.decompose(*this);
417 
418  double det = 1.0;
419 
420  for (size_t i = 0; i != A.size(); ++i) {
421  det *= A[i][i];
422  }
423 
424  return det * A.getSign();
425  }
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
bool JMATH::JMatrixND::isPositiveDefinite ( ) const
inline

Test positive definiteness.

Returns
true if positive definite; else false

Definition at line 433 of file JMatrixND.hh.

434  {
435  JMatrixHelper& A = JMatrixHelper::getInstance();
436 
437  A.decompose(*this);
438 
439  for (size_t i = 0; i != A.size(); ++i) {
440  if (A[i][i] <= 0.0) {
441  return false;
442  }
443  }
444 
445  return true;
446  }
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
const bool JMATH::JMatrixND::isPositiveSemiDefinite ( ) const
inline

Test positive semi-definiteness.

Returns
true if positive semi-definite; else false

Definition at line 454 of file JMatrixND.hh.

455  {
456  JMatrixHelper& A = JMatrixHelper::getInstance();
457 
458  A.decompose(*this);
459 
460  for (size_t i = 0; i != A.size(); ++i) {
461  if (A[i][i] < 0.0) {
462  return false;
463  }
464  }
465 
466  return true;
467  }
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
JMatrixND & JMATH::JMath< JMatrixND , JNullType >::mul ( const JNullType &  object)
inlineinherited

Multiply with object.

Parameters
objectobject
Returns
result object

Definition at line 273 of file JMath.hh.

274  {
275  return static_cast<JFirst_t&>(*this) = JCalculator<JFirst_t>::calculator.mul(static_cast<const JFirst_t&>(*this), object);
276  }
Auxiliary class for product evaluation of objects.
Definition: JCalculator.hh:18

Friends And Related Function Documentation

JReader& operator>> ( JReader &  in,
JMatrixND &  matrix 
)
friend

Read matrix from input.

Parameters
inreader
matrixmatrix
Returns
reader

Definition at line 477 of file JMatrixND.hh.

478  {
479  int size;
480 
481  in >> size;
482 
483  matrix.resize(size);
484 
485  for (row_type row = matrix.begin(); row != matrix.end(); ++row) {
486  for (col_type col = row->begin(); col != row->end(); ++col) {
487  in >> *col;
488  }
489  }
490 
491  return in;
492  }
std::vector< double >::iterator col_type
Definition: JMatrixND.hh:50
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:98
matrix_type::iterator row_type
Definition: JMatrixND.hh:49
JWriter& operator<< ( JWriter &  out,
const JMatrixND &  matrix 
)
friend

Write matrix to output.

Parameters
outwriter
matrixmatrix
Returns
writer

Definition at line 502 of file JMatrixND.hh.

503  {
504  int size = matrix.size();
505 
506  out << size;
507 
508  for (const_row_type row = matrix.begin(); row != matrix.end(); ++row) {
509  for (const_col_type col = row->begin(); col != row->end(); ++col) {
510  out << *col;
511  }
512  }
513 
514  return out;
515  }
matrix_type::const_iterator const_row_type
Definition: JMatrixND.hh:46
std::vector< double >::const_iterator const_col_type
Definition: JMatrixND.hh:47
std::ostream& operator<< ( std::ostream &  out,
const JMatrixND &  A 
)
friend

Print ASCII formatted output.

Parameters
outoutput stream
Amatrix
Returns
output stream

Definition at line 525 of file JMatrixND.hh.

526  {
527  using namespace std;
528 
529  JFlags flags(out);
530 
531  for (const_row_type row = A.begin(); row != A.end(); ++row) {
532 
533  for (const_col_type col = row->begin(); col != row->end(); ++col) {
534  out << FIXED(10,2) << *col << ' ';
535  }
536 
537  out << endl;
538  }
539 
540  return out;
541  }
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:461
matrix_type::const_iterator const_row_type
Definition: JMatrixND.hh:46
std::vector< double >::const_iterator const_col_type
Definition: JMatrixND.hh:47
Auxiliary class to temporarily modify format specifications.
Definition: JPrint.hh:535

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