From a2458512ad6aafa93d1a9b36c21286ae4d656785 Mon Sep 17 00:00:00 2001 From: wrcorcoran Date: Wed, 25 Sep 2024 20:43:59 -0700 Subject: [PATCH] changing error handling --- include/CXXGraph/Graph/Algorithm/Pow_impl.hpp | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/include/CXXGraph/Graph/Algorithm/Pow_impl.hpp b/include/CXXGraph/Graph/Algorithm/Pow_impl.hpp index ade2fe71..3e11e5fc 100644 --- a/include/CXXGraph/Graph/Algorithm/Pow_impl.hpp +++ b/include/CXXGraph/Graph/Algorithm/Pow_impl.hpp @@ -46,11 +46,23 @@ std::vector> matMult(std::vector> &a, } template +/** + * @brief exponentation takes a matrix of arithmetic type and + * raises it to the power of k. + * @param mat a square matrix + * @param k a nonnegative integer + * @return M, the result of mat^k + */ std::vector> exponentiation(std::vector> &mat, unsigned int k) { static_assert(std::is_arithmetic::value, "Type T must be any artithmetic type"); + // validate size and shape of matrix + if (mat.size() == 0 || mat.size() != mat[0].size()) { + throw std::invalid_argument("Matrix must be square and at least 1x1."); + } + int n = static_cast(mat.size()); std::vector> res(n, std::vector(n, 0)); @@ -76,7 +88,8 @@ namespace CXXGraph { * @return (success, errorMessage, matrix): where matrix is equivalent to A^k */ template -const PowAdjResult matrixPow(const shared> &adj, unsigned int k) { +const PowAdjResult matrixPow(const shared> &adj, + unsigned int k) { PowAdjResult result; result.success = false; result.errorMessage = ""; @@ -106,9 +119,10 @@ const PowAdjResult matrixPow(const shared> &adj, unsigned int const auto edge = e.second->getNodePair(); const auto firstId = edge.first->getUserId(); const auto secondId = edge.second->getUserId(); - + // if undirected, add both sides - if (!(e.second->isDirected().has_value() && e.second->isDirected().value())) + if (!(e.second->isDirected().has_value() && + e.second->isDirected().value())) tempIntAdj[userIdToIdx[secondId]][userIdToIdx[firstId]] = 1; tempIntAdj[userIdToIdx[firstId]][userIdToIdx[secondId]] = 1; } @@ -138,7 +152,7 @@ const PowAdjResult matrixPow(const shared> &adj, unsigned int */ template const PowTransResult matrixPow(const shared> &trans, - unsigned int k) { + unsigned int k) { PowTransResult result; result.success = false; result.errorMessage = ""; @@ -159,8 +173,7 @@ const PowTransResult matrixPow(const shared> &trans, } std::vector> tempDoubleTrans(n, - std::vector(n, - 0)); + std::vector(n, 0)); // given transition matrix, convert it to // stochastic matrix