⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.h

📁 这是两个很好用的集合类
💻 H
📖 第 1 页 / 共 2 页
字号:
///Matrix.h and .cpp

#ifndef __MATRIX_H__
#define __MATRIX_H__

// System includes

#include <iostream>
#include <fstream>
#include <math.h>

// Utilities includes

#include "Vector.h"

namespace Flood
{

/// This template class defines a matrix for general purpose use.
///
/// @see Vector.

template <class Type>
class Matrix 
{

private:

   /// Number of rows in matrix.

   int numberOfRows;

   /// Number of columns in matrix.

   int numberOfColumns;

   /// Double pointer to a Type.

   Type** matrix;

public:

   // CONSTRUCTORS

   Matrix(void);

   Matrix(int, int);

   Matrix(int, int, const Type&);

   Matrix(int, int, const Type*);

   Matrix(const Matrix&);


   // ASSIGNMENT OPERATOR

   Matrix& operator=(const Matrix&);


   // REFERENCE OPERATORS

   inline Type* operator[](const int);

   inline const Type* operator[](const int) const;


   // METHODS

   inline int getNumberOfRows(void) const;
   inline int getNumberOfColumns(void) const;  

   inline void resize(int, int);
    
   inline Vector<Type> getRow(int);
   inline Vector<Type> getColumn(int);

   inline Matrix<Type> getTranspose(void);

   inline void setRow(int, Vector<Type>);
   inline void setColumn(int, Vector<Type>);

   inline Matrix<Type> operator+(Type);
   inline Matrix<Type> operator+(Matrix<Type>);

   inline Matrix<Type> operator-(Type);
   inline Matrix<Type> operator-(Matrix<Type>);

   inline Matrix<Type> operator*(Type);
   inline Vector<Type> operator*(Vector<Type>);
   inline Matrix<Type> operator*(Matrix<Type>);

   inline Matrix<Type> operator/(Type);

   inline void fillAtRandom(void);
   inline void fillAtRandom(double, double);

   inline void setToIdentity(void);

   inline Type calculateDeterminant(void);

   inline Matrix<Type> calculateInverse(void);

   void load(char*);
   void save(char*);

   // DESTRUCTOR

