[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 | { |
---|
| 35 | int condition_order = ((toprow*)this)->condition_order-1; |
---|
[1254] | 36 | |
---|
[1271] | 37 | // cout << "C:" << condition_order << " N:" << my_emlig->number_of_parameters << " C+N:" << condition_order-my_emlig->number_of_parameters << endl; |
---|
| 38 | // pause(0.1); |
---|
[1254] | 39 | |
---|
[1271] | 40 | if(condition_order-my_emlig->number_of_parameters >= 0) |
---|
| 41 | { |
---|
| 42 | emlig* current_emlig; |
---|
[1254] | 43 | |
---|
[1271] | 44 | if(this->my_emlig!=NULL) |
---|
| 45 | { |
---|
| 46 | current_emlig = this->my_emlig; |
---|
| 47 | } |
---|
| 48 | else |
---|
| 49 | { |
---|
| 50 | throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!"); |
---|
| 51 | } |
---|
[1254] | 52 | |
---|
[1271] | 53 | toprow* as_toprow = (toprow*)this; |
---|
[1254] | 54 | |
---|
[1271] | 55 | vec cur_condition = as_toprow->condition.get(1,as_toprow->condition.size()-1); |
---|
[1254] | 56 | |
---|
[1272] | 57 | // cout << as_toprow->condition << endl; |
---|
| 58 | |
---|
[1271] | 59 | vertex* base_vertex = (*simplex.begin()); |
---|
[1254] | 60 | |
---|
[1271] | 61 | int dimension = simplex.size()-1; |
---|
[1254] | 62 | |
---|
[1271] | 63 | mat jacobian(dimension,dimension); |
---|
[1254] | 64 | |
---|
[1272] | 65 | // cout << base_vertex->get_coordinates() << endl; |
---|
| 66 | |
---|
| 67 | double a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition[0]; |
---|
[1271] | 68 | map<double,int> as; |
---|
[1254] | 69 | |
---|
[1271] | 70 | int row_count = 0; |
---|
| 71 | for(set<vertex*>::iterator vert_ref = (++simplex.begin()); vert_ref!=simplex.end();vert_ref++) |
---|
| 72 | { |
---|
| 73 | vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates(); |
---|
[1268] | 74 | |
---|
[1272] | 75 | // cout << (*vert_ref)->get_coordinates() << endl; |
---|
[1271] | 76 | // cout << relative_coords << endl; |
---|
[1268] | 77 | |
---|
[1271] | 78 | jacobian.set_row(row_count,relative_coords); |
---|
[1254] | 79 | |
---|
[1271] | 80 | double new_a = relative_coords*cur_condition; |
---|
| 81 | |
---|
| 82 | pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1)); |
---|
| 83 | if(returned.second == false) |
---|
| 84 | { |
---|
| 85 | (*returned.first).second++; |
---|
| 86 | } |
---|
[1272] | 87 | /* |
---|
[1271] | 88 | else |
---|
| 89 | { |
---|
| 90 | cout << "a[" << row_count << "] = " << new_a << endl; |
---|
| 91 | } |
---|
[1272] | 92 | */ |
---|
| 93 | |
---|
[1271] | 94 | //as.ins(as.size(),new_a); |
---|
| 95 | |
---|
| 96 | row_count++; |
---|
| 97 | } |
---|
[1254] | 98 | |
---|
[1272] | 99 | |
---|
[1269] | 100 | |
---|
[1271] | 101 | double int_value = 0; |
---|
[1268] | 102 | |
---|
[1271] | 103 | // cout << jacobian << endl; |
---|
[1268] | 104 | |
---|
[1272] | 105 | double det_jacobian = abs(det(jacobian)); |
---|
| 106 | double correction_term = det_jacobian; |
---|
[1271] | 107 | for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) |
---|
| 108 | { |
---|
| 109 | double multiplier = det_jacobian; |
---|
[1272] | 110 | |
---|
[1271] | 111 | if(a_0!=(*as_ref).first) |
---|
[1272] | 112 | { |
---|
[1271] | 113 | int as1_order = (*as_ref).second; |
---|
[1272] | 114 | |
---|
| 115 | correction_term /= -pow((*as_ref).first,as1_order); |
---|
| 116 | |
---|
[1271] | 117 | current_emlig->set_correction_factors(as1_order); |
---|
| 118 | |
---|
| 119 | vector<double> factors; |
---|
| 120 | int number_of_factors = 0; |
---|
| 121 | for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++) |
---|
| 122 | { |
---|
| 123 | |
---|
| 124 | if(as2_ref!=as_ref) |
---|
| 125 | { |
---|
| 126 | for(int k = 0;k<(*as2_ref).second;k++) |
---|
| 127 | { |
---|
| 128 | factors.push_back((*as_ref).first-(*as2_ref).first); |
---|
[1262] | 129 | } |
---|
[1271] | 130 | |
---|
| 131 | multiplier /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second); |
---|
| 132 | number_of_factors += (*as2_ref).second; |
---|
[1254] | 133 | } |
---|
[1271] | 134 | else |
---|
| 135 | { |
---|
| 136 | factors.push_back((*as_ref).first); |
---|
[1254] | 137 | |
---|
[1271] | 138 | multiplier /= (*as_ref).first; |
---|
| 139 | number_of_factors += 1; |
---|
[1272] | 140 | } |
---|
| 141 | |
---|
[1271] | 142 | } |
---|
[1270] | 143 | |
---|
[1272] | 144 | double bracket = fact(condition_order-number_of_factors)/fact(as1_order-1)/pow(a_0+(*as_ref).first,condition_order-number_of_factors+1); |
---|
[1271] | 145 | for(int k = 0;k < as1_order-1;k++) |
---|
| 146 | { |
---|
[1272] | 147 | double bracket_factor = pow((double)-1,k+1)*fact(condition_order-1-number_of_factors-k)/fact(as1_order-2-k)/pow(a_0+(*as_ref).first,condition_order-number_of_factors-k); |
---|
[1271] | 148 | |
---|
| 149 | 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++) |
---|
| 150 | { |
---|
| 151 | double bracket_combination = 1; |
---|
| 152 | for(int j = 0;j<(*combi_ref).size();j++) |
---|
| 153 | { |
---|
| 154 | bracket_combination /= factors[(*combi_ref)[j]]; |
---|
| 155 | } |
---|
[1270] | 156 | |
---|
[1271] | 157 | bracket+=bracket_factor*bracket_combination; |
---|
| 158 | } |
---|
[1272] | 159 | } |
---|
[1254] | 160 | |
---|
[1272] | 161 | |
---|
[1254] | 162 | |
---|
[1272] | 163 | int_value += multiplier*bracket; |
---|
[1271] | 164 | } |
---|
| 165 | else |
---|
[1254] | 166 | { |
---|
[1271] | 167 | throw new exception("Should this happen? a_i=a_0 in the formula for integrating over a simplex! I think it can happen with 0 probability(when phi*2*v_0=phi*v_i)"); |
---|
[1272] | 168 | } |
---|
| 169 | } |
---|
[1271] | 170 | |
---|
[1272] | 171 | |
---|
| 172 | correction_term *= fact(condition_order-my_emlig->number_of_parameters)/pow(a_0,condition_order-my_emlig->number_of_parameters+1); |
---|
[1271] | 173 | |
---|
[1272] | 174 | // cout << c << int_value << endl; |
---|
| 175 | |
---|
| 176 | int_value += correction_term; |
---|
| 177 | |
---|
| 178 | // cout << int_value << endl; |
---|
| 179 | |
---|
| 180 | // cout << "***************************" << endl << endl; |
---|
| 181 | |
---|
| 182 | return int_value; |
---|
[1271] | 183 | |
---|
| 184 | } |
---|
| 185 | else |
---|
| 186 | { |
---|
| 187 | return 0.0; |
---|
| 188 | } |
---|
[1254] | 189 | } |
---|