欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

matrix.h

开放源码的编译器open watcom 1.6.0版的源代码
H
第 1 页 / 共 2 页
字号:
void hack(CoolMatrix<double>);
//## add your type above
#include <cool/Envelope.h>    //## BC++ 3.1 bug prevents from moving to top

// Use envelope to avoid deep copy on return by value, and mutate in place
template<class Type>
inline CoolEnvelope< CoolMatrix<Type> > operator+ (const CoolMatrix<Type>&arg1,const CoolMatrix<Type>&arg2)
   { return CoolEnvOp(add)(arg1, arg2); }
template<class Type>
inline CoolEnvelope< CoolMatrix<Type> > operator- (const CoolMatrix<Type>&arg1,const CoolMatrix<Type>&arg2)
   { return CoolEnvOp(minus)(arg1, arg2); }


// get -- Get the element at specified index and return value
// Input: this*, row, column
// Output: Element value

template<class Type> 
inline Type& CoolMatrix<Type>::get (unsigned int row, unsigned int column) {
#if ERROR_CHECKING
  if (row >= this->num_rows)                    // If invalid size specified
    this->row_index_error ("get", #Type, row);  // Raise exception
  if (column >= this->num_cols)                 // If invalid size specified
    this->col_index_error ("get", #Type, column); // Raise exception
#endif
  return this->data[row][column];
}

// put -- Put the element value at specified index
// Input: *this, row, column, value
// Output: Element value

template<class Type> 
inline void CoolMatrix<Type>::put (unsigned int row, unsigned int column, const Type& value) {
#if ERROR_CHECKING
  if (row >= this->num_rows)                    // If invalid size specified
    this->row_index_error ("put", #Type, row);  // Raise exception
  if (column >= this->num_cols)                 // If invalid size specified
    this->col_index_error ("put", #Type, column); // Raise exception
#endif
  this->data[row][column] = value;              // Assign data value
}

// operator() -- Overload () to get the element at specified index
//               and return by reference
// Input: this*, row, column
// Output: Element value

template<class Type> 
inline Type& CoolMatrix<Type>::operator() (unsigned int row, unsigned int column) {
  return this->data[row][column];               // fast access without checks.
}


// operator=  -- Assignment from an envelope back to real matrix
// Input:     envelope reference
// Output:    matrix reference with contents in envelope being swapped over

template<class Type>
inline CoolMatrix<Type>& CoolMatrix<Type>::operator= (CoolEnvelope< CoolMatrix<Type> >& env){
  env.shallow_swap((CoolEnvelope< CoolMatrix<Type> >*)this, &env); // same physical layout
  return *this;
}

// operator<< -- Overload the output operator to print a CoolMatrix
// Input:        ostream reference, CoolMatrix pointer
// Output:       ostream reference

template<class Type>
inline ostream& operator<< (ostream& os, const CoolMatrix<Type>* m) {
  if (m) os << *m;
  return os;
}


// operator!= -- Perform not equal comparison test
// Input:        this*, matrix reference
// Output:       TRUE/FALSE

template<class Type> 
inline Boolean CoolMatrix<Type>::operator!= (const CoolMatrix<Type>& m) const {
  return (!operator== (m));
}

// operator-= -- Destructive matrix subtraction of a scalar.
// Input:        this*, scalar value
// Output:       New matrix reference

template<class Type> 
inline CoolMatrix<Type>& CoolMatrix<Type>::operator-= (const Type& value) {
  return *this += (- value);
}


template<class Type>
inline CoolEnvelope< CoolMatrix<Type> > operator+ (const Type& value,
                                    const CoolMatrix<Type>& m) {
  return m + value;
}

// operator- -- Non-destructive matrix substraction of a scalar.
// Input:       this*, scalar value
// Output:      New matrix 

template<class Type> 
inline CoolEnvelope< CoolMatrix<Type> > CoolMatrix<Type>::operator-(const Type& value) const {
  return (*this) + (- value);
}

template<class Type>
inline CoolEnvelope< CoolMatrix<Type> > operator- (const Type& value,
                                    const CoolMatrix<Type>& m) {
  return (- m) + value;
}


template<class Type>
inline CoolEnvelope< CoolMatrix<Type> > operator* (const Type& value,
                                    const CoolMatrix<Type>& m) {
  return m * value;
}

template<class Type>
inline CoolMatrix<Type>& CoolMatrix<Type>::operator*= (const CoolMatrix<Type>&m) {
  *this = (*this) * m;                          // multiply, then shallow swap
  return *this;
}

////--------------------------  Vector ----------------------------------
//// use a column matrix to represent 1d vector

template<class Type>                            
inline Type& CoolMatrix<Type>::x(){
  return data[0][0];
}               

template<class Type>
inline Type& CoolMatrix<Type>::y() {
  return data[1][0];
}

template<class Type>
inline Type& CoolMatrix<Type>::z() {
  return data[2][0];
}               

template<class Type>
inline Type& CoolMatrix<Type>::t() {
  return data[3][0];
}       


// template<class Type>                         // use a row matrix to
// inline Type& CoolMatrix<Type>::x() {         // represent 1d vector
//   return data[0][0];
// }            
// 
// template<class Type>
// inline Type& CoolMatrix<Type>::y() {
//   return data[0][1];
// }
// 
// template<class Type>
// inline Type& CoolMatrix<Type>::z() {
//   return data[0][2];
// }            
// 
// template<class Type>
// inline Type& CoolMatrix<Type>::t() {
//   return data[0][3];
// }    

// data_block -- Provide access to the contiguous block storing 
//               the elements in the matrix row-wise.
//               Use this only to cast matrix contents,
//               to C array like: Type [rows][columns].

template<class Type>
inline const Type* CoolMatrix<Type>::data_block () {
  return data[0];                               // start of array of data
}

//## hack to workaround BC++ 3.1 Envelope bug
#undef CoolEnvelope

#endif                                          // End of MATRIXH


⌨️ 快捷键说明

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