Ice Sheet System Model  4.18
Code documentation
SparseRow.h
Go to the documentation of this file.
1 
5 #ifndef _SPARSE_ROW_H_
6 #define _SPARSE_ROW_H_
7 
8 /*Headers:*/
9 #include "../toolkitsenums.h"
10 #include "../../shared/shared.h"
11 
12 template <class doubletype>
13 class SparseRow{
14 
15  public:
16 
17  int M; //real size
18  int ncols; //number of non-zeros
19  int* indices;
20  doubletype* values;
21 
22  /*SparseRow constructors, destructors*/
23  SparseRow(){ /*{{{*/
24  M=0;
25  ncols=0;
26  indices=NULL;
27  values=NULL;
28  } /*}}}*/
29  SparseRow(int in_M){/*{{{*/
30 
31  M=in_M;
32  ncols=0;
33  indices=NULL;
34  values=NULL;
35 
36  } /*}}}*/
37  ~SparseRow(){/*{{{*/
38  if(ncols){
39  xDelete<int>(indices);
40  xDelete<doubletype>(values);
41  }
42  } /*}}}*/
43 
44  /*SparseRow specific routines*/
45  void Echo(){ /*{{{*/
46  int i;
47 
48  for(i=0;i<ncols;i++){
49  _printf_("(" << indices[i] << "," << values[i] << ") ");
50  }
51  } /*}}}*/
52  void SetValues(int numvalues,int* cols,doubletype* vals, int* mods){ /*{{{*/
53 
54  int count;
55  int i,j;
56 
57  if(!M)_error_("unknow dimension for this sparse row!");
58 
59  /*Deallocate if already allocated: */
60  if(ncols){
61  xDelete<int>(indices);
62  xDelete<doubletype>(values);
63  }
64 
65  /*check numvalues: */
66  if(!numvalues)return;
67 
68  /*Go through cols and resolve duplicates: */
69  for(i=0;i<numvalues;i++){
70  for(j=i+1;j<numvalues;j++){
71  if (cols[j]==cols[i]){
72  if (mods[j]==ADD_VAL){
73  vals[i]+=vals[j];
74  }
75  else vals[i]=vals[j];
76  cols[j]=-1;
77  }
78  /*Ensure that this value will not be used anymore: */
79  }
80  }
81 
82  /*Now go through cols once more, and retrieve only what we need: */
83  ncols=0;
84  for(i=0;i<numvalues;i++)if(cols[i]>=0)ncols++;
85 
86  /*Allocate and fill: */
87  indices=xNew<int>(ncols); _assert_(indices);
88  values=xNewZeroInit<doubletype>(ncols); _assert_(values);
89 
90  count=0;
91  for(i=0;i<numvalues;i++){
92  if(cols[i]>=0){
93  indices[count]=cols[i];
94  values[count]=vals[i];
95  count++;
96  }
97  }
98 
99  if(count!=ncols)_error_("counter problem during set values operations");
100  } /*}}}*/
101  doubletype Norm(NormMode mode){ /*{{{*/
102 
103  int i;
104  doubletype norm=0.;
105 
106  switch(mode){
107  case NORM_INF:
108  for(i=0;i<ncols;i++){
109  norm+=fabs(values[i]);
110  }
111  return norm;
112  break;
113  case NORM_FROB:
114  for(i=0;i<ncols;i++){
115  norm+=values[i]*values[i];
116  }
117  return norm;
118  break;
119 
120  default:
121  _error_("unknown norm !");
122  break;
123  }
124  }
125  /*}}}*/
126  doubletype Mult(doubletype* X){ /*{{{*/
127 
128  int i;
129  doubletype mult=0;
130 
131  for(i=0;i<ncols;i++){
132  mult+=values[i]*X[indices[i]];
133  }
134 
135  return mult;
136  }
137  /*}}}*/
138  int Nnz(void){ /*{{{*/
139 
140  return ncols;
141  }
142  /*}}}*/
143  void SetIrnJcnA(int* irn_loc,int* jcn_loc,doubletype* a_loc,int i_index,int count){/*{{{*/
144  int i;
145 
146  for(i=0;i<ncols;i++){
147  irn_loc[count+i]=i_index;
148  jcn_loc[count+i]=indices[i]+1; //fortran indexing
149  a_loc[count+i]=values[i];
150  }
151  }
152  /*}}}*/
153 };
154 #endif //#ifndef _SPARSE_ROW_H_
_assert_
#define _assert_(ignore)
Definition: exceptions.h:37
SparseRow::~SparseRow
~SparseRow()
Definition: SparseRow.h:37
_printf_
#define _printf_(StreamArgs)
Definition: Print.h:22
ADD_VAL
@ ADD_VAL
Definition: toolkitsenums.h:14
NORM_INF
@ NORM_INF
Definition: toolkitsenums.h:15
SparseRow::SetValues
void SetValues(int numvalues, int *cols, doubletype *vals, int *mods)
Definition: SparseRow.h:52
SparseRow::Norm
doubletype Norm(NormMode mode)
Definition: SparseRow.h:101
SparseRow::Mult
doubletype Mult(doubletype *X)
Definition: SparseRow.h:126
SparseRow
Definition: SparseRow.h:13
NORM_FROB
@ NORM_FROB
Definition: toolkitsenums.h:15
SparseRow::Echo
void Echo()
Definition: SparseRow.h:45
SparseRow::ncols
int ncols
Definition: SparseRow.h:18
SparseRow::Nnz
int Nnz(void)
Definition: SparseRow.h:138
SparseRow::SparseRow
SparseRow(int in_M)
Definition: SparseRow.h:29
NormMode
NormMode
Definition: toolkitsenums.h:15
SparseRow::SetIrnJcnA
void SetIrnJcnA(int *irn_loc, int *jcn_loc, doubletype *a_loc, int i_index, int count)
Definition: SparseRow.h:143
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
SparseRow::values
doubletype * values
Definition: SparseRow.h:20
SparseRow::M
int M
Definition: SparseRow.h:17
SparseRow::SparseRow
SparseRow()
Definition: SparseRow.h:23
SparseRow::indices
int * indices
Definition: SparseRow.h:19