init.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "init.hpp"
  20. //******************
  21. //* Global objects *
  22. //******************
  23. Gomu::Type* type_ArtinWordA;
  24. Gomu::Type* type_DualWordA;
  25. Gomu::Type* type_monoid_family;
  26. Gomu::Type* type_word;
  27. //*************************
  28. //* Extension inilisation *
  29. //*************************
  30. extern "C"{
  31. Word empty_word;
  32. Word x1({1});
  33. Word x2({2});
  34. Word x3({3});
  35. Word x4({4});
  36. Word X1({-1});
  37. Word X2({-2});
  38. Word X3({-3});
  39. Word X4({-4});
  40. Word x12({generator(1,2)});
  41. Word x13({generator(1,3)});
  42. Word x23({generator(2,3)});
  43. Word x14({generator(1,4)});
  44. Word x24({generator(2,4)});
  45. Word x34({generator(3,4)});
  46. Word X12({(Generator)(-generator(1,2))});
  47. Word X13({(Generator)(-generator(1,3))});
  48. Word X23({(Generator)(-generator(2,3))});
  49. Word X14({(Generator)(-generator(1,4))});
  50. Word X24({(Generator)(-generator(2,4))});
  51. Word X34({(Generator)(-generator(3,4))});
  52. void init(){
  53. braids_init();
  54. }
  55. Gomu::Module::Type types[]={
  56. {"ArtinWordA",ArtinWordA_display,word_delete,word_copy,word_compare,&type_ArtinWordA},
  57. {"DualWordA",DualWordA_display,word_delete,word_copy,word_compare,&type_DualWordA},
  58. {"ArtinMonoidFamilyA",mf_display,mf_delete,Gomu::no_copy,Gomu::no_comp,&type_monoid_family},
  59. {"DualMonoidFamilyA",mf_display,mf_delete,Gomu::no_copy,Gomu::no_comp,&type_monoid_family},
  60. {"MonoidFamily",mf_display,mf_delete,Gomu::no_copy,Gomu::no_comp,&type_monoid_family},
  61. {"Word",word_display,word_delete,word_copy,word_compare,&type_word},
  62. TYPE_SENTINEL
  63. };
  64. Gomu::Module::Function functions[]={
  65. {"ArtinWordA","operator*",{"ArtinWordA","ArtinWordA"},(void*)word_concatenate},
  66. {"DualWordA","operator*",{"DualWordA","DualWordA"},(void*)word_concatenate},
  67. {"Word","word",{"Array"},(void*)word_from_array},
  68. FUNC_SENTINEL
  69. };
  70. Gomu::Module::Function member_functions[]={
  71. //ArtinMonoidFamilyA
  72. {"Boolean","is_left_divisible",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_is_left_divisible},
  73. {"Tuple","is_left_divisible_x",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_is_left_divisible_x},
  74. {"Boolean","is_right_divisible",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_is_right_divisible},
  75. {"Tuple","is_right_divisible_x",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_is_right_divisible_x},
  76. {"ArtinWordA","left_complement",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_left_complement},
  77. {"ArtinWordA","left_denominator",{"ArtinMonoidFamilyA"},(void*)mt_left_denominator},
  78. {"ArtinWordA","left_lcm",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_left_lcm},
  79. {"ArtinWordA","left_lcm_complement",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_left_lcm_complement},
  80. {"ArtinWordA","left_gcd",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_left_gcd},
  81. {"Tuple","left_gcd_x",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_left_gcd_x},
  82. {"ArtinWordA","left_numerator",{"ArtinMonoidFamilyA"},(void*)mt_left_numerator},
  83. {"ArtinWordA","left_reverse",{"ArtinMonoidFamilyA","ArtinWordA"},(void*)mt_left_reverse},
  84. {"ArtinWordA","left_reverse",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_left_reverse2},
  85. {"ArtinWordA","right_complement",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_right_complement},
  86. {"ArtinWordA","right_denominator",{"ArtinMonoidFamilyA"},(void*)mt_right_denominator},
  87. {"ArtinWordA","right_lcm",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_right_lcm},
  88. {"ArtinWordA","right_lcm_complement",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_right_lcm_complement},
  89. {"ArtinWordA","right_gcd",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_right_gcd},
  90. {"Tuple","right_gcd_x",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_right_gcd_x},
  91. {"ArtinWordA","right_numerator",{"ArtinMonoidFamilyA"},(void*)mt_right_numerator},
  92. {"ArtinWordA","right_reverse",{"ArtinMonoidFamilyA","ArtinWordA"},(void*)mt_right_reverse},
  93. {"ArtinWordA","right_reverse",{"ArtinMonoidFamilyA","ArtinWordA","ArtinWordA"},(void*)mt_right_reverse2},
  94. //ArtinWordA
  95. {"Integer","length",{"ArtinWordA"},(void*)word_length},
  96. {"ArtinWordA","inverse",{"ArtinWordA"},(void*)word_inverse},
  97. //DualMonoidFamilyA
  98. {"Boolean","is_left_divisible",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_is_left_divisible},
  99. {"Tuple","is_left_divisible_x",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_is_left_divisible_x},
  100. {"Boolean","is_right_divisible",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_is_right_divisible},
  101. {"Tuple","is_right_divisible_x",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_is_right_divisible_x},
  102. {"DualWordA","left_complement",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_left_complement},
  103. {"DualWordA","left_denominator",{"DualMonoidFamilyA"},(void*)mt_left_denominator},
  104. {"DualWordA","left_lcm",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_left_lcm},
  105. {"DualWordA","left_lcm_complement",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_left_lcm_complement},
  106. {"DualWordA","left_gcd",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_left_gcd},
  107. {"Tuple","left_gcd_x",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_left_gcd_x},
  108. {"DualWordA","left_numerator",{"DualMonoidFamilyA"},(void*)mt_left_numerator},
  109. {"DualWordA","left_reverse",{"DualMonoidFamilyA","DualWordA"},(void*)mt_left_reverse},
  110. {"DualWordA","left_reverse",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_left_reverse2},
  111. {"DualWordA","right_complement",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_right_complement},
  112. {"DualWordA","right_denominator",{"DualMonoidFamilyA"},(void*)mt_right_denominator},
  113. {"DualWordA","right_lcm",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_right_lcm},
  114. {"DualWordA","right_lcm_complement",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_right_lcm_complement},
  115. {"DualWordA","right_gcd",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_right_gcd},
  116. {"Tuple","right_gcd_x",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_right_gcd_x},
  117. {"DualWordA","right_numerator",{"DualMonoidFamilyA"},(void*)mt_right_numerator},
  118. {"DualWordA","right_reverse",{"DualMonoidFamilyA","DualWordA"},(void*)mt_right_reverse},
  119. {"DualWordA","right_reverse",{"DualMonoidFamilyA","DualWordA","DualWordA"},(void*)mt_right_reverse2},
  120. //DualWordA
  121. {"Integer","length",{"DualWordA"},(void*)word_length},
  122. {"ArtinWordA","inverse",{"DualWordA"},(void*)word_inverse},
  123. //MonoidFamily
  124. {"Integer","generators_number",{"MonoidFamily","Integer"},(void*)mf_generators_number},
  125. //Word
  126. {"Integer","length",{"Word"},(void*)word_length},
  127. {"Word","inverse",{"Word"},(void*)word_inverse},
  128. FUNC_SENTINEL
  129. };
  130. Gomu::Module::Symbol symbols[]={
  131. {"a0","ArtinWordA",(void*)&empty_word},
  132. {"a1","ArtinWordA",(void*)&x1},
  133. {"a2","ArtinWordA",(void*)&x2},
  134. {"a3","ArtinWordA",(void*)&x3},
  135. {"a4","ArtinWordA",(void*)&x4},
  136. {"A1","ArtinWordA",(void*)&X1},
  137. {"A2","ArtinWordA",(void*)&X2},
  138. {"A3","ArtinWordA",(void*)&X3},
  139. {"A4","ArtinWordA",(void*)&X4},
  140. {"a00","DualWordA",(void*)&empty_word},
  141. {"a12","DualWordA",(void*)&x12},
  142. {"a13","DualWordA",(void*)&x13},
  143. {"a23","DualWordA",(void*)&x23},
  144. {"a14","DualWordA",(void*)&x14},
  145. {"a24","DualWordA",(void*)&x24},
  146. {"a34","DualWordA",(void*)&x34},
  147. {"A12","DualWordA",(void*)&X12},
  148. {"A13","DualWordA",(void*)&X13},
  149. {"A23","DualWordA",(void*)&X23},
  150. {"A14","DualWordA",(void*)&X14},
  151. {"A24","DualWordA",(void*)&X24},
  152. {"A34","DualWordA",(void*)&X34},
  153. {"ArtinA","ArtinMonoidFamilyA",(void*)&ArtinA_mf},
  154. {"DualA","DualMonoidFamilyA",(void*)&dualA_mf},
  155. SYMB_SENTINEL
  156. };
  157. };
  158. //*************************
  159. //* Fonctions definitions *
  160. //*************************
  161. //--------------------------------------------------
  162. // Boolean is_left_divisible(MonoidTrait,Word,Word)
  163. //--------------------------------------------------
  164. void* mt_is_left_divisible(void* m,void* a,void* b){
  165. MonoidTrait* monoid=(MonoidTrait*)m;
  166. return Gomu::to_boolean(monoid->is_left_divisible(*(Word*)a,*(Word*)b));
  167. }
  168. //---------------------------------------------------------------
  169. // pair<Boolean,Word> is_left_divisible_x(MonoidTrait,Word,Word)
  170. //---------------------------------------------------------------
  171. void* mt_is_left_divisible_x(void* m,void* a,void* b){
  172. MonoidTrait* monoid=(MonoidTrait*)m;
  173. pair<bool,Word> p=monoid->is_left_divisible_x(*(Word*)a,*(Word*)b);
  174. Gomu::TupleValue* res=new Gomu::TupleValue(2);
  175. res->tab[0]=Gomu::Value(Gomu::type_boolean,Gomu::to_boolean(p.first));
  176. res->tab[1]=Gomu::Value((Gomu::Type*)monoid->data,new Word(p.second));
  177. return (void*)res;
  178. }
  179. //-----------------------------------------------
  180. // bool is_right_divisible(MonoidTrait,Word,Word)
  181. //-----------------------------------------------
  182. void* mt_is_right_divisible(void* m,void* a,void* b){
  183. MonoidTrait* monoid=(MonoidTrait*)m;
  184. return Gomu::to_boolean(monoid->is_right_divisible(*(Word*)a,*(Word*)b));
  185. }
  186. //----------------------------------------------------------------
  187. // pair<Boolean,Word> is_right_divisible_x(MonoidTrait,Word,Word)
  188. //----------------------------------------------------------------
  189. void* mt_is_right_divisible_x(void* m,void* a,void* b){
  190. MonoidTrait* monoid=(MonoidTrait*)m;
  191. pair<bool,Word> p=monoid->is_right_divisible_x(*(Word*)a,*(Word*)b);
  192. Gomu::TupleValue* res=new Gomu::TupleValue(2);
  193. res->tab[0]=Gomu::Value(Gomu::type_boolean,Gomu::to_boolean(p.first));
  194. res->tab[1]=Gomu::Value((Gomu::Type*)monoid->data,new Word(p.second));
  195. return (void*)res;
  196. }
  197. //---------------------------------------------
  198. // Word left_complement(MonoidTrait,Word,Word)
  199. //---------------------------------------------
  200. void* mt_left_complement(void* m,void* a,void* b){
  201. MonoidTrait* monoid=(MonoidTrait*)m;
  202. Word* u=(Word*)a;
  203. Word* v=(Word*)b;
  204. if(u->size()!=1 or v->size()!=1) RuntimeError("Words must be of length 1");
  205. return new Word(monoid->left_complement(u->array[0],v->array[0]));
  206. }
  207. //------------------------------------
  208. // Word left_denominator(MonoidTrait)
  209. //------------------------------------
  210. void* mt_left_denominator(void* m){
  211. MonoidTrait* monoid=(MonoidTrait*)m;
  212. if(not monoid->is_left_complemented())
  213. RuntimeError("Monoid is not left complemented");
  214. return new Word(monoid->left_denominator());
  215. }
  216. //---------------------------------------
  217. // Word left_gcd(MonoidTrait,Word,Word)
  218. //---------------------------------------
  219. void* mt_left_gcd(void* m,void* a,void *b){
  220. MonoidTrait* monoid=(MonoidTrait*)m;
  221. return new Word(monoid->left_gcd(*(Word*)a,*(Word*)b));
  222. }
  223. //-----------------------------------------------
  224. // (Word,Word) left_gcd_x(MonoidTrait,Word,Word)
  225. //-----------------------------------------------
  226. void* mt_left_gcd_x(void* m,void* a,void *b){
  227. MonoidTrait* monoid=(MonoidTrait*)m;
  228. pair<Word,Word> p=monoid->left_gcd_x(*(Word*)a,*(Word*)b);
  229. Gomu::TupleValue* res=new Gomu::TupleValue(2);
  230. Gomu::Type* type=(Gomu::Type*)monoid->data;
  231. res->tab[0]=Gomu::Value(type,new Word(p.first));
  232. res->tab[1]=Gomu::Value(type,new Word(p.second));
  233. return (void*)res;
  234. }
  235. //--------------------------------------
  236. // Word left_lcm(MonoidTrait,Word,Word)
  237. //--------------------------------------
  238. void* mt_left_lcm(void* m,void* a,void *b){
  239. MonoidTrait* monoid=(MonoidTrait*)m;
  240. return new Word(monoid->left_lcm(*(Word*)a,*(Word*)b));
  241. }
  242. //-------------------------------------------------
  243. // Word left_lcm_complement(MonoidTrait,Word,Word)
  244. //-------------------------------------------------
  245. void* mt_left_lcm_complement(void* m,void* a,void *b){
  246. MonoidTrait* monoid=(MonoidTrait*)m;
  247. return new Word(monoid->left_lcm_complement(*(Word*)a,*(Word*)b));
  248. }
  249. //----------------------------------
  250. // Word left_numerator(MonoidTrait)
  251. //----------------------------------
  252. void* mt_left_numerator(void* m){
  253. MonoidTrait* monoid=(MonoidTrait*)m;
  254. if(not monoid->is_left_complemented())
  255. RuntimeError("Monoid is not left complemented");
  256. return new Word(monoid->left_numerator());
  257. }
  258. //-------------------------------------
  259. // Word left_reverse(MonoidTrait,Word)
  260. //-------------------------------------
  261. void* mt_left_reverse(void* m,void* w){
  262. MonoidTrait* monoid=(MonoidTrait*)m;
  263. if(not monoid->is_left_complemented())
  264. RuntimeError("Monoid is not left complemented");
  265. return (void*)new Word(monoid->left_reverse(*(Word*)w));
  266. }
  267. //------------------------------------------
  268. // Word left_reverse(MonoidTrait,Word,Word)
  269. //------------------------------------------
  270. void* mt_left_reverse2(void* m,void* num,void* den){
  271. MonoidTrait* monoid=(MonoidTrait*)m;
  272. if(not monoid->is_left_complemented())
  273. RuntimeError("Monoid is not left complemented");
  274. return (void*)new Word(monoid->left_reverse(*(Word*)num,*(Word*)den));
  275. }
  276. //----------------------------------------------
  277. // Word right_complement(MonoidTrait,Word,Word)
  278. //----------------------------------------------
  279. void* mt_right_complement(void* m,void* a,void* b){
  280. MonoidTrait* monoid=(MonoidTrait*)m;
  281. Word* u=(Word*)a;
  282. Word* v=(Word*)b;
  283. if(u->size()!=1 or v->size()!=1) RuntimeError("Words must be of length 1");
  284. return new Word(monoid->right_complement(u->array[0],v->array[0]));
  285. }
  286. //-------------------------------------
  287. // Word right_denominator(MonoidTrait)
  288. //-------------------------------------
  289. void* mt_right_denominator(void* m){
  290. MonoidTrait* monoid=(MonoidTrait*)m;
  291. if(not monoid->is_right_complemented())
  292. RuntimeError("Monoid is not right complemented");
  293. return new Word(monoid->right_denominator());
  294. }
  295. //---------------------------------------
  296. // Word right_gcd(MonoidTrait,Word,Word)
  297. //---------------------------------------
  298. void* mt_right_gcd(void* m,void* a,void *b){
  299. MonoidTrait* monoid=(MonoidTrait*)m;
  300. return new Word(monoid->right_gcd(*(Word*)a,*(Word*)b));
  301. }
  302. //-----------------------------------------------
  303. // (Word,Word) right_gcd_x(MonoidTrait,Word,Word)
  304. //-----------------------------------------------
  305. void* mt_right_gcd_x(void* m,void* a,void *b){
  306. MonoidTrait* monoid=(MonoidTrait*)m;
  307. pair<Word,Word> p=monoid->right_gcd_x(*(Word*)a,*(Word*)b);
  308. Gomu::TupleValue* res=new Gomu::TupleValue(2);
  309. Gomu::Type* type=(Gomu::Type*)monoid->data;
  310. res->tab[0]=Gomu::Value(type,new Word(p.first));
  311. res->tab[1]=Gomu::Value(type,new Word(p.second));
  312. return (void*)res;
  313. }
  314. //---------------------------------------
  315. // Word right_lcm(MonoidTrait,Word,Word)
  316. //---------------------------------------
  317. void* mt_right_lcm(void* m,void* a,void *b){
  318. MonoidTrait* monoid=(MonoidTrait*)m;
  319. return new Word(monoid->right_lcm(*(Word*)a,*(Word*)b));
  320. }
  321. //--------------------------------------------------
  322. // Word right_lcm_complement(MonoidTrait,Word,Word)
  323. //--------------------------------------------------
  324. void* mt_right_lcm_complement(void* m,void* a,void *b){
  325. MonoidTrait* monoid=(MonoidTrait*)m;
  326. return new Word(monoid->right_lcm_complement(*(Word*)a,*(Word*)b));
  327. }
  328. //-----------------------------------
  329. // Word right_numerator(MonoidTrait)
  330. //-----------------------------------
  331. void* mt_right_numerator(void* m){
  332. MonoidTrait* monoid=(MonoidTrait*)m;
  333. if(not monoid->is_right_complemented())
  334. RuntimeError("Monoid is not right complemented");
  335. return new Word(monoid->right_numerator());
  336. }
  337. //--------------------------------------
  338. // Word right_reverse(MonoidTrait,Word)
  339. //--------------------------------------
  340. void* mt_right_reverse(void* m,void* w){
  341. MonoidTrait* monoid=(MonoidTrait*)m;
  342. if(not monoid->is_right_complemented())
  343. RuntimeError("Monoid is not right complemented");
  344. return (void*)new Word(monoid->right_reverse(*(Word*)w));
  345. }
  346. //-------------------------------------------
  347. // Word right_reverse(MonoidTrair,Word,Word)
  348. //-------------------------------------------
  349. void* mt_right_reverse2(void* m,void* den,void* num){
  350. MonoidTrait* monoid=(MonoidTrait*)m;
  351. if(not monoid->is_right_complemented())
  352. RuntimeError("Monoid is not right complemented");
  353. return (void*)new Word(monoid->right_reverse(*(Word*)den,*(Word*)num));
  354. }
  355. //-----------------------
  356. // Word word(ArrayValue)
  357. //-----------------------
  358. void* word_from_array(void* arr){
  359. Gomu::ArrayValue* array=(Gomu::ArrayValue*)arr;
  360. if(array->type!=Gomu::type_integer)
  361. RuntimeError("An array of integer is needed");
  362. size_t size=array->size;
  363. Word* res=new Word(size);
  364. for(size_t i=0;i<size;++i){
  365. res->write(i,Gomu::get_slong(array->tab[i]));
  366. }
  367. return res;
  368. }