Skip to content

Commit

Permalink
fix 将子矩阵求解修改为迭代器模式
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaominghe2014 committed Nov 17, 2023
1 parent 2b610f1 commit 83677ec
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 97 deletions.
128 changes: 65 additions & 63 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,71 @@ executable("test_main") {
"include"
]
sources = [
"main.cpp",
"include/xlib.h",
"include/crypto/base64.h",
"include/crypto/md5.h",
"include/crypto/url.h",
"include/crypto/base85.h",
"include/crypto/sha1.h",
"include/net/XEpoll.h",
"include/net/http.h",
"include/net/XSocket.h",
"include/net/netconfig.h",
"include/memory/StackAlloc.h",
"include/memory/xallocator.h",
"include/algorithm/AStar.h",
"include/algorithm/dlx.h",
"include/algorithm/sort.h",
"include/algorithm/search.h",
"include/parser/SgfParser.h",
"include/parser/xml.h",
"include/parser/json.h",
"include/parser/YamlParser.h",
"include/math/matrix.h",
"include/math/util.h",
"include/math/math.h",
"include/macro/XDefine.h",
"include/macro/xplatform.h",
"include/macro/XBase.h",
"include/macro/XBinary.h",
"include/base/XRegex.h",
"include/base/XUtf8.h",
"include/base/XRegist.h",
"include/base/XLog.h",
"include/base/Serializer.h",
"include/base/XThread.h",
"include/base/XTime.h",
"include/base/XFileUtil.h",
"include/base/XString.h",
"include/base/XRandom.h",
"src/crypto/md5.cpp",
"src/crypto/base64.cpp",
"src/crypto/url.cpp",
"src/crypto/sha1.cpp",
"src/crypto/base85.cpp",
"src/net/XEpoll.cpp",
"src/net/XSocket.cpp",
"src/algorithm/sort.cpp",
"src/algorithm/search.cpp",
"src/algorithm/dlx.cpp",
"src/algorithm/AStar.cpp",
"src/parser/SgfParser.cpp",
"src/parser/YamlParser.cpp",
"src/parser/xml.cpp",
"src/parser/json.cpp",
"src/math/util.cpp",
"src/math/matrix.cpp",
"src/base/XRegex.cpp",
"src/base/XLog.cpp",
"src/base/XThread.cpp",
"src/base/XString.cpp",
"src/base/XFileUtil.cpp",
"src/base/XUtf8.cpp",
"src/base/XRandom.cpp",
"src/base/XTime.cpp",
"main.cpp",
"include/xlib.h",
"include/crypto/base64.h",
"include/crypto/md5.h",
"include/crypto/url.h",
"include/crypto/base85.h",
"include/crypto/sha1.h",
"include/net/XEpoll.h",
"include/net/http.h",
"include/net/XSocket.h",
"include/net/netconfig.h",
"include/memory/StackAlloc.h",
"include/memory/xallocator.h",
"include/algorithm/AStar.h",
"include/algorithm/dlx.h",
"include/algorithm/sort.h",
"include/algorithm/search.h",
"include/parser/SgfParser.h",
"include/parser/xml.h",
"include/parser/json.h",
"include/parser/YamlParser.h",
"include/math/SubmatrixGenerator.h",
"include/math/matrix.h",
"include/math/util.h",
"include/math/math.h",
"include/macro/XDefine.h",
"include/macro/xplatform.h",
"include/macro/XBase.h",
"include/macro/XBinary.h",
"include/base/XRegex.h",
"include/base/XUtf8.h",
"include/base/XRegist.h",
"include/base/XLog.h",
"include/base/Serializer.h",
"include/base/XThread.h",
"include/base/XTime.h",
"include/base/XFileUtil.h",
"include/base/XString.h",
"include/base/XRandom.h",
"src/crypto/md5.cpp",
"src/crypto/base64.cpp",
"src/crypto/url.cpp",
"src/crypto/sha1.cpp",
"src/crypto/base85.cpp",
"src/net/XEpoll.cpp",
"src/net/XSocket.cpp",
"src/algorithm/sort.cpp",
"src/algorithm/search.cpp",
"src/algorithm/dlx.cpp",
"src/algorithm/AStar.cpp",
"src/parser/SgfParser.cpp",
"src/parser/YamlParser.cpp",
"src/parser/xml.cpp",
"src/parser/json.cpp",
"src/math/util.cpp",
"src/math/SubmatrixGenerator.cpp",
"src/math/matrix.cpp",
"src/base/XRegex.cpp",
"src/base/XLog.cpp",
"src/base/XThread.cpp",
"src/base/XString.cpp",
"src/base/XFileUtil.cpp",
"src/base/XUtf8.cpp",
"src/base/XRandom.cpp",
"src/base/XTime.cpp",
]
}

