46 const MatrixXd& matGain,
47 const MatrixXd& matData,
52 const int nSources =
static_cast<int>(matGain.cols());
53 const int nTimes =
static_cast<int>(matData.cols());
56 MatrixXd matGtG = matGain.transpose() * matGain;
57 MatrixXd matGtM = matGain.transpose() * matData;
60 VectorXd vecWeights = VectorXd::Ones(nSources);
61 VectorXd vecWeightsOld = vecWeights;
64 std::vector<int> activeIdx(nSources);
65 std::iota(activeIdx.begin(), activeIdx.end(), 0);
68 MatrixXd matX = MatrixXd::Zero(nSources, nTimes);
70 int actualIterations = 0;
72 for (
int iter = 0; iter < nIterations; ++iter) {
73 actualIterations = iter + 1;
75 const int nActive =
static_cast<int>(activeIdx.size());
80 MatrixXd matGtG_active(nActive, nActive);
81 MatrixXd matGtM_active(nActive, nTimes);
83 for (
int i = 0; i < nActive; ++i) {
84 matGtM_active.row(i) = matGtM.row(activeIdx[i]);
85 for (
int j = 0; j < nActive; ++j) {
86 matGtG_active(i, j) = matGtG(activeIdx[i], activeIdx[j]);
91 VectorXd vecWdiag(nActive);
92 for (
int i = 0; i < nActive; ++i) {
93 vecWdiag(i) = 1.0 / vecWeights(activeIdx[i]);
97 MatrixXd matLhs = matGtG_active;
98 matLhs.diagonal() += alpha * vecWdiag;
100 MatrixXd matX_active = matLhs.ldlt().solve(matGtM_active);
104 for (
int i = 0; i < nActive; ++i) {
105 matX.row(activeIdx[i]) = matX_active.row(i);
109 vecWeightsOld = vecWeights;
110 for (
int i = 0; i < nSources; ++i) {
111 vecWeights(i) = std::max(matX.row(i).norm(), 1e-10);
115 std::vector<int> newActive;
116 newActive.reserve(nActive);
117 for (
int i = 0; i < nSources; ++i) {
118 if (vecWeights(i) >= 1e-8) {
119 newActive.push_back(i);
122 activeIdx = newActive;
125 double maxChange = 0.0;
126 for (
int idx : activeIdx) {
127 maxChange = std::max(maxChange, std::abs(vecWeights(idx) - vecWeightsOld(idx)));
129 if (maxChange < tolerance)
138 QVector<int> finalActive;
139 for (
int i = 0; i < nSources; ++i) {
140 if (matX.row(i).norm() >= 1e-8) {
141 finalActive.append(i);
147 const int nActiveFinal = finalActive.size();
148 MatrixXd matActiveSol(nActiveFinal, nTimes);
149 VectorXi vecActiveVerts(nActiveFinal);
150 for (
int i = 0; i < nActiveFinal; ++i) {
151 matActiveSol.row(i) = matX.row(finalActive[i]);
152 vecActiveVerts(i) = finalActive[i];
159 MatrixXd matResidual = matData - matGain * matX;