📄 matrix_hermpacked.cxx
字号:
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 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_HermPacked<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 value to fill the matrix with. */ template <class T, class Prop, class Storage, class Allocator> template <class T0> void Matrix_HermPacked<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 value to fill the matrix with. */ template <class T, class Prop, class Storage, class Allocator> template <class T0> Matrix_HermPacked<T, Prop, Storage, Allocator>& Matrix_HermPacked<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_HermPacked<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_HermPacked<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_HermPacked<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_HermPacked<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_HermPacked<T, Prop, Storage, Allocator>::value_type Matrix_HermPacked<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_HermPacked<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_HermPacked::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_HermPacked<T, Prop, Storage, Allocator> ::Write(ofstream& FileStream) const { #ifdef SELDON_CHECK_IO // Checks if the file is ready. if (!FileStream.good()) throw IOError("Matrix_HermPacked::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->GetDataSize() * sizeof(value_type));#ifdef SELDON_CHECK_IO // Checks if data was written. if (!FileStream.good()) throw IOError("Matrix_HermPacked::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_HermPacked<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_HermPacked::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_HermPacked<T, Prop, Storage, Allocator> ::WriteText(ofstream& FileStream) const {#ifdef SELDON_CHECK_IO // Checks if the stream is ready. if (!FileStream.good()) throw IOError("Matrix_HermPacked::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_HermPacked::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_HermPacked<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.good()) throw IOError("Matrix_HermPacked::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_HermPacked<T, Prop, Storage, Allocator> ::Read(ifstream& FileStream) {#ifdef SELDON_CHECK_IO // Checks if the stream is ready. if (!FileStream.good()) throw IOError("Matrix_HermPacked::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_), this->GetDataSize() * sizeof(value_type));#ifdef SELDON_CHECK_IO // Checks if data was read. if (!FileStream.good()) throw IOError("Matrix_HermPacked::Read(ifstream& FileStream)", string("Output operation failed.") + string(" The intput file may have been removed") + " or may not contain enough data.");#endif } /////////////////////////// // MATRIX<COLHERMPACKED> // /////////////////////////// /**************** * CONSTRUCTORS * ****************/ //! Default constructor. /*! On exit, the matrix is an empty 0x0 matrix. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, ColHermPacked, Allocator>::Matrix(): Matrix_HermPacked<T, Prop, ColHermPacked, Allocator>() { } //! Main constructor. /*! Builds a i x j column-major hermitian matrix in packed form. \param i number of rows. \param j number of columns. \note 'j' is assumed to be equal to 'i' and is therefore discarded. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, ColHermPacked, Allocator>::Matrix(int i, int j): Matrix_HermPacked<T, Prop, ColHermPacked, Allocator>(i, j) { } /******************* * OTHER FUNCTIONS * *******************/ //! 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 Allocator> template <class T0> inline Matrix<T, Prop, ColHermPacked, Allocator>& Matrix<T, Prop, ColHermPacked, Allocator> ::operator= (const T0& x) { this->Fill(x); return *this; } /////////////////////////// // MATRIX<ROWHERMPACKED> // /////////////////////////// /**************** * CONSTRUCTORS * ****************/ //! Default constructor. /*! On exit, the matrix is an empty 0x0 matrix. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, RowHermPacked, Allocator>::Matrix(): Matrix_HermPacked<T, Prop, RowHermPacked, Allocator>() { } //! Main constructor. /*! Builds a i x j row-major hermitian matrix in packed form. \param i number of rows. \param j number of columns. \note 'j' is assumed to be equal to 'i' and is therefore discarded. */ template <class T, class Prop, class Allocator> Matrix<T, Prop, RowHermPacked, Allocator>::Matrix(int i, int j): Matrix_HermPacked<T, Prop, RowHermPacked, Allocator>(i, j) { } /******************* * OTHER FUNCTIONS * *******************/ //! 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 Allocator> template <class T0> inline Matrix<T, Prop, RowHermPacked, Allocator>& Matrix<T, Prop, RowHermPacked, Allocator> ::operator= (const T0& x) { this->Fill(x); return *this; }} // namespace Seldon.#define SELDON_FILE_MATRIX_HERMPACKED_CXX#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -