📄 matrix_pointers.cxx
字号:
//! Access to elements of the data array. /*! Provides a direct access to the data array. \param i index. \return i-th element of the data array. */ template <class T, class Prop, class Storage, class Allocator> inline typename Matrix_Pointers<T, Prop, Storage, Allocator> ::const_reference Matrix_Pointers<T, Prop, Storage, Allocator>::operator[] (int i) const { #ifdef SELDON_CHECK_BOUNDARIES if (i < 0 || i >= this->GetDataSize()) throw WrongIndex("Matrix_Pointers::operator[] (int) const", string("Index should be in [0, ") + to_str(this->GetDataSize()-1) + "], but is equal to " + to_str(i) + ".");#endif return this->data_[i]; } //! Duplicates a matrix (assignment operator). /*! \param A matrix to be copied. \note Memory is duplicated: 'A' is therefore independent from the current instance after the copy. */ template <class T, class Prop, class Storage, class Allocator> inline Matrix_Pointers<T, Prop, Storage, Allocator>& Matrix_Pointers<T, Prop, Storage, Allocator> ::operator= (const Matrix_Pointers<T, Prop, Storage, Allocator>& A) { this->Copy(A); return *this; } //! Duplicates a matrix. /*! \param A matrix to be copied. \note Memory is duplicated: 'A' is therefore independent from the current instance after the copy. */ template <class T, class Prop, class Storage, class Allocator> inline void Matrix_Pointers<T, Prop, Storage, Allocator> ::Copy(const Matrix_Pointers<T, Prop, Storage, Allocator>& A) { this->Reallocate(A.GetM(), A.GetN()); this->allocator_.memorycpy(this->data_, A.GetData(), this->GetDataSize()); } /************************ * CONVENIENT FUNCTIONS * ************************/ //! Sets all elements to zero. /*! \warning It fills the memory with zeros. If the matrix stores complex structures, use 'Fill' instead. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::Zero() { this->allocator_.memoryset(this->data_, char(0), this->GetDataSize() * sizeof(value_type)); } //! Sets the current matrix to the identity. /*! \warning It fills the memory with zeros. If the matrix stores complex structures, discard this method. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::SetIdentity() { this->allocator_.memoryset(this->data_, char(0), this->GetDataSize() * sizeof(value_type)); T one(1); for (int i = 0; i < min(this->m_, this->n_); i++) (*this)(i,i) = one; } //! Fills the matrix the matrix with 0, 1, 2, ... /*! On exit, the matrix is filled with 0, 1, 2, 3, ... The order of those numbers depends on the storage. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::Fill() { for (int i = 0; i < this->GetDataSize(); i++) this->data_[i] = i; } //! Fills the matrix with a given value. /*! \param x the value to fill the matrix with. */ template <class T, class Prop, class Storage, class Allocator> template <class T0> void Matrix_Pointers<T, Prop, Storage, Allocator>::Fill(const T0& x) { for (int i = 0; i < this->GetDataSize(); i++) this->data_[i] = x; } //! Fills the matrix with a given value. /*! \param x the value to fill the matrix with. */ template <class T, class Prop, class Storage, class Allocator> template <class T0> Matrix_Pointers<T, Prop, Storage, Allocator>& Matrix_Pointers<T, Prop, Storage, Allocator>::operator= (const T0& x) { this->Fill(x); return *this; } //! Fills the matrix randomly. /*! \note The random generator is very basic. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::FillRand() { srand(time(NULL)); for (int i = 0; i < this->GetDataSize(); i++) this->data_[i] = rand(); } //! Displays the matrix on the standard output. /*! Displays elements on the standard output, in text format. Each row is displayed on a single line and elements of a row are delimited by tabulations. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::Print() const { for (int i = 0; i < this->m_; i++) { for (int j = 0; j < this->n_; j++) cout << (*this)(i, j) << "\t"; cout << endl; } } //! Displays a sub-matrix on the standard output. /*! The sub-matrix is defined by its upper-left corner (a, b) and its bottom-right corner (m, n). So, elements with indices in [a, m] x [b, n] are displayed on the standard output, in text format. Each row is displayed on a single line and elements of a row are delimited by tabulations. \param a row index of the upper-left corner. \param b column index of the upper-left corner. \param m row index of the bottom-right corner. \param n column index of the bottom-right corner. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::Print(int a, int b, int m, int n) const { for (int i = a; i < min(this->m_, a+m); i++) { for (int j = b; j < min(this->n_, b+n); j++) cout << (*this)(i, j) << "\t"; cout << endl; } } //! Displays a square sub-matrix on the standard output. /*! The sub-matrix is defined by its bottom-right corner (l, l). So, elements with indices in [0, 0] x [l, l] are displayed on the standard output, in text format. Each row is displayed on a single line and elements of a row are delimited by tabulations. \param l dimension of the square matrix to be displayed. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::Print(int l) const { Print(0, 0, l, l); } /********* * NORMS * *********/ //! Returns the maximum (in absolute value) of the matrix. /*! \return The maximum (in absolute value) of the matrix. \note The name of this method is of course not relevant since the infinity norm of the matrix is something else. The name of this method will be GetMaxAbs in a next version. */ template <class T, class Prop, class Storage, class Allocator> typename Matrix_Pointers<T, Prop, Storage, Allocator>::value_type Matrix_Pointers<T, Prop, Storage, Allocator>::GetNormInf() const { value_type res = value_type(0); int i, j; for (i = 0; i<this->GetM(); i++) for (j = 0; j<this->GetN(); j++) { res = max(res, (*this)(i, j)); res = max(res, -(*this)(i, j)); } return res; } /************************** * INPUT/OUTPUT FUNCTIONS * **************************/ //! Writes the matrix in a file. /*! Stores the matrix in a file in binary format. The number of rows (integer) and the number of columns (integer) are written, and matrix elements are then written in the same order as in memory (e.g. row-major storage). \param FileName output file name. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator> ::Write(string FileName) const { ofstream FileStream; FileStream.open(FileName.c_str());#ifdef SELDON_CHECK_IO // Checks if the file was opened. if (!FileStream.is_open()) throw IOError("Matrix_Pointers::Write(string FileName)", string("Unable to open file \"") + FileName + "\".");#endif this->Write(FileStream); FileStream.close(); } //! Writes the matrix to an output stream. /*! Writes the matrix to an output stream in binary format. The number of rows (integer) and the number of columns (integer) are written, and matrix elements are then written in the same order as in memory (e.g. row-major storage). \param FileStream output stream. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator> ::Write(ofstream& FileStream) const {#ifdef SELDON_CHECK_IO // Checks if the stream is ready. if (!FileStream.good()) throw IOError("Matrix_Pointers::Write(ofstream& FileStream)", "Stream is not ready.");#endif FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&this->m_)), sizeof(int)); FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&this->n_)), sizeof(int)); FileStream.write(reinterpret_cast<char*>(this->data_), this->m_ * this->n_ * sizeof(value_type));#ifdef SELDON_CHECK_IO // Checks if data was written. if (!FileStream.good()) throw IOError("Matrix_Pointers::Write(ofstream& FileStream)", string("Output operation failed.") + string(" The output file may have been removed") + " or there is no space left on device.");#endif } //! Writes the matrix in a file. /*! Stores the matrix in a file in text format. Only matrix elements are written (not dimensions). Each row is written on a single line and elements of a row are delimited by tabulations. \param FileName output file name. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator> ::WriteText(string FileName) const { ofstream FileStream; FileStream.open(FileName.c_str());#ifdef SELDON_CHECK_IO // Checks if the file was opened. if (!FileStream.is_open()) throw IOError("Matrix_Pointers::WriteText(string FileName)", string("Unable to open file \"") + FileName + "\".");#endif this->WriteText(FileStream); FileStream.close(); } //! Writes the matrix to an output stream. /*! Writes the matrix to an output stream in text format. Only matrix elements are written (not dimensions). Each row is written on a single line and elements of a row are delimited by tabulations. \param FileStream output stream. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator> ::WriteText(ofstream& FileStream) const {#ifdef SELDON_CHECK_IO // Checks if the file is ready. if (!FileStream.good()) throw IOError("Matrix_Pointers::WriteText(ofstream& FileStream)", "Stream is not ready.");#endif int i, j; for (i = 0; i < this->GetM(); i++) { for (j = 0; j < this->GetN(); j++) FileStream << (*this)(i, j) << '\t'; FileStream << endl; }#ifdef SELDON_CHECK_IO // Checks if data was written. if (!FileStream.good()) throw IOError("Matrix_Pointers::WriteText(ofstream& FileStream)", string("Output operation failed.") + string(" The output file may have been removed") + " or there is no space left on device.");#endif } //! Reads the matrix from a file. /*! Reads a matrix stored in binary format in a file. The number of rows (integer) and the number of columns (integer) are read, and matrix elements are then read in the same order as it should be in memory (e.g. row-major storage). \param FileName input file name. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator>::Read(string FileName) { ifstream FileStream; FileStream.open(FileName.c_str());#ifdef SELDON_CHECK_IO // Checks if the file was opened. if (!FileStream.is_open()) throw IOError("Matrix_Pointers::Read(string FileName)", string("Unable to open file \"") + FileName + "\".");#endif this->Read(FileStream); FileStream.close(); } //! Reads the matrix from an input stream. /*! Reads a matrix in binary format from an input stream. The number of rows (integer) and the number of columns (integer) are read, and matrix elements are then read in the same order as it should be in memory (e.g. row-major storage). \param FileStream input stream. */ template <class T, class Prop, class Storage, class Allocator> void Matrix_Pointers<T, Prop, Storage, Allocator> ::Read(ifstream& FileStream) {#ifdef SELDON_CHECK_IO // Checks if the stream is ready. if (!FileStream.good()) throw IOError("Matrix_Pointers::Read(ifstream& FileStream)", "Stream is not ready.");#endif int new_m, new_n; FileStream.read(reinterpret_cast<char*>(&new_m), sizeof(int)); FileStream.read(reinterpret_cast<char*>(&new_n), sizeof(int)); this->Reallocate(new_m, new_n); FileStream.read(reinterpret_cast<char*>(this->data_), new_m * new_n * sizeof(value_type));#ifdef SELDON_CHECK_IO // Checks if data was read. if (!FileStream.good()) throw IOError("Matrix_Pointers::Read(ifstream& FileStream)", string("Output operation failed.") + string(" The intput file may have been removed") + " or may not contain enough data.");#endif } ////////////////////// // MATRIX<COLMAJOR> // ////////////////////// /**************** * CONSTRUCTORS * ****************/ //! Default constructor. /*! Builds a empty matrix. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, ColMajor, Allocator>::Matrix() throw(): Matrix_Pointers<T, Prop, ColMajor, Allocator>() { } //! Main constructor. /*! Builds a i by j full column-major matrix. \param i number of rows. \param j number of columns. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, ColMajor, Allocator>::Matrix(int i, int j): Matrix_Pointers<T, Prop, ColMajor, Allocator>(i, j) { } /***************** * OTHER METHODS * *****************/ //! Fills the matrix with a given value. /*! \param x the value to fill the matrix with. */ template <class T, class Prop, class Allocator> template <class T0> Matrix<T, Prop, ColMajor, Allocator>& Matrix<T, Prop, ColMajor, Allocator>::operator= (const T0& x) { this->Fill(x); return *this; } ////////////////////// // MATRIX<ROWMAJOR> // ////////////////////// /**************** * CONSTRUCTORS * ****************/ //! Default constructor. /*! Builds a empty matrix. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, RowMajor, Allocator>::Matrix() throw(): Matrix_Pointers<T, Prop, RowMajor, Allocator>() { } //! Main constructor. /*! Builds a i by j full row-major matrix. \param i number of rows. \param j number of columns. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, RowMajor, Allocator>::Matrix(int i, int j): Matrix_Pointers<T, Prop, RowMajor, Allocator>(i, j) { } /***************** * OTHER METHODS * *****************/ //! Fills the matrix with a given value. /*! \param x the value to fill the matrix with. */ template <class T, class Prop, class Allocator> template <class T0> Matrix<T, Prop, RowMajor, Allocator>& Matrix<T, Prop, RowMajor, Allocator>::operator= (const T0& x) { this->Fill(x); return *this; }} // namespace Seldon.#define SELDON_FILE_MATRIX_POINTERS_CXX#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -