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