📄 mmat_00.cc
字号:
// file: $isip/class/math/matrix/MMatrix/mmat_00.cc// version: $Id: mmat_00.cc,v 1.64 2002/07/26 20:07:06 hamaker Exp $//// system include files//#include <typeinfo>// isip include files// #include "MMatrix.h"#include <Console.h>#include <VectorLong.h>//-----------------------------------------------------------------------------//// we define non-integral constants in the default constructor// //-----------------------------------------------------------------------------// constants: required constants such as the class name//template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::CLASS_NAME(L"MMatrix");// constants: i/o related constants//template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::DEF_PARAM(L"");template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::PARAM_DATA(L"values");template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::PARAM_NROWS(L"num_rows");template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::PARAM_NCOLS(L"num_cols");template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::PARAM_ROW_IND(L"row_indices");template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::PARAM_COL_IND(L"col_indices");template<class TScalar, class TIntegral>const String MMatrix<TScalar, TIntegral>::PARAM_TYPE(L"type");// constants: default value//template<class TScalar, class TIntegral>const TIntegral MMatrix<TScalar, TIntegral>::DEF_VALUE(TScalar::DEF_VALUE);// constants: matrix type-related constants//// note that unchanged and unknown are not included, since they are// not valid matrix types (and only useful as arguments).//template<class TScalar, class TIntegral>const NameMap MMatrix<TScalar, TIntegral>::TYPE_MAP(L"FULL, DIAGONAL, SYMMETRIC, LOWER_TRIANGULAR, UPPER_TRIANGULAR, SPARSE");// static instantiations: memory manager//template<class TScalar, class TIntegral>Integral::DEBUG MMatrix<TScalar, TIntegral>::debug_level_d = Integral::NONE;//---------------------------------------------------------------------------//// required public methods////---------------------------------------------------------------------------// method: debug//// arguments:// const String& name: (input) class name// const unichar* message: (input) message//// return: a boolean value indicating status//template<class TScalar, class TIntegral>boolean MMatrix<TScalar, TIntegral>::debug(const String& name_a, const unichar* message_a) const { // declare local strings to hold the class data // String output; String value; String param; // display the type // output.debugStr(name_a, message_a, L"type_d", TYPE_MAP((long)type_d)); Console::put(output); // display the number of rows // value.assign((long)nrows_d); output.debugStr(name_a, message_a, L"nrows_d", value); Console::put(output); // display the number of columns // value.assign((long)ncols_d); output.debugStr(name_a, message_a, L"ncols_d", value); Console::put(output); // display the elements of the matrix // output.debugStr(name_a, message_a, L"m_d"); Console::put(output); // loop over all rows // for (long i = 0; i < nrows_d; i++) { // output starting string // output.assign(L"[ "); // loop over columns and output each element // for (long j = 0; j < ncols_d; j++) { value.assign(getValue(i, j)); output.concat(value); output.concat(L" "); } // output terminating string // output.concat(L"]\n"); Console::put(output); } // for the really inquisitive user, display more detail about // the internal vectors // if (debug_level_d > Integral::DETAILED) { m_d.debug(L"m_d"); row_index_d.debug(L"row_index_d"); col_index_d.debug(L"col_index_d"); } // exit gracefully // return true;}// method: default constructor//// arguments: none//// return: none//// this method constructs a matrix object with default values//template<class TScalar, class TIntegral>MMatrix<TScalar, TIntegral>::MMatrix() { // set the type // type_d = Integral::DEF_MTYPE; // initialize the non-vector values just in case their default // initial values are non-zero. note that the other vector-valued // objects are initialized to zero. // nrows_d = 0; ncols_d = 0; // exit gracefully //}// method: copy constructor//// arguments:// const MMatrix& arg: (input) MMatrix//// return: none// // this method constructs a matrix that is a copy of the input matrix.// since it is a copy constructor that will ultimately call the assign// method, we only need initialize integral types that are not// initialized by default constructors.//template<class TScalar, class TIntegral>MMatrix<TScalar, TIntegral>::MMatrix(const MMatrix& arg_a) { // set the type // type_d = Integral::DEF_MTYPE; // copy the data from input matrix // assign(arg_a); // exit gracefully // }//---------------------------------------------------------------------------//// class-specific public methods:// extensions to required methods////---------------------------------------------------------------------------// method: eq//// argument(s):// const MMatrix& arg: (input) operand//// return: a boolean value indicating status//// this method checks if the current matrix is identical to the input matrix//template<class TScalar, class TIntegral>boolean MMatrix<TScalar, TIntegral>::eq(const MMatrix& arg_a) const { // check the Dimensions // if (!checkDimensions(arg_a)) { return false; } // if the types of the matrices are equal, we can check their // internal data directly // if (type_d == arg_a.type_d) { // for full, symmetric, diagonal, lower, and upper triangular matrices, // check the internal data directly // if ((arg_a.type_d == Integral::FULL) || (arg_a.type_d == Integral::DIAGONAL) || (arg_a.type_d == Integral::LOWER_TRIANGULAR) || (arg_a.type_d == Integral::UPPER_TRIANGULAR) || (arg_a.type_d == Integral::SYMMETRIC)) { return m_d.eq(arg_a.m_d); } // for a sparse matrix, check the equality of each vector // that is part of the sparse matrix implementation // else if ((m_d.length() == arg_a.m_d.length()) && (m_d.eq(arg_a.m_d)) && (row_index_d.eq(arg_a.row_index_d)) && (col_index_d.eq(arg_a.col_index_d))) { return true; } // exit ungracefully: not equal // else { return false; } } // else: the matrices are different types, and we must check each matrix // element by element // else { // loop over all elements // long last_row_index = getNumRows(); long last_col_index = getNumColumns(); for (long row_index = 0; row_index < last_row_index; row_index++) { for (long col_index = 0; col_index < last_col_index; col_index++) { if (getValue(row_index, col_index) != arg_a.getValue(row_index, col_index)) { return false; } } } } // exit gracefully: they must be equal // return true;}// method: clear//// arguments:// Integral::CMODE cmode: (input) clear mode//// return: a boolean value indicating status//// based on the input mode, this method either resets the values of// the class data, or releases each element, or releases memory//template<class TScalar, class TIntegral>boolean MMatrix<TScalar, TIntegral>::clear(Integral::CMODE cmode_a) { // mode: RETAIN // set all elements to zero, but leave // the dimensions and capacity unchanged // if (cmode_a == Integral::RETAIN) { return assign((TIntegral)DEF_VALUE); } // mode: RESET // set the dimensions to zero, but leave the capacity unchanged // else if (cmode_a == Integral::RESET) { return setDimensions(0, 0, false, type_d); } // modes: RELEASE and FREE // set the capacity to zero, but leave the type unchanged // else { return (setDimensions(0, 0, false, type_d) && setCapacity(0, 0, false, type_d)); }}// method: constructor//// arguments:// long nrows: (input) number of rows// long ncols: (input) number of columns// Integral::MTYPE type: (input) type of the matrix//// return: none//// this method constructs a matrix having a specified number of rows,// number of columns and type//template<class TScalar, class TIntegral>MMatrix<TScalar, TIntegral>::MMatrix(long nrows_a, long ncols_a, Integral::MTYPE type_a) { // check the type // type_d = Integral::DEF_MTYPE; type_d = checkType(type_a); // initialize the non-vector values just in case their default // initial values are non-zero. note that the other vector-valued // objects are initialized to zero. // nrows_d = 0; ncols_d = 0; // check for square matrics // checkSquare(nrows_a, ncols_a, type_d); // set the dimensions of the matrix // setDimensions(nrows_a, ncols_a, false, type_d); // exit gracefully //}// method: assign//// arguments:// TIntegral value: (input) value to be assigned//// return: a boolean value indicating status//// this method assigns the input value to every element in the matrix.// in this version of assign, it was decided that the type of the// matrix would not be changed. hence, only the vectors need to be// assigned.//template<class TScalar, class TIntegral>boolean MMatrix<TScalar, TIntegral>::assign(TIntegral value_a) { // type: SPARSE // if (type_d == Integral::SPARSE) { // if the current matrix is a sparse matrix, and the value is zero, // then we essentially set the length to zero, because sparse matrices // only need to store non-zero values. // if (value_a == (TIntegral)0) { row_index_d.setLength(0); col_index_d.setLength(0); m_d.setLength(0); } // else: assign new values. this is going to be really inefficient // because a sparse matrix doesn't work well when all the values // are non-zero. nevertheless, let the user's wish prevail. // else { // increase the size of the vectors // long num_vals = nrows_d * ncols_d; row_index_d.setLength(num_vals); col_index_d.setLength(num_vals); m_d.setLength(num_vals); // create the indices // long index = 0; for (long row = 0; row < nrows_d; row++) { for (long col = 0; col < ncols_d; col++) { row_index_d(index) = row; col_index_d(index) = col; m_d(index) = value_a; index++; } } } // exit gracefully // return true; } // type: anything other than SPARSE // else { return m_d.assign(value_a); }}// method: setCapacity//// arguments:// const MMatrix& pmat: (input) prototype matrix// boolean preserve_values: (input) save old values// Integral::MTYPE type: (input) type of the new matrix//// return: a boolean value indicating status//// this method sets the capacity of the matrix to be the same as the// capacity of the input matrix. it saves the values of the matrix if// the boolean flag is true else it just creates space in the current// matrix.//template<class TScalar, class TIntegral>boolean MMatrix<TScalar, TIntegral>::setCapacity(const MMatrix& pmat_a, boolean preserve_values_a, Integral::MTYPE type_a) { // call the master function // return setCapacity(pmat_a.getNumRows(), pmat_a.getNumColumns(), preserve_values_a, type_a);}// method: setCapacity//// arguments:// long nrows: (input) new number of rows (for capacity)// long ncols: (input) new number of cols (for capacity)// boolean preserve_values: (input) save old values// Integral::MTYPE type: (input) type of the new matrix//// return: a boolean value indicating status//// this method sets the capacity of the matrix to the specified// number of rows and columns. it preserves the values in the matrix// only when the boolean flag is set to true.//template<class TScalar, class TIntegral>boolean MMatrix<TScalar, TIntegral>::setCapacity(long nrows_a, long ncols_a, boolean preserve_values_a, Integral::MTYPE type_a) { // condition: bad arguments // if ((nrows_a < 0) || (ncols_a < 0)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -