| [976] | 1 | #include "robustlib.h" | 
|---|
 | 2 |  | 
|---|
| [1282] | 3 |  | 
|---|
| [1335] | 4 | double polyhedron::triangulate(bool should_integrate) | 
|---|
| [1273] | 5 |         { | 
|---|
| [1324] | 6 |                 for(set<simplex*>::iterator t_ref = triangulation.begin();t_ref!=triangulation.end();t_ref++) | 
|---|
 | 7 |                 { | 
|---|
 | 8 |                         delete (*t_ref); | 
|---|
 | 9 |                 }                | 
|---|
| [1301] | 10 |                 triangulation.clear(); | 
|---|
| [1299] | 11 |  | 
|---|
| [1271] | 12 |                 if(should_integrate) | 
|---|
 | 13 |                 { | 
|---|
 | 14 |                         ((toprow *)this)->probability = 0.0; | 
|---|
 | 15 |                 }                | 
|---|
 | 16 |                  | 
|---|
| [1301] | 17 |                 if(vertices.size()==1) | 
|---|
 | 18 |                 { | 
|---|
| [1324] | 19 |                         simplex* vert_simplex = new simplex((vertex*)this);                      | 
|---|
| [1301] | 20 |  | 
|---|
| [1324] | 21 |                         triangulation.insert(vert_simplex); | 
|---|
| [1301] | 22 |                 } | 
|---|
 | 23 |  | 
|---|
| [1254] | 24 |                 for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) | 
|---|
| [1271] | 25 |                 {                        | 
|---|
| [1324] | 26 |                         for(set<simplex*>::iterator s_ref = (*child_ref)->triangulation.begin();s_ref!=(*child_ref)->triangulation.end();s_ref++) | 
|---|
| [1254] | 27 |                         { | 
|---|
| [1324] | 28 |                                 simplex* new_simplex = new simplex((*s_ref)->vertices);                                                          | 
|---|
| [1301] | 29 |  | 
|---|
| [1324] | 30 |                                 pair<set<vertex*>::iterator,bool> ret_val = new_simplex->vertices.insert(*vertices.begin()); | 
|---|
| [1301] | 31 |  | 
|---|
 | 32 |                                 if(ret_val.second == true) | 
|---|
 | 33 |                                 { | 
|---|
| [1320] | 34 |                                         double cur_prob = 0; | 
|---|
| [1301] | 35 |  | 
|---|
| [1338] | 36 |                                         /* | 
|---|
 | 37 |                                         if(should_integrate&&new_simplex->vertices.size()!=3) | 
|---|
 | 38 |                                         { | 
|---|
 | 39 |                                                 cout << "Error: Wrong vertex count for integration!"; | 
|---|
 | 40 |                                         } | 
|---|
 | 41 |                                         */ | 
|---|
 | 42 |  | 
|---|
| [1301] | 43 |                                         if(should_integrate) | 
|---|
 | 44 |                                         { | 
|---|
| [1320] | 45 |                                                 cur_prob = ((toprow *)this)->integrate_simplex(new_simplex, 'S'); | 
|---|
 | 46 |  | 
|---|
 | 47 |                                                 ((toprow *)this)->probability += cur_prob; | 
|---|
| [1301] | 48 |                                         } | 
|---|
| [1320] | 49 |  | 
|---|
| [1324] | 50 |                                         triangulation.insert(new_simplex); | 
|---|
 | 51 |                                 } | 
|---|
 | 52 |                                 else | 
|---|
 | 53 |                                 { | 
|---|
 | 54 |                                         delete new_simplex; | 
|---|
 | 55 |                                 } | 
|---|
| [1301] | 56 |                         }        | 
|---|
| [1335] | 57 |                 }        | 
|---|
 | 58 |  | 
|---|
 | 59 |                 if(should_integrate) | 
|---|
 | 60 |                 { | 
|---|
 | 61 |                         return ((toprow *)this)->probability; | 
|---|
 | 62 |                 } | 
|---|
 | 63 |                 else | 
|---|
 | 64 |                 { | 
|---|
 | 65 |                         return 0.0; | 
|---|
 | 66 |                 } | 
