polygon.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "polygon.hpp"
  2. //---------------
  3. // Polygon::init
  4. //---------------
  5. void
  6. Polygon::init(){
  7. for(size_t i=0;i<grid_size;++i){
  8. grid[i]=empty;
  9. grid_histo[i]=0;
  10. }
  11. for(size_t i=0;i<grid_width;++i){
  12. grid[pos(i,0)]=border;
  13. grid[pos(i,grid_height-1)]=border;
  14. }
  15. for(size_t i=0;i<grid_height;++i){
  16. grid[pos(0,i)]=border;
  17. grid[pos(grid_width-1,i)]=border;
  18. }
  19. for(size_t i=0;i<xinit;++i){
  20. grid[pos(i,1)]=border;
  21. }
  22. head=pos(xinit,yinit);
  23. grid[head]=1;
  24. }
  25. //--------------------
  26. // Polygon::operator=
  27. //--------------------
  28. Polygon&
  29. Polygon::operator=(const Polygon& P){
  30. head=P.head;
  31. length=P.length;
  32. memcpy(grid,P.grid,grid_size*sizeof(size_t));
  33. memcpy(grid_histo,P.grid_histo,grid_size*sizeof(size_t));
  34. memcpy(step_histo,P.step_histo,(max_len+1)*sizeof(size_t));
  35. return *this;
  36. }
  37. //------------------
  38. // Polygon::display
  39. //------------------
  40. void
  41. Polygon::display() const{
  42. for(size_t z=0;z<grid_height;++z){
  43. size_t y=grid_height-z-1;
  44. for(size_t x=0;x<grid_width;++x){
  45. size_t v=grid[pos(x,y)];
  46. if(v==border) cout<<"\033[47m \033[0m";
  47. else if(v==empty or step_histo[v]!=grid_histo[pos(x,y)] or v>length) cout<<" ";
  48. else cout<<"\033[42m\033[30m"<<" "<<"\033[0m";
  49. }
  50. cout<<endl;
  51. }
  52. }
  53. //-------------
  54. // Polygon::fp
  55. //-------------
  56. void
  57. Polygon::fp(fmpq_poly_t res) const{
  58. map<size_t,InfoVertex> infos;
  59. size_t id=0;
  60. int64 x=xinit;
  61. int64 y=yinit;
  62. size_t c=pos(x,y);
  63. for(size_t t=1;t<=length;++t){
  64. size_t l=left(c);
  65. size_t r=right(c);
  66. size_t u=up(c);
  67. size_t d=down(c);
  68. auto it=infos.find(c);
  69. if(it==infos.end()){
  70. infos[c]=InfoVertex(id++,x,y,l,r,u,d);
  71. }
  72. else{
  73. InfoVertex& info=it->second;
  74. info.v[0]=l;
  75. info.v[1]=r;
  76. info.v[2]=u;
  77. info.v[3]=d;
  78. info.deg=4;
  79. }
  80. it=infos.find(l);
  81. if(it==infos.end()) infos[l]=InfoVertex(id++,x-1,y);
  82. it=infos.find(r);
  83. if(it==infos.end()) infos[r]=InfoVertex(id++,x+1,y);
  84. it=infos.find(u);
  85. if(it==infos.end()) infos[u]=InfoVertex(id++,x,y+1);
  86. it=infos.find(d);
  87. if(it==infos.end()) infos[d]=InfoVertex(id++,x,y-1);
  88. size_t s=step_histo[t+1];
  89. if(grid[l]==t+1 and s==grid_histo[l]){
  90. c=l;
  91. --x;
  92. }
  93. else if(grid[r]==t+1 and s==grid_histo[r]){
  94. c=r;
  95. ++x;
  96. }
  97. else if(grid[u]==t+1 and s==grid_histo[u]){
  98. c=u;
  99. ++y;
  100. }
  101. else if(grid[d]==t+1 and s==grid_histo[d]){
  102. c=d;
  103. --y;
  104. }
  105. }
  106. size_t ne=infos.size();
  107. fmpz_poly_mat_t B,C,M,B2,X,U,V,R;
  108. fmpz_poly_t cc,den,det;
  109. fmpz_t z;
  110. fmpz_poly_mat_init(B,ne,ne);
  111. fmpz_poly_mat_init(C,ne,ne);
  112. fmpz_poly_mat_init(M,ne,ne);
  113. fmpz_poly_mat_init(B2,ne,ne);
  114. fmpz_poly_mat_init(X,ne,1);
  115. fmpz_poly_mat_init(U,ne,1);
  116. fmpz_poly_mat_init(V,1,ne);
  117. fmpz_poly_mat_init(R,1,1);
  118. fmpz_poly_mat_zero(B);
  119. fmpz_poly_init(cc);
  120. fmpz_poly_init(det);
  121. fmpz_poly_init(den);
  122. fmpz_init(z);
  123. for(auto it=infos.begin();it!=infos.end();++it){
  124. InfoVertex& info=it->second;
  125. size_t i=info.id;
  126. for(size_t k=0;k<info.deg;++k){
  127. size_t j=infos[info.v[k]].id;
  128. fmpz_poly_set_si(fmpz_poly_mat_entry(B,i,j),1);
  129. fmpz_poly_set_si(fmpz_poly_mat_entry(B,j,i),1);
  130. }
  131. for(auto it2=infos.begin();it2!=infos.end();++it2){
  132. InfoVertex& info2=it2->second;
  133. size_t j=info2.id;
  134. int64 dx=abs(info.x-info2.x);
  135. int64 dy=abs(info.y-info2.y);
  136. set_poly_coeff(dx,dy,fmpz_poly_mat_entry(C,i,j));
  137. }
  138. }
  139. //! Compute B²
  140. fmpz_poly_mat_mul(B2,B,B);
  141. //! Compute M=C*B
  142. fmpz_poly_mat_mul(M,C,B);
  143. //! M=4*coeffs_denI+CB
  144. //! U[i][0]=1;
  145. fmpz_mul_si(z,coeffs_den,4);
  146. fmpz_poly_set_fmpz(cc,z);
  147. for(size_t i=0;i<ne;++i){
  148. fmpz_poly_add(fmpz_poly_mat_entry(M,i,i),fmpz_poly_mat_entry(M,i,i),cc);
  149. fmpz_poly_set_si(fmpz_poly_mat_entry(U,i,0),1);
  150. }
  151. //! Solve system M*X=U, actually M*X=den*U where den is an output
  152. fmpz_poly_mat_solve(X,den,M,U);
  153. //! Store B^2(i,i) in V(0,i)
  154. for(size_t i=0;i<ne;++i){
  155. fmpz_poly_set(fmpz_poly_mat_entry(V,0,i),fmpz_poly_mat_entry(B2,i,i));
  156. }
  157. //! Compute determinant of M
  158. fmpz_poly_mat_det(det,M);
  159. //! Determine the sign of den/det
  160. int s=fmpz_sgn(fmpz_poly_lead(den))*fmpz_sgn(fmpz_poly_lead(det));
  161. //! Compute R=V*X, R is matrix 1x1
  162. fmpz_poly_mat_mul(R,V,X);
  163. //! Retrieve the unique coefficient of R
  164. fmpq_poly_set_fmpz_poly(res,fmpz_poly_mat_entry(R,0,0));
  165. //! Compute the multiplicative scalar factor
  166. fmpz_mul_si(z,coeffs_den,4);
  167. fmpz_pow_ui(z,z,ne-1);
  168. fmpz_mul_si(z,z,4*s);
  169. //! Multiply res by z and we are done :)
  170. fmpq_poly_scalar_div_fmpz(res,res,z);
  171. //! Clear all flint object
  172. fmpz_poly_mat_clear(B);
  173. fmpz_poly_mat_clear(C);
  174. fmpz_poly_mat_clear(M);
  175. fmpz_poly_mat_clear(B2);
  176. fmpz_poly_mat_clear(X);
  177. fmpz_poly_mat_clear(U);
  178. fmpz_poly_mat_clear(V);
  179. fmpz_poly_mat_clear(R);
  180. fmpz_poly_clear(cc);
  181. fmpz_poly_clear(den);
  182. fmpz_poly_clear(det);
  183. fmpz_clear(z);
  184. }
  185. //----------------------
  186. // Polygon::next_vertex
  187. //----------------------
  188. size_t
  189. Polygon::next_vertex(size_t t,size_t c) const{
  190. size_t l=left(c);
  191. size_t r=right(c);
  192. size_t u=up(c);
  193. size_t d=down(c);
  194. size_t s=step_histo[t+1];
  195. if(grid[l]==t+1 and s==grid_histo[l]) return l;
  196. if(grid[r]==t+1 and s==grid_histo[r]) return r;
  197. if(grid[u]==t+1 and s==grid_histo[u]) return u;
  198. if(grid[d]==t+1 and s==grid_histo[d]) return d;
  199. assert(false);
  200. return 0;
  201. }