C ++ - Как преобразовать разреженную матрицу в плотную только с использованием векторов - PullRequest
0 голосов
/ 15 декабря 2018

Вот что я делал до сих пор:

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

struct Matrix {
   vector<vector<double> > store;
   int nr;
   int nc;
};

struct SparseEntry {
   int r, c;
   double val;
   SparseEntry *next;
};

struct SparceMatrix {
   SparseEntry *store;
   int nr, nc, nnz;
};

bool iszero(double val);
Matrix matrix(const vector<double> &v, const int nr, const int nc);
void printFullMatrix(const Matrix &m);

int main() {  
   double val;
   cout << "Insert the element in position r, c: " << endl;
   cin >> val;
   iszero(val);
   vector<double> v;
   Matrix emme;
   int nr=8; 
   int nc=5;
   emme=matrix(v, nr, nc);

   printFullMatrix(emme);
   return 0;
}

bool iszero(double val) {
    static const int tolerance = 1e-12;
    return (fabs(val)<tolerance);
}

Matrix matrix(const vector<double> &v, const int nr, const int nc) { 
    Matrix m;
    m.nr=nr;
    m.nc=nc;
    cout << nr << nc;
    m.store.resize(nr); 
    for (int r=0; r<nr; ++r) {
    m.store[r].resize(nc);
    for(int c=0; c<(nc-1); ++c)
    {
        m.store[r][c]=c;
        cout << m.store[r][c];
    }
    cout << endl;
}
for (int r=0; r<(nr-1); ++r) {
    for (int c=0; c<(nc-1); ++c) {
        if(v.at(c+r*nc)!=0) {   
            m.store[r][c]=v.at(c+r*nc); 
        }
        else {
            m.store[r][c]=0;
        }
    }
}
return m;
}

void printFullMatrix(const Matrix &m) {
    for(int r=0; r<(m.nr-1); ++r) {
        for(int c=0; c<(m.nc-1); ++c) {
            cout << m.store[r][c];
        }
        cout << endl;
    }
}

Мне нужно написать функцию, которая преобразует плотные матрицы в плотные, а другую - с точностью до наоборот, используя только векторы.Для первого я думал о SparseMatrix sparse (const Matrix & m);во-вторых, Matrix full (const SparseMatrix & s).Может ли кто-нибудь так любезно объяснить мыслительный процесс, стоящий за преобразованием?Заранее спасибо.

...