KSquare Utilities
KKB::Matrix Class Reference

Supports two dimensional matrices. More...

#include <Matrix.h>

Public Types

typedef MatrixMatrixPtr
 

Public Member Functions

 Matrix ()
 
 Matrix (kkint32 _numOfRows, kkint32 _numOfCols)
 
 Matrix (const Matrix &_matrix)
 
 Matrix (const VectorDouble &_v)
 
 ~Matrix ()
 
MatrixPtr CalcCoFactorMatrix ()
 
MatrixPtr Covariance () const
 Returns a Covariance matrix. More...
 
double **const Data () const
 
double Determinant ()
 
double DeterminantSlow ()
 Recursive Implementation. More...
 
void EigenVectors (MatrixPtr &eigenVectors, VectorDouble *&eigenValues) const
 Will derive the Eigen vectors and values of the matrix. More...
 
void FindMaxValue (double &maxVal, kkint32 &row, kkint32 &col)
 Locates the maximum value in a matrix along with the row and column that is located. More...
 
VectorDouble GetCol (kkint32 col) const
 
Matrix Inverse ()
 
kkint32 NumOfCols () const
 
kkint32 NumOfRows () const
 
Matrix operator* (const Matrix &right)
 
Matrix operator* (double right)
 
Matrixoperator*= (double right)
 
Matrix operator+ (const Matrix &right)
 
Matrix operator+ (double right)
 
Matrixoperator+= (double right)
 
Matrixoperator+= (const Matrix &right)
 
Matrix operator- (const Matrix &right)
 
Matrix operator- (double right)
 
Matrixoperator= (const Matrix &right)
 
Matrixoperator= (const VectorDouble &right)
 
Rowoperator[] (kkint32 rowIDX) const
 
void ReSize (kkint32 _numOfRows, kkint32 _numOfCols)
 
bool Symmetric () const
 
Matrix Transpose ()
 

Static Public Member Functions

template<typename T >
static MatrixPtr BuildFromArray (kkint32 numOfRows, kkint32 numOfCols, T **data)
 Will create a new matrix using the 2dArray "data" for source. More...
 

Friends

Matrix operator- (double left, const Matrix &right)
 
std::ostream & operator<< (std::ostream &os, const Matrix &matrix)
 

Detailed Description

Supports two dimensional matrices.

Developed for Machine Learning Project for support of Import Vector Machine. Handles two dimensional matrices. Functions supported are matrix addition, subtraction multiplication, transpose, determinant, and inversion. Where appropriate arithmetic arithmetic operators +, -, * were overloaded. Addition, Subtraction and Multiplication can be done against either another matrix or scaler. Also Transpose and Determinant operations are supported.

Definition at line 46 of file Matrix.h.

Member Typedef Documentation

Definition at line 49 of file Matrix.h.

Constructor & Destructor Documentation

Matrix::Matrix ( )

Definition at line 94 of file Matrix.cpp.

94  :
95 
96  data (NULL),
97  dataArea (NULL),
98  numOfCols (0),
99  numOfRows (0),
100  rows (NULL),
101  totNumCells (0)
102 {
103 }
Matrix::Matrix ( kkint32  _numOfRows,
kkint32  _numOfCols 
)

Definition at line 108 of file Matrix.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), and KKB::KKStr::KKStr().

Referenced by KKB::Raster::BuildGaussian2dKernel(), CalcCoFactorMatrix(), Covariance(), KKB::Raster::CreateGrayScaleKLT(), KKB::Raster::CreateGrayScaleKLTOnMaskedArea(), operator*(), KKB::operator-(), KKMLL::ClassificationBiasMatrix::PerformAdjustmnts(), and Transpose().

