hcvctors.red 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. module hcvctors;
  2. % The following set of symbolic procedures allow to manipulate
  3. % indices of vectors in the same way as for lists. Coercion from array
  4. % to vectors is also allowed.
  5. % Module necessary to handle DUMMY.RED
  6. % Only functions available in the algebraic mode are commented in
  7. % the TeX file.
  8. symbolic smacro procedure mkve n;
  9. mkvect(n-1);
  10. symbolic smacro procedure mkve!* n;
  11. % n is an integer
  12. % as mkvect but initialize to 0 instead of nil.
  13. % for general tables, use mkarray1(list(i1,...),'algebraic).
  14. mkarray1(list(n),'algebraic);
  15. symbolic smacro procedure putve(ve,i,elt);
  16. % To identify numerology to the one of lists.
  17. % Use: for i:=1:upbve tri do putve(tri,i,i); ==> [1 2 3 4]
  18. putv(ve,i-1,elt);
  19. symbolic smacro procedure venth(u,i);
  20. % To identify numerology to the one of lists.
  21. getv(u,i-1);
  22. symbolic smacro procedure array_to_vect u;
  23. % For the use in the algebraic mode, it may be useful to coerce to
  24. % ARRAYS and vice-versa
  25. % Use: array_to_vect algebraic <array>
  26. cadr get(u,'avalue);
  27. symbolic procedure mkrandtabl(u,base,ar);
  28. % u is a list of 2 integers which determine the dimensions of the array
  29. % base is integer or decimal.
  30. % Output is a table of random numbers
  31. if not fixp base and not !*rounded then
  32. rederr("ROUNDED should be on") else
  33. begin scalar ve; integer lu;
  34. lu:=length(u:=alg_to_symb u);
  35. % if lu > 2 then typerr(u,"one or two integer list");
  36. ve:=mkarray1(u,'algebraic);
  37. if lu=1 then
  38. for i:=1:car u do
  39. putve(ve,i, if not fixp base then
  40. mk!*sq((make!:rd random(cdr base)) . 1)
  41. else random(base)) else
  42. if lu=2 then <<
  43. for i:=1:car u do putve(ve,i,mkve!* cadr u);
  44. for i:=1:car u do for j:=1:cadr u do
  45. putve(venth(ve,i),j, if not fixp base then
  46. mk!*sq((make!:rd random(cdr base)) . 1)
  47. else random(base))>>
  48. else return typerr(u,"one or two integer list");
  49. vect_to_array(list(ve,ar),u);
  50. return symb_to_alg lengthreval list ar
  51. end;
  52. flag('(mkrandtabl),'opfn);
  53. symbolic procedure upbve u;
  54. % Should be used in FOR ... DO loops.
  55. if null upbv u then 0 else upbv u +1;
  56. % ILLUSTRATION of use of the above macros and function.
  57. %for i:=1:upbve tri do
  58. % for j:=1:upbve venth(tri,i) do
  59. % putve(venth(tri,i),j,i*j);
  60. symbolic procedure dimvect u;
  61. % u is a vector or vector of vector or ..
  62. % Gives the dimension of each level.
  63. % Valid only for rectangular patterns.
  64. % May also be used for Young tableaux to get the dimensions of the
  65. % FIRST row and column.
  66. if null u then nil else
  67. (upbv u + 1) . dimvect ((if not vectorp x then nil
  68. else x) where x=getv(u,0));
  69. symbolic procedure index_elt(elt,u);
  70. % elt is an atom or a number
  71. % return the position index.
  72. begin scalar idx; integer ii;
  73. ii:=1;
  74. repeat <<if elt = venth(u,ii) then idx:=ii else nil; ii:=ii+1;>>
  75. until not null idx or ii=upbve u + 1;
  76. return idx
  77. end;
  78. symbolic procedure vect2list u;
  79. % Coerce a vector into a list at any level. Suitable for the
  80. % symbolic mode.
  81. for i := 0 : upbv u collect
  82. (if null upbv x then x
  83. else vect2list x) where x= getv(u,i);
  84. symbolic procedure list_str u;
  85. % generates the list of dimensions for the array construction.
  86. %if not listp u then
  87. % rederr "Argument to 'list_str' must be a list"
  88. % it is supposed to pass the test of homo_lst.
  89. if not listp car u then length u . nil
  90. else length u . list_str car u;
  91. symbolic procedure n_first_lst(u,n);
  92. if n=0 then nil else
  93. car u . n_first_lst(cdr u,n-1);
  94. symbolic procedure homo_lst(u,n);
  95. % n indicates the level of homogeneity.
  96. % u is the list.
  97. % It should be filtered by depth which gives n+1 and
  98. % generated by alg_to_symb <algebraic list>
  99. if not listp u then
  100. rederr " Argument to 'homo_lst' has not the correct dimension"
  101. else
  102. if n=0 then 1 else
  103. begin integer nl;
  104. scalar su;
  105. su:=u; nl:=length car su;
  106. % It is supposed here that car su is also a list.
  107. su:=cdr su ;
  108. if null su then 1;
  109. while su and nl= length car su do su:=cdr su;
  110. if null su then return
  111. for each i in u product homo_lst(i,n-1)
  112. else return 0
  113. end;
  114. symbolic procedure list_to_array(u,n,arr);
  115. % Suitable for the algebraic mode.
  116. % Defines n-dimensional arrays.
  117. begin scalar lu;
  118. lu:=alg_to_symb u;
  119. <<vect_to_array(list(list2vectn(lu,n), arr),
  120. n_first_lst(list_str lu,n));
  121. remflag(list arr,'used!*)>>;
  122. end;
  123. flag('(list_to_array,array_to_list),'opfn);
  124. symbolic procedure array_to_list u;
  125. % Transforms an array into a list.
  126. % Suitable for the algebraic mode.
  127. % Works at all levels.
  128. symb_to_alg vect2list array_to_vect u;
  129. symbolic procedure list2vectn(u,n);
  130. if n=1 then list2vect u else
  131. begin scalar ll,x;
  132. if homo_lst(u,n-1)=1 then ll:=list_str u else
  133. rerror(alg,1,list(n,"Too large to coerce to an array"));
  134. x:=mkvect (first ll -1); ll:=cdr ll;
  135. for i:=1: upbv x +1 do putve(x,i,list2vectn(nth(u,i),n-1));
  136. return x
  137. end;
  138. symbolic procedure list2vect u; list2vect!*(u,'algebraic);
  139. symbolic procedure list2vect!*(u,v); % replaces list2vect
  140. % Coerce a list into a vector
  141. % v may be either SYMBOLIC or ALGEBRAIC
  142. begin scalar x;
  143. x:=mkvect(length u -1);
  144. for i:=1:upbv x +1 do putve(x,i,
  145. if v = 'algebraic then symb_to_alg nth(u,i) else nth(u,i));
  146. return x end;
  147. symbolic procedure vect_to_array(u,dim);
  148. % u is a list (vector, array_id)
  149. <<typechk(cadr u,'array); put(cadr u,'rtype,'array);
  150. put(cadr u , 'avalue, list('array, car u));
  151. put(cadr u, 'dimension, dim)>>;
  152. symbolic procedure vectappend(v1,v2);
  153. if not vectorp v1 then typerr(v1,"vector") else
  154. if not vectorp v2 then vectappend1(v1,v2) else
  155. begin scalar new;integer dim;
  156. new:=mkvect(upbv v1 + upbv v2 +1 );
  157. dim:=upbv v1 + 1;
  158. for i:=1:dim do putve(new,i,venth(v1,i));
  159. for i:=(dim+1):(upbv new + 1) do putve(new,i,venth(v2,i-dim));
  160. return new
  161. end;
  162. symbolic procedure vectappend1(v1,v2);
  163. begin scalar new; integer dim;
  164. new:=mkvect(dim:=upbv v1 +1);
  165. for i:=1:dim do putve(new,i,venth(v1,i));
  166. putve(new,dim+1,v2);
  167. return new end;
  168. symbolic procedure vectadd(v1,v2);
  169. % v1 and v2 are supposed to be two numeric vectors.
  170. % So we use PLUS and not SIMPPLUS.
  171. if not vectorp v1 or not vectorp v2 then
  172. rederr("arguments must be vectors")
  173. else
  174. begin scalar vadd;
  175. vadd:=mkvect upbv v1;
  176. for i:=1:upbve v1 do putve(vadd,i, venth(v1,i)+venth(v2,i));
  177. return vadd
  178. end;
  179. symbolic procedure setelve(ve,l,val);
  180. % Sets any elements of ve, at any level to val.
  181. % Example of use:
  182. % for i:=1:upbve tri do
  183. % for j:=1:upbve venth(tri,i) do
  184. % setelve(tri,list(i,j),i+j);
  185. if null l then nil else
  186. if null cdr l then putve(ve,car l, val) else
  187. setelve(venth(ve,car l),cdr l,val);
  188. symbolic procedure ltrident n;
  189. % Constructs a lower triangular matrix of unit vectors
  190. begin scalar a;
  191. a:=mkve!* n;
  192. for i:=1:n do
  193. << putve(a,i,mkve!* i);
  194. for j:=1:i-1 do putve(venth(a,i), j, 0);
  195. putve(venth(a,i),i,1);>>;
  196. return a
  197. end;
  198. endmodule;
  199. end;