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