   ~Matrix();
};


// CONSTRUCTORS


/// Default constructor. It creates a matrix with zero rows and zero columns.

template <class Type>
Matrix<Type>::Matrix() : numberOfRows(0), numberOfColumns(0), matrix(0) 
{

}


/// Constructor. It creates a matrix with n rows and m columns, containing n*m copies of the default value for 
/// Type. 
///
/// @param newNumberOfRows Number of rows in Matrix.
/// @param newNumberOfColumns Number of columns in Matrix.

template <class Type>
Matrix<Type>::Matrix(int newNumberOfRows, int newNumberOfColumns) 
: numberOfRows(newNumberOfRows), numberOfColumns(newNumberOfColumns), 
matrix(new Type*[newNumberOfRows])
{
   matrix[0] = new Type[numberOfColumns*numberOfRows];

   for(int i = 1; i < numberOfRows; i++)
   {
      matrix[i] = matrix[i-1] + numberOfColumns;
   }
}


/// Constructor. It creates a matrix with n rows and m columns, containing n*m copies of the type value of Type. 
///
/// @param newNumberOfRows Number of rows in Matrix.
/// @param newNumberOfColumns Number of columns in Matrix.
/// @param type Value of Type.

template <class Type>
Matrix<Type>::Matrix(int newNumberOfRows, int newNumberOfColumns, const Type& type) 
: numberOfRows(newNumberOfRows), numberOfColumns(newNumberOfColumns), 
matrix(new Type*[newNumberOfRows])
{
   matrix[0] = new Type[numberOfColumns*numberOfRows];

   for(int i = 1; i < numberOfRows; i++)
   {
      matrix[i] = matrix[i-1] + numberOfColumns;
   }

   for(int k = 0; k < numberOfRows; k++)   //k substitutes the original i!
   {
      for(int j = 0; j < numberOfColumns; j++)
      {
         matrix[k][j] = type;              //k substitutes the original i!
      }
   }
}



/// Constructor. It creates a matrix with n rows and m columns, containing n*m copies of the type value of Type. 
///
/// @param newNumberOfRows Number of rows in Matrix.
/// @param newNumberOfColumns Number of columns in Matrix.
/// @param type Value of Type.

template <class Type>
Matrix<Type>::Matrix(int newNumberOfRows, int newNumberOfColumns, const Type* type) 
: numberOfRows(newNumberOfRows), numberOfColumns(newNumberOfColumns), 
matrix(new Type*[newNumberOfRows])
{
   // Construct matrix

   matrix[0] = new Type[numberOfColumns*numberOfRows];

   for(int i = 1; i < numberOfRows; i++)
   {
      matrix[i] = matrix[i-1] + numberOfColumns;
   }

   // Set all elements of matrix to the type value of Type

   for(int i = 0; i < numberOfRows; i++)                  //here maybe i into k
   {
      for(int j = 0; j < numberOfColumns; j++)
      {
         matrix[i][j] = *type++;                          //maybe here also!!!
     }
   }
}


/// Copy constructor. It creates a copy of an existing matrix. 
///
/// @param oldMatrix Matrix to be copied.

template <class Type>
Matrix<Type>::Matrix(const Matrix& oldMatrix) 
: numberOfRows(oldMatrix.numberOfRows), numberOfColumns(oldMatrix.numberOfColumns), 
matrix(new Type*[numberOfRows])
{
   // Construct matrix

   matrix[0] = new Type[numberOfColumns*numberOfRows];

   for(int i = 1; i < numberOfRows; i++)
   {
      matrix[i] = matrix[i-1] + numberOfColumns;
   }

   // Set all elements of matrix to the old matrix type values

   for(int k = 0; k < numberOfRows; k++)           //k substitutes the original i!
   {
      for(int j = 0; j < numberOfColumns; j++)
      {
         matrix[k][j] = oldMatrix[k][j];           //k substitutes the original i!
      }
   }
}


// ASSIGNMENT OPERATORS

/// Assignment operator. It assigns to self a copy of an existing Matrix.
///
/// @param oldMatrix Matrix to be assigned.

template <class Type>
Matrix<Type>& Matrix<Type>::operator=(const Matrix<Type>& oldMatrix)
{
   if(this != &oldMatrix) 
   {
      if(numberOfRows != oldMatrix.numberOfRows 
      || numberOfColumns != oldMatrix.numberOfColumns) 
      {
         if(matrix != 0) 
         {
            delete[] (matrix[0]);

            delete[] (matrix);
         }

         numberOfRows = oldMatrix.numberOfRows;

         numberOfColumns = oldMatrix.numberOfColumns;

         matrix = new Type*[numberOfRows];

         matrix[0] = new Type[numberOfColumns*numberOfRows];
      }

      for(int i = 1; i < numberOfRows; i++)
      {
         matrix[i] = matrix[i-1] + numberOfColumns;
      }

      // Set all elements of matrix to the old matrix type values

      for(int i = 0; i < numberOfRows; i++)
      {
         for(int j = 0; j < numberOfColumns; j++)
         {
            matrix[i][j] = oldMatrix[i][j];
         }
      }
   }

   return(*this);
}


// REFERENCE OPERATORS

/// Reference operator.  

template <class Type>
inline Type* Matrix<Type>::operator[](const int i) 
{
   // Control sentence (if debug)

   #ifndef NDEBUG 
   
   if(i >= numberOfRows)
   {
      std::cerr << std::endl
                << "Flood Error: Matrix Template. " << std::endl
                << "Reference operator []." << std::endl
                << "Index must be less than matrix dimensions." << std::endl
                << std::endl;

      exit(1);
   }

   #endif

   // Return matrix element

   return(matrix[i]);
}


/// Reference operator.  

template <class Type>
inline const Type* Matrix<Type>::operator[](const int i) const
{
   // Control sentence (if debug)

   #ifndef NDEBUG 
   
   if(i >= numberOfRows)
   {
      std::cerr << std::endl
                << "Flood Error: Matrix Template. " << std::endl
                << "Reference operator []." << std::endl
                << "Index must be and less than matrix dimensions." << std::endl
                << std::endl;

      exit(1);
   }

   #endif

   // Return matrix element

   return(matrix[i]);
}


// METHODS

// int getNumberOfRows(void) method

/// This method returns the number of rows in the matrix. 

template <class Type>
inline int Matrix<Type>::getNumberOfRows() const
{
   return(numberOfRows);
}


// int getNumberOfColumns(void) method

/// This method returns the number of columns in the matrix. 

template <class Type>
inline int Matrix<Type>::getNumberOfColumns() const
{
   return(numberOfColumns);
}


// void resize(int, int) method

/// This method sets new numbers of rows and columns in the vector.
/// It does not initialize the new matrix with the previous values. 
///
/// @param newNumberOfRows New number of rows.
/// @param newNumberOfColumns New number of columns.

template <class Type>
inline void Matrix<Type>::resize(int newNumberOfRows, int newNumberOfColumns) 
{
   numberOfRows = newNumberOfRows;
   numberOfColumns = newNumberOfColumns;

   matrix = new Type*[numberOfRows];

   matrix[0] = new Type[numberOfColumns*numberOfRows];

   for(int i = 1; i < numberOfRows; i++)
   {
      matrix[i] = matrix[i-1] + numberOfColumns;
   }
}


// Matrix<Type> getTranspose(void) method

/// This method returns the transpose of the matrix. 

template <class Type>
inline Matrix<Type> Matrix<Type>::getTranspose(void)
{
   Matrix<Type> transpose(numberOfColumns, numberOfRows);

   for(int i = 0; i < numberOfColumns; i++)
   {
      for(int j = 0; j < numberOfRows; j++)
      {
         transpose[i][j] = matrix[j][i];
      }     
   }

   return(transpose);
}


// Vector<Type> getRow(int) method

/// This method returns the row i of the matrix. 

template <class Type>
inline Vector<Type> Matrix<Type>::getRow(int i)
{
   Vector<Type> row(numberOfColumns, 0.0);

   for(int j = 0; j < numberOfColumns; j++)
   {
      row[j] = matrix[i][j];
   }

   return(row);
}


// Vector<Type> getColumn(int) method

/// This method returns the column j of the matrix. 

template <class Type>
inline Vector<Type> Matrix<Type>::getColumn(int j)
{
   Vector<Type> column(numberOfRows, 0.0);

   for(int i = 0; i < numberOfRows; i++)
   {
      column[i] = matrix[i][j];
   }

   return(column);
}


// void setRow(int, Vector<Type>) method

/// This method sets new values of a single row in the matrix. 
///
/// @param rowIndex Index of row. 
/// @param newRow New values of single row. 

template <class Type>
inline void Matrix<Type>::setRow(int rowIndex, Vector<Type> newRow)
{
   // Control sentence (if debug)

   #ifndef NDEBUG 

   if(rowIndex >= numberOfRows)
   {
      std::cerr << std::endl
                << "Flood Error: Matrix Template. " << std::endl
                << "setRow(int, Vector<Type>) method." << std::endl
                << "Index must be less than number of rows." << std::endl
                << std::endl;

      exit(1);
   }

   int size = newRow.getSize();

   if(size != numberOfColumns)
   {
      std::cerr << std::endl
                << "Flood Error: Matrix Template. " << std::endl
                << "setRow(int, Vector<Type>) method." << std::endl
                << "Size must be equal to number of columns." << std::endl
                << std::endl;

      exit(1);
   }

   #endif

   // Set new row

   for(int i = 0; i < numberOfColumns; i++)
   {
      matrix[rowIndex][i] = newRow[i];
   }
}


// void setColumn(int, Vector<Type>) method

/// This method sets new values of a single column in the matrix. 
///
/// @param columnIndex Index of column. 
/// @param newColumn New values of single column. 

template <class Type>
inline void Matrix<Type>::setColumn(int columnIndex, Vector<Type> newColumn)
{
   // Control sentence (if debug)

   #ifndef NDEBUG 

   if(columnIndex >= numberOfColumns)
   {
      std::cerr << std::endl
                << "Flood Error: Matrix Template. " << std::endl
                << "setColumn(int, Vector<Type>)." << std::endl
                << "Index must be less than number of columns." << std::endl
                << std::endl;

      exit(1);
   }

   int size = newColumn.getSize();

   if(size != numberOfRows)
   {
      std::cerr << std::endl
                << "Flood Error: Matrix Template. " << std::endl
                << "setColumn(int, Vector<Type>)." << std::endl
                << "Size must be equal to number of rows." << std::endl
                << std::endl;

      exit(1);
   }

   #endif

   // Set new column

   for(int i = 0; i < numberOfRows; i++)
   {
      matrix[i][columnIndex] = newColumn[i];
   }
}


// void fillAtRandom(void) method

/// This method fills all the elements in the matrix with random values comprised between -1 and 1.

template <class Type>
inline void Matrix<Type>::fillAtRandom(void)
{
   double random = 0.0;

   for(int i = 0; i < numberOfRows; i++)
   {
      for(int j = 0; j < numberOfColumns; j++)
     {
         random = (double)rand()/(RAND_MAX+1.0);

         matrix[i][j] = -1.0 + 2.0*random;
     }
   }
}


// void fillAtRandom(double, double) method

/// This method fills all the elements in the matrix with random values comprised between a minimum and a maximum
/// values.
///
/// @param minimum Minimum possible value. 
/// @param maximum Maximum possible value. 

template <class Type>
inline void Matrix<Type>::fillAtRandom(double minimum, double maximum)
{
   // Control sentence (if debug)

   #ifndef NDEBUG 

   if(minimum > maximum)
   {
      std::cerr << std::endl
                << "Flood Error: Matrix Template." << std::endl 
                << "void fillAtRandom(double, double) method." << std::endl
                << "Minimum value must be less or equal than maximum value." << std::endl
                << std::endl;

      exit(1);
   }

   #endif

   double random = 0.0;

   for(int i = 0; i < numberOfRows; i++)
   {
      for(int j = 0; j < numberOfColumns; j++)
     {
         random = (double)rand()/(RAND_MAX+1.0);

         matrix[i][j] = minimum + (maximum - minimum)*random;
     }
   }
}


// void setToIdentity(void) method

/// This method sets the diagonal elements in the matrix with ones and the rest elements with zeros. The matrix 
/// must be square. 

template <class Type>
inline void Matrix<Type>::setToIdentity(void)
{
   // Control sentence (if debug)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -