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