110  :
111 
112  data (NULL),
113  dataArea (NULL),
114  numOfCols (_numOfCols),
115  numOfRows (_numOfRows),
116  rows (NULL),
117  totNumCells (0)
118 
119 {
120  if (_numOfRows < 0)
121  {
122  KKStr msg(80);
123  msg << "Matrix::Matrix **** ERROR ****, Row Dimension[" << _numOfRows << "] Invalid.";
124  cerr << std::endl << msg <<std::endl << std::endl;
125  throw KKException (msg);
126  }
127 
128  if (_numOfCols < 0)
129  {
130  KKStr msg(80);
131  msg << "Matrix::Matrix **** ERROR ****, Col Dimension[" << _numOfCols << "] Invalid.";
132  cerr << std::endl << msg <<std::endl << std::endl;
133  throw KKException (msg);
134  }
135 
136  AllocateStorage ();
137 } /* Matrix::Matrix */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
Matrix::Matrix ( const Matrix _matrix)

Definition at line 142 of file Matrix.cpp.

Referenced by KKMLL::ClassificationBiasMatrix::ClassificationBiasMatrix(), EigenVectors(), operator*(), operator+(), and operator-().

142  :
143  data (NULL),
144  dataArea (NULL),
145  numOfCols (_matrix.numOfCols),
146  numOfRows (_matrix.numOfRows),
147  rows (NULL),
148  totNumCells (0)
149 
150 {
151  AllocateStorage ();
152  memcpy (dataArea, _matrix.dataArea, totNumCells * sizeof (double));
153 } /* Matrix::Matrix */
Matrix::Matrix ( const VectorDouble _v)

Definition at line 157 of file Matrix.cpp.

References Matrix().

Referenced by Matrix().

157  :
158  data (NULL),
159  dataArea (NULL),
160  numOfCols (1),
161  numOfRows (kkint32 (_v.size ())),
162  rows (NULL)
163 {
164  AllocateStorage ();
165  kkint32 row;
166  for (row = 0; row < numOfRows; ++row)
167  dataArea[row] = _v[row];
168 } /* Matrix::Matrix */
__int32 kkint32
Definition: KKBaseTypes.h:88
Matrix::~Matrix ( )

Definition at line 173 of file Matrix.cpp.

174 {
175  Destroy ();
176 }

Member Function Documentation

template<typename T >
MatrixPtr Matrix::BuildFromArray ( kkint32  numOfRows,
kkint32  numOfCols,
T **  data 
)
static

Will create a new matrix using the 2dArray "data" for source.

If any row in "data" is equal to NULL then the corresponding row in the returned matrix will be initialized to zeros.

Parameters
[in]numOfRowsNumber of rows in resultant matrix.
[in]numOfColsNumber of cols if resultant matrix.
[in]dataA two dimensional array.
Returns
A matrix that is initialized with the contents of "data".

Definition at line 190 of file Matrix.cpp.

194 {
195  if ((numOfRows < 1) || (numOfCols < 1))
196  {
197  cerr << std::endl << std::endl << "Matrix::BuildFromArray ***ERROR*** NumOfRows[" << numOfRows << "] or NumOfCols[" << numOfCols << "] is invalid." << std::endl << std::endl;
198  return NULL;
199  }
200 
201  if (!data)
202  {
203  cerr << std::endl << std::endl << "Matrix::BuildFromArray ***ERROR*** No source data (data == NULL)." << std::endl << std::endl;
204  return NULL;
205  }
206 
207  MatrixPtr m = new Matrix (numOfRows, numOfCols);
208 
209  for (kkint32 row = 0; row < numOfRows; ++row)
210  {
211  T* srcRow = data[row];
212  double* destRow = m->data[row];
213  if (srcRow)
214  {
215  for (kkint32 col = 0; col < numOfCols; ++col)
216  destRow[col] = (double)(srcRow[col]);
217  }
218  }
219  return m;
220 } /* BuildFromArray */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
MatrixPtr Matrix::CalcCoFactorMatrix ( )

Definition at line 606 of file Matrix.cpp.

References Determinant(), Matrix(), operator[](), KKB::Row::operator[](), and KKB::osWaitForEnter().

Referenced by Inverse().

