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