平台:VC++ 2005 测试通过!
.vcproj
这是使用应用程序向导生成的 VC++ 项目的主项目文件。
它包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。
StdAfx.h, StdAfx.cpp
这些文件用于生成名为 twod.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。
这些都是使用应用程序向导生成的 VC++ 文件故不列出
我只列出程序主要部分!
//ADT_SparseMatrix.h
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//---------------抽象数据类型稀疏矩阵的定义------------------//
#define ElemMaxSize 12500;
typedef int ElemType;
typedef struct {
int i, j;
ElemType e;
} Triple; //稀疏矩阵的元素-三元组
typedef struct {
vector<Triple> elems;
int row, col, tu;
} TSMatrix; //稀疏矩阵结构体类型
//---------------------稀疏矩阵的接口函数声明-----------------------//
//构造一个稀疏矩阵
bool enterTSMatrix(TSMatrix &atsm);
//释放稀疏矩阵所占内存空间
void DestroyTSMatrix(TSMatrix &atsm);
//打印稀疏矩阵
void Print(const TSMatrix &atsm);
//对稀疏矩阵进行转置
bool TranposeTSMatrix(const TSMatrix &stsm, TSMatrix &ttsm);
//------私有的成员函数-----//
bool rowmajorCriterion(const Triple& t1, const Triple& t2);
ElemType getTriple(const TSMatrix &stsm, int ix, int jx);
//---------------------稀疏矩阵的接口函数的实现-----------------------//
bool enterTSMatrix(TSMatrix &atsm){
cout << "请输入矩阵的行数: "; cin >> atsm.row;
cout << "请输入矩阵的列数: "; cin >> atsm.col;
cout << "请输入矩阵的非零元数: "; cin >> atsm.tu;
int ix, jx; ElemType elem;
for(int i = 0; i < atsm.tu; i++){//接受atsm.tu个非零元
Triple aTriple;
cout << "请输入第" << i <<"个非零元: ";
cin >> ix >>jx >>elem;
aTriple.i = ix; aTriple.j = jx; aTriple.e = elem;
atsm.elems.push_back(aTriple);
}
vector<Triple>::iterator first = atsm.elems.begin();
vector<Triple>::iterator last = atsm.elems.end();
sort(first, last, rowmajorCriterion);
/*for(int i = 0; i < atsm.tu; i++){
cout <<"<"<< atsm.elems[i].i <<", "
<< atsm.elems[i].j <<", "
<< atsm.elems[i].e <<">";
cout <<endl;
}*/
return true;
}
void DestroyTSMatrix(TSMatrix &atsm){
}
void Print(const TSMatrix &atsm){
//能否漂亮打印??
if (!atsm.elems.empty()){
for(int r = 1; r <= atsm.row; r++){
for(int c = 1; c <= atsm.col; c++)
cout << getTriple(atsm, r, c) <<" ";
cout <<endl;
}
}
}
bool TranposeTSMatrix(const TSMatrix &stsm, TSMatrix &ttsm){
ttsm.row = stsm.col; ttsm.col = stsm.row; ttsm.tu = stsm.tu;
if(!ttsm.elems.empty()) //清空目标矩阵的三元组表
ttsm.elems.clear();
if (ttsm.tu){//如果有非零元
for (int c = 1; c <= stsm.col; c++){
for (int tx = 0; tx < stsm.tu; tx++)
if (stsm.elems[tx].j == c ){
Triple aTriple;
aTriple.i = stsm.elems[tx].j;
aTriple.j = stsm.elems[tx].i;
aTriple.e = stsm.elems[tx].e;
ttsm.elems.push_back(aTriple);
}
}//end of for
}
return true;
}
bool rowmajorCriterion(const Triple& t1, const Triple& t2){
if( (t1.i < t2.i) ) return true;
if( (t1.i == t2.i) && (t1.j < t2.j) ) return true;
return false;
}
ElemType getTriple(const TSMatrix &atsm, int ix, int jx){
for (int i = 0; i < atsm.tu; i++){
if ((atsm.elems[i].i == ix)&&(atsm.elems[i].j == jx)){
exit;
return atsm.elems[i].e;
}
}
return 0;//呵呵,有点问题,自己解决.
}
// ADT_SparseMatrix.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "SparseMatrix.h"
int _tmain(int argc, _TCHAR* argv[])
{
TSMatrix m, n;
enterTSMatrix(m);
Print(m);
TranposeTSMatrix(m,n);
cout << "m的转置矩阵是:"<<endl;
Print(n);
return 0;
}
分享到:
相关推荐
主要介绍了Python 稀疏矩阵-sparse 存储和转换的相关资料,需要的朋友可以参考下
c语言中的稀疏矩阵问题以及代码 希望大家喜欢
稀疏矩阵节点优化 matlab 程序 !!!!!!!
Batched Sparse Matrix Multiplication for Accelerating Graph Convolutional Networks 对图卷积网络进行加速的批量稀疏矩阵乘法 作者的ppt的pdf版本
稀疏矩阵 稀疏矩阵库。 安装 $ npm i ml-sparse-matrix 用法 import { SparseMatrix } from "ml-sparse-matrix" ; const matrix1 = new SparseMatrix ( [ [ 1 , 2 ] , [ 3 , 4 ] , ] ) ; const matrix2 = new ...
本代码是系数矩阵的c++实现,在数据结构中,有很多的数据结构,比如树,栈,队列,图,数组,链表等,而矩阵就是其中的一个简单应用。
稀疏矩阵的人脸检测 基于sparse的人脸检测paper
最快的稀疏矩阵乘法运算,英文版 Let A and B two n £ n matrices over a ring R (e.g., the reals or the integers) each containing at most m non-zero elements. We present a new algorithm that multiplies A...
如果在矩阵中,多数的元素并没有资料,称此矩阵为稀疏矩阵(sparse matrix),由于矩阵在程式中常使用二维阵列表示,二维阵列的大小与使用的记忆体空间成正比,如果多数的元素没有资料,则会造成记忆体空间的浪费,...
SparseMatrix.java
Source Code for 2009 Supercomputing Paper Implementing Sparse Matrix-Vector Multiplication on Throughput-Oriented Processors
SparseMatrix Implement the storage and operation of sparse matrix in matlab. 按行三数组与全存储相互转换 全存储转换为三数组 通过两个for循环将全矩阵转换为按行三数组存储模式,时间复杂度为$O(n^2)$,运行...
C++经典学习代码 (初学,高手均适合)工具是VC++ 2005专业版 揭示C++技术内幕经典学习代码! 有注释的,但是您需要时间学习的,一分耕耘一分收获!不管您是C++初学者还是高手均适合 这些都是经典中的...
什么是稀疏矩阵呢,就是在M*N的矩阵中,有效值的个数远小于无效值的个数,并且这些数据的分布没有规律。在压缩存储稀疏矩阵的时候我们只存储极少数的有效...class SparseMatrix { //三元组 template struct Trituple
对于那些零元素数目远远多于非零元素数目,并且非零元素的分布没有规律的矩阵称为稀疏矩阵(sparse)。 人们无法给出稀疏矩阵的确切定义,一般都只是凭个人的直觉来理解这个概念,即矩阵中非零元素的个数远远...
With sparse matrix computation
大气排放源清单处理模型 Sparse Matrix Operator Kernel Emissions 大气污染源源清单处理模型手册
算法主要是关于稀疏矩阵的表示比较适合初学者学习应用 sparse_representation\omp\1-grey.bmp .....................\...\1.bmp .....................\...\2.jpg .....................\...\DWT.m ...............
1. Re:随机采样方法整理与讲解 2. Re:稀疏矩阵存储格式总结+存储效 3. Re:稀疏矩阵存储格式总结+存储效 4. Re:机器学习降维算法一:PCA