607 {
608  if (numOfCols != numOfRows)
609  {
610  cerr << std::endl
611  << "Matrix::CalcCoFactors **** ERROR ****, Matrix not a Square["
612  << numOfRows << "," << numOfCols << "]."
613  << std::endl
614  << std::endl;
615  osWaitForEnter ();
616  exit (1);
617  }
618 
619  MatrixPtr result = new Matrix (numOfRows, numOfCols);
620 
621  kkint32 newSize = numOfCols - 1;
622 
623  kkint32* colMap = new kkint32 [newSize];
624  kkint32* rowMap = new kkint32 [newSize];
625 
626  kkint32 row;
627  kkint32 col;
628  kkint32 x;
629 
630 
631  kkint32 sign;
632 
633  for (row = 0; row < numOfRows; row++)
634  {
635  // Create a map of all rows except row we are calculating
636  // CoFactors for.
637 
638  kkint32 newRow = 0;
639  for (x = 0; x < numOfRows; x++)
640  {
641  if (x != row)
642  {
643  rowMap[newRow] = x;
644  newRow++;
645  }
646  }
647 
648  for (col = 0; col < numOfCols; col++)
649  {
650  // Create a map of all cells except row we are calculating
651  // CoFactor for.
652 
653  kkint32 newCol = 0;
654  for (x = 0; x < numOfCols; x++)
655  {
656  if (x != col)
657  {
658  colMap[newCol] = x;
659  newCol++;
660  }
661  }
662 
663  if (((row + col) % 2) == 0)
664  sign = 1;
665  else
666  sign = -1;
667 
668 
669  Matrix temp (newSize, newSize);
670  for (kkint32 r = 0; r < newSize; r++)
671  {
672  kkint32 tempR = rowMap[r];
673  for (kkint32 c = 0; c < newSize; c++)
674  temp[r][c] = data[tempR][colMap[c]];
675  }
676 
677  result->data[row][col] = temp.Determinant () * sign;
678  }
679  }
680 
681  return result;
682 } /* CalcCoFactor */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
void osWaitForEnter()
Supports two dimensional matrices.
Definition: Matrix.h:46
MatrixPtr Matrix::Covariance ( ) const

Returns a Covariance matrix.

Each column represents a variable and each row represents an instance of each variable.

Returns
Returns a symmetric matrix that will be (numOfRows x numOfRows) where each element will represent the covariance between their respective variables.

Definition at line 1004 of file Matrix.cpp.

References Matrix(), operator[](), and KKB::Row::operator[]().

1005 {
1006  if ((data == NULL) || (numOfRows < 1))
1007  {
1008  cerr << std::endl << "Matrix::Covariance ***ERROR*** 'data' not defined in Matrix." << std::endl << std::endl;
1009  return NULL;
1010  }
1011 
1012  // Used web site below to help with Covariance calculations
1013  // http://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm
1014 
1015  kkint32 col = 0;
1016  kkint32 row = 0;
1017 
1018  double* totals = new double[numOfCols];
1019  double* means = new double[numOfCols];
1020  double** centeredVals = new double*[numOfCols];
1021  for (col = 0; col < numOfCols; ++col)
1022  {
1023  totals[col] = 0.0;
1024  centeredVals[col] = new double[numOfRows];
1025  }
1026 
1027  for (row = 0; row < numOfRows; ++row)
1028  {
1029  double* rowData = data[row];
1030  for (col = 0; col < numOfCols; ++col)
1031  totals[col] += rowData[col];
1032  }
1033 
1034  for (col = 0; col < numOfCols; ++col)
1035  means[col] = totals[col] / numOfRows;
1036 
1037  for (row = 0; row < numOfRows; ++row)
1038  {
1039  double* rowData = data[row];
1040  for (col = 0; col < numOfCols; ++col)
1041  centeredVals[col][row] = rowData[col] - means[col];
1042  }
1043 
1044  MatrixPtr covariances = new Matrix (numOfCols, numOfCols);
1045 
1046  for (kkint32 varIdxX = 0; varIdxX < numOfCols; ++varIdxX)
1047  {
1048  double* varXs = centeredVals[varIdxX];
1049  for (kkint32 varIdxY = varIdxX; varIdxY < numOfCols; ++varIdxY)
1050  {
1051  // Calculate the covariance between chanIdx0 and chanIdx1
1052 
1053  double* varYs = centeredVals[varIdxY];
1054  double total = 0.0f;
1055  for (row = 0; row < numOfRows; ++row)
1056  total += varXs[row] * varYs[row];
1057  (*covariances)[varIdxX][varIdxY] = total / (numOfRows - 1);
1058  (*covariances)[varIdxY][varIdxX] = (*covariances)[varIdxX][varIdxY];
1059  }
1060  }
1061 
1062  for (col = 0; col < numOfCols; col++)
1063  {
1064  delete[] centeredVals[col];
1065  centeredVals[col] = NULL;
1066  }
1067  delete[] centeredVals; centeredVals = NULL;
1068  delete[] means; means = NULL;
1069  delete[] totals; totals = NULL;
1070 
1071  return covariances;
1072 } /* Covariance */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
double** const KKB::Matrix::Data ( ) const
inline