|---|
| [1301] | 67 |                  | 
|---|
 | 68 |                 /* | 
|---|
 | 69 |                 for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) | 
|---|
 | 70 |                 {                        | 
|---|
 | 71 |                         for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++) | 
|---|
 | 72 |                         { | 
|---|
 | 73 |                                 set<vertex*> new_simplex; | 
|---|
| [1299] | 74 |                                 new_simplex.insert((*t_ref).begin(),(*t_ref).end());                     | 
|---|
| [1254] | 75 |  | 
|---|
| [1299] | 76 |                                 for(set<vertex*>::iterator suitable_vert_ref = vertices.begin();*suitable_vert_ref<*(*t_ref).begin();suitable_vert_ref++) | 
|---|
 | 77 |                                 { | 
|---|
| [1301] | 78 |                                         set<vertex*> suitable_simplex; | 
|---|
 | 79 |                                         suitable_simplex.insert(new_simplex.begin(),new_simplex.end()); | 
|---|
 | 80 |                                          | 
|---|
 | 81 |                                         suitable_simplex.insert(*suitable_vert_ref); | 
|---|
| [1254] | 82 |  | 
|---|
| [1301] | 83 |                                         triangulation.push_back(suitable_simplex); | 
|---|
| [1254] | 84 |  | 
|---|
| [1271] | 85 |                                         if(should_integrate) | 
|---|
| [1254] | 86 |                                         { | 
|---|
| [1301] | 87 |                                                 ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(suitable_simplex, 'S'); | 
|---|
| [1271] | 88 |                                         } | 
|---|
| [1299] | 89 |                                 }                                | 
|---|
| [1271] | 90 |                         }        | 
|---|
| [1301] | 91 |                 }*/              | 
|---|
| [1271] | 92 |         } | 
|---|
| [1268] | 93 |  | 
|---|
 | 94 |  | 
