构造稀疏矩阵例子

构造稀疏矩阵的目的是在处理具有大量零元素的大规模数据时,节省内存空间和计算资源,并提高计算效率。稀疏矩阵是一种特殊的矩阵,其中包含许多零元素和一些非零元素。

#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;
}

其中W_D就是表示稀疏矩阵的非零元素,包含三个成员变量:行索引、列索引和元素值

输出结果:内含权重作用于每个点得到的新值,在每个点的新坐标后存放了权重值(在同一行)

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;
}

结果:一个边上的两个顶点a和b决定了alpha的列数,但在同一行,每3行之后获得alpha * gamma和-alpha * gamma(也在同一行)

#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 = 3;  // 某个维度的大小

    // 创建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}
    };

    std::vector<std::vector<double>> xyz_values2 = {
      {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>(4 * m + i, i * 4 + j, weight * xyz[j]));//从4*m行0列添加非零元素(行标号从0开始),下一个点的列数依次增加4
        W_D.push_back(Eigen::Triplet<float>(4 * m + i, i * 4 + 3, weight));//在weight * xyz[j]同一行的后面添加weight
    }

    // 构建稀疏矩阵
    int numRows = 4 * m + n;
    int numCols = n * 4;
    Eigen::SparseMatrix<float> sparseMatrix(numRows, numCols);
    sparseMatrix.setFromTriplets(W_D.begin(), W_D.end());

    // 计算B,构造B矩阵,初始值都为0
    Eigen::MatrixX3f B = Eigen::MatrixX3f::Zero(4 * m + 2*n, 3);
    for (int i = 0; i < n; ++i)
    {
        double weight = weights[i];
        const std::vector<double>& xyz = xyz_values[i];
        const std::vector<double>& xyz2 = xyz_values2[i];
        for (int j = 0; j < 3; j++) B(4 * m + i, j) = weight * xyz[j];
        for (int j = 0; j < 3; j++) B(4 * m + i +n, j) = weight * xyz2[j];
    }

    // 输出稀疏矩阵内容
    std::cout << "Sparse Matrix: " << std::endl << sparseMatrix << std::endl;
    std::cout << "B Matrix: " << std::endl << B << std::endl;

    return 0;
}

其中B矩阵的结果是:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值