@@ -10,6 +10,7 @@ kr_balancing::kr_balancing(const SparseMatrixCol & input){
10
10
I.setIdentity ();
11
11
I = I*0.00001 ;
12
12
A = A + I;
13
+ rescaled = false ;
13
14
}
14
15
15
16
@@ -130,12 +131,15 @@ void kr_balancing::inner_loop(){
130
131
131
132
void kr_balancing::compute_normalised_matrix (bool & rescale){
132
133
assert (A.rows () == A.cols ());
133
- A = SparseMatrixCol (A.triangularView <Eigen::Upper>());
134
- if (rescale ==true ){
134
+ if (rescale ==true && rescaled == false ){
135
135
rescale_norm_vector ();
136
+ rescaled = true ;
137
+ }else {
138
+ A = SparseMatrixCol (A.triangularView <Eigen::Upper>());
136
139
}
137
140
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
138
141
for (int k=0 ; k<A.outerSize (); ++k){
142
+ #pragma omp critical
139
143
for (SparseMatrixCol::InnerIterator it (A,k); it; ++it){
140
144
it.valueRef () = it.value ()*x.coeff (it.row (),0 )*x.coeff (it.col (),0 );
141
145
}
@@ -145,10 +149,14 @@ void kr_balancing::compute_normalised_matrix(bool & rescale){
145
149
146
150
// Rescaling the normalisation factor (x)
147
151
void kr_balancing::rescale_norm_vector (){
148
- double original_sum = 0 ;
149
- double norm_vector_sum = 0 ;
150
- #pragma omp parallel for num_threads(num_threads) schedule(dynamic)
152
+ double original_sum = 0.0 ;
153
+ double norm_vector_sum = 0.0 ;
154
+ assert (A.rows () == A.cols ());
155
+ A = SparseMatrixCol (A.triangularView <Eigen::Upper>());
156
+
157
+ #pragma omp parallel for num_threads(num_threads)
151
158
for (int k=0 ; k<A.outerSize (); ++k){
159
+ #pragma omp critical
152
160
for (SparseMatrixCol::InnerIterator it (A,k); it; ++it){
153
161
if (it.row ()==it.col ()){
154
162
original_sum += it.value ();
@@ -159,6 +167,7 @@ void kr_balancing::rescale_norm_vector(){
159
167
}
160
168
}
161
169
}
170
+
162
171
std::cout << " normalisation factor is " << std::sqrt (norm_vector_sum/original_sum) <<std::endl;
163
172
x /= std::sqrt (norm_vector_sum/original_sum);
164
173
@@ -172,8 +181,9 @@ const SparseMatrixCol* kr_balancing::get_normalised_matrix(bool & rescale){
172
181
173
182
174
183
const SparseMatrixCol* kr_balancing::get_normalisation_vector (bool & rescale){
175
- if (rescale ==true ){
184
+ if (rescale ==true && rescaled == false ){
176
185
rescale_norm_vector ();
186
+ rescaled = true ;
177
187
}
178
188
return &x;
179
189
}
0 commit comments