@@ -34,26 +34,15 @@ class kr_balancing{
34
34
SparseMatrixCol I;
35
35
I.resize (A.rows (),A.cols ());
36
36
I.setIdentity ();
37
- I = I*0.00001 ;
38
- A = A + I;
39
- x = e.sparseView ();
40
- // Eigen::MatrixXd test;
41
- // test.resize(A.rows(),A.cols());
42
- // SparseMatrixCol temp = test.sparseView();
43
- // A = A + temp;
44
- // A = A*1.00001;
45
- // for (int k=0; k<A.outerSize(); ++k){
46
- // for(SparseMatrixCol::InnerIterator it(A,k); it; ++it){
47
- // std::cout << it.value() << " , ";
48
- // }
49
- // }
37
+ // I = I*0.00001;
38
+ // A = A + I;
50
39
}
51
40
52
41
void outter_loop (){
42
+ x = e.sparseView ();
53
43
size_t outter_loop_count = 0 ;
54
44
double stop_tol = tol*0.5 ;
55
45
double eta = etamax;
56
- // x = x0;
57
46
assert (x.isVector ());
58
47
double rt = std::pow (tol,2 );
59
48
v = x.cwiseProduct (A*x);
@@ -66,26 +55,22 @@ class kr_balancing{
66
55
double rold = rout;
67
56
if (fl == 1 ) std::cout<< ' intermediate convergence statistics is off' <<std::endl;
68
57
size_t nochange = 0 ;
69
- while (rout > rt && nochange < 100 ){// outer itteration
58
+ while (rout > rt){// outer itteration
70
59
outter_loop_count ++;
60
+ // if(outter_loop_count == 30) break;
71
61
i=i+1 ; k=0 ; y=e.sparseView ();
72
62
innertol = std::max (std::pow (eta,2 )*rout,rt);
73
63
inner_loop ();
74
- assert (rho_km1.coeff (0 ,0 ) > 0 );
64
+ // assert(rho_km1.coeff(0,0) > 0);
75
65
assert (rho_km1.coeff (0 ,0 ) != std::numeric_limits<double >::infinity ());
76
66
x=x.cwiseProduct (y);
77
- // std::cout << "x" << x << std::endl;
78
67
assert (x.isVector ());
79
68
v=x.cwiseProduct ((A*x));
80
- // std::cout << "v "<< v << std::endl;
81
69
rk = Eigen::VectorXd::Constant (v.rows (),1 ) - v;
82
- rho_km1 = rk.transpose ()*rk;
83
- assert (rho_km1.coeff (0 ,0 ) > 0 );
70
+ rho_km1 = rk.conjugate (). transpose ()*rk;
71
+ // assert(rho_km1.coeff(0,0) > 0);
84
72
assert (rho_km1.coeff (0 ,0 ) != std::numeric_limits<double >::infinity ());
85
- if (std::abs (rout - rho_km1.coeff (0 ,0 )) < 0.0000001 ||rho_km1.coeff (0 ,0 ) ==std::numeric_limits<double >::infinity ()){
86
- nochange++;
87
- // std::cout << "nochange " << rho_km1.coeff(0,0)<<std::endl;
88
- }
73
+
89
74
rout = rho_km1.coeff (0 ,0 );
90
75
MVP = MVP + k + 1 ;
91
76
// Update inner iteration stopping criterion.
@@ -99,7 +84,13 @@ class kr_balancing{
99
84
res.push_back (res_norm);
100
85
}
101
86
if (outter_loop_count %50 ==0 ) std::cout << " outer loop number " << outter_loop_count <<std::endl;
102
- }
87
+ }// End of outer loop
88
+ // if (nochange >= 100) {
89
+ // std::cout << "nochange >=100" <<std::endl;
90
+ // return 0;
91
+ // }else{
92
+ // return &x;
93
+ // }
103
94
}
104
95
void inner_loop (){
105
96
size_t inner_loop_count = 0 ;
@@ -108,77 +99,65 @@ class kr_balancing{
108
99
k++;
109
100
if (k == 1 ){
110
101
Z = rk.cwiseQuotient (v);
111
- // std::cout << "Z "<< Z << std::endl;
112
- // std::cout << "rk " << rk << std::endl;
113
- // std::cout << "v "<< v <<std::endl;
114
102
p=Z;
115
- rho_km1 = rk.transpose ()*Z;
103
+ rho_km1 = rk.conjugate (). transpose ()*Z;
116
104
}else {
117
- // std::cout << "rho_km2 " << rho_km2 << std::endl;
118
105
Eigen::VectorXd beta=rho_km1.cwiseQuotient (rho_km2);
119
106
p = Z + (beta (0 )*p);
120
107
121
108
}
122
- // std::cout << "p "<< p <<std::endl;
123
109
// Update search direction efficiently.
124
110
SparseMatrixCol w=x.cwiseProduct (A*(x.cwiseProduct (p))) + v.cwiseProduct (p);
125
111
SparseMatrixCol alpha_mat = rho_km1.cwiseQuotient (p.conjugate ().transpose ()*w);
126
112
double alpha = alpha_mat.coeff (0 ,0 );
127
113
Eigen::VectorXd ap = alpha * p;
128
114
// Test distance to boundary of cone.
129
115
Eigen::VectorXd ynew = y + ap;
130
- // std::cout << "ynew.minCoeff() " << ynew.minCoeff() <<std::endl;
131
116
if (ynew.minCoeff () <= delta){
132
117
if (delta == 0 ) break ;
133
118
Eigen::Matrix<bool , Eigen::Dynamic , Eigen::Dynamic> ind_helper = (ap.array ()<0 );
134
119
Eigen::VectorXi ind = Eigen::VectorXi::LinSpaced (ind_helper.size (),0 ,ind_helper.size ()-1 );
135
120
ind.conservativeResize (std::stable_partition (
136
121
ind.data (), ind.data ()+ind.size (), [&ind_helper](int i){return ind_helper (i);})-ind.data ());
137
- Eigen::VectorXd y_copy = y;
138
- for (size_t i = 0 ; i < ind.size (); i++){ // TODO proper masking? //ind_vec.unaryExpr(x);
139
- y_copy (ind (i)) = (delta - y_copy (ind (i)))/ap (i);
122
+ Eigen::VectorXd y_copy;
123
+ y_copy.resize (ind.size ());
124
+ for (size_t i = 0 ; i < ind.size (); i++){
125
+ y_copy (i) = (delta - y (ind (i)))/ap (ind (i));
140
126
}
141
127
double gamma = y_copy.minCoeff ();
142
128
y = y + gamma*ap;
143
- // std::cout << y << std::endl;
144
129
break ;
145
130
}
146
- if (ynew.minCoeff () >= Delta){
131
+ if (ynew.maxCoeff () >= Delta){
147
132
Eigen::Matrix<bool , Eigen::Dynamic , Eigen::Dynamic> ind_helper = (ynew.array () > Delta);
148
133
Eigen::VectorXi ind = Eigen::VectorXi::LinSpaced (ind_helper.size (),0 ,ind_helper.size ()-1 );
149
134
ind.conservativeResize (std::stable_partition (
150
135
ind.data (), ind.data ()+ind.size (), [&ind_helper](int i){return ind_helper (i);})-ind.data ());
151
- Eigen::VectorXd y_copy = y;
152
- for (size_t i = 0 ; i < ind.size (); i++){ // TODO proper masking? //ind_vec.unaryExpr(x);
153
- y_copy (ind (i)) = (Delta - y_copy (ind (i)))/ap (i);
136
+ Eigen::VectorXd y_copy;
137
+ y_copy.resize (ind.size ());
138
+ for (size_t i = 0 ; i < ind.size (); i++){
139
+ y_copy (i) = (Delta - y (ind (i)))/ap (ind (i));
154
140
}
155
141
double gamma = y_copy.minCoeff ();
156
- y = y + gamma*ap;
142
+ y = y + ( gamma*ap) ;
157
143
break ;
158
144
}
159
-
160
145
y = ynew;
161
146
rk = rk - (alpha*w); rho_km2 = rho_km1;
162
147
Z = rk.cwiseQuotient (v); rho_km1 = rk.conjugate ().transpose ()*Z;
163
- // std::cout << "rho_km1 "<< rho_km1 <<std::endl;
164
- if (inner_loop_count %100 ==0 ) std::cout << " inner loop number " << inner_loop_count <<std::endl;
165
-
166
148
167
149
}// End of the inner 'while'
168
150
}
169
151
const SparseMatrixCol* get_output (){
170
152
assert (A.rows () == A.cols ());
171
153
A = SparseMatrixCol (A.triangularView <Eigen::Lower>());
172
- // std::cout << "x is "<< std::endl;
173
- // std::cout << x << std::endl;
154
+
174
155
#pragma omp parallel for num_threads(num_threads) schedule(dynamic)
175
156
for (int k=0 ; k<A.outerSize (); ++k){
176
157
for (SparseMatrixCol::InnerIterator it (A,k); it; ++it){
177
158
it.valueRef () = it.value ()*x.coeff (it.row (),0 )*x.coeff (it.col (),0 );
178
- // std::cout << it.value() << " ";
179
159
}
180
160
}
181
- // std::cout << " "<<std::endl;
182
161
return &A;
183
162
}
184
163
private:
0 commit comments