📄 mmat_06.cc
字号:
(const MMatrix<ISIP_TEMPLATE_TARGET>&, ISIP_TEMPLATE_T1);// method: maxMag//// arguments:// const MMatrix<TScalar, TIntegral>& this: (output) class operand// long& row_pos: (output) row position of maximum mag// long& col_pos: (output) col position of maximum mag//// return: a double value containing the magnitude of the maximum// magnitude element//// this method returns the magnitude, row index and column index of the// maximum magnitude element of the matrix//template<class TScalar, class TIntegral>double MMatrixMethods::maxMag(const MMatrix<TScalar, TIntegral>& this_a, long& row_pos_a, long& col_pos_a) { // condition: empty input matrix // if ((this_a.getNumRows() < 1) || (this_a.getNumColumns() < 1)) { this_a.debug(L"this_a"); return Error::handle(name(), L"maxMag", Error::ARG, __FILE__, __LINE__); } // declare local variables // long position; // get the element with maximum magnitude // double value = this_a.m_d.maxMag(position); // get the row and column indices of the maximum magnitude element // if ((double)0 == value) { // find the first zero element // this_a.nextZero(row_pos_a, col_pos_a, -1, -1); } else { // get the row index and column index of the element // this_a.reverseIndex(row_pos_a, col_pos_a, position); } // exit gracefully // return value;}// explicit instantiations//template doubleMMatrixMethods::maxMag<ISIP_TEMPLATE_TARGET>(const MMatrix<ISIP_TEMPLATE_TARGET>&, long&, long&);// method: minMag//// arguments:// const MMatrix<TScalar, TIntegral>& this: (output) class operand// long& row_pos: (output) row position of minimum Magnitude// long& col_pos: (output) col position of minimum Magnitude//// return: a double value containing the value of the minimum// magnitude element//// this method returns the magnitude, row index and column index of the// minimum magnitude element of the matrix//template<class TScalar, class TIntegral>double MMatrixMethods::minMag(const MMatrix<TScalar, TIntegral>& this_a, long& row_pos_a, long& col_pos_a) { // condition: empty input matrix // if ((this_a.getNumRows() < 1) || (this_a.getNumColumns() < 1)) { this_a.debug(L"this_a"); return Error::handle(name(), L"minMag", Error::ARG, __FILE__, __LINE__); } // type: DIAGONAL, LOWER_TRIANGULAR, UPPER_TRIANGULAR, SPARSE // for diagonal, triangular and sparse matrices, if the maximum value is // zero and dimensions are greater than 1, then find the position of // first zero element of the matrix // if (((TIntegral)(this_a.nrows_d) > 1) && ((this_a.type_d == Integral::DIAGONAL) || (this_a.type_d == Integral::LOWER_TRIANGULAR) || (this_a.type_d == Integral::UPPER_TRIANGULAR) || (this_a.type_d == Integral::SPARSE))) { // get the row index and column index of the first zero element // if (this_a.nextZero(row_pos_a, col_pos_a, -1, -1)) { // exit gracefully // return (double)0; } } // declare the local variables // long position; // get the element with minimum magnitude // double value = this_a.m_d.minMag(position); // get the row index and column index of the element // this_a.reverseIndex(row_pos_a, col_pos_a, position); // exit gracefully // return value;}// explicit instantiations//template doubleMMatrixMethods::minMag<ISIP_TEMPLATE_TARGET>(const MMatrix<ISIP_TEMPLATE_TARGET>&, long&, long&);// method: max//// arguments:// const MMatrix<TScalar, TIntegral>& this: (output) class operand// long& row_pos: (output) row position of maximum// long& col_pos: (output) col position of maximum//// return: a TIntegral value containing the value of the maximum element//// this method finds the maximum value and its position//template<class TScalar, class TIntegral>TIntegral MMatrixMethods::max(const MMatrix<TScalar, TIntegral>& this_a, long& row_pos_a, long& col_pos_a) { // condition: empty input matrix // if ((this_a.getNumRows() < 1) || (this_a.getNumColumns() < 1)) { this_a.debug(L"this_a"); return Error::handle(name(), L"max", Error::ARG, __FILE__, __LINE__); } // declare local variables // long position; // get the element with maximum value // TIntegral value = this_a.m_d.max(position); // get the row and column indices of the maximum magnitude element // if ((TIntegral)0 >= value) { // type: DIAGONAL, LOWER_TRIANGULAR, UPPER_TRIANGULAR, SPARSE // for diagonal, triangular and sparse matrices, if the maximum value is // zero and dimensions are greater than 1, then find the position of // first zero element of the matrix // if (((TIntegral)this_a.nrows_d > 1) && ((this_a.type_d == Integral::DIAGONAL) || (this_a.type_d == Integral::LOWER_TRIANGULAR) || (this_a.type_d == Integral::UPPER_TRIANGULAR) || (this_a.type_d == Integral::SPARSE))) { // get the row index and column index of the first zero element // if (this_a.nextZero(row_pos_a, col_pos_a, -1, -1)) { // exit gracefully // return (TIntegral)0; } } } // there is no zero element in this matrix get the row index and // column index of the element // this_a.reverseIndex(row_pos_a, col_pos_a, position); // exit gracefully // return value;}// explicit instantiations//template ISIP_TEMPLATE_T1MMatrixMethods::max<ISIP_TEMPLATE_TARGET>(const MMatrix<ISIP_TEMPLATE_TARGET>&, long&, long&);// method: min//// arguments:// const MMatrix<TScalar, TIntegral>& this: (output) class operand// long& row_pos: (output) row position of minimum// long& col_pos: (output) col position of minimum//// return: a TIntegral value containing the value of the minimum element//// this method finds the minimum value and its position//template<class TScalar, class TIntegral>TIntegral MMatrixMethods::min(const MMatrix<TScalar, TIntegral>& this_a, long& row_pos_a, long& col_pos_a) { // condition: empty input matrix // if ((this_a.getNumRows() < 1) || (this_a.getNumColumns() < 1)) { this_a.debug(L"this_a"); return Error::handle(name(), L"min", Error::ARG, __FILE__, __LINE__); } // declare local variables // long position; // get the element with minimum value // TIntegral value = this_a.m_d.min(position); // get the row and column indices of the minimum valued element //#ifndef ISIP_TEMPLATE_unsigned if ((TIntegral)0 <= value) {#endif // type: DIAGONAL, LOWER_TRIANGULAR, UPPER_TRIANGULAR, SPARSE // for diagonal, triangular and sparse matrices, if the maximum value is // zero and dimensions are greater than 1, then find the position of // first zero element of the matrix // if (((TIntegral)this_a.nrows_d > 1) && ((this_a.type_d == Integral::DIAGONAL) || (this_a.type_d == Integral::LOWER_TRIANGULAR) || (this_a.type_d == Integral::UPPER_TRIANGULAR) || (this_a.type_d == Integral::SPARSE))) { // get the row index and column index of the first zero element // if (this_a.nextZero(row_pos_a, col_pos_a, -1, -1)) { // exit gracefully // return (TIntegral)0; } } #ifndef ISIP_TEMPLATE_unsigned }#endif // there is no zero element in this matrix, get the row and column // index of the element // this_a.reverseIndex(row_pos_a, col_pos_a, position); // exit gracefully // return value;}// explicit instantiations//template ISIP_TEMPLATE_T1MMatrixMethods::min<ISIP_TEMPLATE_TARGET>(const MMatrix<ISIP_TEMPLATE_TARGET>&, long&, long&);// method: concatByColumn//// arguments:// MMatrix<TScalar, TIntegral>& this: (output) class operand// const MMatrix<TScalar, TIntegral>& m: (input) MMatrix//// return: a boolean value indicating status//// this method concatenates the matrix "m_a" to "this" by adding// elements of "m_a" to the rows of this. note that the output of this// operation is ALWAYS either FULL or sparse, regardless of current// type or input types.//template<class TScalar, class TIntegral>booleanMMatrixMethods::concatByColumn(MMatrix<TScalar, TIntegral>& this_a, const MMatrix<TScalar, TIntegral>& m_a) { // condition: the numbers of rows of two matrices are not equal // if (this_a.getNumRows() != m_a.getNumRows()) { m_a.debug(L"m_a"); this_a.debug(L"this_a"); return Error::handle(name(), L"concatByColumn", Error::ARG, __FILE__, __LINE__); } // make a temporary copy of the matrix // MMatrix<TScalar, TIntegral> tmp(m_a); // type: SPARSE // for sparse matrices we do not change type to full // if (this_a.type_d == Integral::SPARSE) { // change types of input matrix to SPARSE. this way the output // type will be unchanged. // tmp.changeType(Integral::SPARSE); // get the length of the value vectors of the current and the // input matrix in order to get the length of the resultant matrix // long length = this_a.m_d.length() + tmp.m_d.length(); // declare local variables // long count = 0; // get the lengths of the two matrices // long len = this_a.m_d.length(); long last_j = tmp.m_d.length(); MMatrix<TScalar, TIntegral> temp(0, 0, Integral::SPARSE); // set the length of the three sparse vectors // temp.m_d.setLength(length); temp.row_index_d.setLength(length); temp.col_index_d.setLength(length); // loop through the number of rows of the input matrix // for (long row = 0; row < tmp.nrows_d; row++) { // loop through the elements of current matrix // for (long i = 0; i < len; i++) { // check row indices of non-zero elements // if (row == this_a.row_index_d(i)) { // add values to all the three vectors of the temporary matrix // temp.m_d(count) = this_a.m_d(i); temp.row_index_d(count) = this_a.row_index_d(i); temp.col_index_d(count) = this_a.col_index_d(i); count++; } } // loop through the elements of the input matrix // for (long j = 0; j < last_j; j++) { // check row indices of non-zero elements // if (row == tmp.row_index_d(j)) { // add values to all the three vectors of the temporary matrix // temp.m_d(count) = tmp.m_d(j); temp.row_index_d(count) = tmp.row_index_d(j); temp.col_index_d(count) = tmp.col_index_d(j) + this_a.ncols_d; count++; } } } // assign resultant values to the current matrix // this_a.nrows_d = tmp.nrows_d; this_a.ncols_d += tmp.ncols_d; this_a.m_d.setLength(length); this_a.row_index_d.setLength(length); this_a.col_index_d.setLength(length); // store elements, row index and column index in the current matrix // for (long k = 0; k < length; k++) { this_a.m_d(k) = temp.m_d(k); this_a.row_index_d(k) = temp.row_index_d(k); this_a.col_index_d(k) = temp.col_index_d(k); } // exit gracefully // return true; } // type: DIAGONAL, FULL, SYMMETRIC, LOWER_TRIANGULAR, UPPER_TRIANGULAR // else { // get the dimensions of the current matrix // long old_ncols = this_a.getNumColumns(); long new_nrows = this_a.getNumRows(); // get the new dimensions of the matrix // long add_ncols = tmp.getNumColumns(); long new_ncols = old_ncols + add_ncols; // declare local variables // long src_pos = 0; // change the dimension and type of the input and current matrices // this_a.setDimensions(new_nrows, new_ncols, true, Integral::FULL); tmp.changeType(Integral::FULL); // loop over the number of rows // for (long row_index = 0; row_index < new_nrows; row_index++) { // get the position of start column // long start_pos = this_a.index(row_index, old_ncols); // loop over the number of columns // for (long col_index = old_ncols; col_index < new_ncols; col_index++) { // assign data // this_a.m_d(start_pos) = tmp.m_d(src_pos); // increment the position pointer // start_pos++; src_pos++; } } // exit gracefully // return true; }}// explicit instantiations//template booleanMMatrixMethods::concatByColumn<ISIP_TEMPLATE_TARGET>(MMatrix<ISIP_TEMPLATE_TARGET>&, const MMatrix<ISIP_TEMPLATE_TARGET>&);// method: concatByRow//// arguments:// MMatrix<TScalar, TIntegral>& this: (output) class operand// const MMatrix<TScalar, TIntegral>& m: (input) MMatrix//// return: a boolean value indicating status//// this method concatenates the matrix "new_matrix" to "this" by// enlarging "this" by new_matrix.getNumRows(), and by assigning// "new_matrix" to the newly created rows in "this". note that the// output of this operation is ALWAYS either FULL or sparse,// regardless of current type or input types.//template<class TScalar, class TIntegral>boolean MMatrixMethods::concatByRow(MMatrix<TScalar, TIntegral>& this_a, const MMatrix<TScalar, TIntegral>& m_a) { // condition: the numbers of columns of two matrices are not equal // if (this_a.getNumColumns() != m_a.getNumColumns()) { m_a.debug(L"m_a"); this_a.debug(L"this_a"); return Error::handle(name(), L"concatByRow", Error::ARG, __FILE__, __LINE__); } // copy the matrix to a temporary matrix // MMatrix<TScalar, TIntegral> tmp(m_a); // for sparse matrix // if (this_a.type_d == Integral::SPARSE) { // change the type of the input matrices to SPARSE // tmp.changeType(Integral::SPARSE); // get the length of the value vectors of the current and the // input matrix in order to get the length of the resultant matrix // long length = this_a.m_d.length() + tmp.m_d.length(); // declare local variable // long count = 0; long last_j = tmp.m_d.length(); MMatrix<TScalar, TIntegral> temp(0, 0, Integral::SPARSE); // set the lengths of the three sparse vector to hold the elements // temp.m_d.setLength(length); temp.row_index_d.setLength(length); temp.col_index_d.setLength(length); // loop through the elements of current matrix // for (long i = 0; i < this_a.m_d.length(); i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -