Skip to content

Commit

Permalink
Overload multiply functions
Browse files Browse the repository at this point in the history
  • Loading branch information
heat1q committed Nov 6, 2020
1 parent aa46ed6 commit 17ec031
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/core/sparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ namespace ldpc

void read_from_file(const std::string &filename, int skipLines);

std::vector<T> multiply_left(const std::vector<T> &left) const; // for encoding
std::vector<T> multiply_right(const std::vector<T> &right) const; // for syndrome
void multiply_left(const std::vector<T> &left, std::vector<T> &result) const; // for encoding
std::vector<T> multiply_left(const std::vector<T> &left) const;

void multiply_right(const std::vector<T> &right, std::vector<T> &result) const; // for syndrome
std::vector<T> multiply_right(const std::vector<T> &right) const;

int num_cols() const { return numCols; }
int num_rows() const { return numRows; }
Expand Down Expand Up @@ -101,21 +104,32 @@ namespace ldpc
*
* @tparam T finite field
* @param left Row vector
* @return std::vector<T>
* @param result Result row vector
*/
template <typename T>
std::vector<T> sparse_csr<T>::multiply_left(const std::vector<T> &left) const
void sparse_csr<T>::multiply_left(const std::vector<T> &left, std::vector<T> &result) const
{
std::vector<T> result(numCols);

for (int j = 0; j < numCols; ++j)
{
for (const auto &n : colN[j])
{
result[j] += left[n.nodeIndex] * nonZeroVals[n.edgeIndex].value;
}
}
}

/**
* @brief Multiply vector from left handside with matrix over field T
*
* @tparam T finite field
* @param left Row vector
* @return std::vector<T> Result row vector
*/
template <typename T>
std::vector<T> sparse_csr<T>::multiply_left(const std::vector<T> &left) const
{
std::vector<T> result(numCols);
multiply_left(left, result);
return result;
}

Expand All @@ -124,21 +138,32 @@ namespace ldpc
*
* @tparam T finite field
* @param right Column vector
* @return std::vector<T>
* @param result Result column vector
*/
template <typename T>
std::vector<T> sparse_csr<T>::multiply_right(const std::vector<T> &right) const
void sparse_csr<T>::multiply_right(const std::vector<T> &right, std::vector<T> &result) const
{
std::vector<T> result(numRows);

for (int i = 0; i < numRows; ++i)
{
for (const auto &n : rowN[i])
{
result[i] += right[n.nodeIndex] * nonZeroVals[n.edgeIndex].value;
}
}
}

/**
* @brief Multiply vector from right handside with matrix over field T
*
* @tparam T finite field
* @param right Column vector
* @return std::vector<T> Result column vector
*/
template <typename T>
std::vector<T> sparse_csr<T>::multiply_right(const std::vector<T> &right) const
{
std::vector<T> result(numRows);
multiply_right(right, result);
return result;
}
} // namespace ldpc

0 comments on commit 17ec031

Please sign in to comment.