Definition at line 106 of file Matrix.h.

106 {return data;}
double Matrix::Determinant ( )
Returns
the determinant of mat

Definition at line 945 of file Matrix.cpp.

Referenced by CalcCoFactorMatrix(), and Inverse().

946 {
947  if (numOfCols != numOfRows)
948  return -999999.99;
949 
950  kkint32 r, c;
951 
952  double** mat = new double*[numOfRows];
953  for (r = 0; r < numOfRows; r++)
954  {
955  mat[r] = new double[numOfCols];
956  for (c = 0; c < numOfCols; c++)
957  {
958  mat[r][c] = data[r][c];
959  }
960  }
961 
962  double det = 1.0;
963  unsigned short i = 0;
964 
965  for (i = 0; i < numOfRows; i++)
966  {
967  const double Aii = DeterminantSwap (mat, i);
968  unsigned short j = 0;
969 
970  if (fabs (Aii) < DBL_EPSILON)
971  {
972  det = 0.0;
973  break;
974  }
975 
976  det *= Aii;
977 
978  //Do elimination
979  for (j = i + 1; j < numOfRows; j++)
980  {
981  const double pivot = mat[j][i] / Aii;
982  for (ushort k = i; k < numOfRows; k++)
983  mat[j][k] -= pivot * mat[i][k];
984  }
985  }
986 
987  for (r = 0; r < numOfRows; r++)
988  {
989  delete mat[r];
990  mat[r] = NULL;
991  }
992  delete[] mat;
993  mat = NULL;
994  return (det);
995 } /* Determinant */
#define DBL_EPSILON
Definition: Matrix.cpp:902
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned short ushort
Unsigned short.
Definition: KKBaseTypes.h:79
double Matrix::DeterminantSlow ( )

Recursive Implementation.

Definition at line 286 of file Matrix.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), and KKB::KKStr::KKStr().

287 {
288  if (numOfCols != numOfRows)
289  {
290  KKStr msg (80);
291  msg << "Matrix::Determinant *** ERROR *** Dimensions are not Square[" << numOfRows << "," << numOfCols << "] Invalid.";
292  cerr << std::endl << msg << std::endl << std::endl;
293  throw KKException (msg);
294  }
295 
296  if (numOfCols == 1)
297  {
298  // return rows[0]->cells[0];
299  return data[0][0];
300  }
301 
302  kkint32 x;
303 
304  kkint32* rowMap = new kkint32[numOfRows];
305  for (x = 0; x < numOfRows; x++)
306  rowMap[x] = x;
307 
308  kkint32* colMap = new kkint32[numOfCols];
309  for (x = 0; x < numOfCols; x++)
310  colMap[x] = x;
311 
312  double det = CalcDeterminent (rowMap, colMap, numOfCols);
313 
314  delete[] colMap;
315  return det;
316 } /* Determinant */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
void Matrix::EigenVectors ( MatrixPtr eigenVectors,
VectorDouble *&  eigenValues 
) const

Will derive the Eigen vectors and values of the matrix.

