📄 mmat_06.cc
字号:
// copy the values, row index and column index // 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 input matrix // for (long j = 0; j < last_j; j++) { // copy values and increment row indexes // temp.m_d(count) = tmp.m_d(j); temp.row_index_d(count) = tmp.row_index_d(j) + this_a.nrows_d; temp.col_index_d(count) = tmp.col_index_d(j); count++; } // set the number of rows and columns of the matrix and the length of the // vectors of the 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); // assign the elements and their row and column indices from the // temporary matrix to 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; } // for diagonal, symmetric, lower triangle or upper triangle matrices the // type will be changed to full as matrix will no longer retain its type // else { // get the dimensions of the current matrix // long old_nrows = this_a.getNumRows(); long new_ncols = this_a.getNumColumns(); // get new dimensions of the matrix // long add_nrows = tmp.getNumRows(); long new_nrows = old_nrows + add_nrows; // create space in the current matrix // this_a.setDimensions(new_nrows, new_ncols, true, Integral::FULL); // change the type of the matrix to full // MMatrix<TScalar, TIntegral> tmp; tmp.assign(m_a, Integral::FULL); // get the start position // long start_pos = this_a.index(old_nrows, 0); for (long src_pos = 0; src_pos < tmp.m_d.length(); src_pos++) { // assign data // this_a.m_d(start_pos) = tmp.m_d(src_pos); // increment the position pointer // start_pos++; } // exit gracefully // return true; }}// explicit instantiations//template booleanMMatrixMethods::concatByRow<ISIP_TEMPLATE_TARGET>(MMatrix<ISIP_TEMPLATE_TARGET>&, const MMatrix<ISIP_TEMPLATE_TARGET>&);// method: reverseIndex//// arguments:// const MMatrix<TScalar, TIntegral>& this: (input) class operand// long& row_index: (output) converted row index// long& col_index: (output) converted column index// long vec_index: (input) the index of vector in the matrix//// return: a boolean value indicating status//// this method gets the row index and column index of the element in// the matrix from the vector index//template<class TScalar, class TIntegral>boolean MMatrixMethods::reverseIndex(const MMatrix<TScalar, TIntegral>& this_a, long& row_index_a, long& col_index_a, long vec_index_a) { // condition: empty input matrix // if (((TIntegral)this_a.nrows_d < 1) || ((TIntegral)this_a.ncols_d < 1)) { this_a.debug(L"this_a"); return Error::handle(name(), L"reverseIndex", Error::ARG, __FILE__, __LINE__); } // condition: invalid argument // if (vec_index_a > this_a.m_d.length()) { this_a.debug(L"this_a"); return Error::handle(name(), L"reverseIndex", Error::ARG, __FILE__, __LINE__); } // type: FULL // if (this_a.type_d == Integral::FULL) { // calculate the row index and column index // row_index_a = vec_index_a / this_a.ncols_d; col_index_a = vec_index_a - row_index_a * this_a.ncols_d; // exit gracefully // return true; } // type: DIAGONAL // else if (this_a.type_d == Integral::DIAGONAL) { // in case of the diagonal matrix, row and column indices are the // same as vector index // row_index_a = vec_index_a; col_index_a = vec_index_a; // exit gracefully // return true; } // type: SYMMETRIC, LOWER_TRIANGULAR // else if ((this_a.type_d == Integral::SYMMETRIC) || (this_a.type_d == Integral::LOWER_TRIANGULAR)) { // loop through the matrix rows to calculate the row index and // column indices // for (long index = 1; index <= this_a.nrows_d; index++) { // whether the element is in this row // if (vec_index_a < index) { // in case of the symmetric matrix, we return the element which // occurs first while scanning the matrix row-wise. the element, // which is stored on the vector, is the second element so we swap // the row and column indices of this element to get the element // in the upper triangle. // if (this_a.type_d == Integral::SYMMETRIC) { // get the row index and column index which are in upper triangle // row_index_a = vec_index_a; col_index_a = index - 1; } // lower triangle matrix // else { // get the row index and column index // row_index_a = index - 1; col_index_a = vec_index_a; } // exit gracefully // return true; } // subtract the row length from vector index // vec_index_a -= index; } // error: out of the range of matrix // this_a.debug(L"this_a"); return Error::handle(name(), L"reverseIndex", MMatrix<TScalar, TIntegral>::ERR, __FILE__, __LINE__); } // type: UPPER_TRIANGULAR // else if (this_a.type_d == Integral::UPPER_TRIANGULAR) { // loop through the matrix columns to calculate the row index and // column indices // for (long index = 1; index <= this_a.ncols_d; index++) { // whether the element is in this column // if (vec_index_a < index) { // get the row index and column index which are in upper triangle // row_index_a = vec_index_a; col_index_a = index - 1; // exit gracefully // return true; } // subtract the column length from vector index // vec_index_a -= index; } // error: out of the range of matrix // this_a.debug(L"this_a"); return Error::handle(name(), L"reverseIndex", MMatrix<TScalar, TIntegral>::ERR, __FILE__, __LINE__); } // type: SPARSE // in case of sparse matrix, return the row and column index from the // vectors containing row and column indices // else { // get row index and column index from row_index_d and col_index_d // row_index_a = this_a.row_index_d(vec_index_a); col_index_a = this_a.col_index_d(vec_index_a); // exit gracefully // return true; }}// explicit instantiations//template booleanMMatrixMethods::reverseIndex<ISIP_TEMPLATE_TARGET>(const MMatrix<ISIP_TEMPLATE_TARGET>&, long&, long&, long);// method: rand//// arguments:// MMatrix<TScalar, TIntegral>& this: (output) class operand// Random& generator: (input) random number generator//// return: a boolean value indicating status//// this method initializes each element of the matrix with a uniformly// distributed random value//template<class TScalar, class TIntegral>boolean MMatrixMethods::rand(MMatrix<TScalar, TIntegral>& this_a, Random& generator_a) { // type: non-SPARSE // if (this_a.type_d != Integral::SPARSE) { this_a.m_d.rand(generator_a); } // type: SPARSE // else { // generate random locations of non-zero elements // this_a.randIndicesSparse(); // generate non-zero random numbers // long num_elements = this_a.m_d.length(); for (long index = 0; index < num_elements; index++) { TIntegral value; do { value = this_a.m_d(index).rand(generator_a); } while (value == 0); } } // exit gracefully // return true;}// explicit instantiations//template booleanMMatrixMethods::rand<ISIP_TEMPLATE_TARGET>(MMatrix<ISIP_TEMPLATE_TARGET>&, Random&);// method: rand//// arguments:// MMatrix<TScalar, TIntegral>& this: (output) class operand// TIntegral min: (input) min value// TIntegral max: (input) max value// Random& generator: (input) random number generator//// return: a boolean value indicating status//// this method initializes each element of the matrix with a uniformly// distributed random value between min_a and max_a//template<class TScalar, class TIntegral>boolean MMatrixMethods::rand(MMatrix<TScalar, TIntegral>& this_a, TIntegral min_a, TIntegral max_a, Random& generator_a) { // type: non-SPARSE // if (this_a.type_d != Integral::SPARSE) { this_a.m_d.rand(min_a, max_a, generator_a); } // type: SPARSE // else { // generate random locations of non-zero elements // this_a.randIndicesSparse(); // generate non-zero random numbers // long num_elements = this_a.m_d.length(); for (long index = 0; index < num_elements; index++) { TIntegral value; do { value = this_a.m_d(index).rand(min_a, max_a, generator_a); } while (value == 0); } } // exit gracefully // return true;}// explicit instantiations//template boolean MMatrixMethods::rand<ISIP_TEMPLATE_TARGET>(MMatrix<ISIP_TEMPLATE_TARGET>&, ISIP_TEMPLATE_T1, ISIP_TEMPLATE_T1, Random&);// method: grand//// arguments:// MMatrix<TScalar, TIntegral>& this: (output) class operand// TIntegral mean: (input) mean value// TIntegral stdev: (input) standard deviation// Random& generator: (input) random number generator//// return: a boolean value indicating status//// this method initializes each element of the matrix with a uniformly// distributed gaussian random value//template<class TScalar, class TIntegral>boolean MMatrixMethods::grand(MMatrix<TScalar, TIntegral>& this_a, TIntegral mean_a, TIntegral stdev_a, Random& generator_a) { // type: non-SPARSE // if (this_a.type_d != Integral::SPARSE) { return this_a.m_d.grand(mean_a, stdev_a, generator_a); } // type: SPARSE // else { // generate random locations of non-zero elements // this_a.randIndicesSparse(); // generate non-zero random numbers // long num_elements = this_a.m_d.length(); for (long index = 0; index < num_elements; index++) { TIntegral value; do { value = this_a.m_d(index).grand(mean_a, stdev_a, generator_a); } while (value == 0); } } // exit gracefully // return true;}// explicit instantiations//template boolean MMatrixMethods::grand<ISIP_TEMPLATE_TARGET>(MMatrix<ISIP_TEMPLATE_TARGET>&, ISIP_TEMPLATE_T1, ISIP_TEMPLATE_T1, Random&);// method: randIndicesSparse//// arguments:// MMatrix<TScalar, TIntegral>& this: (output) class operand// Random& generator: (input) random number generator//// return: a boolean value containing the status// // this method generates randomized locations for non-zero elements// in a sparse matrix.//template<class TScalar, class TIntegral>boolean MMatrixMethods::randIndicesSparse(MMatrix<TScalar, TIntegral>& this_a, Random& generator_a) { // generate a random length using the sparseness threshold: generate // a length that is, on the average, close to the sparseness threshold // Long val; long max_nelem = this_a.nrows_d * this_a.ncols_d; long spr_nelem = (long)(2 * max_nelem * (1 - MMatrix<TScalar, TIntegral>::THRESH_SPARSE)); long nelem = val.rand(1, spr_nelem, generator_a); // set the dimensions // this_a.m_d.setLength(nelem); this_a.row_index_d.setLength(nelem); this_a.col_index_d.setLength(nelem); // generate some random indices by: // (1) generating a pair of indices // (2) checking if these already exist // (3) if they do, try again; if not, continue // for (long index = 0; index < nelem; index++) { // keep generating a pair of random indices until we find a unique pair // while (1) { // generate values // long row = val.rand(0, (long)this_a.nrows_d - 1); long col = val.rand(0, (long)this_a.ncols_d - 1); // check if this pair is unique // boolean found = false; for (long i = 0; i < index; i++) { if ((this_a.row_index_d(i) == row) && (this_a.col_index_d(i) == col)) { found = true; break; } } // if found is false, they are unique, and we can break // if (!found) { this_a.row_index_d(index) = row; this_a.col_index_d(index) = col; break; } } } // sort the indices // this_a.sortSparse(); // exit gracefully // return true;}// explicit instantiations//template boolean MMatrixMethods::randIndicesSparse<ISIP_TEMPLATE_TARGET>(MMatrix<ISIP_TEMPLATE_TARGET>&, Random&);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -