Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Public Member Functions | Public Attributes | List of all members
kome::core::Matrix Class Reference

matrix class More...

#include <Matrix.h>

Public Member Functions

 Matrix (const unsigned int row, const unsigned int col)
 constructor More...
 
 Matrix (const Matrix &mat)
 copy constructor More...
 
virtual ~Matrix ()
 destructor
 
unsigned int getRowSize ()
 gets row size of matrix More...
 
unsigned int getColSize ()
 gets col size of matrix More...
 
double det ()
 calculates determinant. (This matrix must be a square matrix.) More...
 
double cofactor (const unsigned int row, const unsigned int col)
 calculates cofactor (This matrix must be a square matrix.) More...
 
Matrix invert ()
 gets inverse matrix (This matrix must be a square matrix.) More...
 
Vector multiply (Vector &v)
 multiplies vector More...
 
Matrix operator= (const Matrix &m)
 copy matrix More...
 
double & operator() (const unsigned int row, const unsigned int col)
 gets reference of value of specified element More...
 
Matrixoperator+= (const Matrix &m)
 matrix addition More...
 
Matrixoperator-= (const Matrix &m)
 matrix subtraction More...
 
Matrixoperator*= (const double k)
 scalar multiplication More...
 
Matrixoperator/= (const double k)
 scalar division More...
 
Matrix operator+ (const Matrix &m)
 matrix addition More...
 
Matrix operator- (const Matrix &m)
 matrix subtraction More...
 
Matrix operator* (const Matrix &m)
 matrix multiplication More...
 
Matrix operator* (const double k)
 scalar multiplication More...
 
Matrix operator/ (const double k)
 scalar division More...
 

Public Attributes

const unsigned int m_row
 
const unsigned int m_col
 
double *const m_array
 

Detailed Description

matrix class

Definition at line 25 of file Matrix.h.

Constructor & Destructor Documentation

kome::core::Matrix::Matrix ( const unsigned int  row,
const unsigned int  col 
)

constructor

Parameters
[in]rowlength of row
[in]collength of col

Definition at line 29 of file Matrix.cpp.

30  : m_row( row ), m_col( col ),
31  m_array( ( row == 0 || col == 0 ) ? NULL : new double[ row * col ] ) {
32  if( m_array != NULL ) {
33  fillzero( m_array, sizeof( double ) * m_row * m_col );
34  }
35 }
double *const m_array
Definition: Matrix.h:54
#define NULL
Definition: CoreMacros.h:18
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
void fillzero(void *p, size_t n)
This function fills a block of memory zeros.

Here is the call graph for this function:

kome::core::Matrix::Matrix ( const Matrix mat)

copy constructor

Parameters
[in]matMatrix object to be copied

Definition at line 38 of file Matrix.cpp.

39  : m_row( mat.m_row ), m_col( mat.m_col ),
40  m_array( mat.m_array == NULL ? NULL : new double[ mat.m_row * mat.m_col ] ) {
41  if( m_array != NULL ) {
42  memcpy( m_array, mat.m_array, sizeof( double ) * mat.m_row * mat.m_col );
43  }
44 }
double *const m_array
Definition: Matrix.h:54
#define NULL
Definition: CoreMacros.h:18
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50

Member Function Documentation

double kome::core::Matrix::cofactor ( const unsigned int  row,
const unsigned int  col 
)

calculates cofactor (This matrix must be a square matrix.)

Parameters
[in]rowrow
[in]colcol
Returns
cofactor

Definition at line 86 of file Matrix.cpp.

86  {
87  // check size
88  if( m_row != m_col ) {
89  throw FMT( "This is not a square matrix." );
90  }
91  if( m_row <= 1 ) {
92  throw FMT( "Matrix size is illegal." );
93  }
94 
95  // check parameter
96  if( row >= m_row || col >= m_col ) {
97  throw FMT( "The specified element does not exist. [row=%d, col=%d]", row, col );
98  }
99 
100  // create matrix to calculate determinant
101  Matrix m( m_row - 1 , m_col - 1 );
102  for( unsigned int i = 0; i < m_row - 1; i++ ) {
103  int r = ( i < row ) ? i : i + 1;
104  for( unsigned int j = 0; j < m_col - 1; j++ ) {
105  int c = ( j < col ) ? j : j + 1;
106 
107  m( i, j ) = operator()( r, c );
108  }
109  }
110 
111  // calculate cofactor
112  double d = m.det();
113  if( ( row + col ) % 2 != 0 ) {
114  d = - d;
115  }
116 
117  return d;
118 }
double & operator()(const unsigned int row, const unsigned int col)
gets reference of value of specified element
Definition: Matrix.cpp:187
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
matrix class
Definition: Matrix.h:25

Here is the call graph for this function:

double kome::core::Matrix::det ( )

calculates determinant. (This matrix must be a square matrix.)

Returns
determinant

Definition at line 64 of file Matrix.cpp.

