-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b610f1
commit 83677ec
Showing
7 changed files
with
208 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// SubmatrixGenerator.h | ||
// com.xm.xlib | ||
// | ||
// Created by xiaominghe2014@gmail.com on 2023/11/17. | ||
// | ||
|
||
#ifndef SubmatrixGenerator_h | ||
#define SubmatrixGenerator_h | ||
|
||
#include "math/matrix.h" | ||
#include <vector> | ||
|
||
namespace xlib | ||
{ | ||
class SubmatrixGenerator | ||
{ | ||
public: | ||
|
||
SubmatrixGenerator(const Matrix &matrix, int k); | ||
|
||
class iterator | ||
{ | ||
public: | ||
iterator(const Matrix &matrix, int k, bool end); | ||
Matrix operator*() const; | ||
const iterator &operator++(); | ||
bool operator!=(const iterator &other) const; | ||
|
||
private: | ||
const Matrix &matrix_; | ||
int k_; | ||
bool end_; | ||
std::vector<std::vector<int>> combM_; | ||
std::vector<std::vector<int>> combN_; | ||
std::vector<std::vector<int>>::iterator itM_; | ||
std::vector<std::vector<int>>::iterator itN_;; | ||
}; | ||
|
||
iterator begin() const; | ||
iterator end() const; | ||
|
||
private: | ||
const Matrix &matrix_; | ||
int k_; | ||
}; | ||
} | ||
|
||
#endif /* SubmatrixGenerator_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ | |
|
||
#include "math/matrix.h" | ||
#include "math/util.h" | ||
#include "math/SubmatrixGenerator.h" | ||
|
||
#endif /* math_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// SubmatrixGenerator.cpp | ||
// com.xm.xlib | ||
// | ||
// Created by apple on 2023/11/17. | ||
// | ||
|
||
#include "math/SubmatrixGenerator.h" | ||
#include "math/util.h" | ||
#include <numeric> | ||
|
||
namespace xlib | ||
{ | ||
SubmatrixGenerator::SubmatrixGenerator(const Matrix &matrix, int k) : matrix_(matrix), k_(k) | ||
{ | ||
} | ||
|
||
SubmatrixGenerator::iterator::iterator(const Matrix &matrix, int k, bool end) : matrix_(matrix), k_(k), end_(end) | ||
{ | ||
// 初始化迭代器状态 | ||
std::vector<int> arrM(matrix.m); | ||
std::iota(arrM.begin(), arrM.end(), 0); | ||
std::vector<int> arrN(matrix.n); | ||
std::iota(arrN.begin(), arrN.end(), 0); | ||
|
||
// m行任取k行 | ||
combM_ = combination(arrM, k); | ||
// n列任取k列 | ||
combN_ = combination(arrN, k); | ||
itM_ = combM_.begin(); | ||
itN_ = combN_.begin(); | ||
} | ||
|
||
Matrix SubmatrixGenerator::iterator::operator*() const | ||
{ | ||
auto cm = *itM_; | ||
auto cn = *itN_; | ||
// 生成当前子矩阵 | ||
Matrix submatrix(k_, k_); | ||
// ...填充子矩阵 | ||
for (int r = 0; r < k_; r++) | ||
{ | ||
for (int c = 0; c < k_; c++) | ||
{ | ||
submatrix.a[r][c] = matrix_.a[cm[r]][cn[c]]; | ||
} | ||
} | ||
return submatrix; | ||
} | ||
|
||
const SubmatrixGenerator::iterator &SubmatrixGenerator::iterator::operator++() | ||
{ | ||
// 更新迭代器状态以生成下一个子矩阵 | ||
// 如果没有更多的子矩阵,设置end_为true | ||
++itN_; | ||
if(itN_ == combN_.end()){ | ||
itN_ = combN_.begin(); | ||
++itM_; | ||
if(itM_ == combM_.end()){ | ||
end_ = true; | ||
} | ||
} | ||
return *this; | ||
} | ||
|
||
bool SubmatrixGenerator::iterator::operator!=(const iterator &other) const | ||
{ | ||
return end_ != other.end_; | ||
} | ||
|
||
SubmatrixGenerator::iterator SubmatrixGenerator::begin() const | ||
{ | ||
return iterator(matrix_, k_, false); | ||
} | ||
|
||
SubmatrixGenerator::iterator SubmatrixGenerator::end() const | ||
{ | ||
return iterator(matrix_, k_, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters