monoid.hpp 15 KB

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