[976] | 1 | #include "robustlib.h" |
---|
| 2 | |
---|
[1254] | 3 | void polyhedron::triangulate(bool should_integrate) |
---|
[1273] | 4 | { |
---|
[1271] | 5 | if(should_integrate) |
---|
| 6 | { |
---|
| 7 | ((toprow *)this)->probability = 0.0; |
---|
| 8 | } |
---|
| 9 | |
---|
[1254] | 10 | for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) |
---|
[1271] | 11 | { |
---|
[1254] | 12 | for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++) |
---|
| 13 | { |
---|
| 14 | set<vertex*> new_simplex; |
---|
| 15 | new_simplex.insert((*t_ref).begin(),(*t_ref).end()); |
---|
| 16 | |
---|
| 17 | pair<set<vertex*>::iterator,bool> ret_val = new_simplex.insert(*vertices.begin()); |
---|
| 18 | |
---|
| 19 | if(ret_val.second == true) |
---|
| 20 | { |
---|
| 21 | triangulation.push_back(new_simplex); |
---|
| 22 | |
---|
[1271] | 23 | if(should_integrate) |
---|
[1254] | 24 | { |
---|
[1271] | 25 | ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(new_simplex, 'S'); |
---|
| 26 | } |
---|
| 27 | } |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | } |
---|
[1268] | 31 | |
---|
| 32 | |
---|
[1271] | 33 | double toprow::integrate_simplex(set<vertex*> simplex, char c) |
---|
| 34 | { |
---|
[1275] | 35 | // cout << ((toprow*)this)->condition << endl; |
---|
| 36 | |
---|
[1280] | 37 | int condition_order = ((toprow*)this)->condition_order-2; // -2 by bylo, pokud chceme uniformni apriorno |
---|
[1254] | 38 | |
---|
[1280] | 39 | int sigma_order = condition_order-my_emlig->number_of_parameters; |
---|
| 40 | |
---|
[1271] | 41 | // cout << "C:" << condition_order << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters << endl; |
---|
| 42 | // pause(0.1); |
---|
[1254] | 43 | |
---|
[1280] | 44 | if(sigma_order >= 0) |
---|
[1271] | 45 | { |
---|
[1275] | 46 | |
---|
| 47 | cout << endl; |
---|
| 48 | cout << ((toprow*)this)->condition << endl; |
---|
[1280] | 49 | cout << "C:" << condition_order+2 << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl; |
---|
[1275] | 50 | |
---|
[1271] | 51 | emlig* current_emlig; |
---|
[1254] | 52 | |
---|
[1271] | 53 | if(this->my_emlig!=NULL) |
---|
| 54 | { |
---|
| 55 | current_emlig = this->my_emlig; |
---|
| 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!"); |
---|
| 60 | } |
---|
[1254] | 61 | |
---|
[1271] | 62 | toprow* as_toprow = (toprow*)this; |
---|
[1254] | 63 | |
---|
[1271] | 64 | vec cur_condition = as_toprow->condition.get(1,as_toprow->condition.size()-1); |
---|
[1254] | 65 | |
---|
[1275] | 66 | // cout << as_toprow->condition << endl; |
---|
[1272] | 67 | |
---|
[1271] | 68 | int dimension = simplex.size()-1; |
---|
[1254] | 69 | |
---|
[1275] | 70 | mat jacobian(dimension,dimension); |
---|
[1254] | 71 | |
---|
[1275] | 72 | double a_0; |
---|
[1271] | 73 | map<double,int> as; |
---|
[1275] | 74 | vertex* base_vertex; |
---|
| 75 | set<vertex*>::iterator base_ref = simplex.begin(); |
---|
| 76 | bool order_correct; |
---|
| 77 | |
---|
[1254] | 78 | |
---|
[1275] | 79 | do |
---|
[1271] | 80 | { |
---|
[1275] | 81 | order_correct = true; |
---|
| 82 | as.clear(); |
---|
[1268] | 83 | |
---|
[1275] | 84 | base_vertex = (*base_ref); |
---|
[1268] | 85 | |
---|
[1275] | 86 | cout << "Base coords:" << base_vertex->get_coordinates() << endl; |
---|
[1254] | 87 | |
---|
[1275] | 88 | a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition[0]; |
---|
[1281] | 89 | |
---|
[1275] | 90 | int row_count = 0; |
---|
| 91 | for(set<vertex*>::iterator vert_ref = simplex.begin(); vert_ref!=simplex.end();vert_ref++) |
---|
[1271] | 92 | { |
---|
[1275] | 93 | if(vert_ref != base_ref) |
---|
| 94 | { |
---|
[1276] | 95 | vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates(); |
---|
[1275] | 96 | |
---|
| 97 | jacobian.set_row(row_count,relative_coords); |
---|
| 98 | |
---|
[1281] | 99 | double new_a = -relative_coords*cur_condition; |
---|
[1275] | 100 | |
---|
[1276] | 101 | if(new_a + a_0 == 0 || new_a == 0) |
---|
[1275] | 102 | { |
---|
| 103 | base_ref++; |
---|
| 104 | |
---|
| 105 | if(base_ref == simplex.end()) |
---|
| 106 | { |
---|
| 107 | throw new exception("Equal local conditions are paired. If this ever occurs, the software has to be modified to include multiplied a_0!!"); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | order_correct = false; |
---|
| 111 | |
---|
| 112 | break; |
---|
| 113 | } |
---|
| 114 | |
---|
[1281] | 115 | //cout << "Absolute coords:(V" << row_count << ")" << (*vert_ref)->get_coordinates() << endl; |
---|
| 116 | cout << "Relative coords:(V" << row_count << ")" << relative_coords << endl; |
---|
[1275] | 117 | |
---|
| 118 | pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1)); |
---|
| 119 | if(returned.second == false) |
---|
| 120 | { |
---|
| 121 | (*returned.first).second++; |
---|
| 122 | } |
---|
| 123 | /* |
---|
| 124 | else |
---|
| 125 | { |
---|
| 126 | cout << "a[" << row_count << "] = " << new_a << endl; |
---|
| 127 | } |
---|
| 128 | */ |
---|
| 129 | |
---|
| 130 | //as.ins(as.size(),new_a); |
---|
| 131 | |
---|
| 132 | row_count++; |
---|
| 133 | } |
---|
[1271] | 134 | } |
---|
| 135 | } |
---|
[1275] | 136 | while(!order_correct); |
---|
[1272] | 137 | |
---|
[1281] | 138 | cout << "a_0: " << a_0 << " "; |
---|
| 139 | int as_count = 1; |
---|
| 140 | for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) |
---|
| 141 | { |
---|
| 142 | cout << "a_" << as_count << ": " << (*as_ref).first << " "; |
---|
| 143 | as_count++; |
---|
| 144 | } |
---|
[1269] | 145 | |
---|
[1271] | 146 | double int_value = 0; |
---|
[1268] | 147 | |
---|
[1271] | 148 | // cout << jacobian << endl; |
---|
[1268] | 149 | |
---|
[1272] | 150 | double det_jacobian = abs(det(jacobian)); |
---|
| 151 | double correction_term = det_jacobian; |
---|
[1271] | 152 | for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) |
---|
| 153 | { |
---|
[1275] | 154 | double multiplier = det_jacobian; |
---|
[1272] | 155 | |
---|
[1275] | 156 | int as1_order = (*as_ref).second; |
---|
| 157 | |
---|
[1280] | 158 | correction_term /= pow(-(*as_ref).first,as1_order); |
---|
[1275] | 159 | |
---|
| 160 | current_emlig->set_correction_factors(as1_order); |
---|
| 161 | |
---|
| 162 | vector<double> factors; |
---|
| 163 | int number_of_factors = 0; |
---|
| 164 | for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++) |
---|
[1272] | 165 | { |
---|
| 166 | |
---|
[1275] | 167 | if(as2_ref!=as_ref) |
---|
| 168 | { |
---|
| 169 | for(int k = 0;k<(*as2_ref).second;k++) |
---|
[1271] | 170 | { |
---|
[1275] | 171 | factors.push_back((*as_ref).first-(*as2_ref).first); |
---|
[1272] | 172 | } |
---|
[1270] | 173 | |
---|
[1275] | 174 | multiplier /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second); |
---|
| 175 | number_of_factors += (*as2_ref).second; |
---|
| 176 | } |
---|
| 177 | else |
---|
[1271] | 178 | { |
---|
[1275] | 179 | factors.push_back((*as_ref).first); |
---|
[1270] | 180 | |
---|
[1275] | 181 | multiplier /= (*as_ref).first; |
---|
| 182 | number_of_factors += 1; |
---|
[1272] | 183 | } |
---|
[1275] | 184 | |
---|
[1280] | 185 | } |
---|
[1254] | 186 | |
---|
[1280] | 187 | |
---|
| 188 | |
---|
[1281] | 189 | double bracket = fact(sigma_order)/fact(as1_order-1)/pow(a_0-(*as_ref).first,sigma_order+1); |
---|
[1275] | 190 | for(int k = 0;k < as1_order-1;k++) |
---|
| 191 | { |
---|
[1281] | 192 | double bracket_factor = -pow((double)-1,k+1)*fact(sigma_order-k)/fact(as1_order-1-k)/pow(a_0-(*as_ref).first,sigma_order-k); |
---|
[1272] | 193 | |
---|
[1280] | 194 | ivec control_vec = ivec(); |
---|
| 195 | control_vec.ins(0,my_emlig->number_of_parameters-as1_order+1); |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | 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] | 199 | { |
---|
| 200 | double bracket_combination = 1; |
---|
[1280] | 201 | for(int j = 0;j<(*combi_ref).size();j++) |
---|
[1275] | 202 | { |
---|
[1281] | 203 | //cout << "Factor vector:" << (*combi_ref) << endl; |
---|
[1280] | 204 | |
---|
| 205 | bracket_combination /= factors[(*combi_ref)[j]-1]; |
---|
[1275] | 206 | } |
---|
[1254] | 207 | |
---|
[1275] | 208 | bracket+=bracket_factor*bracket_combination; |
---|
| 209 | } |
---|
[1271] | 210 | } |
---|
[1275] | 211 | |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | int_value += multiplier*bracket; |
---|
| 215 | |
---|
[1272] | 216 | } |
---|
[1271] | 217 | |
---|
[1272] | 218 | |
---|
[1281] | 219 | correction_term *= fact(sigma_order)/abs(pow(a_0,sigma_order+1)); |
---|
[1271] | 220 | |
---|
[1272] | 221 | // cout << c << int_value << endl; |
---|
| 222 | |
---|
| 223 | int_value += correction_term; |
---|
[1275] | 224 | |
---|
[1272] | 225 | |
---|
[1275] | 226 | cout << "Probability:" << int_value << endl; |
---|
| 227 | |
---|
| 228 | return int_value; |
---|
[1272] | 229 | |
---|
[1271] | 230 | |
---|
[1275] | 231 | |
---|
[1271] | 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
| 235 | return 0.0; |
---|
| 236 | } |
---|
[1254] | 237 | } |
---|