64  {
65  // check size
66  if( m_row != m_col ) {
67  throw FMT( "This is not a square matrix." );
68  }
69  if( m_row == 0 ) {
70  throw FMT( "Matrix size is illegal." );
71  }
72  if( m_row == 1 ) {
73  return m_array[ 0 ];
74  }
75 
76  // calculate
77  double d = 0.0;
78  for( unsigned int i = 0; i < m_col; i++ ) {
79  d += operator()( 0, i ) * cofactor( 0, i );
80  }
81 
82  return d;
83 }
double cofactor(const unsigned int row, const unsigned int col)
calculates cofactor (This matrix must be a square matrix.)
Definition: Matrix.cpp:86
double & operator()(const unsigned int row, const unsigned int col)
gets reference of value of specified element
Definition: Matrix.cpp:187
double *const m_array
Definition: Matrix.h:54
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50

Here is the call graph for this function:

unsigned int kome::core::Matrix::getColSize ( )

gets col size of matrix

Returns
col size of matrix

Definition at line 59 of file Matrix.cpp.

59  {
60  return m_col;
61 }
const unsigned int m_col
Definition: Matrix.h:52
unsigned int kome::core::Matrix::getRowSize ( )

gets row size of matrix

Returns
row size of matrix

Definition at line 54 of file Matrix.cpp.

54  {
55  return m_row;
56 }
const unsigned int m_row
Definition: Matrix.h:50
Matrix kome::core::Matrix::invert ( )

gets inverse matrix (This matrix must be a square matrix.)

Returns
inverse matrix

Definition at line 121 of file Matrix.cpp.

121  {
122  // check size
123  if( m_row != m_col ) {
124  throw FMT( "This is not a square matrix." );
125  }
126  if( m_row == 0 ) {
127  throw FMT( "Matrix size is illegal." );
128  }
129 
130  // calculate determinant
131  double d = det();
132  if( d == 0.0 ) {
133  throw FMT( "There isn't an inverse matrix." );
134  }
135 
136  // create inverse matrix
137  Matrix m( m_row, m_col );
138  if( m_row == 1 ) {
139  m( 0, 0 ) = 1.0 / d;
140  }
141  else {
142  for( unsigned int i = 0; i < m_row; i++ ) {
143  for( unsigned int j = 0; j < m_col; j++ ) {
144  m( i, j ) = cofactor( j, i ) / d;
145  }
146  }
147  }
148 
149  return m;
150 }
double cofactor(const unsigned int row, const unsigned int col)
calculates cofactor (This matrix must be a square matrix.)
Definition: Matrix.cpp:86
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
double det()
calculates determinant. (This matrix must be a square matrix.)
Definition: Matrix.cpp:64
matrix class
Definition: Matrix.h:25

Here is the call graph for this function:

Vector kome::core::Matrix::multiply ( Vector v)

multiplies vector

Returns
multiplied vector

Definition at line 153 of file Matrix.cpp.

153  {
154  // check size
155  if( m_col != v.getDimention() ) {
156  throw FMT( "The size is illegal. [col=%d, dim=%d]", m_row, v.getDimention() );
157  }
158 
159  // multiply
160  Vector tmp( m_row );
161  for( unsigned int i = 0; i < m_row; i++ ) {
162  double elm = 0.0;
163  for( unsigned int j = 0; j < m_col; j++ ) {
164  elm += (*this)( i, j ) * v( j );
165  }
166 
167  tmp( i ) = elm;
168  }
169 
170  return tmp;
171 }
vector class
Definition: Vector.h:22
unsigned int getDimention()
gets the dimention size of vector
Definition: Vector.cpp:53
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50

Here is the call graph for this function:

double & kome::core::Matrix::operator() ( const unsigned int  row,
const unsigned int  col 
)

gets reference of value of specified element

Parameters
[in]rowrow
[in]colcol
Returns
reference of value of specified element

Definition at line 187 of file Matrix.cpp.

187  {
188  // check parameter
189  if( row >= m_row || col >= m_col ) {
190  throw FMT( "The specified element does not exist. [row=%d, col=%d]", row, col );
191  }
192 
193  return m_array[ row + col * m_row ];
194 }
double *const m_array
Definition: Matrix.h:54
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
Matrix kome::core::Matrix::operator* ( const Matrix m)

matrix multiplication

Parameters
[in]mmatrix to multiple
Returns
multiplication matrix

Definition at line 275 of file Matrix.cpp.

275  {
276  // check size
277  if( this->m_col != m.m_row ) {
278  throw FMT( "Matrix size is illegal." );
279  }
280 
281  // copy
282  Matrix tmp( this->m_row, m.m_col );
283 
284  // change size
285  unsigned int num = this->m_col;
286 
287  // multiplication
288  for( unsigned int i = 0; i < this->m_row; i++ ) {
289  for( unsigned int j = 0; j < m.m_col; j++ ) {
290  double val = 0.0;
291  for( unsigned int k = 0; k < num; k++ ) {
292  val += this->m_array[ i + k * this->m_row ] * m.m_array[ k + j * m.m_row ];
293  }
294  tmp( i, j ) = val;
295  }
296  }
297 
298  return tmp;
299 }
double *const m_array
Definition: Matrix.h:54
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
matrix class
Definition: Matrix.h:25
Matrix kome::core::Matrix::operator* ( const double  k)

