Skip to content

Commit

Permalink
Stable matrix version
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmoein committed Nov 11, 2024
1 parent 02b2f5a commit 2b0fd33
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 24 deletions.
100 changes: 86 additions & 14 deletions include/DataFrame/Utils/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ class Matrix {

public:

using size_type = std::size_t;
using size_type = long;
using value_type = T;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;

using self_t = Matrix<value_type, MO>;

using trans_result_t =
typename std::conditional<
MO == matrix_orient::column_major,
Matrix<T, matrix_orient::row_major>,
Matrix<T, matrix_orient::column_major>>::type;

Matrix() = default;
Matrix(size_type rows, size_type cols, const_reference def_v = T());
Matrix(const Matrix &) = default;
Expand Down Expand Up @@ -99,6 +104,9 @@ class Matrix {
template<typename I>
void set_row(I row_data, size_type row);

trans_result_t transpose() const noexcept;
Matrix transpose2() const noexcept;

private:

using storage_t = std::vector<value_type>;
Expand All @@ -114,7 +122,7 @@ class Matrix {
public:

class row_iterator;
class row_const_iterator : public std::random_access_iterator_tag {
class row_const_iterator : public std::random_access_iterator_tag {

public:

Expand All @@ -127,8 +135,6 @@ class Matrix {
using const_reference = const value_type &;
using difference_type = typename std::vector<T>::difference_type;

public:

inline row_const_iterator () = default;

inline row_const_iterator(const self_t *m,
Expand Down Expand Up @@ -189,6 +195,10 @@ class Matrix {

col_ += 1;
if (col_ >= mptr_->cols()) { col_ = 0; row_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}
inline row_const_iterator operator ++ (int) noexcept { // Postfix++
Expand All @@ -198,6 +208,10 @@ class Matrix {

col_ += 1;
if (col_ >= mptr_->cols()) { col_ = 0; row_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (row_const_iterator (mptr_, row, col));
}

Expand All @@ -208,6 +222,10 @@ class Matrix {
row_ += col_ / mptr_->cols();
col_ %= mptr_->cols();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}

Expand Down Expand Up @@ -247,6 +265,10 @@ class Matrix {
row_ += col_ / mptr_->cols();
col_ %= mptr_->cols();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (row_const_iterator (mptr_, row, col));
}

Expand Down Expand Up @@ -279,7 +301,7 @@ class Matrix {
const size_type row_diff = lhs.row_ - rhs.row_;
const size_type col_diff = lhs.col_ - rhs.col_;

assert(lhs.mptr_ == rhs.mptr);
assert(lhs.mptr_ == rhs.mptr_);
return (difference_type(
std::abs(row_diff) * rhs.mptr_->cols() - col_diff));
}
Expand All @@ -293,7 +315,7 @@ class Matrix {

// It goes through the matrix row-by-row starting at [0, 0]
//
class row_iterator : public std::random_access_iterator_tag {
class row_iterator : public std::random_access_iterator_tag {

public:

Expand Down Expand Up @@ -362,6 +384,10 @@ class Matrix {

col_ += 1;
if (col_ >= mptr_->cols()) { col_ = 0; row_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}
inline row_iterator operator ++ (int) noexcept { // Postfix++
Expand All @@ -371,6 +397,10 @@ class Matrix {

col_ += 1;
if (col_ >= mptr_->cols()) { col_ = 0; row_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (row_iterator (mptr_, row, col));
}

Expand All @@ -381,6 +411,10 @@ class Matrix {
row_ += col_ / mptr_->cols();
col_ %= mptr_->cols();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}

Expand Down Expand Up @@ -420,6 +454,10 @@ class Matrix {
row_ += col_ / mptr_->cols();
col_ %= mptr_->cols();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (row_iterator (mptr_, row, col));
}

Expand Down Expand Up @@ -449,10 +487,11 @@ class Matrix {
friend difference_type
operator - (row_iterator lhs, row_iterator rhs) noexcept {

assert(lhs.mptr_ == rhs.mptr_);

const size_type row_diff = lhs.row_ - rhs.row_;
const size_type col_diff = lhs.col_ - rhs.col_;

assert(lhs.mptr_ == rhs.mptr);
return (difference_type(
std::abs(row_diff) * rhs.mptr_->cols() - col_diff));
}
Expand All @@ -465,7 +504,7 @@ class Matrix {
};

class col_iterator;
class col_const_iterator : public std::random_access_iterator_tag {
class col_const_iterator : public std::random_access_iterator_tag {

public:

Expand Down Expand Up @@ -540,6 +579,10 @@ class Matrix {

row_ += 1;
if (row_ >= mptr_->rows()) { row_ = 0; col_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}
inline col_const_iterator operator ++ (int) noexcept { // Postfix++
Expand All @@ -549,6 +592,10 @@ class Matrix {

row_ += 1;
if (row_ >= mptr_->rows()) { row_ = 0; col_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (col_const_iterator (mptr_, row, col));
}

Expand All @@ -559,6 +606,10 @@ class Matrix {
col_ += row_ / mptr_->rows();
row_ %= mptr_->rows();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}

Expand Down Expand Up @@ -598,6 +649,10 @@ class Matrix {
col_ += row_ / mptr_->rows();
row_ %= mptr_->rows();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (col_const_iterator (mptr_, row, col));
}

Expand Down Expand Up @@ -627,12 +682,13 @@ class Matrix {
friend difference_type
operator - (col_const_iterator lhs, col_const_iterator rhs) noexcept {

assert(lhs.mptr_ == rhs.mptr_);

const size_type row_diff = lhs.row_ - rhs.row_;
const size_type col_diff = lhs.col_ - rhs.col_;

assert(lhs.mptr_ == rhs.mptr);
return (difference_type(
std::abs(row_diff) * rhs.mptr_->cols() - col_diff));
std::abs(col_diff) * rhs.mptr_->rows() - row_diff));
}

private:
Expand All @@ -644,7 +700,7 @@ class Matrix {

// It goes through the matrix row-by-row starting at [0, 0]
//
class col_iterator : public std::random_access_iterator_tag {
class col_iterator : public std::random_access_iterator_tag {

public:

Expand Down Expand Up @@ -713,6 +769,10 @@ class Matrix {

row_ += 1;
if (row_ >= mptr_->rows()) { row_ = 0; col_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}
inline col_iterator operator ++ (int) noexcept { // Postfix++
Expand All @@ -722,6 +782,10 @@ class Matrix {

row_ += 1;
if (row_ >= mptr_->rows()) { row_ = 0; col_ += 1; }
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (col_iterator (mptr_, row, col));
}

Expand All @@ -732,6 +796,10 @@ class Matrix {
col_ += row_ / mptr_->rows();
row_ %= mptr_->rows();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (*this);
}

Expand Down Expand Up @@ -771,6 +839,10 @@ class Matrix {
col_ += row_ / mptr_->rows();
row_ %= mptr_->rows();
}
if (col_ >= mptr_->cols() || row_ >= mptr_->rows()) {
col_ = mptr_->cols();
row_ = mptr_->rows();
}
return (col_iterator (mptr_, row, col));
}

Expand Down Expand Up @@ -803,9 +875,9 @@ class Matrix {
const size_type row_diff = lhs.row_ - rhs.row_;
const size_type col_diff = lhs.col_ - rhs.col_;

assert(lhs.mptr_ == rhs.mptr);
assert(lhs.mptr_ == rhs.mptr_);
return (difference_type(
std::abs(row_diff) * rhs.mptr_->cols() - col_diff));
std::abs(col_diff) * rhs.mptr_->rows() - row_diff));
}

private:
Expand Down
44 changes: 44 additions & 0 deletions include/DataFrame/Utils/Matrix.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,50 @@ template<typename T, matrix_orient MO>
constexpr matrix_orient
Matrix<T, MO>::orientation() { return (MO); }

// ----------------------------------------------------------------------------

template<typename T, matrix_orient MO>
Matrix<T, MO>::trans_result_t
Matrix<T, MO>::transpose() const noexcept {

trans_result_t result { cols(), rows() };

if constexpr (MO == matrix_orient::column_major) {
for (size_type c = 0; c < cols(); ++c)
for (size_type r = 0; r < rows(); ++r)
result(c, r) = at(r, c);
}
else {
for (size_type r = 0; r < rows(); ++r)
for (size_type c = 0; c < cols(); ++c)
result(c, r) = at(r, c);
}

return (result);
}

// ----------------------------------------------------------------------------

template<typename T, matrix_orient MO>
Matrix<T, MO>
Matrix<T, MO>::transpose2() const noexcept {

Matrix result { cols(), rows() };

if constexpr (MO == matrix_orient::column_major) {
for (size_type c = 0; c < cols(); ++c)
for (size_type r = 0; r < rows(); ++r)
result(c, r) = at(r, c);
}
else {
for (size_type r = 0; r < rows(); ++r)
for (size_type c = 0; c < cols(); ++c)
result(c, r) = at(r, c);
}

return (result);
}

} // namespace hmdf

// ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 2b0fd33

Please sign in to comment.