NeoPZ
PZMatrixMarket.h
Go to the documentation of this file.
1 //
2 // PZMatrixMarket.hpp
3 // PZ
4 //
5 // Created by Philippe Devloo on 5/3/16.
6 //
7 //
8 
9 #ifndef PZMatrixMarket_hpp
10 #define PZMatrixMarket_hpp
11 
12 #include <stdio.h>
13 #include "pzfmatrix.h"
14 #include "pzsbndmat.h"
15 #include "pzbndmat.h"
16 #include "pzskylmat.h"
17 #include "pzsysmp.h"
18 #include <fstream>
19 
21 {
22 public:
23 
24  template<class T>
25  static void Read(std::string filename, TPZFMatrix<T> &fmat);
26 
27  template<class T>
28  static void Read(std::string filename, TPZFBMatrix<T> &fmat);
29 
30  template<class T>
31  static void Read(std::string filename, TPZSBMatrix<T> &fmat);
32 
33  template<class T>
34  static void Read(std::string filename, TPZSkylMatrix<T> &fmat);
35 
36  template<class T>
37  static void Read(std::string filename, TPZSYsmpMatrix<T> &fmat);
38 };
39 
40 template<class T>
42 {
43  int64_t nrow,ncol,nonzero=0,nsup=0,nlower=0;
44  {
45  std::ifstream input(filename.c_str());
46  std::string buf;
47  std::getline(input,buf);
48  input >> nrow >> ncol >> nonzero;
49  fmat.Redim(nrow,ncol);
50  for (int64_t el=0; el<nonzero; el++) {
51  int64_t row, col;
52  T val;
53  input >> row >> col >> val;
54  row--;
55  col--;
56  if(row > col) nlower++;
57  if(row< col ) nsup++;
58  fmat.PutVal(row,col,val);
59  }
60  }
61  if(nsup == 0 || nlower == 0)
62  {
63  std::ifstream input(filename);
64  std::string buf;
65  std::getline(input,buf);
66  input >> nrow >> ncol >> nonzero;
67  fmat.Redim(nrow,ncol);
68  for (int64_t el=0; el<nonzero; el++) {
69  int64_t row, col;
70  T val;
71  input >> row >> col >> val;
72  row--;
73  col--;
74  int64_t swap = row;
75  row = col;
76  col = swap;
77  fmat.PutVal(row,col,val);
78  }
79  }
80 }
81 
82 template<class T>
84 {
85  int64_t nrow,ncol,nonzero=0,nsup=0,nlower=0, band=0;
86  {
87  std::ifstream input(filename);
88  std::string buf;
89  std::getline(input,buf);
90  input >> nrow >> ncol >> nonzero;
91  fmat.Redim(nrow,ncol);
92  for (int64_t el=0; el<nonzero; el++) {
93  int64_t row, col;
94  T val;
95  input >> row >> col >> val;
96  if(row-col > band) band = row-col;
97  if(col-row > band) band = col-row;
98  row--;
99  col--;
100  if(row > col) nlower++;
101  if(row< col ) nsup++;
102  }
103  }
104  fmat.Redim(nrow,ncol);
105  fmat.SetBand(band);
106  {
107  std::ifstream input(filename);
108  std::string buf;
109  std::getline(input,buf);
110  input >> nrow >> ncol >> nonzero;
111  for (int64_t el=0; el<nonzero; el++) {
112  int64_t row, col;
113  T val;
114  input >> row >> col >> val;
115  row--;
116  col--;
117  int64_t swap = row;
118  row = col;
119  col = swap;
120  fmat.PutVal(row,col,val);
121  if (nsup == 0 || nlower == 0) {
122  fmat.PutVal(col,row,val);
123  }
124  }
125  }
126 }
127 
128 template<class T>
130 {
131  int64_t nrow,ncol,nonzero=0,nsup=0,nlower=0, band=0;
132  {
133  std::ifstream input(filename);
134  std::string buf;
135  std::getline(input,buf);
136  input >> nrow >> ncol >> nonzero;
137  for (int64_t el=0; el<nonzero; el++) {
138  int64_t row, col;
139  T val;
140  input >> row >> col >> val;
141  if(row-col > band) band = row-col;
142  if(col-row > band) band = col-row;
143  row--;
144  col--;
145  if(row > col) nlower++;
146  if(row< col ) nsup++;
147  }
148  }
149  fmat.Redim(nrow,ncol);
150  fmat.SetBand(band);
151  {
152  std::ifstream input(filename);
153  std::string buf;
154  std::getline(input,buf);
155  input >> nrow >> ncol >> nonzero;
156  for (int64_t el=0; el<nonzero; el++) {
157  int64_t row, col;
158  T val;
159  input >> row >> col >> val;
160  row--;
161  col--;
162  fmat.PutVal(row,col,val);
163  }
164  }
165 }
166 
167 template<class T>
169 {
170  int64_t nrow,ncol,nonzero=0,nsup=0,nlower=0;
171  TPZVec<int64_t> skyline(nrow,0);
172  {
173  std::ifstream input(filename.c_str());
174  std::string buf;
175  std::getline(input,buf);
176  input >> nrow >> ncol >> nonzero;
177  if (nrow != ncol) {
178  DebugStop();
179  }
180  skyline.resize(nrow);
181  for (int64_t el=0; el<nrow; el++) {
182  skyline[el] = el;
183  }
184  fmat.Redim(nrow,ncol);
185  for (int64_t el=0; el<nonzero; el++) {
186  int64_t row, col;
187  T val;
188  input >> row >> col >> val;
189  row--;
190  col--;
191  if(row < skyline[col]) skyline[col] = row;
192  if(col < skyline[row]) skyline[row] = col;
193  if(row > col) nlower++;
194  if(row< col ) nsup++;
195  }
196  }
197  if (nlower && nsup) {
198  std::cout << "The matrix is probably not symmetric, expect trouble\n";
199  }
200  fmat.Redim(nrow,ncol);
201  fmat.SetSkyline(skyline);
202  {
203  std::ifstream input(filename.c_str());
204  std::string buf;
205  std::getline(input,buf);
206  input >> nrow >> ncol >> nonzero;
207  for (int64_t el=0; el<nonzero; el++) {
208  int64_t row, col;
209  T val;
210  input >> row >> col >> val;
211  row--;
212  col--;
213  fmat.PutVal(row,col,val);
214  }
215  }
216 }
217 
218 template<class T>
220 {
221  int64_t nrow,ncol,nonzero=0,nsup=0,nlower=0;
222  TPZVec<int64_t> numrowel(nrow,0);
223  {
224  std::ifstream input(filename);
225  std::string buf;
226  std::getline(input,buf);
227  input >> nrow >> ncol >> nonzero;
228  if (nrow != ncol) {
229  DebugStop();
230  }
231  numrowel.resize(nrow);
232  numrowel.Fill(0);
233  fmat.Redim(nrow,ncol);
234  for (int64_t el=0; el<nonzero; el++) {
235  int64_t row, col;
236  T val;
237  input >> row >> col >> val;
238  row--;
239  col--;
240  numrowel[row]++;
241  if(row > col) nlower++;
242  if(row< col ) nsup++;
243  }
244  }
245  if (nlower && nsup) {
246  std::cout << "The matrix is probably not symmetric, expect trouble\n";
247  }
248  TPZVec<int64_t> IA(nrow+1);
249  TPZVec<int64_t> JA(nonzero,0);
250  TPZVec<T> A(nonzero,0.);
251  TPZVec<int64_t> IAcounter(nonzero);
252  IA[0] = 0;
253  for (int64_t i=0; i<nrow; i++) {
254  IA[i+1] = IA[i]+numrowel[i];
255  }
256  IAcounter = IA;
257  fmat.Redim(nrow,ncol);
258  {
259  std::ifstream input(filename);
260  std::string buf;
261  std::getline(input,buf);
262  input >> nrow >> ncol >> nonzero;
263  for (int64_t el=0; el<nonzero; el++) {
264  int64_t row, col;
265  T val;
266  input >> row >> col >> val;
267  row--;
268  col--;
269  JA[IAcounter[row]] = col;
270  A[IAcounter[row]] = val;
271  IAcounter[row]++;
272  }
273  }
274  fmat.SetData(IA,JA,A);
275 }
276 
277 
278 #endif /* PZMatrixMarket_hpp */
int Redim(const int64_t newDim)
Redimension the matrix and zeroes its elements.
Definition: pzsbndmat.h:94
Implements symmetric band matrices. Matrix.
Definition: pzsbndmat.h:25
filename
Definition: stats.py:82
Contains TPZSBMatrix class which implements symmetric band matrices(hermitian, for the complex case...
int SetBand(const int64_t newBand)
Sets band size.
Definition: pzbndmat.cpp:458
virtual void resize(const int64_t newsize)
Definition: pzvec.h:213
clarg::argString input("-if", "input file", "cube1.txt")
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
static void Read(std::string filename, TPZFMatrix< T > &fmat)
Implements a skyline storage format. A Skyline matrix is symmetric so square. Matrix.
Definition: pzskylmat.h:394
virtual void SetData(const TPZVec< int64_t > &IA, const TPZVec< int64_t > &JA, const TPZVec< TVar > &A)
Sets data to the class.
Definition: pzsysmp.h:198
void SetSkyline(const TPZVec< int64_t > &skyline)
modify the skyline of the matrix, throwing away its values skyline indicates the minimum row number w...
Definition: pzskylmat.cpp:1792
Contains TPZMatrixclass which implements full matrix (using column major representation).
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension the matrix and make zero its elements.
Definition: pzbndmat.cpp:412
int PutVal(const int64_t row, const int64_t col, const TVar &element) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
Definition: pzsbndmat.cpp:163
int SetBand(const int64_t newBand)
Definition: pzsbndmat.cpp:605
Contains TPZSkyline class which implements a skyline storage format.
virtual int Redim(const int64_t newRows, const int64_t newCols)
Redimensions the matrix reinitializing it with zero.
Definition: pzmatrix.h:289
int PutVal(const int64_t row, const int64_t col, const TVar &value) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
Definition: pzbndmat.h:170
int Redim(const int64_t newRows, const int64_t newCols) override
Redimension a matrix and ZERO your elements.
Definition: pzfmatrix.h:616
int Redim(const int64_t newDim, const int64_t) override
Redimensions the matrix reinitializing it with zero.
Definition: pzskylmat.cpp:2516
Contains TPZFBMatrix class which defines a non symmetric banded matrix.
int PutVal(const int64_t row, const int64_t col, const TVar &element) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
Definition: pzskylmat.cpp:1920
Implements a symmetric sparse matrix. Matrix.
Definition: pzsysmp.h:22
Defines a non symmetric banded matrix. Matrix.
Definition: pzbndmat.h:25
void Fill(const T &copy, const int64_t from=0, const int64_t numelem=-1)
Will fill the elements of the vector with a copy object.
Definition: pzvec.h:460
int PutVal(const int64_t row, const int64_t col, const TVar &value) override
Put values without bounds checking This method is faster than "Put" if DEBUG is defined.
Definition: pzfmatrix.h:548
Contains TPZSYsmpMatrix class which implements a symmetric sparse matrix. Purpose: Defines operation...