|---|
| [1324] | 95 |         double toprow::integrate_simplex(simplex* simplex, char c) | 
|---|
| [1271] | 96 |         { | 
|---|
| [1282] | 97 |                 // cout << ((toprow*)this)->condition << endl;           | 
|---|
| [1362] | 98 |                 int sigma_order = ((toprow*)this)->condition_order-simplex->vertices.size()-1; | 
|---|
| [1282] | 99 |  | 
|---|
| [1271] | 100 |                 // cout << "C:" << condition_order << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters << endl; | 
|---|
 | 101 |                 // pause(0.1); | 
|---|
| [1254] | 102 |  | 
|---|
| [1280] | 103 |                 if(sigma_order >= 0) | 
|---|
| [1271] | 104 |                 { | 
|---|
| [1275] | 105 |  | 
|---|
| [1282] | 106 |                         //cout << endl; | 
|---|
 | 107 |                         //cout << ((toprow*)this)->condition << endl; | 
|---|
 | 108 |                         //cout << "C:" << condition_order+2 << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl; | 
|---|
 | 109 |                          | 
|---|
| [1275] | 110 |  | 
|---|
| [1271] | 111 |                         emlig* current_emlig; | 
|---|
| [1331] | 112 |                         simplex->clear_gammas(); | 
|---|
| [1254] | 113 |  | 
|---|
| [1271] | 114 |                         if(this->my_emlig!=NULL) | 
|---|
 | 115 |                         { | 
|---|
 | 116 |                                 current_emlig = this->my_emlig; | 
|---|
 | 117 |                         } | 
|---|
 | 118 |                         else | 
|---|
 | 119 |                         { | 
|---|
 | 120 |                                 throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!"); | 
|---|
 | 121 |                         }                                                | 
|---|
| [1254] | 122 |  | 
|---|
| [1271] | 123 |                         toprow* as_toprow = (toprow*)this; | 
|---|
| [1254] | 124 |  | 
|---|
| [1300] | 125 |                         vec cur_condition = as_toprow->condition_sum.get(1,as_toprow->condition_sum.size()-1); | 
|---|
| [1254] | 126 |  | 
|---|
| [1275] | 127 |                         // cout << as_toprow->condition << endl;                         | 
|---|
| [1272] | 128 |  | 
|---|
| [1324] | 129 |                         int dimension = simplex->vertices.size()-1; | 
|---|
| [1254] | 130 |  | 
|---|
| [1275] | 131 |                         mat jacobian(dimension,dimension);                       | 
|---|
| [1254] | 132 |  | 
|---|
| [1275] | 133 |                         double a_0; | 
|---|
| [1271] | 134 |                         map<double,int> as; | 
|---|
| [1275] | 135 |                         vertex* base_vertex; | 
|---|
| [1324] | 136 |                         set<vertex*>::iterator base_ref = simplex->vertices.begin(); | 
|---|
| [1275] | 137 |                         bool order_correct; | 
|---|
 | 138 |                                                  | 
|---|
| [1254] | 139 |  | 
|---|
| [1275] | 140 |                         do | 
|---|
| [1271] | 141 |                         { | 
|---|
| [1275] | 142 |                                 order_correct = true; | 
|---|
 | 143 |                                 as.clear(); | 
|---|
| [1268] | 144 |  | 
|---|
| [1275] | 145 |                                 base_vertex = (*base_ref); | 
|---|
| [1282] | 146 |                          | 
|---|
| [1366] | 147 |                                 /* | 
|---|
 | 148 |                                 cout << "Base coords: " << base_vertex->get_coordinates() << endl; | 
|---|
 | 149 |                                 cout << "Condition: " << as_toprow->condition_sum << endl; | 
|---|
 | 150 |                                 pause(0.1); | 
|---|
 | 151 |                                 */ | 
|---|
| [1254] | 152 |  | 
|---|
| [1365] | 153 |                                 a_0 = -base_vertex->get_coordinates()*cur_condition+as_toprow->condition_sum[0];                 | 
|---|
| [1282] | 154 |                                  | 
|---|
| [1275] | 155 |                                 int row_count = 0; | 
|---|
| [1324] | 156 |                                 for(set<vertex*>::iterator vert_ref = simplex->vertices.begin(); vert_ref!=simplex->vertices.end();vert_ref++) | 
|---|
| [1271] | 157 |                                 { | 
|---|
| [1275] | 158 |                                         if(vert_ref != base_ref) | 
|---|
 | 159 |                                         { | 
|---|
| [1276] | 160 |                                                 vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates();                                             | 
|---|
| [1275] | 161 |  | 
|---|
 | 162 |                                                 jacobian.set_row(row_count,relative_coords); | 
|---|
 | 163 |  | 
|---|
| [1281] | 164 |                                                 double new_a = -relative_coords*cur_condition; | 
|---|
| [1275] | 165 |  | 
|---|
| [1276] | 166 |                                                 if(new_a + a_0 == 0 || new_a == 0) | 
|---|
| [1275] | 167 |                                                 { | 
|---|
 | 168 |                                                         base_ref++; | 
|---|
 | 169 |  | 
|---|
| [1357] | 170 |                                                         if(base_ref == simplex->vertices.end()&&simplex->vertices.size()!=2) | 
|---|
| [1275] | 171 |                                                         { | 
|---|
 | 172 |                                                                 throw new exception("Equal local conditions are paired. If this ever occurs, the software has to be modified to include multiplied a_0!!"); | 
|---|
 | 173 |                                                         } | 
|---|
| [1357] | 174 |                                                         /* | 
|---|
 | 175 |                                                         else if(implex->vertices.size()==2) | 
|---|
 | 176 |                                                         { | 
|---|
 | 177 |                                                                 return relative_coords[0]*exp | 
|---|
 | 178 |                                                         } | 
|---|
 | 179 |                                                         */ | 
|---|
| [1275] | 180 |                                                          | 
|---|
 | 181 |                                                         order_correct = false;                                           | 
|---|
 | 182 |  | 
|---|
 | 183 |                                                         break; | 
|---|
| [1282] | 184 |                                                 }                                                | 
|---|
 | 185 |                                                  | 
|---|
 | 186 |                                                  | 
|---|
 | 187 |  | 
|---|
| [1335] | 188 |                                                 if(a_0<current_emlig->min_ll) | 
|---|
| [1282] | 189 |                                                 { | 
|---|
 | 190 |                                                         current_emlig->minimal_vertex = base_vertex; | 
|---|
| [1335] | 191 |                                                         current_emlig->min_ll = a_0; | 
|---|
| [1275] | 192 |                                                 } | 
|---|
| [1282] | 193 |  | 
|---|
| [1300] | 194 |                                                 double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0]; | 
|---|
| [1335] | 195 |                                                 if(a_m<current_emlig->min_ll) | 
|---|
| [1282] | 196 |                                                 { | 
|---|
 | 197 |                                                         current_emlig->minimal_vertex = (*vert_ref); | 
|---|
| [1335] | 198 |                                                         current_emlig->min_ll = a_m;                                             | 
|---|
| [1282] | 199 |                                                 } | 
|---|
 | 200 |  | 
|---|
 | 201 |                                                 //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl; | 
|---|
 | 202 |  | 
|---|
| [1301] | 203 |                                                 // cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl; | 
|---|
| [1282] | 204 |                                                 //cout << "Relative coords:(V"  << row_count << ")" << relative_coords << endl; | 
|---|
| [1275] | 205 |  | 
|---|
 | 206 |                                                 pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1)); | 
|---|
 | 207 |                                                 if(returned.second == false) | 
|---|
 | 208 |                                                 { | 
|---|
 | 209 |                                                         (*returned.first).second++; | 
|---|
 | 210 |                                                 } | 
|---|
 | 211 |                                                 /* | 
|---|
 | 212 |                                                 else | 
|---|
 | 213 |                                                 { | 
|---|
 | 214 |                                                         cout << "a[" << row_count << "] = " << new_a << endl; | 
|---|
 | 215 |                                                 } | 
|---|
 | 216 |                                                 */ | 
|---|
 | 217 |                                                  | 
|---|
 | 218 |                                                 //as.ins(as.size(),new_a);                                                       | 
|---|
 | 219 |                                                  | 
|---|
 | 220 |                                                 row_count++; | 
|---|
 | 221 |                                         } | 
|---|
| [1271] | 222 |                                 } | 
|---|
 | 223 |                         } | 