Will make use of routines from Numerical Recipes for c++, ex: Tred2 and Tqli.

Definition at line 768 of file Matrix.cpp.

References Matrix(), and Symmetric().

Referenced by KKB::Raster::CreateGrayScaleKLT(), and KKB::Raster::CreateGrayScaleKLTOnMaskedArea().

771 {
772  eigenVectors = NULL;
773  eigenValues = NULL;
774  if ((data == NULL) || (numOfRows < 1))
775  {
776  cerr << std::endl << "Matrix::EigenVectors ***ERROR*** 'data' not defined in Matrix." << std::endl << std::endl;
777  return;
778  }
779 
780  if (numOfRows != numOfCols)
781  {
782  cerr << std::endl << "Matrix::EigenVectors ***ERROR*** Not a square matrix NumOfRows[" << numOfRows << "] NumOfCols[" << numOfCols << "]." << std::endl << std::endl;
783  return;
784  }
785 
786  eigenVectors = new Matrix (*this);
787 
788  if (Symmetric ())
789  {
790  double* d = new double[numOfRows];
791  double* e = new double[numOfRows];
792  for (kkint32 x = 0; x < numOfRows; ++x)
793  {
794  d[x] = 0.0;
795  e[x] = 0.0;
796  }
797  Tred2 (eigenVectors->data, numOfRows, d, e);
798  kkint32 successful = Tqli (d, e, numOfRows, eigenVectors->data);
799  if (successful != 1)
800  {
801  delete eigenVectors; eigenVectors = NULL;
802  delete[] d; d = NULL;
803  delete[] e; e = NULL;
804  return;
805  }
806 
807  eigenValues = new VectorDouble ();
808  for (kkint32 x = 0; x < numOfRows; ++x)
809  eigenValues->push_back (d[x]);
810 
811  delete[] d; d = NULL;
812  delete[] e; e = NULL;
813  }
814 } /* GetEigenVectors */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
bool Symmetric() const
Definition: Matrix.cpp:688
void Tred2(kkint32 n, double a[2][2], double *d, double *e)
Definition: EigenVector.cpp:31
std::vector< double > VectorDouble
Vector of doubles.
Definition: KKBaseTypes.h:148
void Matrix::FindMaxValue ( double &  maxVal,
kkint32 row,
kkint32 col 
)

Locates the maximum value in a matrix along with the row and column that is located.

Definition at line 818 of file Matrix.cpp.

Referenced by KKB::SegmentorOTSU::SegmentImage(), and KKB::SegmentorOTSU::SegmentMaskedImage().

822 {
823  rowIdx = -1;
824  colIdx = -1;
825  maxVal = DBL_MIN;
826 
827  if (!data)
828  return;
829 
830  maxVal = data[0][0];
831  rowIdx = colIdx = 0;
832 
833  for (kkint32 row = 0; row < numOfRows; ++row)
834  {
835  double* dataRow = data[row];
836  for (kkint32 col = 0; col < numOfCols; ++col)
837  {
838  if (dataRow[col] > maxVal)
839  {
840  maxVal = dataRow[col];
841  colIdx = col;
842  rowIdx = row;
843  }
844  }
845  }
846 
847  return;
848 } /* FindMaxValue */
__int32 kkint32
Definition: KKBaseTypes.h:88
VectorDouble Matrix::GetCol ( kkint32  col) const

Definition at line 891 of file Matrix.cpp.

Referenced by KKB::Raster::CreateGrayScaleKLT(), and KKB::Raster::CreateGrayScaleKLTOnMaskedArea().

892 {
893  VectorDouble colResult (numOfRows, 0.0);
894  for (kkint32 r = 0; r < numOfRows; r++)
895  colResult[r] = data[r][col];
896 
897  return colResult;
898 } /* GetCol */
__int32 kkint32
Definition: KKBaseTypes.h:88
std::vector< double > VectorDouble
Vector of doubles.
Definition: KKBaseTypes.h:148
Matrix Matrix::Inverse ( )

Definition at line 731 of file Matrix.cpp.

