mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-14 07:23:26 +00:00
Merge pull request #72 from orange-cpp/feature/mat_refactor
Feature/mat refactor
This commit is contained in:
@@ -155,28 +155,11 @@ namespace omath
|
|||||||
constexpr Mat<Rows, OtherColumns, Type, StoreType>
|
constexpr Mat<Rows, OtherColumns, Type, StoreType>
|
||||||
operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const
|
operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const
|
||||||
{
|
{
|
||||||
Mat<Rows, OtherColumns, Type, StoreType> result;
|
|
||||||
|
|
||||||
if constexpr (StoreType == MatStoreType::ROW_MAJOR)
|
if constexpr (StoreType == MatStoreType::ROW_MAJOR)
|
||||||
for (std::size_t i = 0; i < Rows; ++i)
|
return cache_friendly_multiply_row_major(other);
|
||||||
for (std::size_t k = 0; k < Columns; ++k)
|
if constexpr (StoreType == MatStoreType::COLUMN_MAJOR)
|
||||||
{
|
return cache_friendly_multiply_col_major(other);
|
||||||
const Type aik = at(i, k);
|
std::unreachable();
|
||||||
for (std::size_t j = 0; j < OtherColumns; ++j)
|
|
||||||
result.at(i, j) += aik * other.at(k, j);
|
|
||||||
}
|
|
||||||
else if constexpr (StoreType == MatStoreType::COLUMN_MAJOR)
|
|
||||||
for (std::size_t j = 0; j < OtherColumns; ++j)
|
|
||||||
for (std::size_t k = 0; k < Columns; ++k)
|
|
||||||
{
|
|
||||||
const Type bkj = other.at(k, j);
|
|
||||||
for (std::size_t i = 0; i < Rows; ++i)
|
|
||||||
result.at(i, j) += at(i, k) * bkj;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
std::unreachable();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr Mat& operator*=(const Type& f) noexcept
|
constexpr Mat& operator*=(const Type& f) noexcept
|
||||||
@@ -378,6 +361,36 @@ namespace omath
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<Type, Rows * Columns> m_data;
|
std::array<Type, Rows * Columns> m_data;
|
||||||
|
|
||||||
|
template<size_t OtherColumns> [[nodiscard]]
|
||||||
|
constexpr Mat<Rows, OtherColumns, Type, MatStoreType::ROW_MAJOR>
|
||||||
|
cache_friendly_multiply_row_major(const Mat<Columns, OtherColumns, Type, MatStoreType::ROW_MAJOR>& other) const
|
||||||
|
{
|
||||||
|
Mat<Rows, OtherColumns, Type, MatStoreType::ROW_MAJOR> result;
|
||||||
|
for (std::size_t i = 0; i < Rows; ++i)
|
||||||
|
for (std::size_t k = 0; k < Columns; ++k)
|
||||||
|
{
|
||||||
|
const Type aik = at(i, k);
|
||||||
|
for (std::size_t j = 0; j < OtherColumns; ++j)
|
||||||
|
result.at(i, j) += aik * other.at(k, j);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<size_t OtherColumns> [[nodiscard]]
|
||||||
|
constexpr Mat<Rows, OtherColumns, Type, MatStoreType::COLUMN_MAJOR> cache_friendly_multiply_col_major(
|
||||||
|
const Mat<Columns, OtherColumns, Type, MatStoreType::COLUMN_MAJOR>& other) const
|
||||||
|
{
|
||||||
|
Mat<Rows, OtherColumns, Type, MatStoreType::COLUMN_MAJOR> result;
|
||||||
|
for (std::size_t j = 0; j < OtherColumns; ++j)
|
||||||
|
for (std::size_t k = 0; k < Columns; ++k)
|
||||||
|
{
|
||||||
|
const Type bkj = other.at(k, j);
|
||||||
|
for (std::size_t i = 0; i < Rows; ++i)
|
||||||
|
result.at(i, j) += at(i, k) * bkj;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> [[nodiscard]]
|
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> [[nodiscard]]
|
||||||
|
|||||||
Reference in New Issue
Block a user