|---|
| [1275] | 224 |                         while(!order_correct); | 
|---|
| [1272] | 225 |                          | 
|---|
| [1282] | 226 |                         /* | 
|---|
| [1281] | 227 |                         cout << "a_0: " << a_0 << "    "; | 
|---|
 | 228 |                         int as_count = 1; | 
|---|
 | 229 |                         for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) | 
|---|
 | 230 |                         { | 
|---|
 | 231 |                                 cout << "a_" << as_count << ": " << (*as_ref).first << "    "; | 
|---|
 | 232 |                                 as_count++; | 
|---|
| [1365] | 233 |                         } | 
|---|
 | 234 |                         */ | 
|---|
| [1269] | 235 |  | 
|---|
| [1335] | 236 |                         double int_value = 0;                    | 
|---|
| [1268] | 237 |  | 
|---|
| [1271] | 238 |                         // cout << jacobian << endl; | 
|---|
| [1268] | 239 |  | 
|---|
| [1272] | 240 |                         double det_jacobian    = abs(det(jacobian)); | 
|---|
 | 241 |                         double correction_term = det_jacobian;                   | 
|---|
| [1271] | 242 |                         for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) | 
|---|
 | 243 |                         { | 
|---|
| [1275] | 244 |                                 double multiplier = det_jacobian;                                | 
|---|
| [1272] | 245 |                                  | 
|---|
| [1275] | 246 |                                 int as1_order = (*as_ref).second; | 
|---|
 | 247 |                                  | 
|---|
| [1280] | 248 |                                 correction_term /= pow(-(*as_ref).first,as1_order);                                      | 
|---|
| [1275] | 249 |                                  | 
|---|
 | 250 |                                 current_emlig->set_correction_factors(as1_order); | 
|---|
 | 251 |  | 
|---|
 | 252 |                                 vector<double> factors; | 
|---|
 | 253 |                                 int number_of_factors = 0;                                                               | 
|---|
 | 254 |                                 for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++) | 
|---|
| [1272] | 255 |                                 { | 
|---|
 | 256 |                                          | 
|---|
| [1275] | 257 |                                         if(as2_ref!=as_ref) | 
|---|
 | 258 |                                         {                                                                                | 
|---|
 | 259 |                                                 for(int k = 0;k<(*as2_ref).second;k++) | 
|---|
| [1271] | 260 |                                                 { | 
|---|
| [1275] | 261 |                                                         factors.push_back((*as_ref).first-(*as2_ref).first); | 
|---|
| [1272] | 262 |                                                 } | 
|---|
| [1270] | 263 |  | 
|---|
| [1275] | 264 |                                                 multiplier        /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second); | 
|---|
 | 265 |                                                 number_of_factors += (*as2_ref).second; | 
|---|
 | 266 |                                         } | 
|---|
 | 267 |                                         else | 
|---|
| [1271] | 268 |                                         { | 
|---|
| [1275] | 269 |                                                 factors.push_back((*as_ref).first); | 
|---|
| [1270] | 270 |  | 
|---|
| [1275] | 271 |                                                 multiplier        /= (*as_ref).first; | 
|---|
 | 272 |                                                 number_of_factors += 1; | 
|---|
| [1365] | 273 |                                         }                                        | 
|---|
 | 274 |                                 }        | 
|---|
| [1254] | 275 |  | 
|---|
| [1366] | 276 |                                 double cur_as_ref = (*as_ref).first;                             | 
|---|
| [1365] | 277 |  | 
|---|
| [1366] | 278 |                                 double gamma_multiplier = -cur_as_ref-a_0; | 
|---|
 | 279 |  | 
|---|
 | 280 |                                 double bracket = fact(as1_order-1)/pow(gamma_multiplier,sigma_order); | 
