Le but de la construction d'une matrice creuse est d'économiser de l'espace mémoire et des ressources informatiques et d'améliorer l'efficacité informatique lorsqu'il s'agit de données à grande échelle avec un grand nombre d'éléments nuls. Une matrice creuse est un type spécial de matrice qui contient de nombreux éléments nuls et quelques éléments non nuls.
#include "pcl.h"
#include "common.h"
#include "optimal_nonrigid_icp.h"
#include <stdio.h>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <stdlib.h>
#include <math.h>
#include <ostream>
#include <iomanip>
#include <algorithm>
#include <Eigen/Sparse>
#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
using namespace Eigen;
int main() {
// 假设已经定义了适当的变量和数据用于构建稀疏矩阵
int n = 3; // 数据点的数量
int m = 4; // 某个维度的大小
// 创建Triplet对象容器 W_D
std::vector<Eigen::Triplet<float>> W_D;
// 为示例目的,构造一些假设的数据
std::vector<double> weights = { 0.1, 0.2, 0.3 };
std::vector<std::vector<double>> xyz_values = {
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0},
{7.0, 8.0, 9.0}
};
// 循环添加Triplet对象到 W_D
for (int i = 0; i < n; ++i) {
double weight = weights[i];
const std::vector<double>& xyz = xyz_values[i];
// 添加 Triplet 对象到 W_D
for (int j = 0; j < 3; ++j)
W_D.push_back(Eigen::Triplet<float>(6 * m + i, i * 4 + j, weight * xyz[j]));
W_D.push_back(Eigen::Triplet<float>(6 * m + i, i * 4 + 3, weight));
}
// 构建稀疏矩阵
int numRows = 6 * m + n;
int numCols = n * 4;
Eigen::SparseMatrix<float> sparseMatrix(numRows, numCols);
sparseMatrix.setFromTriplets(W_D.begin(), W_D.end());
// 输出稀疏矩阵内容
std::cout << "Sparse Matrix: " << std::endl << sparseMatrix << std::endl;
return 0;
}
Parmi eux, W_D est l'élément non nul représentant la matrice creuse, comprenant trois variables membres : l'indice de ligne, l'indice de colonne et la valeur de l'élément.
Résultat de sortie : la nouvelle valeur obtenue par le poids interne appliqué à chaque point, et la valeur du poids est stockée après les nouvelles coordonnées de chaque point (dans la même ligne)
int main()
{
int m = 3;
int n = 3;
float alpha = 1.0f;
float gamma = 2.0f;
// Construct sparse matrix A with appropriate dimensions
SparseMatrix<float> A(4 * m + n, 4 * n);
// Calculate alpha_M_G, representing the non-zero elements of the matrix
vector<Triplet<float>> alpha_M_G;
// Loop through each edge (m in total) and insert non-zero elements
for (int i = 0; i < (m - 1); ++i)
{
int a = i;
int b = i + 1;
// Loop through three axes, insert alpha at specified positions
for (int j = 0; j < 3; j++)
{
alpha_M_G.push_back(Triplet<float>(i * 4 + j, a * 4 + j, alpha));
alpha_M_G.push_back(Triplet<float>(i * 4 + j, b * 4 + j, -alpha));
}
// Insert alpha * gamma at the fourth coordinate index for vertex a and -alpha * gamma for vertex b
alpha_M_G.push_back(Triplet<float>(i * 4 + 3, a * 4 + 3, alpha * gamma));
alpha_M_G.push_back(Triplet<float>(i * 4 + 3, b * 4 + 3, -alpha * gamma));
}
// Build sparse matrix A
A.setFromTriplets(alpha_M_G.begin(), alpha_M_G.end());
// Output sparse matrix A
cout << "Sparse Matrix A:" << endl;
cout << A << endl;
return 0;
}
Résultat : deux sommets a et b sur une arête déterminent le nombre de colonnes d'alpha, mais dans la même ligne, toutes les 3 lignes, obtenez alpha*gamma et -alpha*gamma (également dans la même ligne)