Skip to content

Commit

Permalink
Operations on general matrices: randomize(), operator==(), assign(), …
Browse files Browse the repository at this point in the history
…operator<<()
  • Loading branch information
Mikhail Katliar authored and mkatliar committed Sep 24, 2024
1 parent 9d9f9ce commit 5fd2387
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions include/blast/math/Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
#include <blast/math/dense/StaticMatrixPointer.hpp>
#include <blast/math/panel/DynamicPanelMatrixPointer.hpp>
#include <blast/math/panel/StaticPanelMatrixPointer.hpp>
#include <blast/util/Types.hpp>
#include <blast/system/Inline.hpp>

#include <iosfwd>
#include <stdexcept>
#include <random>


namespace blast
{
Expand Down Expand Up @@ -44,4 +49,65 @@ namespace blast
{
return ptr<IsAligned_v<MT>>(m, 0, 0);
}


template <Matrix M>
inline void randomize(M& m) noexcept
{
std::mt19937 rng;
std::uniform_real_distribution<ElementType_t<M>> dist;

for (size_t i = 0; i < rows(m); ++i)
for (size_t j = 0; j < columns(m); ++j)
m(i, j) = dist(rng);
}


template <Matrix MA, Matrix MB>
inline bool operator==(MA const& a, MB const& b)
{
size_t const M = rows(a);
size_t const N = columns(a);

if (M != rows(b) || N != columns(b))
throw std::invalid_argument {"Inconsistent matrix sizes"};

for (size_t i = 0; i < M; ++i)
for (size_t j = 0; j < N; ++j)
if (a(i, j) != b(i, j))
return false;

return true;
}


template <Matrix MA, Matrix MB>
inline MA& assign(MA& a, MB const& b)
{
size_t const M = rows(a);
size_t const N = columns(a);

if (M != rows(b) || N != columns(b))
throw std::invalid_argument {"Inconsistent matrix sizes"};

for (size_t i = 0; i < M; ++i)
for (size_t j = 0; j < N; ++j)
a(i, j) = b(i, j);

return a;
}


template <Matrix M>
inline std::ostream& operator<<(std::ostream& os, M const& m)
{
for (size_t i = 0; i < rows(m); ++i)
{
for (size_t j = 0; j < columns(m); ++j)
os << m(i, j) << "\t";
os << std::endl;
}

return os;
}
}
1 change: 1 addition & 0 deletions include/blast/math/typetraits/IsDenseMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <type_traits>


namespace blast
{
/**
Expand Down

0 comments on commit 5fd2387

Please sign in to comment.