References CalcCoFactorMatrix(), KKB::KKStr::Concat(), Determinant(), KKB::KKException::KKException(), KKB::KKStr::KKStr(), operator*=(), and Transpose().

Referenced by KKMLL::ClassificationBiasMatrix::PerformAdjustmnts().

732 {
733  if (numOfCols != numOfRows)
734  {
735  KKStr msg (80);
736  msg << "Matrix::Inverse *** ERROR *** Dimensions are not Square[" << numOfRows << "," << numOfCols << "] Invalid.";
737 
738  cerr << std::endl << msg << std::endl << std::endl;
739  throw KKException (msg);
740  }
741 
742  double det = Determinant ();
743  if (det == 0)
744  {
745  cerr << std::endl << "Matrix::Inverse *** ERROR *** Determinant of Matrix is Zero." << std::endl << std::endl;
746  }
747 
748  MatrixPtr coFactors = CalcCoFactorMatrix ();
749 
750  Matrix result = coFactors->Transpose ();
751 
752  delete coFactors;
753 
754  if (det == 0.0)
755  result *= 0.0;
756  else
757  result *= (1.0 / det);
758 
759  return result;
760 } /* Inverse */
Matrix Transpose()
Definition: Matrix.cpp:707
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
Supports two dimensional matrices.
Definition: Matrix.h:46
double Determinant()
Definition: Matrix.cpp:945
MatrixPtr CalcCoFactorMatrix()
Definition: Matrix.cpp:606
kkint32 KKB::Matrix::NumOfCols ( ) const
inline

Definition at line 126 of file Matrix.h.

Referenced by KKB::operator-(), and operator<<().

126 {return numOfCols;}
kkint32 KKB::Matrix::NumOfRows ( ) const
inline

Definition at line 128 of file Matrix.h.

Referenced by KKB::operator-(), and operator<<().

128 {return numOfRows;}
Matrix Matrix::operator* ( const Matrix right)

Definition at line 478 of file Matrix.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), KKB::KKStr::KKStr(), and Matrix().

Referenced by KKMLL::ClassificationBiasMatrix::PerformAdjustmnts().

479 {
480  if (numOfCols != right.numOfRows)
481  {
482  KKStr msg (100);
483  msg << "Matrix::operator* **** ERROR ****, Dimension Mismatch Left[" << numOfRows << "," << numOfCols << "] Right[" << right.numOfRows << "," << right.numOfCols << "].";
484  cerr << std::endl << msg << std::endl << std::endl;
485  throw KKException (msg);
486  }
487 
488  kkint32 col;
489  kkint32 row;
490 
491  kkint32 rRows = numOfRows;
492  kkint32 rCols = right.numOfCols;
493 
494  Matrix result (rRows, rCols);
495 
496  double** resultData = result.data;
497  double** rightData = right.data;
498 
499  kkint32 innerDim = numOfCols;
500 
501  for (row = 0; row < rRows; row++)
502  {
503  for (col = 0; col < rCols; col++)
504  {
505  double val = 0;
506  kkint32 x = 0;
507  for (x = 0; x < innerDim; x++)
508  val = val + data[row][x] * rightData[x][col];
509  resultData[row][col] = val;
510  }
511  }
512 
513  return result;
514 } /* Matrix::operator */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
Matrix Matrix::operator* ( double  right)

Definition at line 530 of file Matrix.cpp.

References Matrix().

531 {
532  Matrix result (*this);
533  double* resultDataArea = result.dataArea;
534  for (kkint32 x = 0; x < totNumCells; ++x)
535  resultDataArea[x] = dataArea[x] * right;
536  return result;
537 } /* operator* */
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
Matrix & Matrix::operator*= ( double  right)

Definition at line 349 of file Matrix.cpp.

Referenced by Inverse().

350 {
351  kkint32 x = 0;
352  for (x = 0; x < totNumCells; ++x)
353  dataArea[x] *= right;
354  return *this;
355 }
__int32 kkint32
Definition: KKBaseTypes.h:88
Matrix Matrix::operator+ ( const Matrix right)

