monoid.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**
  2. * This file is part of Gomu.
  3. *
  4. * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
  5. *
  6. * Gomu is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Gomu is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "monoid.hpp"
  20. //*************
  21. //* Reversing *
  22. //*************
  23. Word
  24. Reversing::get_word() const{
  25. size_t s=word.size;
  26. Word res(s);
  27. if(s==0) return res;
  28. NInd ind=word.nodes[0].next;
  29. res[0]=word.nodes[ind].data;
  30. size_t i=1;
  31. while((ind=word.nodes[ind].next)!=0){
  32. res[i++]=word.nodes[ind].data;
  33. }
  34. return res;
  35. }
  36. //*****************
  37. //* LeftReversing *
  38. //*****************
  39. //-----------------------------------
  40. // LeftReversing::check_positivity()
  41. //-----------------------------------
  42. bool
  43. LeftReversing::check_positivity(){
  44. while(not to_reverse.empty()){
  45. reverse();
  46. if(word.size>0 and word.first()<0) return false;
  47. }
  48. return true;
  49. }
  50. //------------------------------
  51. // LeftReversing::denominator()
  52. //------------------------------
  53. Word
  54. LeftReversing::denominator(){
  55. size_t ind=word.nodes[0].next;
  56. if(ind==0 or word.nodes[ind].data>0) return Word();
  57. size_t s=1;
  58. while((ind=word.nodes[ind].next)!=0 and word.nodes[ind].data<0){
  59. ++s;
  60. }
  61. Word res(s);
  62. s=0;
  63. while((ind=word.nodes[ind].previous)!=0){
  64. res[s++]=-word.nodes[ind].data;
  65. }
  66. return res;
  67. }
  68. //----------------------------
  69. // LeftReversing::numerator()
  70. //----------------------------
  71. Word
  72. LeftReversing::numerator(){
  73. size_t ind=word.nodes[0].previous;
  74. if(ind==0 or word.nodes[ind].data<0) return Word();
  75. size_t s=1;
  76. while((ind=word.nodes[ind].previous)!=0 and word.nodes[ind].data>0){
  77. ++s;
  78. }
  79. Word res(s);
  80. s=0;
  81. while((ind=word.nodes[ind].next)!=0){
  82. res[s++]=word.nodes[ind].data;
  83. }
  84. return res;
  85. }
  86. //--------------------------
  87. // LeftReversing::reverse()
  88. //--------------------------
  89. void
  90. LeftReversing::reverse(){
  91. //replace ___x.Y___ by ___U.v___
  92. NInd i=to_reverse.back();
  93. to_reverse.pop_back();
  94. Generator x=word.nodes[i].data;
  95. NInd j=word.nodes[i].next;
  96. NInd p=word.nodes[i].previous;
  97. Generator y=-word.nodes[j].data;
  98. NInd n=word.nodes[j].next;
  99. //the word is __$.x.Y.#__ with $=[p],X=[i],y=[j],#=[n]
  100. size_t s=set_comp(x,y,comp);
  101. //TODO :: Unroll loop
  102. for(size_t ind=0;ind<s;++ind){
  103. word.insert_after(i,-comp[ind]);
  104. }
  105. word.erase(i);
  106. s=set_comp(y,x,comp);
  107. for(size_t ind=0;ind<s;++ind) word.insert_before(j,comp[ind]);
  108. word.erase(j);
  109. //the word is now __$.U.v.#___ with $=[p] and #=[n]
  110. if(s>0){
  111. if(word.nodes[p].data>0){
  112. if(word.nodes[word.nodes[p].next].data<0){
  113. to_reverse.push_back(p);
  114. }
  115. }
  116. if(word.nodes[n].data<0){
  117. if(word.nodes[word.nodes[n].previous].data>0){
  118. to_reverse.push_back(word.nodes[n].previous);
  119. }
  120. }
  121. }
  122. else{
  123. if(word.nodes[p].data>0 and word.nodes[n].data<0){
  124. to_reverse.push_back(p);
  125. }
  126. }
  127. }
  128. //----------------------------------------
  129. // LeftReversing::set_word(const Word& w)
  130. //----------------------------------------
  131. void
  132. LeftReversing::set_word(const Word& w){
  133. clear();
  134. Reversing::set_word(w);
  135. if(w.is_empty()) return;
  136. for(size_t i=0;i<w.size()-1;++i){
  137. if(w.read(i)>0 and w.read(i+1)<0){
  138. to_reverse.push_back(i+1);
  139. }
  140. }
  141. }
  142. //----------------------------------------------------------
  143. // LeftReversing::set_word(const Word& num,const Word& den)
  144. //----------------------------------------------------------
  145. void
  146. LeftReversing::set_word(const Word& num,const Word& den){
  147. clear();
  148. size_t ns=num.size();
  149. size_t ds=den.size();
  150. Reversing::init_word(ns+ds);
  151. for(int i=0;i<ns;++i){
  152. word.nodes[i+1].data=num[i];
  153. }
  154. for(int i=0;i<ds;++i){
  155. word.nodes[i+ns+1].data=-den[ds-i-1];
  156. }
  157. if(ns*ds!=0) to_reverse.push_back(ns);
  158. }
  159. //******************
  160. //* RightReversing *
  161. //******************
  162. //------------------------------------
  163. // RightReversing::check_positivity()
  164. //------------------------------------
  165. bool
  166. RightReversing::check_positivity(){
  167. while(not to_reverse.empty()){
  168. reverse();
  169. if(word.size>0 and word.last()<0) return false;
  170. }
  171. return true;
  172. }
  173. //-------------------------------
  174. // RightReversing::denominator()
  175. //-------------------------------
  176. Word
  177. RightReversing::denominator(){
  178. size_t ind=word.nodes[0].previous;
  179. if(ind==0 or word.nodes[ind].data>0) return Word();
  180. size_t s=1;
  181. while((ind=word.nodes[ind].previous)!=0 and word.nodes[ind].data<0){
  182. ++s;
  183. }
  184. Word res(s);
  185. size_t i=0;
  186. while((ind=word.nodes[ind].next)!=0){
  187. res[s-i-1]=-word.nodes[ind].data;
  188. ++i;
  189. }
  190. return res;
  191. }
  192. //-----------------------------
  193. // RightReversing::numerator()
  194. //-----------------------------
  195. Word
  196. RightReversing::numerator(){
  197. size_t ind=word.nodes[0].next;
  198. if(ind==0 or word.nodes[ind].data<0) return Word();
  199. size_t s=1;
  200. while((ind=word.nodes[ind].next)!=0 and word.nodes[ind].data>0){
  201. ++s;
  202. }
  203. Word res(s);
  204. size_t i=0;
  205. while((ind=word.nodes[ind].previous)!=0){
  206. res[s-i-1]=word.nodes[ind].data;
  207. ++i;
  208. }
  209. return res;
  210. }
  211. //---------------------------
  212. // RightReversing::reverse()
  213. //---------------------------
  214. void RightReversing::reverse(){
  215. //replace ___X.y___ by ___u.V___
  216. NInd i=to_reverse.back();
  217. to_reverse.pop_back();
  218. Generator x=-word.nodes[i].data;
  219. NInd j=word.nodes[i].next;
  220. NInd p=word.nodes[i].previous;
  221. Generator y=word.nodes[j].data;
  222. NInd n=word.nodes[j].next;
  223. //the word is __$.X.y.#__ with $=[p],X=[i],y=[j],#=[n]
  224. size_t s=set_comp(x,y,comp);
  225. //TODO :: Unroll loop
  226. for(size_t ind=0;ind<s;++ind) word.insert_before(i,comp[ind]);
  227. word.erase(i);
  228. s=set_comp(y,x,comp);
  229. for(size_t ind=0;ind<s;++ind) word.insert_after(j,-comp[ind]);
  230. word.erase(j);
  231. //the word is now __$.u.V.#___ with $=[p] and #=[n]
  232. if(s>0){
  233. if(word.nodes[p].data<0){
  234. if(word.nodes[word.nodes[p].next].data>0){
  235. to_reverse.push_back(p);
  236. }
  237. }
  238. if(word.nodes[n].data>0){
  239. if(word.nodes[word.nodes[n].previous].data<0){
  240. to_reverse.push_back(word.nodes[n].previous);
  241. }
  242. }
  243. }
  244. else{
  245. if(word.nodes[p].data<0 and word.nodes[n].data>0){
  246. to_reverse.push_back(p);
  247. }
  248. }
  249. }
  250. //-----------------------------------------
  251. // RightReversing::set_word(const Word& w)
  252. //-----------------------------------------
  253. void
  254. RightReversing::set_word(const Word& w){
  255. clear();
  256. Reversing::set_word(w);
  257. if(w.is_empty()) return;
  258. for(size_t i=0;i<w.size()-1;++i){
  259. if(w.read(i)<0 and w.read(i+1)>0){
  260. to_reverse.push_back(i+1);
  261. }
  262. }
  263. }
  264. //----------------------------------------------------------
  265. // RightReversing::set_word(const Word& num,const Word& den)
  266. //----------------------------------------------------------
  267. void
  268. RightReversing::set_word(const Word& den,const Word& num){
  269. clear();
  270. size_t ds=den.size();
  271. size_t ns=num.size();
  272. Reversing::init_word(ds+ns);
  273. for(int i=0;i<ds;++i){
  274. word.nodes[i+1].data=-den[ds-i-1];
  275. }
  276. for(int i=0;i<ns;++i){
  277. word.nodes[i+ds+1].data=num[i];
  278. }
  279. if(ns*ds!=0) to_reverse.push_back(ds);
  280. }
  281. //****************
  282. //* MonoidFamily *
  283. //****************
  284. MonoidFamily::~MonoidFamily(){
  285. }
  286. //***************
  287. //* MonoidTrait *
  288. //***************
  289. //-----------------------------
  290. // MonoidTrait::~MonoidTrait()
  291. //-----------------------------
  292. MonoidTrait::~MonoidTrait(){
  293. if(left_reversing!=nullptr) delete left_reversing;
  294. if(right_reversing!=nullptr) delete right_reversing;
  295. }
  296. //-----------------------------------------------------------
  297. // MonoidTrait::is_left_divisible_x(const Word&,const Word&)
  298. //-----------------------------------------------------------
  299. pair<bool,Word>
  300. MonoidTrait::is_left_divisible_x(const Word& a,const Word& b){
  301. right_reversing->set_word(b,a);
  302. if(right_reversing->check_positivity())
  303. return pair<bool,Word>(true,right_numerator());
  304. return pair<bool,Word>(false,Word());
  305. }
  306. //------------------------------------------------------------
  307. // MonoidTrait::is_right_divisible_x(const Word&,const Word&)
  308. //------------------------------------------------------------
  309. pair<bool,Word>
  310. MonoidTrait::is_right_divisible_x(const Word& a,const Word& b){
  311. left_reversing->set_word(a,b);
  312. if(left_reversing->check_positivity())
  313. return pair<bool,Word>(true,left_numerator());
  314. return pair<bool,Word>(false,Word());
  315. }
  316. //---------------------------------------------------
  317. // MonoidTrait::left_complement(Generator,Generator)
  318. //---------------------------------------------------
  319. Word
  320. MonoidTrait::left_complement(const Generator& x,const Generator& y){
  321. Generator comp[MAX_COMPLEMENT_SIZE];
  322. size_t l=left_reversing->set_comp(x,y,comp);
  323. Word res(l);
  324. for(size_t i=0;i<l;++i) res[i]=comp[i];
  325. return res;
  326. }
  327. //------------------------------------------------
  328. // MonoidTrait::left_gcd(const Word&,const Word&)
  329. //------------------------------------------------
  330. Word
  331. MonoidTrait::left_gcd(const Word& a,const Word& b){
  332. right_reverse(a,b);
  333. left_reverse(right_reversing->get_word());
  334. left_reverse(a,left_denominator());
  335. return left_numerator();
  336. }
  337. //--------------------------------------------------
  338. // MonoidTrait::left_gcd_x(const Word&,const Word&)
  339. //--------------------------------------------------
  340. pair<Word,Word>
  341. MonoidTrait::left_gcd_x(const Word& a,const Word& b){
  342. right_reverse(a,b);
  343. left_reverse(right_reversing->get_word());
  344. Word div=left_denominator();
  345. left_reverse(a,div);
  346. return pair<Word,Word>(left_numerator(),div);
  347. }
  348. //----------------------------------------------------
  349. // MonoidTrait::right_complement(Generator,Generator)
  350. //----------------------------------------------------
  351. Word
  352. MonoidTrait::right_complement(const Generator& x,const Generator& y){
  353. Generator comp[MAX_COMPLEMENT_SIZE];
  354. size_t l=right_reversing->set_comp(x,y,comp);
  355. Word res(l);
  356. for(size_t i=0;i<l;++i) res[i]=comp[i];
  357. return res;
  358. }
  359. //------------------------------------------------
  360. // MonoidTrait::right_gcd(const Word&,const Word&)
  361. //------------------------------------------------
  362. Word
  363. MonoidTrait::right_gcd(const Word& a,const Word& b){
  364. left_reverse(b,a);
  365. right_reverse(left_reversing->get_word());
  366. right_reverse(right_denominator(),a);
  367. return right_numerator();
  368. }
  369. //---------------------------------------------------
  370. // MonoidTrait::right_gcd_x(const Word&,const Word&)
  371. //---------------------------------------------------
  372. pair<Word,Word>
  373. MonoidTrait::right_gcd_x(const Word& a,const Word& b){
  374. left_reverse(b,a);
  375. right_reverse(left_reversing->get_word());
  376. Word div=right_denominator();
  377. right_reverse(div,a);
  378. return pair<Word,Word>(right_numerator(),div);
  379. }
  380. //********
  381. //* Word *
  382. //********
  383. //------------------------------------------------
  384. // Word::Word(const initializer_list<Generator>&)
  385. //------------------------------------------------
  386. Word::Word(const initializer_list<Generator>& l):Array(l.size()){
  387. size_t i=0;
  388. for(auto it=l.begin();it!=l.end();++it){
  389. array[i++]=*it;
  390. }
  391. }
  392. //-----------------
  393. // Word::inverse()
  394. //-----------------
  395. Word
  396. Word::inverse() const{
  397. Word res(s);
  398. for(size_t i=0;i<s;++i){
  399. res.array[i]=-array[s-i-1];
  400. }
  401. return res;
  402. }
  403. //-----------------------------------
  404. // Word::display(DisplayGenerator d)
  405. //-----------------------------------
  406. string
  407. Word::display(DisplayGenerator d) const{
  408. if(s==0) return "e";
  409. string str=d(array[0]);
  410. for(size_t i=1;i<s;++i){
  411. str+='.';
  412. str+=d(array[i]);
  413. }
  414. return str;
  415. }
  416. //***********************
  417. //* Auxiliary functions *
  418. //***********************