scalar multiplication

Parameters
[in]kreal number to multiple
Returns
multiplication matrix

Definition at line 302 of file Matrix.cpp.

302  {
303  Matrix tmp = *this;
304  tmp *= k;
305 
306  return tmp;
307 }
matrix class
Definition: Matrix.h:25
Matrix & kome::core::Matrix::operator*= ( const double  k)

scalar multiplication

Parameters
[in]kreal number to multiple
Returns
multiplication matrix

Definition at line 233 of file Matrix.cpp.

233  {
234  // get the size of array
235  unsigned int size = m_row * m_col;
236 
237  // multiplication
238  for( unsigned int i = 0; i < size; i++ ) {
239  m_array[ i ] *= k;
240  }
241 
242  return *this;
243 }
double *const m_array
Definition: Matrix.h:54
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
Matrix kome::core::Matrix::operator+ ( const Matrix m)

matrix addition

Parameters
[in]mmatrix to add
Returns
addition matrix

Definition at line 259 of file Matrix.cpp.

259  {
260  Matrix tmp = *this;
261  tmp += m;
262 
263  return tmp;
264 }
matrix class
Definition: Matrix.h:25
Matrix & kome::core::Matrix::operator+= ( const Matrix m)

matrix addition

Parameters
[in]mmatrix to add
Returns
addition matrix

Definition at line 197 of file Matrix.cpp.

197  {
198  // check size
199  if( m.m_row != m_row || m.m_col != m_col ) {
200  throw FMT( "Matrix size is illegal." );
201  }
202 
203  // size
204  unsigned int size = m_row * m_col;
205 
206  // addition
207  for( unsigned int i = 0; i < size; i++ ) {
208  m_array[ i ] += m.m_array[ i ];
209  }
210 
211  return *this;
212 }
double *const m_array
Definition: Matrix.h:54
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
Matrix kome::core::Matrix::operator- ( const Matrix m)

matrix subtraction

Parameters
[in]mmatrix to subtract
Returns
subtraction matrix

Definition at line 267 of file Matrix.cpp.

267  {
268  Matrix tmp = *this;
269  tmp -= m;
270 
271  return tmp;
272 }
matrix class
Definition: Matrix.h:25
Matrix & kome::core::Matrix::operator-= ( const Matrix m)

matrix subtraction

Parameters
[in]mmatrix to subtract
Returns
subtraction matrix

Definition at line 215 of file Matrix.cpp.

215  {
216  // check size
217  if( m.m_row != m_row || m.m_col != m_col ) {
218  throw FMT( "Matrix size is illegal." );
219  }
220 
221  // size
222  unsigned int size = m_row * m_col;
223 
224  // addition
225  for( unsigned int i = 0; i < size; i++ ) {
226  m_array[ i ] -= m.m_array[ i ];
227  }
228 
229  return *this;
230 }
double *const m_array
Definition: Matrix.h:54
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
Matrix kome::core::Matrix::operator/ ( const double  k)

scalar division

Parameters
[in]kreal number to divide
Returns
division matrix

Definition at line 310 of file Matrix.cpp.

310  {
311  Matrix tmp = *this;
312  tmp /= k;
313 
314  return tmp;
315 }
matrix class
Definition: Matrix.h:25
Matrix & kome::core::Matrix::operator/= ( const double  k)

scalar division

Parameters
[in]kreal number to divide
Returns
division matrix

Definition at line 246 of file Matrix.cpp.

246  {
247  // get the size of array
248  unsigned int size = m_row * m_col;
249 
250  // dividion
251  for( unsigned int i = 0; i < size; i++ ) {
252  m_array[ i ] /= k;
253  }
254 
255  return *this;
256 }
double *const m_array
Definition: Matrix.h:54
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50
Matrix kome::core::Matrix::operator= ( const Matrix m)

copy matrix

Parameters
[in]msource matrix to be copied
Returns
copied matrix

Definition at line 174 of file Matrix.cpp.

174  {
175  if( m.m_row != m_row || m.m_col != m_col ) {
176  throw FMT( "Matrix size is illegal." );
177  }
178 
179  if( m_array != NULL ) {
180  memcpy( m_array, m.m_array, sizeof( double ) * m_row * m_col );
181  }
182 
183  return *this;
184 }
double *const m_array
Definition: Matrix.h:54
#define NULL
Definition: CoreMacros.h:18
const unsigned int m_col
Definition: Matrix.h:52
const unsigned int m_row
Definition: Matrix.h:50

Member Data Documentation

double* const kome::core::Matrix::m_array

value array

Definition at line 54 of file Matrix.h.

const unsigned int kome::core::Matrix::m_col

length of col

Definition at line 52 of file Matrix.h.

const unsigned int kome::core::Matrix::m_row

length of row

Definition at line 50 of file Matrix.h.


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