Commit 2cde884c21264ad16406220074873b5121a2d046
1 parent
b2206691
Allow allocation of an empty matrix.
According to the property of the GMatrix class, the allocation of an empty matrix (i.e. with zero rows and columns) is now allowed.
Showing
1 changed file
with
28 additions
and
27 deletions
src/linalg/GSymMatrix.cpp
| ... | ... | @@ -942,8 +942,6 @@ std::string GSymMatrix::print(void) const |
| 942 | 942 | * |
| 943 | 943 | * @exception GException::matrix_not_symmetric |
| 944 | 944 | * Matrix is not symmetric. |
| 945 | - * @exception GException::empty | |
| 946 | - * Matrix is empty. | |
| 947 | 945 | * |
| 948 | 946 | * This is the main constructor code that allocates and initialises memory |
| 949 | 947 | * for matrix elements. |
| ... | ... | @@ -957,31 +955,34 @@ void GSymMatrix::constructor(int rows, int cols) |
| 957 | 955 | // Determine number of physical elements in matrix |
| 958 | 956 | int elements = rows*(rows+1)/2; |
| 959 | 957 | |
| 960 | - // Throw exception if requested matrix size is zero | |
| 961 | - if (elements == 0) | |
| 962 | - throw GException::empty(G_CONSTRUCTOR); | |
| 963 | - | |
| 964 | - // Allocate matrix array and column start index array. Throw an exception | |
| 965 | - // if allocation failed | |
| 966 | - m_data = new double[elements]; | |
| 967 | - m_colstart = new int[cols+1]; | |
| 968 | - m_inx = new int[cols]; | |
| 969 | - | |
| 970 | - // Store matrix size (logical and physical) | |
| 971 | - m_rows = rows; | |
| 972 | - m_cols = cols; | |
| 973 | - m_elements = elements; | |
| 974 | - m_alloc = elements; | |
| 975 | - | |
| 976 | - // Set-up column start indices | |
| 977 | - m_colstart[0] = 0; | |
| 978 | - int offset = rows; | |
| 979 | - for (int col = 1; col <= m_cols; ++col) | |
| 980 | - m_colstart[col] = m_colstart[col-1] + offset--; | |
| 981 | - | |
| 982 | - // Initialise matrix elements to 0.0 | |
| 983 | - for (int i = 0; i < m_elements; ++i) | |
| 984 | - m_data[i] = 0.0; | |
| 958 | + // Continue only if we have elements to store | |
| 959 | + if (elements > 0) { | |
| 960 | + | |
| 961 | + // Allocate matrix array and column start index array. Throw an exception | |
| 962 | + // if allocation failed | |
| 963 | + m_data = new double[elements]; | |
| 964 | + m_colstart = new int[cols+1]; | |
| 965 | + m_inx = new int[cols]; | |
| 966 | + | |
| 967 | + // Store matrix size (logical and physical) | |
| 968 | + m_rows = rows; | |
| 969 | + m_cols = cols; | |
| 970 | + m_elements = elements; | |
| 971 | + m_alloc = elements; | |
| 972 | + | |
| 973 | + // Set-up column start indices | |
| 974 | + m_colstart[0] = 0; | |
| 975 | + int offset = rows; | |
| 976 | + for (int col = 1; col <= m_cols; ++col) { | |
| 977 | + m_colstart[col] = m_colstart[col-1] + offset--; | |
| 978 | + } | |
| 979 | + | |
| 980 | + // Initialise matrix elements to 0.0 | |
| 981 | + for (int i = 0; i < m_elements; ++i) { | |
| 982 | + m_data[i] = 0.0; | |
| 983 | + } | |
| 984 | + | |
| 985 | + } // endif: we had elements | |
| 985 | 986 | |
| 986 | 987 | // Return |
| 987 | 988 | return; | ... | ... |