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

📄 matrix_triangular.cxx

📁 Multivac 的Level set包
💻 CXX
📖 第 1 页 / 共 3 页
字号:
  //! Access operator.  /*!    Returns the value of element (i, j).    \param i row index.    \param j column index.    \return Element (i, j) of the matrix.  */  template <class T, class Prop, class Storage, class Allocator>  inline typename Matrix_Triangular<T, Prop, Storage, Allocator>::value_type  Matrix_Triangular<T, Prop, Storage, Allocator>::operator() (int i, int j)  {#ifdef SELDON_CHECK_BOUNDARIES    if (i < 0 || i >= this->m_)      throw WrongRow("Matrix_Triangular::operator()",		     string("Index should be in [0, ") + to_str(this->m_-1)		     + "], but is equal to " + to_str(i) + ".");    if (j < 0 || j >= this->n_)      throw WrongCol("Matrix_Triangular::operator()",		     string("Index should be in [0, ") + to_str(this->n_-1)		     + "], but is equal to " + to_str(j) + ".");#endif    if (Storage::UpLo())      {	if (i > j)	  return T(0);	else	  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];      }    else      {	if (i < j)	  return T(0);	else	  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];      }  }   //! Access operator.  /*!    Returns the value of element (i, j).    \param i row index.    \param j column index.    \return Element (i, j) of the matrix.  */  template <class T, class Prop, class Storage, class Allocator>  inline typename Matrix_Triangular<T, Prop, Storage, Allocator>::value_type  Matrix_Triangular<T, Prop, Storage, Allocator>  ::operator() (int i, int j) const  {    #ifdef SELDON_CHECK_BOUNDARIES    if (i < 0 || i >= this->m_)      throw WrongRow("Matrix_Triangular::operator() const",		     string("Index should be in [0, ") + to_str(this->m_-1)		     + "], but is equal to " + to_str(i) + ".");    if (j < 0 || j >= this->n_)      throw WrongCol("Matrix_Triangular::operator() const",		     string("Index should be in [0, ") + to_str(this->n_-1)		     + "], but is equal to " + to_str(j) + ".");#endif    if (Storage::UpLo())      {	if (i > j)	  return T(0);	else	  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];      }    else      {	if (i < j)	  return T(0);	else	  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];      }  }  //! Access operator.  /*!    Returns the value of element (i, j).    \param i row index.    \param j column index.    \return Element (i, j) of the matrix.    \note It returns the value stored i, memory. Even if the value is in the    part of the matrix that should be filled with zeros, the value could be    non-zero.  */  template <class T, class Prop, class Storage, class Allocator>  inline typename Matrix_Triangular<T, Prop, Storage, Allocator>  ::const_reference  Matrix_Triangular<T, Prop, Storage, Allocator>::Val(int i, int j) const  {    #ifdef SELDON_CHECK_BOUNDARIES    if (i < 0 || i >= this->m_)      throw WrongRow("Matrix_Triangular::Val(int, int) const",		     string("Index should be in [0, ") + to_str(this->m_-1)		     + "], but is equal to " + to_str(i) + ".");    if (j < 0 || j >= this->n_)      throw WrongCol("Matrix_Triangular::Val(int, int) const",		     string("Index should be in [0, ") + to_str(this->n_-1)		     + "], but is equal to " + to_str(j) + ".");#endif    return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];  }  //! Access operator.  /*!    Returns the value of element (i, j).    \param i row index.    \param j column index.    \return Element (i, j) of the matrix.    \note It returns the value stored i, memory. Even if the value is in the    part of the matrix that should be filled with zeros, the value could be    non-zero.  */  template <class T, class Prop, class Storage, class Allocator>  inline typename Matrix_Triangular<T, Prop, Storage, Allocator>::reference  Matrix_Triangular<T, Prop, Storage, Allocator>::Val(int i, int j)  {    #ifdef SELDON_CHECK_BOUNDARIES    if (i < 0 || i >= this->m_)      throw WrongRow("Matrix_Triangular::Val(int, int)",		     string("Index should be in [0, ") + to_str(this->m_-1)		     + "], but is equal to " + to_str(i) + ".");    if (j < 0 || j >= this->n_)      throw WrongCol("Matrix_Triangular::Val(int, int)",		     string("Index should be in [0, ") + to_str(this->n_-1)		     + "], but is equal to " + to_str(j) + ".");#endif    return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];  }  //! 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_Triangular<T, Prop, Storage, Allocator>::reference  Matrix_Triangular<T, Prop, Storage, Allocator>::operator[] (int i)  {    #ifdef SELDON_CHECK_BOUNDARIES    if (i < 0 || i >= this->GetDataSize())      throw WrongIndex("Matrix_Triangular::operator[] (int)",		       string("Index should be in [0, ")		       + to_str(this->GetDataSize()-1) + "], but is equal to "		       + to_str(i) + ".");#endif        return this->data_[i];  }  //! 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_Triangular<T, Prop, Storage, Allocator>  ::const_reference  Matrix_Triangular<T, Prop, Storage, Allocator>::operator[] (int i) const  {    #ifdef SELDON_CHECK_BOUNDARIES    if (i < 0 || i >= this->GetDataSize())      throw WrongIndex("Matrix_Triangular::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_Triangular<T, Prop, Storage, Allocator>&  Matrix_Triangular<T, Prop, Storage, Allocator>  ::operator= (const Matrix_Triangular<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_Triangular<T, Prop, Storage, Allocator>  ::Copy(const Matrix_Triangular<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_Triangular<T, Prop, Storage, Allocator>::Zero()  {    this->allocator_.memoryset(this->data_, char(0),			       this->GetDataSize() * sizeof(value_type));  }  //! Sets the 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_Triangular<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->Val(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_Triangular<T, Prop, Storage, Allocator>::Fill()  {    for (int i = 0; i < this->GetDataSize(); i++)      this->data_[i] = i;  }  //! Fills the matrix with a given value.  /*!    On exit, the matrix is filled with 'x'.    \param x the value to fill the matrix with.  */  template <class T, class Prop, class Storage, class Allocator>  template <class T0>  void Matrix_Triangular<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.  /*!    On exit, the matrix is filled with 'x'.    \param x the value to fill the matrix with.  */  template <class T, class Prop, class Storage, class Allocator>  template <class T0>  Matrix_Triangular<T, Prop, Storage, Allocator>&  Matrix_Triangular<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_Triangular<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_Triangular<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_Triangular<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_Triangular<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_Triangular<T, Prop, Storage, Allocator>::value_type  Matrix_Triangular<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_Triangular<T, Prop, Storage, Allocator>  ::Write(string FileName) const

⌨️ 快捷键说明

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