DD-AVX  2.0.0
matrix_sys.cpp
Go to the documentation of this file.
1 #include<DD-AVX_internal.hpp>
2 
3 double d_real_SpMat::at(const int r, const int c){
4  for(int j = row_ptr[r]; j < row_ptr[r+1]; j++){
5  if(col_ind[j] == c){
6  return val[j];
7  }
8  }
9  return 0.0;
10 }
11 
12 void d_real_SpMat::insert(const int r, const int c, const double a){
13  //printf("insert start r = %d, c = %d, a = %f\n", r, c, a);
14 
15  for(int j = row_ptr[r]; j < row_ptr[r+1]; j++){
16  if(col_ind[j] == c){
17  val[j] = a;
18  return;
19  }
20  }
21 
22  //pos_check
23  int pos = row_ptr[r+1];
24 
25  for(int j = row_ptr[r]; j < row_ptr[r+1]; j++){
26  if( c < col_ind[j]){
27  pos = j;
28  break;
29  }
30  }
31  //printf("pos = %d\n", pos);
32 
33 
34  val.insert(val.begin() + pos, a);
35  col_ind.insert(col_ind.begin() + pos, c);
36  nnz++;
37 
38  for(int i = r+1; i < row+1; i++){
39  row_ptr[i]++;
40  }
41  //printf("row_ptr: %d, %d, %d\n", row_ptr[0], row_ptr[1], row_ptr[2]);
42 }
43 
45  row = mat.row;
46  nnz = mat.nnz;
47  std::copy(mat.val.begin(), mat.val.end(), back_inserter(val));
48  std::copy(mat.row_ptr.begin(), mat.row_ptr.end(), back_inserter(row_ptr));
49  std::copy(mat.col_ind.begin(), mat.col_ind.end(), back_inserter(col_ind));
50 }
51 
53  this->copy(mat);
54  return *this;
55 }
56 
59  if(row < r){
60  std::cerr << "error bad row" << std::endl;
61  assert(1);
62  }
63 
64  std::vector<double> ret(row, 0.0);
65 
66  for(int j = row_ptr[r]; j < row_ptr[r+1]; j++){
67  ret.data()[col_ind[j]] = val[j];
68  }
69  return ret;
70 }
71 
72 
74  if(row < c){
75  std::cerr << "error bad col" << std::endl;
76  assert(1);
77  }
78  std::vector<double> ret(row, 0.0);
79 
80 #pragma omp parallel for
81  for(int i=0; i<row; i++){
82  for(int j = row_ptr[i]; j < row_ptr[i+1]; j++){
83  if(col_ind[j] == c)
84  ret.data()[i] = val[j];
85  }
86  }
87  return ret;
88 }
89 
90 
92  std::vector<double> ret(row, 0.0);
93 
94 #pragma omp parallel for
95  for(int i=0; i<row; i++){
96  for(int j = row_ptr[i]; j < row_ptr[i+1]; j++){
97  if(i == col_ind[j])
98  ret.data()[i] = val[j];
99  }
100  }
101  return ret;
102 }
double precision CRS format sparse matrix
d_real_vector get_col_vec(const int c)
get col vector A[*,c]
Definition: matrix_sys.cpp:73
std::vector< int > col_ind
d_real_vector get_row_vec(const int r)
get row vector A[r,*]
Definition: matrix_sys.cpp:58
std::vector< int > row_ptr
void insert(const int r, const int c, const double a)
insert value a to A[r, c]
Definition: matrix_sys.cpp:12
d_real_vector get_diag_vec()
get diagonal vector
Definition: matrix_sys.cpp:91
double at(const int r, const int c)
get A[r, c]
Definition: matrix_sys.cpp:3
std::vector< double > val
d_real_SpMat & operator=(const d_real_SpMat &mat)
copy operator
Definition: matrix_sys.cpp:52
void copy(const d_real_SpMat &mat)
copy, mat = A
Definition: matrix_sys.cpp:44
Double precision vector class, This class is almost same as std::vector<double>