[976] | 1 | #include "robustlib.h" |
---|
| 2 | |
---|
[1254] | 3 | void polyhedron::triangulate(bool should_integrate) |
---|
| 4 | { |
---|
| 5 | for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++) |
---|
| 6 | { |
---|
| 7 | set<double> simplex_integrals; |
---|
| 8 | |
---|
| 9 | for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++) |
---|
| 10 | { |
---|
| 11 | set<vertex*> new_simplex; |
---|
| 12 | new_simplex.insert((*t_ref).begin(),(*t_ref).end()); |
---|
| 13 | |
---|
| 14 | pair<set<vertex*>::iterator,bool> ret_val = new_simplex.insert(*vertices.begin()); |
---|
| 15 | |
---|
| 16 | if(ret_val.second == true) |
---|
| 17 | { |
---|
| 18 | triangulation.push_back(new_simplex); |
---|
| 19 | |
---|
[1269] | 20 | int condition_order = ((toprow*)this)->condition_order-1; |
---|
| 21 | if(should_integrate && condition_order-my_emlig->number_of_parameters >= 0) |
---|
[1254] | 22 | { |
---|
[1268] | 23 | emlig* current_emlig; |
---|
| 24 | |
---|
| 25 | if(this->my_emlig!=NULL) |
---|
| 26 | { |
---|
| 27 | current_emlig = this->my_emlig; |
---|
| 28 | } |
---|
| 29 | else |
---|
| 30 | { |
---|
| 31 | throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!"); |
---|
| 32 | } |
---|
| 33 | |
---|
[1254] | 34 | toprow* as_toprow = (toprow*)this; |
---|
| 35 | |
---|
| 36 | vec cur_condition = as_toprow->condition.get(1,as_toprow->condition.size()-1); |
---|
| 37 | |
---|
| 38 | vertex* base_vertex = (*new_simplex.begin()); |
---|
| 39 | |
---|
| 40 | int dimension = new_simplex.size()-1; |
---|
| 41 | |
---|
| 42 | mat jacobian(dimension,dimension); |
---|
| 43 | |
---|
| 44 | double a_0 = base_vertex->get_coordinates()*cur_condition+as_toprow->condition[0]; |
---|
[1262] | 45 | map<double,int> as; |
---|
[1254] | 46 | |
---|
| 47 | int row_count = 0; |
---|
| 48 | |
---|
| 49 | for(set<vertex*>::iterator vert_ref = (++new_simplex.begin()); vert_ref!=new_simplex.end();vert_ref++) |
---|
| 50 | { |
---|
| 51 | vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates(); |
---|
| 52 | jacobian.set_row(row_count,relative_coords); |
---|
| 53 | |
---|
| 54 | double new_a = relative_coords*cur_condition; |
---|
| 55 | |
---|
[1262] | 56 | pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1)); |
---|
| 57 | if(returned.second == false) |
---|
| 58 | { |
---|
| 59 | (*returned.first).second++; |
---|
| 60 | } |
---|
| 61 | //as.ins(as.size(),new_a); |
---|
| 62 | |
---|
[1254] | 63 | row_count++; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | double int_value = 0; |
---|
[1268] | 67 | double det_jacobian = det(jacobian); |
---|
[1262] | 68 | for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++) |
---|
[1254] | 69 | { |
---|
[1268] | 70 | double multiplier = det_jacobian; |
---|
[1262] | 71 | if(a_0!=(*as_ref).first) |
---|
[1268] | 72 | { |
---|
| 73 | int as1_order = (*as_ref).second; |
---|
[1254] | 74 | |
---|
[1268] | 75 | current_emlig->set_correction_factors(as1_order); |
---|
| 76 | |
---|
| 77 | vector<double> factors; |
---|
| 78 | int number_of_factors = 0; |
---|
[1262] | 79 | for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++) |
---|
| 80 | { |
---|
[1268] | 81 | |
---|
[1262] | 82 | if(as2_ref!=as_ref) |
---|
[1268] | 83 | { |
---|
| 84 | for(int k = 0;k<(*as2_ref).second;k++) |
---|
| 85 | { |
---|
| 86 | factors.push_back((*as_ref).first-(*as2_ref).first); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | multiplier /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second); |
---|
| 90 | number_of_factors += (*as2_ref).second; |
---|
| 91 | } |
---|
| 92 | else |
---|
[1262] | 93 | { |
---|
[1268] | 94 | factors.push_back((*as_ref).first); |
---|
[1254] | 95 | |
---|
[1268] | 96 | multiplier /= (*as_ref).first; |
---|
| 97 | number_of_factors += 1; |
---|
| 98 | } |
---|
| 99 | } |
---|
[1254] | 100 | |
---|
[1269] | 101 | double facti = fact(0); |
---|
| 102 | |
---|
| 103 | double bracket = fact(condition_order-number_of_factors)/fact(as1_order-1)/pow((*as_ref).first-a_0,condition_order-number_of_factors); |
---|
[1268] | 104 | for(int k = 0;k < as1_order-1;k++) |
---|
| 105 | { |
---|
[1269] | 106 | double bracket_factor = pow((double)-1,k+1)*fact(condition_order-1-number_of_factors-k)/fact(as1_order-2-k)/pow((*as_ref).first-a_0,condition_order-1-number_of_factors-k); |
---|
[1268] | 107 | |
---|
| 108 | 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++) |
---|
| 109 | { |
---|
| 110 | double bracket_combination = 1; |
---|
| 111 | for(int j = 0;j<(*combi_ref).size();j++) |
---|
| 112 | { |
---|
| 113 | bracket_combination /= factors[(*combi_ref)[j]]; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | bracket+=bracket_factor*bracket_combination; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | int_value += multiplier*bracket; |
---|
| 121 | |
---|
[1262] | 122 | } |
---|
| 123 | else |
---|
[1254] | 124 | { |
---|
[1262] | 125 | 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)"); |
---|
[1268] | 126 | } |
---|
| 127 | |
---|
[1254] | 128 | } |
---|
| 129 | |
---|
| 130 | simplex_integrals.insert(int_value); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | if(should_integrate) |
---|
| 136 | { |
---|
| 137 | ((toprow*)this)->probability = 0.0; |
---|
| 138 | |
---|
| 139 | for(set<double>::iterator integ_ref = simplex_integrals.begin();integ_ref!=simplex_integrals.end();integ_ref++) |
---|
| 140 | { |
---|
| 141 | ((toprow*)this)->probability += (*integ_ref); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|