1 /* ------------------------------------------------------------------- */
2 /* SARAG - Low Level Routines */
3 /* by Fabrizio Caruso */
4 /*modified by Alexandre Le Meur and Marie-Françoise Roy */
6 /* ------------------------------------------------------ */
7 /* Polynomial related functions */
9 /* It expands a polynomial if ASSUME_EXPANDED is false */
12 if ASSUME_EXPANDED then
18 /* Degree of a polynomial (MACRO) */
19 degree(poly,indet) ::=
20 buildq([poly,indet],if poly = 0 then -1 else hipow(poly,indet));
23 /* Leading coefficient of a polynomial */
24 leadCoeff(poly,indet)::=
26 ratcoeff(poly,indet,degree(poly,indet)));
29 /* Leading term of a polynomial */
30 leadTerm(poly,indet)::=
32 indet^degree(poly,indet));
34 /* Leading monomial of a polynomial */
35 leadMono(poly,indet)::=
37 leadCoeff(poly,indet)*leadTerm(poly,indet));
39 /* Tail of a polynomial */
42 poly-leadMono(poly,indet));
45 /* ------------------------------------------------------ */
49 buildq([val], if val = 0 then 0 else if val < 0 then -1 else 1);
52 /* ------------------------------------------------------ */
53 /* Array-related routines */
55 /* Number of dimensions */
57 second(arrayinfo(ar));
61 first(third(arrayinfo(ar)));
65 first(third(arrayinfo(ar)))+1;
69 first(third(arrayinfo(ar)))+1;
71 /* Number of columns */
73 second(third(arrayinfo(ar)))+1;
75 /* It makes a polynomial out of a list */
77 sum(lst[i]*var^(i-1),i,1,length(lst));
80 makelist(coeff(pol,var,i),i,0,degree(pol,var));
83 makelist(mtx[i],i,1,length(mtx));
86 /* list of lists -> bidimensional array */
88 block([nRows,nCols,i,j,res],
90 nCols : length(lst[1]),
91 res : make_array( 'any,nRows,nCols ),
92 for i : 1 thru nRows do
93 for j : 1 thru nCols do
94 res[i-1,j-1] : lst[i][j],
100 /* bidimensional array -> list of lists */
102 block([nRows,nCols,i,j,newRow,res],
103 nRows : first(third(arrayinfo(arr)))+1,
104 nCols : second(third(arrayinfo(arr)))+1,
107 for i : 1 thru nRows do
110 for j : 1 thru nCols do
111 newRow : endcons(arr[i-1,j-1],newRow),
113 res : endcons(newRow,res)
119 array2singleList(arr) :=
120 makelist(arr[j],j,0,first(third(arrayinfo(arr))));
123 singleList2array(lst) :=
125 res:make_array('any,length(lst)),
126 for i : 1 thru length(lst) do
132 poly2array(pol,var) :=
133 singleList2array(poly2list(pol,var));
135 array2poly(arr,var) :=
136 list2poly(array2singleList(arr),var);
139 /* ------------------------------------------------------ */
140 /* List manipulation */
142 /* It removes the zeroes from a list */
147 if first(seq) = 0 then
148 trimZerosRec(rest(seq))
150 cons(first(seq),trimZerosRec(rest(seq)));
155 for i:1 thru length(seq) do
156 if not(seq[i] = 0) then
157 list:append(list,[seq[i]]),
163 /* Mergesort of two sorted lists wrt to a given total ordering*/
165 mergeSorted(lhs,rhs,ordering) :=
166 block([i,j,k,lhsLen,rhsLen,res],
167 lhsLen : length(lhs),
168 rhsLen : length(rhs),
172 while (i<= lhsLen) and (j<= rhsLen) do
175 if ordering(lhs[i],rhs[j]) then
177 res : endcons(lhs[i],res),
181 if lhs[i]=rhs[j] then
183 res : endcons(lhs[i],res),
189 res : endcons(rhs[j],res),
194 res : append(res, makelist(lhs[k],k,i,lhsLen))
197 res : append(res, makelist(rhs[k],k,j,rhsLen)),