1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <cstdlib>
#include <iostream>
#include "matrix.h"
using namespace std;
/** \brief Get matrix element
* \param[in] r Row index
* \param[in] c Colomn index
*/
#define M(r,c) M[r*nCln + c]
/** \brief MATRIX constructor
* \param[in] nR Row number
* \param[in] nC Colomns number
*/
MATRIX::MATRIX(INT16U nR, INT16U nC)
{
INT16U i = 0, j = 0;
nRow = nR;
nCln = nC;
M = new REAL32[nRow*nCln];
if(M != pNULL)
{
for(i = 0; i < nRow; i++)
{
for(j = 0; j < nCln; j++)
{
if(i == j)
{
M(i,j) = 1.0F;
}
else
{
M(i,j) = 0.0F;
}
}
}
}
else
{
cout << "\nObject non allcated - Out Of Memory...\n";
}
}
/** \brief MATRIX destructor
*/
MATRIX:: ~MATRIX(void)
{
if(M != pNULL)
{
delete M;
}
}
/** \brief Print out matrix components
*/
void MATRIX::Print(void)
{
INT16U i = 0U, j = 0U;
if(M != pNULL)
{
for(i = 0; i < nRow; i++)
{
for(j = 0; j < nCln; j++)
{
cout << M(i,j) << " ";
}
cout << "\n";
}
}
}
/** \brief Get matrix row number
* \retval Matrix row number
*/
INT16U MATRIX::GetRowNum(void)
{
return nRow;
}
/** \brief Get matrix Colomns number
* \retval Matrix row number
*/
INT16U MATRIX::GetClnNum(void)
{
return nCln;
}
void MATRIX::SetElement(INT16U i, INT16U j, REAL32 val)
{
M(i,j) = val;
}
REAL32 MATRIX::GetElement(INT16U i, INT16U j)
{
return (REAL32) M(i,j);
}
REAL32* MATRIX::GetMatrix(void)
{
return M;
}
MATRIX& MATRIX::operator=(MATRIX& X)
{
if( (X.GetMatrix() != pNULL) && (&X != this) )
{
if( (X.GetClnNum() == nCln) && \
(X.GetRowNum() == nRow) )
{
memcpy((void*) M, (void*)X.GetMatrix(), nCln*nRow*sizeof(REAL32));
}
}
return *this;
}
MATRIX MATRIX::operator+(MATRIX& X)
{
MATRIX R(nRow, nCln);
if( (X.GetMatrix() != pNULL) && (&X != this) )
{
if( (X.GetClnNum() == nCln) && \
(X.GetRowNum() == nRow) )
{
for(INT16U i = 0; i < nCln; i++)
{
for(INT16U j = 0; j < nRow; j++)
{
R.SetElement(i, j, M(i,j) + X.GetElement(i,j));
}
}
}
return R;
}
else
{
return *this;
}
}
|