14. 稀疏矩阵的乘法运算

博客围绕矩阵三元组乘法算法展开,先按顺序存储前两个矩阵,考虑到第三个矩阵行列未知,需初始化所有位数。强调存储次序及矩阵乘法条件,即A的列等于B的行。还介绍了定义结构、录入数组、初始化结果数组等步骤,最后提到实现代码调试很久。

这个题上课有讲,但是没有完全讲

先按三元组的顺序把前两个矩阵存好,由于第三个矩阵会出现几行几列不知道,所以所有的位数都要初始化

但是矩阵是二维,实际存是一维数组,所以存的次序要好好考虑。而且题目给的数组是从1开始的

最妙的一步是讲三元组做乘法,考虑到矩阵乘法,肯定是两个for,一个A数组遍历所有B数组,关键是A数组的值不一定能和B数组的值在做乘法的时候碰到,而条件就是A的列要等于B的行

此题,先要定义一个结构,有行列,数值,再定义一个整体,有上面结构的data数组,再包括行数,列数,个数初始为0

录入两个数组,再初始化第三个数组

第三个数组是行列全包含,初值为0,之后输出的时候非零元输出

计算过程,遍历两个三元组,假如碰到A的列等于B的行,就计算,判断结构是否为0,不为0则加到相应位置上,新的结果如果等于此结果,说明是第一次加,则个数++,若新结果为0,则个数—

然后我写完,挂掉了,Debug了好久

#include<bits/stdc++.h>

using namespace std;

#define Max 10005

/*

100

1

2

1 1 1

2 1 1

1

100

2

1 1 1

1 100 1

*/

typedef struct {

       int hang,lie,zhi;

}elem;

typedef struct{

       elem data[Max];

       int Hang,Lie,Shu=0;

}juzheng;

int main(void){

       juzheng A,B,C;

       scanf("%d\n%d\n%d",&A.Hang,&A.Lie,&A.Shu);

       for(int i=0;i<A.Shu;i++){

              scanf("%d %d %d",&A.data[i].hang,&A.data[i].lie,&A.data[i].zhi);

              A.data[i].hang--;

              A.data[i].lie--;

       }

       scanf("%d\n%d\n%d",&B.Hang,&B.Lie,&B.Shu);

       for(int i=0;i<B.Shu;i++){

              scanf("%d %d %d",&B.data[i].hang,&B.data[i].lie,&B.data[i].zhi);

              B.data[i].hang--;

              B.data[i].lie--;

       }

       C.Hang=A.Hang;

       C.Lie=B.Lie;

       for(int i=0;i<C.Hang;i++){

              for(int j=0;j<C.Lie;j++){

                     C.data[i*C.Lie+j].zhi=0;

                     C.data[i*C.Lie+j].hang=i;

                     C.data[i*C.Lie+j].lie=j;

              }

       }

       for(int i=0;i<A.Shu;i++){

              for(int j=0;j<B.Shu;j++){

                     int ans=0;

                     if(A.data[i].lie==B.data[j].hang){

                            ans=A.data[i].zhi*B.data[j].zhi;

                     }

                     if(ans!=0){

                            C.data[A.data[i].hang*C.Lie+B.data[j].lie].zhi+=ans;

                            if(C.data[A.data[i].hang*C.Lie+B.data[j].lie].zhi==0){// 第一次加

                                   C.Shu--;

                                   //printf("%d %d\n",C.data[A.data[i].hang*A.Hang+B.data[j].lie].hang,C.data[A.data[i].hang*A.Hang+B.data[j].lie].lie);

                            }

                            else if(C.data[A.data[i].hang*C.Lie+B.data[j].lie].zhi==ans){//后面制0

                                   C.Shu++;

                            }

                     }

              }

       }

       printf("%d\n%d\n%d\n",C.Hang,C.Lie,C.Shu);

       for(int i=0;i<C.Hang*C.Lie;i++){

              if(C.data[i].zhi!=0){

                     printf("%d,%d,%d\n",C.data[i].hang+1,C.data[i].lie+1,C.data[i].zhi);

              }

       }

}

<think>Hmm, the user is asking about "Load rspmm extension. This may take a while... sparse matrix multiplication optimization kernel" after previously inquiring about RSPMM. Interesting progression - they seem to be diving into the technical implementation aspects now. Looking at context, this appears to be a loading message typically seen in scientific computing environments like MATLAB or Python when importing specialized modules. The "sparse matrix multiplication optimization kernel" part specifically suggests we're dealing with high-performance computing optimizations for sparse data structures. The user might be encountering this loading message in their work and wants to understand what's happening behind the scenes. Or perhaps they're benchmarking performance and curious about the optimization techniques. Their focus seems technical but practical - not theoretical questions about RSPMM anymore, but its actual implementation challenges. I should explain: - Why loading takes time (probably JIT compilation) - What makes sparse matrix multiplication special - How optimization kernels work - Practical implications for users The previous response already covered RSPMM basics, so now we're drilling into computational efficiency aspects. The user likely cares about real-world performance - maybe they're processing large satellite datasets? For the required follow-up questions, I'll focus on: - Technical tradeoffs (CPU/GPU, precision) - Implementation details - Performance tuning - Compatibility issues - Benchmarking practices This bridges their original RSPMM question with the practical computational concerns surfaced by the loading message. The kernel aspect is particularly crucial - many users don't realize how much specialized low-level code powers their high-level remote sensing work. </think>"Load rspmm extension. This may take a while...稀疏矩阵乘法优化内核" 指的是在加载 **RSPMM(Remote Sparse Matrix Multiplication)扩展模块**时出现的提示信息。该模块的核心功能是专门优化**稀疏矩阵乘法计算**的高性能内核,主要用于科学计算、机器学习(如推荐系统)和大规模数据处理场景。其技术特点包括: 1. **延迟加载机制** 首次加载时需编译/初始化优化内核(涉及JIT编译、硬件适配等),故提示"may take a while"。 2. **稀疏矩阵专用优化** - 采用 **CSR/ELLPACK** 等压缩存储格式减少内存占用 - 利用 **分块计算** 和 **非零元跳过** 策略加速计算 - 支持多线程/GPU并行(如CUDA内核) 3. **典型应用场景** ```mermaid graph LR A[推荐系统] --> B[用户-物品交互矩阵] C[图神经网络] --> D[邻接矩阵运算] E[量子模拟] --> F[哈密顿矩阵计算] ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值