|---|
| [1331] | 281 |                                                                  | 
|---|
| [1365] | 282 |                                 simplex->insert_gamma(0,bracket*multiplier,gamma_multiplier);                                                            | 
|---|
| [1280] | 283 |  | 
|---|
| [1365] | 284 |                                 // bracket *= fact(sigma_order);                                 | 
|---|
| [1325] | 285 |  | 
|---|
| [1275] | 286 |                                 for(int k = 0;k < as1_order-1;k++) | 
|---|
 | 287 |                                 { | 
|---|
| [1325] | 288 |                                         double local_bracket = 0; | 
|---|
| [1366] | 289 |                                         double bracket_factor = 1/fact(as1_order-1-k)/pow(gamma_multiplier,sigma_order-k-1);//pow((double)-1,k+1) | 
|---|
| [1272] | 290 |                                          | 
|---|
| [1280] | 291 |                                         ivec control_vec = ivec(); | 
|---|
| [1325] | 292 |                                         control_vec.ins(0,my_emlig->number_of_parameters-as1_order+1);                                   | 
|---|
| [1280] | 293 |                                          | 
|---|
 | 294 |                                         for(multiset<my_ivec>::iterator combi_ref = this->my_emlig->correction_factors[k].begin();combi_ref!=this->my_emlig->correction_factors[k].upper_bound(my_ivec(control_vec));combi_ref++) | 
|---|
| [1275] | 295 |                                         { | 
|---|
 | 296 |                                                 double bracket_combination = 1; | 
|---|
| [1280] | 297 |                                                 for(int j = 0;j<(*combi_ref).size();j++) | 
|---|
| [1275] | 298 |                                                 { | 
|---|
| [1281] | 299 |                                                         //cout << "Factor vector:" << (*combi_ref) << endl; | 
|---|
| [1280] | 300 |                                                          | 
|---|
 | 301 |                                                         bracket_combination /= factors[(*combi_ref)[j]-1]; | 
|---|
| [1325] | 302 |                                                 }                                                | 
|---|
 | 303 |                                                                                                  | 
|---|
 | 304 |                                                 local_bracket += bracket_factor*bracket_combination;                                                                     | 
|---|
 | 305 |                                         } | 
|---|
| [1254] | 306 |  | 
|---|
| [1365] | 307 |                                         simplex->insert_gamma(k+1,local_bracket*multiplier,gamma_multiplier); | 
|---|
| [1349] | 308 |  | 
|---|
 | 309 |                                         int division_factor = 1; | 
|---|
 | 310 |                                         for(int s = 0;s<k;s++) | 
|---|
 | 311 |                                         { | 
|---|
 | 312 |                                                 division_factor *= k-s; | 
|---|
 | 313 |                                         } | 
|---|
| [1325] | 314 |                                          | 
|---|
| [1349] | 315 |                                         bracket += local_bracket/division_factor; //local_bracket*fact(sigma_order-k); | 
|---|
| [1325] | 316 |                                 }                                | 
|---|
 | 317 |  | 
|---|
| [1275] | 318 |                                 int_value += multiplier*bracket; | 
|---|
| [1365] | 319 |  | 
|---|
 | 320 |                                  | 
|---|
| [1275] | 321 |                                                                                                                                                  | 
|---|
| [1272] | 322 |                         } | 
|---|
| [1271] | 323 |  | 
|---|
| [1366] | 324 |                         double correction_term_base = correction_term/pow(-a_0,sigma_order); | 
|---|
| [1271] | 325 |  | 
|---|
| [1365] | 326 |                         simplex->insert_gamma(0,correction_term_base,-a_0);                      | 
|---|
| [1272] | 327 |  | 
|---|
| [1349] | 328 |                         correction_term = correction_term_base;//fact(sigma_order)*correction_term_base; | 
|---|
| [1325] | 329 |  | 
|---|
 | 330 |                         //cout << c << int_value << endl; | 
|---|
 | 331 |  | 
|---|
| [1365] | 332 |                         int_value += correction_term;                    | 
|---|
| [1272] | 333 |  | 
|---|
| [1325] | 334 |                         //cout << "Probability:" << int_value << endl; | 
|---|
 | 335 |                         //pause(0.100); | 
|---|
| [1272] | 336 |  | 
|---|
| [1324] | 337 |                         simplex->probability = int_value; | 
|---|
| [1271] | 338 |                          | 
|---|
| [1324] | 339 |                         return int_value;        | 
|---|
| [1275] | 340 |                          | 
|---|
| [1271] | 341 |                 } | 
|---|
 | 342 |                 else | 
|---|
 | 343 |                 { | 
|---|
| [1362] | 344 |                         cout << "Improper probability density." << endl; | 
|---|
| [1271] | 345 |                         return 0.0; | 
|---|
 | 346 |                 } | 
|---|
| [1254] | 347 |         } | 
|---|