49 changes: 49 additions & 0 deletions include/math/SubmatrixGenerator.h
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 */
1 change: 1 addition & 0 deletions include/math/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@

#include "math/matrix.h"
#include "math/util.h"
#include "math/SubmatrixGenerator.h"

#endif /* math_h */
3 changes: 2 additions & 1 deletion include/math/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <vector>
#include <string>
//#include "math/SubmatrixGenerator.h"

namespace xlib {
// 定义矩阵类
Expand Down Expand Up @@ -46,7 +47,7 @@ namespace xlib {
double det();

//k阶子式
std::vector<Matrix> submatrix(int k);
//SubmatrixGenerator submatrix(int k);

//矩阵的秩
int rank();
Expand Down
8 changes: 4 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ void testMatrix()

LOG_I("H:\n%s", H.toString().c_str());

auto subArr = H.submatrix(2);

for (auto &m : subArr)
// auto subArr = H.submatrix(2);
auto subArr = SubmatrixGenerator(H,2);
for (auto m : subArr)
{
LOG_I("H.submatrix 2:\n%s", m.toString().c_str());
}
Expand Down Expand Up @@ -503,7 +503,7 @@ void testMatrix()
LOG_I("L:\n%s,\nU:\n%s",mL.toString().c_str(),mU.toString().c_str());
auto LU_A_NEW = mL*mU;
LOG_I("LU_A_NEW:\n%s",LU_A_NEW.toString().c_str());
LOG_I("LU_A_NEW %s LU_A",mA==LU_A_NEW?"equals":"not equals");
LOG_I("L*U %s LU_A",mA==LU_A_NEW?"equals":"not equals");
}

void testMath()
Expand Down
80 changes: 80 additions & 0 deletions src/math/SubmatrixGenerator.cpp
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);
}
}
36 changes: 7 additions & 29 deletions src/math/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,35 +248,13 @@ namespace xlib {
}

// FIXME 修改为迭代器,性能可以更好
std::vector<Matrix> Matrix::submatrix(int k){
int max = m>n?n:m;
if(k>max){
throw std::runtime_error("The N-th submatrix of a matrix must be smaller than both the row and column dimensions.");
}
std::vector<Matrix> result;
std::vector<int> arrM(m);
std::iota(arrM.begin(), arrM.end(), 0);
std::vector<int> arrN(n);
std::iota(arrN.begin(), arrN.end(), 0);

//m行任取k行
auto combM = combination(arrM, k);
//n列任取k列
auto combN = combination(arrN, k);

for (const auto& cm : combM) {
for (const auto& cn : combN) {
Matrix ma(k, k);
for (int r = 0; r < k; r++) {
for (int c = 0; c < k; c++) {
ma.a[r][c] = a[cm[r]][cn[c]];
}
}
result.push_back(ma);
}
}
return result;
}
// SubmatrixGenerator Matrix::submatrix(int k){
// int max = m>n?n:m;
// if(k>max){
// throw std::runtime_error("The N-th submatrix of a matrix must be smaller than both the row and column dimensions.");
// }
// return SubmatrixGenerator(*this,k);
// }

int Matrix::rank() {
int rank = 0; // 初始化秩为0
Expand Down

0 comments on commit 83677ec

Please sign in to comment.