Comment changes

This commit is contained in:
2020-12-11 13:11:28 +02:00
parent 450b73e8d9
commit 225837ed0e
7 changed files with 75 additions and 41 deletions
+1 -1
View File
@@ -22,7 +22,7 @@
#define V3 3
#define V4 4
// Fail-safe verision selection
// Fail-safe version selection
#if !defined CODE_VERSION
#define CODE_VERSION V4
#endif
+31 -15
View File
@@ -39,7 +39,7 @@ enum class MatrixType {
};
/*
* Forward type declerations
* Forward type declarations
*/
template<typename DataType, typename IndexType, MatrixType Type = MatrixType::SYMMETRIC> struct Matrix;
template<typename DataType, typename IndexType, MatrixType Type = MatrixType::SYMMETRIC> struct SpMat;
@@ -298,7 +298,7 @@ struct SpMat {
*/
DataType get_lin(IndexType i, IndexType j) {
IndexType idx; bool found;
std::tie(idx, found) =find_lin_idx(rows, col_ptr[j], col_ptr[j+1], i);
std::tie(idx, found) =find_place_idx(rows, col_ptr[j], col_ptr[j+1], i);
return (found) ? values[idx] : 0;
}
@@ -309,8 +309,9 @@ struct SpMat {
* If so we just change it to a new value. If not we add the item on the matrix.
*
* @note
* We don't increase the NNZ value of the struct. We expect the user has already
* change the NNZ value to the right one using @see capacity() function.
* When change a value, we don't increase the NNZ value of the struct. We expect the user has already
* change the NNZ value to the right one using @see capacity() function. When adding a value we
* increase the NNZ.
*
* @param i The row number
* @param j The column number
@@ -318,7 +319,7 @@ struct SpMat {
*/
DataType set(DataType v, IndexType i, IndexType j) {
IndexType idx; bool found;
std::tie(idx, found) = find_lin_idx(rows, col_ptr[j], col_ptr[j+1], i);
std::tie(idx, found) = find_place_idx(rows, col_ptr[j], col_ptr[j+1], i);
if (found)
return values[idx] = v; // we don't change NNZ even if we write "0"
else {
@@ -392,11 +393,13 @@ private:
* \param begin The vector's index to begin
* \param end The vector's index to end
* \param match What to search
* @return The index of the item or end on failure.
* \return An <index, status> pair.
* index is the index of the item or end if not found
* status is true if found, false otherwise
*/
std::pair<IndexType, bool> find_idx(const std::vector<IndexType>& v, IndexType begin, IndexType end, IndexType match) {
IndexType b = begin, e = end-1;
while (true) {
while (b <= e) {
IndexType m = (b+e)/2;
if (v[m] == match) return std::make_pair(m, true);
else if (b >= e) return std::make_pair(end, false);
@@ -417,8 +420,11 @@ private:
* \param begin The vector's index to begin
* \param end The vector's index to end
* \param match What to search
* \return An <index, status> pair.
* index is the index of the item or end if not found
* status is true if found, false otherwise
*/
std::pair<IndexType, bool> find_lin_idx(const std::vector<IndexType>& v, IndexType begin, IndexType end, IndexType match) {
std::pair<IndexType, bool> find_place_idx(const std::vector<IndexType>& v, IndexType begin, IndexType end, IndexType match) {
for ( ; begin < end ; ++begin) {
if (match == v[begin]) return std::make_pair(begin, true);
else if (match < v[begin]) return std::make_pair(begin, false);
@@ -437,7 +443,7 @@ private:
//! @{
std::vector<DataType> values {}; //!< vector to store the values of the matrix
std::vector<IndexType> rows{}; //!< vector to store the row information
std::vector<IndexType> col_ptr{1,0}; //!< vector to stor the column pointers
std::vector<IndexType> col_ptr{1,0}; //!< vector to store the column pointers
IndexType N{0}; //!< The dimension of the matrix (square)
IndexType NNZ{0}; //!< The NNZ (capacity of the matrix)
//! @}
@@ -496,20 +502,25 @@ struct SpMatCol {
/*!
* Multiplication operator
*
* We follow only the non-zero values and multiply only the common indexes.
*
* @tparam C Universal reference for the type right half site column
*
* @param c The right hand site matrix
* @return The value of the inner product of two vectors
* @note The time complexity is \$ O(nnz1+nnz2) \$.
* Where the nnz is the max NNZ elements of the column of the matrix
*/
template <typename C>
DataType operator* (C&& c) {
static_assert(std::is_same<remove_cvref_t<C>, SpMatCol<DataType, IndexType>>(), "");
DataType v{};
while (index() != end() && c.index() != c.end()) {
if (index() < c.index()) advance();
else if (index() > c.index()) ++c;
if (index() < c.index()) advance(); // advance me
else if (index() > c.index()) ++c; // advance other
else { //index() == c.index()
v += get() * *c;
v += get() * *c; // multiply and advance both
++c;
advance();
}
@@ -597,20 +608,25 @@ struct SpMatRow {
/*!
* Multiplication operator
*
* We follow only the non-zero values and multiply only the common indexes.
*
* @tparam C Universal reference for the type right half site column
*
* @param c The right hand site matrix
* @return The value of the inner product of two vectors
* @note The time complexity is \$ O(N+nnz2) \$ and way heavier the ColxCol multiplication.
* Where the nnz is the max NNZ elements of the column of the matrix
*/
template <typename C>
DataType operator* (C&& c) {
static_assert(std::is_same<remove_cvref_t<C>, SpMatCol<DataType, IndexType>>(), "");
DataType v{};
while (index() != end() && c.index() != c.end()) {
if (index() < c.index()) advance();
else if (index() > c.index()) ++c;
if (index() < c.index()) advance(); // advance me
else if (index() > c.index()) ++c; // advance other
else { //index() == c.index()
v += get()* *c;
v += get() * *c; // multiply and advance both
++c;
advance();
}