Definition at line 369 of file Matrix.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), KKB::KKStr::KKStr(), and Matrix().

Referenced by KKB::SegmentorOTSU::SegmentImage(), and KKB::SegmentorOTSU::SegmentMaskedImage().

370 {
371  if ((numOfRows != right.numOfRows) ||
372  (numOfCols != right.numOfCols))
373  {
374  KKStr msg (100);
375  msg << "Matrix::operator+ **** ERROR ****, Dimensions Don't Match [" << numOfRows << "," << numOfCols << "] + [" << right.numOfRows << "," << right.numOfCols << "].";
376  cerr << std::endl << msg << std::endl << std::endl;
377  throw KKException (msg);
378  }
379 
380  Matrix result (*this);
381 
382  double* resultDataArea = result.dataArea;
383  double* rightDataArea = right.dataArea;
384 
385  for (kkint32 x = 0; x < totNumCells; ++x)
386  resultDataArea[x] = dataArea[x] + rightDataArea[x];
387 
388  return result;
389 } /* Matrix::operator+ */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
Matrix Matrix::operator+ ( double  right)

Definition at line 519 of file Matrix.cpp.

References Matrix().

520 {
521  Matrix result (*this);
522  double* resultDataArea = result.dataArea;
523  for (kkint32 x = 0; x < totNumCells; ++x)
524  resultDataArea[x] = dataArea[x] + right;
525  return result;
526 } /* operator+ */
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
Matrix & Matrix::operator+= ( double  right)

Definition at line 359 of file Matrix.cpp.

360 {
361  kkint32 x = 0;
362  for (x = 0; x < totNumCells; ++x)
363  dataArea[x] += right;
364  return *this;
365 }
__int32 kkint32
Definition: KKBaseTypes.h:88
Matrix & Matrix::operator+= ( const Matrix right)

Definition at line 396 of file Matrix.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), and KKB::KKStr::KKStr().

397 {
398  if ((numOfRows != right.numOfRows) ||
399  (numOfCols != right.numOfCols))
400  {
401  KKStr msg (100);
402  msg << "Matrix::operator+= **** ERROR ****, Dimensions Don't Match [" << numOfRows << "," << numOfCols << "] + [" << right.numOfRows << "," << right.numOfCols << "].";
403  cerr << std::endl << msg << std::endl << std::endl;
404  throw KKException (msg);
405  }
406 
407  double* rightDataArea = right.dataArea;
408 
409  for (kkint32 x = 0; x < totNumCells; ++x)
410  dataArea[x] += rightDataArea[x];
411 
412  return *this;
413 } /* Matrix::operator+ */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Matrix Matrix::operator- ( const Matrix right)

Definition at line 419 of file Matrix.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), KKB::KKStr::KKStr(), and Matrix().

Referenced by KKB::SegmentorOTSU::SegmentImage(), and KKB::SegmentorOTSU::SegmentMaskedImage().

420 {
421  if ((numOfRows != right.numOfRows) ||
422  (numOfCols != right.numOfCols))
423  {
424  KKStr msg (100);
425  msg << "Matrix::operator- **** ERROR ****, Dimensions Don't Match [" << numOfRows << "," << numOfCols << "] + [" << right.numOfRows << "," << right.numOfCols << "].";
426  cerr << std::endl << msg << std::endl << std::endl;
427  throw KKException (msg);
428  }
429 
430  Matrix result (*this);
431 
432  double* resultDataArea = result.dataArea;
433  double* rightDataArea = right.dataArea;
434 
435  for (kkint32 x = 0; x < totNumCells; ++x)
436  resultDataArea[x] = dataArea[x] - rightDataArea[x];
437 
438  return result;
439 } /* operator- */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
Matrix Matrix::operator- ( double  right)

Definition at line 443 of file Matrix.cpp.

References Matrix().

Referenced by KKB::SegmentorOTSU::SegmentImage(), and KKB::SegmentorOTSU::SegmentMaskedImage().

