monoid.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #ifndef MONOID_HPP
  20. #define MONOID_HPP
  21. #include <cstdint>
  22. #include "../../array.hpp"
  23. #include "stacked_list.hpp"
  24. #define MAX_COMPLEMENT_SIZE 64
  25. //***************************
  26. //* Early class definitions *
  27. //***************************
  28. class Reversing;
  29. class LeftReversing;
  30. class RightReversing;
  31. class PresentedMonoid;
  32. class Word;
  33. //************
  34. //* Typedefs *
  35. //************
  36. //! Monoid generator
  37. typedef int16_t Generator;
  38. //! Complement function of a monoid
  39. typedef int(*SetComplement)(const Generator& x,const Generator& y,Generator* comp);
  40. //! Display function for monoid generator
  41. typedef string(*DisplayGenerator)(const Generator& x);
  42. //! Return the number of generators of the monoid of rank n among a monoid familly
  43. typedef size_t(*GeneratorsNumber)(size_t n);
  44. //! Return the rank of a Generator
  45. typedef size_t(*RankGenerator)(const Generator& x);
  46. //! Ranked Generator bijection
  47. typedef Generator(*RankedGeneratorBijection)(size_t r,const Generator& x,int p);
  48. //! Return a ranked word
  49. typedef Word(*RankedWordFactory)(size_t r);
  50. //*********************
  51. //* Class definitions *
  52. //*********************
  53. //-----------
  54. // Reversing
  55. //-----------
  56. //! Common data and function between left and right reversing algorithms
  57. class Reversing{
  58. public:
  59. //! Internal word
  60. StackedList word;
  61. //! Next detected position to reverse
  62. deque<NInd> to_reverse;
  63. //! Destination structure for complement
  64. Generator comp[MAX_COMPLEMENT_SIZE];
  65. //! Complement function
  66. SetComplement set_comp;
  67. //! Clear internal word
  68. void clear();
  69. //! Display internal word
  70. void disp_word() const;
  71. //! Return internal word
  72. Word get_word() const;
  73. //! Init internal word to be of size s
  74. void init_word(size_t s);
  75. //! Number of detected position to reverse. O implies the word is reversed
  76. size_t remaining_size() const;
  77. //! Set internal word
  78. void set_word(const Word& w);
  79. };
  80. //----------------
  81. // Left reversing
  82. //----------------
  83. //! A class for left reversing algorithm
  84. class LeftReversing:public Reversing{
  85. public:
  86. //! Unique constructor
  87. LeftReversing(SetComplement sc);
  88. //! Test if full reversing gives a positive word when internal word is u.v^(-1)
  89. bool check_positivity();
  90. //! Return numerator of the word
  91. Word denominator();
  92. //! Reverse untill the is no more reversing step
  93. void full_reverse();
  94. //! Return numerator of the word
  95. Word numerator();
  96. //! Perform one reversing step
  97. void reverse();
  98. //! Set internal word to be w
  99. void set_word(const Word& w);
  100. //! Set internal word to be num.den^(-1)
  101. void set_word(const Word& num,const Word& den);
  102. };
  103. //-----------------
  104. // Right reversing
  105. //-----------------
  106. //! A class for right reversing
  107. class RightReversing:public Reversing{
  108. public:
  109. //! Unique constructor
  110. RightReversing(SetComplement sc);
  111. //! Test if full reversing gives a positive word when internal word is u^(-1).v
  112. bool check_positivity();
  113. //! Return numerator of the word
  114. Word denominator();
  115. //! Reverse untill the is no more reversing ste
  116. void full_reverse();
  117. //! Return numerator of the word
  118. Word numerator();
  119. //! Perform one reversing
  120. void reverse();
  121. //! Set internal word
  122. void set_word(const Word& w);
  123. //! Set internal word to be den^(-1).num
  124. void set_word(const Word& den,const Word& num);
  125. };
  126. //-------------
  127. // MonoidTrait
  128. //-------------
  129. //! Class for procedure attached to monoid
  130. class MonoidTrait{
  131. public:
  132. //! Pointer to a LeftReversing
  133. LeftReversing* left_reversing;
  134. //! Pointer to a RightReversing
  135. RightReversing* right_reversing;
  136. //! Extra data
  137. void* data;
  138. //! Empty constructor
  139. MonoidTrait();
  140. //! Destructor
  141. ~MonoidTrait();
  142. //! Test if two words are equivalent
  143. bool are_equivalent(const Word& u,const Word& v);
  144. //! Test if the family has a left complement
  145. bool has_left_complement() const;
  146. //! Test if the family has a right complement
  147. bool has_right_complement() const;
  148. //! Test if a is left divisible by b, i.e.,if it exists c such that a=b.c */
  149. bool is_left_divisible(const Word& a,const Word& b);
  150. //! Return a Couple (f,c) such that f equals true if a is left divisible by b,
  151. //! i.e.,if it exists c such that a=b.c
  152. pair <bool,Word> is_left_divisible_x(const Word& a,const Word& b);
  153. //! Test if a is right divisible by b, i.e.,if it exists c such that a=c.b
  154. bool is_right_divisible(const Word& a,const Word& b);
  155. //! Return a Couple (f,c) such that f equals true if a is right divisible by b,
  156. //! i.e.,if it exists c such that a=c.b
  157. pair<bool,Word> is_right_divisible_x(const Word& a,const Word& b);
  158. //! Return left complement of x and y
  159. Word left_complement(const Generator& x,const Generator& y);
  160. //! Return the left denominator
  161. Word left_denominator();
  162. //! Return the left gcd of a and b, i.e., a maximal element c
  163. //! such that there exist x with a=c.x and y with b=c.y
  164. Word left_gcd(const Word& a,const Word& b);
  165. //! Return a Couple (c,d) where c is the left gcd of a and d is such that a=c.d
  166. pair<Word,Word> left_gcd_x(const Word& a,const Word& b);
  167. //! Return the left lcm of a and b, i.e., a minimal element c
  168. //! such that there exist x with c=x.a and y with c=y.a
  169. Word left_lcm(const Word& a,const Word& b);
  170. //! Return the left lcm complement of a and b, i.e.,
  171. //! an element d such that d.a is equal to the left lcm of a and b
  172. Word left_lcm_complement(const Word& a,const Word& b);
  173. //! Return the left numerator
  174. Word left_numerator();
  175. //! Left reverse the word w
  176. Word left_reverse(const Word& w);
  177. //! Left reverse the u.v^(-1)
  178. Word left_reverse(const Word& u,const Word& v);
  179. //! Return right complement of x and y
  180. Word right_complement(const Generator& x,const Generator& y);
  181. //! Return the right denominator
  182. Word right_denominator();
  183. //! Return the left gcd of a and b, i.e., a maximal element c
  184. //! such that there exist x with a=c.x and y with b=c.y
  185. Word right_gcd(const Word& a,const Word& b);
  186. //! Return a Couple (c,d) where c is the right gcd of a and d is such that a=d.c
  187. pair<Word,Word> right_gcd_x(const Word& a,const Word& b);
  188. //! Return the right lcm of a and b, i.e., a minimal element c
  189. //! such that there exist x with c=a.x and y with c=a.y
  190. Word right_lcm(const Word& a,const Word& b);
  191. //! Return the right lcm complement of a and b, i.e.,
  192. //! an element d such that a.d is equal to the right lcm of a and b
  193. Word right_lcm_complement(const Word& a,const Word& b);
  194. //! Return right numerator
  195. Word right_numerator();
  196. //! Right reverse the word w
  197. Word right_reverse(const Word& w);
  198. //! Right reverse the u^(-1).v
  199. Word right_reverse(const Word& u,const Word& v);
  200. //! Set left complement
  201. void set_left_complement(SetComplement sc);
  202. //! Set right complement
  203. void set_right_complement(SetComplement sc);
  204. };
  205. //--------------
  206. // MonoidFamily
  207. //--------------
  208. //! A class for familly of monoid
  209. class MonoidFamily:public MonoidTrait{
  210. public:
  211. //! Function to display generators
  212. DisplayGenerator gdisp;
  213. //! Function returning the number of generators for a given rank
  214. GeneratorsNumber gnum;
  215. //! Label of the monoid family
  216. string label;
  217. //! Ranked Garside automorphism germ
  218. RankedGeneratorBijection ranked_phi_germ;
  219. //! Ranked Garside element factory
  220. RankedWordFactory ranked_garside_word_factory;
  221. //! Unique constructor
  222. MonoidFamily(string l,DisplayGenerator d,GeneratorsNumber n);
  223. //! Destructor
  224. ~MonoidFamily();
  225. //! Apply phi_r^p to the word
  226. void apply_phi(size_t r,Word& w,int p=1);
  227. //! Display
  228. string display() const;
  229. //! Return garside_element of rank r
  230. Word garside_element(size_t r);
  231. //! Return number of generators for rank n
  232. size_t generators_number(size_t n);
  233. //! Test if the family has a Garside automorphism
  234. bool has_garside_automorphism() const;
  235. //! Test if the family has a Garside element
  236. bool has_garside_element() const;
  237. //! Return the word obtained under phi_r^p
  238. Word phi(size_t r,const Word& w,int p=1);
  239. //! Return ranked phi-tail of an element
  240. Word phi_tail(size_t r,const Word& w);
  241. //! Return ranked phi-tail of an element together with remainder
  242. pair<Word,Word> phi_tail_x(size_t r,const Word& w);
  243. //! Return the ranked phi-splitting of an element
  244. Array<Word> phi_splitting(size_t r,const Word& w);
  245. //! Return rank of a Word
  246. size_t rank(const Word& w);
  247. //! Set ranked phi germ
  248. void set_ranked_phi_germ(RankedGeneratorBijection rpg);
  249. //! Set ranked garside word factory
  250. void set_ranked_garside_word_factory(RankedWordFactory rgwf);
  251. };
  252. //------
  253. // Word
  254. //------
  255. //! Class for word
  256. class Word:public Array<Generator>{
  257. public:
  258. //! Empty constructor
  259. Word();
  260. //! Construct a word from a list of Generator
  261. Word(const initializer_list<Generator>& l);
  262. //! Recopy constructor
  263. Word(const Word&);
  264. //! Move constructor
  265. Word(Word&&);
  266. //! Construct a word from an array
  267. Word(const Array<Generator>&);
  268. Word(Array<Generator>&&);
  269. //! Assignement operator with copy
  270. Word& operator=(const Word& w);
  271. //! Assignement operator with move
  272. Word& operator=(Word&& w);
  273. //! Concatenate a word to this one
  274. Word concatenate(const Word& w) const;
  275. //! Return the word inverse of this one
  276. Word inverse() const;
  277. //! Display a word
  278. string display(DisplayGenerator d) const;
  279. };
  280. //***********************
  281. //* Auxiliary functions *
  282. //***********************
  283. //! Comparison function for Generator
  284. //! \param x a generator
  285. //! \param y a generator
  286. //! \return -1 if x<y, 0 if x==y and 1 if x>y
  287. int cmp(const Generator& x,const Generator& y);
  288. //! Display a generator with letter
  289. string disp_alpha(const Generator& x);
  290. //! Multiply word
  291. //! \param u a word
  292. //! \param w a word
  293. //! \return the word uv
  294. Word operator*(const Word& u,const Word& v);
  295. //***********************
  296. //* Inline declarations *
  297. //***********************
  298. //-----------
  299. // Reversing
  300. //-----------
  301. inline void
  302. Reversing::clear(){
  303. to_reverse.clear();
  304. }
  305. inline void
  306. Reversing::disp_word() const{
  307. cout<<word<<endl;
  308. }
  309. inline void
  310. Reversing::init_word(size_t s){
  311. word.init(s);
  312. }
  313. inline size_t
  314. Reversing::remaining_size() const{
  315. return to_reverse.size();
  316. }
  317. inline void
  318. Reversing::set_word(const Word& w){
  319. word.init((NData*)w.array,w.size());
  320. }
  321. //----------------
  322. // Left reversing
  323. //----------------
  324. inline
  325. LeftReversing::LeftReversing(SetComplement sc){
  326. set_comp=sc;
  327. }
  328. inline void
  329. LeftReversing::full_reverse(){
  330. while(not to_reverse.empty())
  331. reverse();
  332. }
  333. //-----------------
  334. // Right reversing
  335. //-----------------
  336. inline
  337. RightReversing::RightReversing(SetComplement sc){
  338. set_comp=sc;
  339. }
  340. inline void
  341. RightReversing::full_reverse(){
  342. while(not to_reverse.empty()) reverse();
  343. }
  344. //--------------
  345. // MonoidFamily
  346. //--------------
  347. inline
  348. MonoidFamily::~MonoidFamily(){
  349. }
  350. inline string
  351. MonoidFamily::display() const{
  352. return label+" monoid family";
  353. }
  354. inline Word
  355. MonoidFamily::garside_element(size_t r){
  356. return ranked_garside_word_factory(r);
  357. }
  358. inline size_t
  359. MonoidFamily::generators_number(size_t n){
  360. return gnum(n);
  361. }
  362. inline bool
  363. MonoidFamily::has_garside_element() const{
  364. return ranked_garside_word_factory!=nullptr;
  365. }
  366. inline bool
  367. MonoidFamily::has_garside_automorphism() const{
  368. return ranked_phi_germ!=nullptr;
  369. }
  370. inline void
  371. MonoidFamily::set_ranked_phi_germ(RankedGeneratorBijection rpg){
  372. ranked_phi_germ=rpg;
  373. }
  374. inline void
  375. MonoidFamily::set_ranked_garside_word_factory(RankedWordFactory rgwf){
  376. ranked_garside_word_factory=rgwf;
  377. }
  378. //-------------
  379. // MonoidTrait
  380. //-------------
  381. inline bool
  382. MonoidTrait::has_left_complement() const{
  383. return left_reversing!=nullptr;
  384. }
  385. inline bool
  386. MonoidTrait::has_right_complement() const{
  387. return right_reversing!=nullptr;
  388. }
  389. inline bool
  390. MonoidTrait::is_left_divisible(const Word& a,const Word& b){
  391. right_reversing->set_word(b,a);
  392. return right_reversing->check_positivity();
  393. }
  394. inline bool
  395. MonoidTrait::is_right_divisible(const Word& a,const Word& b){
  396. left_reversing->set_word(a,b);
  397. return left_reversing->check_positivity();
  398. }
  399. inline Word
  400. MonoidTrait::left_denominator(){
  401. return left_reversing->denominator();
  402. }
  403. inline Word
  404. MonoidTrait::left_lcm_complement(const Word& a,const Word& b){
  405. left_reverse(b,a);
  406. return left_numerator();
  407. }
  408. inline Word
  409. MonoidTrait::left_lcm(const Word& a,const Word& b){
  410. return left_lcm_complement(a,b)*a;
  411. }
  412. inline Word
  413. MonoidTrait::left_numerator(){
  414. return left_reversing->numerator();
  415. }
  416. inline Word
  417. MonoidTrait::left_reverse(const Word& w){
  418. left_reversing->set_word(w);
  419. left_reversing->full_reverse();
  420. return left_reversing->get_word();
  421. }
  422. inline Word
  423. MonoidTrait::left_reverse(const Word& u,const Word& v){
  424. left_reversing->set_word(u,v);
  425. left_reversing->full_reverse();
  426. return left_reversing->get_word();
  427. }
  428. inline Word
  429. MonoidTrait::right_denominator(){
  430. return right_reversing->denominator();
  431. }
  432. inline Word
  433. MonoidTrait::right_lcm(const Word& a,const Word& b){
  434. return a*right_lcm_complement(a,b);
  435. }
  436. inline Word
  437. MonoidTrait::right_lcm_complement(const Word& a,const Word& b){
  438. right_reverse(a,b);
  439. return right_numerator();
  440. }
  441. inline Word
  442. MonoidTrait::right_numerator(){
  443. return right_reversing->numerator();
  444. }
  445. inline Word
  446. MonoidTrait::right_reverse(const Word& w){
  447. right_reversing->set_word(w);
  448. right_reversing->full_reverse();
  449. return right_reversing->get_word();
  450. }
  451. inline Word
  452. MonoidTrait::right_reverse(const Word& u,const Word& v){
  453. right_reversing->set_word(u,v);
  454. right_reversing->full_reverse();
  455. return right_reversing->get_word();
  456. }
  457. inline void
  458. MonoidTrait::set_left_complement(SetComplement sc){
  459. left_reversing=new LeftReversing(sc);
  460. }
  461. inline void
  462. MonoidTrait::set_right_complement(SetComplement sc){
  463. right_reversing=new RightReversing(sc);
  464. }
  465. //------
  466. // Word
  467. //------
  468. inline
  469. Word::Word(){}
  470. inline
  471. Word::Word(const Word& w):Array(w){}
  472. inline
  473. Word::Word(Word&& w):Array(w){}
  474. inline
  475. Word::Word(const Array<Generator>& a):Array(a){}
  476. inline
  477. Word::Word(Array<Generator>&& a):Array(a){}
  478. inline Word
  479. Word::concatenate(const Word& w) const{
  480. return Word(append(w));
  481. }
  482. inline Word&
  483. Word::operator=(const Word& w){
  484. Array::operator=(w);
  485. }
  486. inline Word&
  487. Word::operator=(Word&& w){
  488. Array::operator=(w);
  489. }
  490. //***********************
  491. //* Auxiliary functions *
  492. //***********************
  493. inline int
  494. cmp(const Generator& x,const Generator& y){
  495. if(x<y) return -1;
  496. if(x==y) return 0;
  497. return 1;
  498. }
  499. inline string
  500. disp_alpha(const Generator& x){
  501. if(x==0) return "e";
  502. string res="";
  503. if(x>0) return res+char(x-1+'a');
  504. return res+char(-x-1+'A');
  505. }
  506. inline Word
  507. operator*(const Word& u,const Word& v){
  508. return u.append(v);
  509. }
  510. #endif