稀疏矩阵1
/* Trituple.cpp*/
#include<iostream.h>
#include<stdlib.h>
using namespace std;
const int DefaultSize=100;
template<class T>
struct Trituple{
int row,col;
T value;
Trituple<T>& operator=(Trituple<T> &t){
row=t.row;col=t.col;value.t.value;
}
};
template<class T>
class SparseMatrix{
friend ostream& operator<<(ostream &out,SparseMatrix<T> &m);
friend istream& operator>>(istream &in,SparseMatrix<T> &m);
protected:
int rows,cols,terms;
Trituple<T> *smArray;
int maxTerms;
public:
SparseMatrix(int maxSize=DefaultSize);
SparseMatrix(SparseMatrix<T> &x);
~SparseMatrix(){delete []smArray;}
SparseMatrix<T>& operator=(SparseMatrix<T> &sm);
SparseMatrix<T> Transpose();
SparseMatrix<T> Add(SparseMatrix<T> &sm);
SparseMatrix<T> Multiply(SparseMatrix<T> &sm);
};
template<class T>
SparseMatrix<T>::SparseMatrix(int maxSize):maxTerms(maxSize){
assert(maxSize>=1);
smArray= new Trituple<T>[maxSize];
assert(smArray!=NULL);
rows=cols=terms=0;
}
template<class T>
SparseMatrix<T>::SparseMatrix(SparseMatrix<T> &x){
rows=x.rows;
terms=x.terms;
cols=x.cols;
maxTerms=x.maxTerms;
smArray=new Trituple<T>[x.maxTerms];
assert(smArray!=NULL);
for(int i=0;i<terms;i++){
smArray[i]=x.smArray[i];
}
}
template<class T>
ostream& operator<<(ostream &out,SparseMatrix<T> &x){
out<<"row:"<<x.rows<<endl;
out<<"col:"<<x.cols<<endl;
out<<"none zero terms:"<<x.terms<<endl;
for(int i=0;i<x.terms;i++){
out<<"X["<<x.rows<<"]"<<"["<<x.cols<<"]="<<x.smArray[i]<<endl;
}
return out;
}
template<class T>
istream& operator>>(istream &in,SparseMatrix<T> &x){
in>>x.rows>>x.cols>>x.terms;
assert(x.terms<=x.maxTerms);
for(int i=0;i<x.terms;i++){
in>>x.smArray[i].row>>x.smArray[i].col>>x.smArray[i].value;
}
return in;
}
template<class T>
SparseMatrix<T> SparseMatrix<T>::Transpose(){
SparseMatrix<T> newMat=new SparseMatrix<T>(maxTerms);
newMat.rows=cols;
newMat.cols=rows;
newMat.terms=terms;
if(terms>0){
int k,i,bp;
//exchange col and row
for(k=0;k<cols;k++)
for(i=0;i<terms;i++)
if(smArray[i).col==k){
newMat.smArray[pb].row=smArray[i].col;
newMat.smArray[pb].col=smArray[i].row;
newMat.smArray[pb].value=smArray[i].value;
pb++;
}
}
return newMat;
}
/* transpose in O(cols*terms) */
/* better choice */
template<class T>
SparseMatrix<T> SparseMatrix<T>::FastTranspose(){
int *rowSize=new int[cols];
int *rowStart=new int[cols];
SparseMatrix<T> b(maxTerms);
b.rows=cols;b.cols=row;b.terms=terms;
if(terms>0){
int i,j
for(i=0;i<cols;i++)
rowSize[i]=0;
for(i=0;i<terms;i++)
rowSize[smArray[i].col]++;
rowStart[0]=0;
for(i=1;i<cols;i++)
rowStart[i]=rowStart[i-1]+rowSize[i-1];
for(i=0;i<terms;i++){
j=rowStart[smArray[i].col];
b.smArray[j].row=smArray[i].col;
b.smArray[j].col=smArray[i].row;
b.smArray[j].value=smArray[i].value;
rowStart[smArray[i].col]++;
}
}
delete []rowSize;delete []rowStart;
}
对称矩阵和三对角矩阵的压缩
void SquareMatrix(int *dest,int **src,int n){
for(int i=0;i<n;i++)
for(int j=0;j<=i;j++){
dest[(i+1)*i/2+j]=src[i][j];
// dest[2i+j-3]=src[i][j];
//
}
}
void SquareMatrix(vector<int> &dest,const vector<vector<int> > &src){
dest=new vector<int>(src.size());
for(int i=0;i<src.size();i++)
for(int j=0;j<=i;j++){
dest[(i+1)*i/2+j]=src[i][j];
}
}