444 {
445  Matrix result (*this);
446  double* resultDataArea = result.dataArea;
447  for (kkint32 x = 0; x < totNumCells; ++x)
448  resultDataArea[x] = dataArea[x] - right;
449 
450  return result;
451 }
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46
Matrix & Matrix::operator= ( const Matrix right)

Definition at line 329 of file Matrix.cpp.

References ReSize().

330 {
331  ReSize (right.numOfRows, right.numOfCols);
332  memcpy (dataArea, right.dataArea, totNumCells * sizeof (double));
333  return *this;
334 }
void ReSize(kkint32 _numOfRows, kkint32 _numOfCols)
Definition: Matrix.cpp:224
Matrix & Matrix::operator= ( const VectorDouble right)

Definition at line 337 of file Matrix.cpp.

338 {
339  ReSize ((kkuint32)right.size (), 1);
340  for (kkint32 row = 0; row < numOfRows; row++)
341  dataArea[row] = right[row];
342 
343  return *this;
344 } /* operator= */
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
void ReSize(kkint32 _numOfRows, kkint32 _numOfCols)
Definition: Matrix.cpp:224
Row & Matrix::operator[] ( kkint32  rowIDX) const

Definition at line 270 of file Matrix.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), and KKB::KKStr::KKStr().

Referenced by KKB::Raster::BuildGaussian2dKernel(), CalcCoFactorMatrix(), Covariance(), KKB::Raster::CreateGrayScaleKLT(), KKB::Raster::CreateGrayScaleKLTOnMaskedArea(), KKMLL::ClassificationBiasMatrix::PerformAdjustmnts(), and KKMLL::ClassificationBiasMatrix::PrintBiasMatrix().

271 {
272  if ((rowIDX < 0) || (rowIDX >= numOfRows))
273  {
274  KKStr msg (80);
275  msg << "Matrix::operator[] **** ERROR ****, Row Index[" << rowIDX << "] Invalid.";
276  cerr << std::endl << msg << std::endl << std::endl;
277  throw KKException (msg);
278  }
279 
280  return (rows[rowIDX]);
281 } /* Matrix::operator[] */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
void Matrix::ReSize ( kkint32  _numOfRows,
kkint32  _numOfCols 
)

Definition at line 224 of file Matrix.cpp.

Referenced by operator=().

227 {
228  Destroy ();
229  numOfRows = _numOfRows;
230  numOfCols = _numOfCols;
231  AllocateStorage ();
232 } /* ReSize */
bool Matrix::Symmetric ( ) const

Returns true is the matrix is Symmetric

Definition at line 688 of file Matrix.cpp.

Referenced by EigenVectors().

689 {
690  if ((data == NULL) || (numOfRows != numOfCols))
691  return false;
692 
693  for (kkint32 row = 0; row < numOfRows; ++row)
694  {
695  for (kkint32 col = row + 1; col < numOfCols; ++col)
696  {
697  if (data[row][col] != data[col][row])
698  return false;
699  }
700  }
701  return true;
702 } /* Symmetric */
__int32 kkint32
Definition: KKBaseTypes.h:88
Matrix Matrix::Transpose ( )

Definition at line 707 of file Matrix.cpp.

References Matrix().

Referenced by Inverse(), and KKMLL::ClassificationBiasMatrix::PerformAdjustmnts().

708 {
709  kkint32 col;
710  kkint32 row;
711 
712  Matrix result (numOfCols, numOfRows);
713 
714  double** resultData = result.data;
715 
716  for (row = 0; row < numOfRows; row++)
717  {
718  for (col = 0; col < numOfCols; col++)
719  {
720  resultData[col][row] = data[row][col];
721  }
722  }
723 
724  return result;
725 } /* Transpose */
__int32 kkint32
Definition: KKBaseTypes.h:88
Supports two dimensional matrices.
Definition: Matrix.h:46

Friends And Related Function Documentation

Matrix operator- ( double  left,
const Matrix right 
)
friend
std::ostream& operator<< ( std::ostream &  os,
const Matrix matrix 
)
friend

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