File: poly.c

package info (click to toggle)
lie 2.2.2%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 1,040 kB
  • sloc: ansic: 12,761; yacc: 395; makefile: 111; sh: 4
file content (303 lines) | stat: -rw-r--r-- 8,887 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "lie.h"

#ifdef __STDC__
static void error_nvars(index n1,index n2);
#endif

static void error_nvars(index n1,index n2)
{
    Printf("Number of variables in polynomials unequal\n");  
    error("( %ld <-> %ld variables).\n",(long)n1,(long)n2);
}

poly* Pol_from_vec(v)
vector *v;
{
    poly *result =  mkpoly(1,v->ncomp);
    copyrow(v->compon, result->elm[0], v->ncomp); 
    result->coef[0] = one;
    freemem(v);
    return result;
}

entry Degree_pol(poly* p)
{   entry ncols = p->ncols;
    entry nrows = p->nrows;
    entry max=MinEntry; /* will be overwritten for i==0 */
    entry i,j;
    if (p->coef[0]->size==0) return 0; /* degree(0X[0,..,0]) set to 0 */
    for (i = 0 ; i < nrows; ++i)
    { entry sum=0; entry* row = p->elm[i];
      for (j=0 ; j<ncols; ++j) sum += row[j];
      if (sum>max) max = sum;
    }
    return max;
}

/*************************************************************
*  Check that a polynomial has row length r, and normalise   *
*************************************************************/

poly* check_pol(p,r) poly* p; entry r;
{ entry d = p->ncols;
  if (d != r) 
      error("Number of variables in polynomial unequal Lie rank.\n"); 
  if (!issorted(p)) return Reduce_pol(p);
  return p;
}

/* The polynomial arithmetic routines will guarantee that no improper
   0 coefficients will remain, even if they were present in the arguments.
*/

poly* Add_pol_pol(a,b,neg_b) poly* a,* b; boolean neg_b;
{ return Addmul_pol_pol_bin(a,b, neg_b ? minus_one : one); }

poly* Mul_bin_pol(a,b) bigint* a; poly* b; /* modifies b unless shared */
{ entry nrows = b->nrows; entry i; poly* result = private_pol(b);
  setshared(a);
  for (i=0; i<nrows; i++)
  { result->coef[i]=mult(a,b->coef[i]); setshared(result->coef[i]); }
  clrshared(a);
#ifndef argumentsave
  freemem(a); /* don't freepol(b) since either isshared(b) or b==result */
#endif
  return result;
}

poly* Addmul_pol_pol_bin(a,b,c) poly* a,* b; bigint* c; /* a+c*b */
{ index i,j,k; entry len=a->ncols; cmp_tp cmp; poly* result;
  if (len != b->ncols) error_nvars(len,b->ncols);
  if (!c->size) return a;
  if (issorted(a) || issorted(b)) /* then make use of this sorting: */
  { cmpfn_tp compare=set_ordering(cmpfn,len,defaultgrp);
    if (!issorted(a)) a=Reduce_pol(a); else
    if (!issorted(b)) b=Reduce_pol(b); /* now both are sorted */
    if (a->nrows==1 && !a->coef[0]->size) return Mul_bin_pol(c,b);
    if (b->nrows==1 && !b->coef[0]->size) return a;
    result=mkpoly(a->nrows+b->nrows,len); i=j=k=0; setshared(c);
    while(j<b->nrows)
    { while (i<a->nrows && (cmp=compare(a->elm[i],b->elm[j],len))>0)
	if (!a->coef[i]->size) i++; /* skip term with 0 coefficient */ else
	{ result->coef[k]=a->coef[i]; setshared(result->coef[k]);
	  copyrow(a->elm[i++],result->elm[k++],len);
	}
      if (i<a->nrows && cmp==0) /* add compatible terms */
      { result->coef[k]= c==one	      ? add(a->coef[i],b->coef[j])
		       : c==minus_one ? sub(a->coef[i],b->coef[j])
		       : add(a->coef[i],mult(c,b->coef[j]));
	if (!result->coef[k]->size) /* if terms cancel */
	{ freemem(result->coef[k]); i++; j++; }
	else
	{ setshared(result->coef[k]);
	  copyrow(a->elm[i++],result->elm[k++],len); j++;
	}
      }
      else /* i==a->nrows || compare(a->elm[i],b->elm[j],len)<0; */
      if (!b->coef[j]->size) j++; else
      { result->coef[k]= c==one	      ? b->coef[j]
		       : c==minus_one ? sub(null,b->coef[j])
		       : mult(c,b->coef[j]);
	setshared(result->coef[k]);
	copyrow(b->elm[j++],result->elm[k++],len);
      }
    } /* Now all terms of b have been included, but a may have some left */
    while (i<a->nrows)
      if (!a->coef[i]->size) i++; else
      { result->coef[k]=a->coef[i]; setshared(result->coef[k]);
	copyrow(a->elm[i++],result->elm[k++],len);
      }
    clrshared(c);
#ifndef argumentsave
    freepol(a); freepol(b); freemem(c);
#endif
    if (k) { result->nrows=k; setsorted(result); return result; }
    freemem(result); return poly_null(len);
  } /* end of sorted case; if unsorted simply append polynomials and reduce */
  setshared(c); result=mkpoly(a->nrows+b->nrows,len);
  for (i=0; i<a->nrows; i++)
  { result->coef[i]=a->coef[i]; setshared(result->coef[i]);
    copyrow(a->elm[i],result->elm[i],len);
  }
  for (j=0; j<b->nrows; j++,i++)
  { result->coef[i]= c==one ? b->coef[j] : mult(b->coef[j],c);
    setshared(result->coef[i]);
    copyrow(b->elm[j],result->elm[i],len);
  }
  clrshared(c);
#ifndef argumentsave
  freepol(a); freepol(b); freemem(c);
#endif
  return Reduce_pol(result);
}

poly* Div_pol_bin(a,b) poly* a; bigint* b;
{
    entry nrows = a->nrows;
    entry i;
    poly *result =  isshared(a)?
	copypoly(a) : (setshared(a),a);
    setshared(b);
    for (i = 0; i < nrows; i++) {
	result->coef[i] = quotient(a->coef[i],b);
	setshared(result->coef[i]);
    }
    clrshared(b);
#ifndef argumentsave
    freepol(a);freemem(b);
#endif
    return result;
}

poly* Mod_pol_bin(a,b) poly* a; bigint* b;
{
    entry nrows = a->nrows;
    entry i;
    poly *result =  isshared(a)?
	copypoly(a) : (setshared(a),a);
    setshared(b);
    for (i = 0; i < nrows; i++) {
	result->coef[i] = mod(a->coef[i],b);
	setshared(result->coef[i]);
    }
    clrshared(b);
#ifndef argumentsave
    freepol(a);freemem(b);
#endif
    return result;
}

poly
*Mul_pol_int(b,a)
   intcel *a;
   poly *b;
{
    entry nrows = b->nrows, ncols = b->ncols;
    entry d = a->intval;
    entry i,j;
    poly *result =  isshared(b)?
	copypoly(b) : (setshared(b),b);
    for (i = 0; i < nrows; i++) 
    for (j = 0; j < ncols; j++) {
	result->elm[i][j] =  b->elm[i][j] * d;
    }
#ifndef argumentsave
    freepol(b);freemem(a);
#endif
    return result;
}

poly* Div_pol_vec(b,a) poly* b; vector* a;
{ index nrows = b->nrows, ncols = b->ncols;
  entry i,j;
  poly* result = private_pol(b);
  if (ncols != a->ncomp)
    error("Size of vector should equal number of indeterminates.\n");
  for (j=0; j<ncols; j++)
  { entry d=a->compon[j]; if (d==0) error("Division by zero.\n");
    for (i=0; i<nrows; i++) result->elm[i][j]/=d;
  }
  return Reduce_pol(result);
}

poly* Mod_pol_vec(b,a) poly *b; vector *a;
{ index nrows = b->nrows, ncols = b->ncols;
  entry i,j;
  poly* result = private_pol(b);
  if (ncols != a->ncomp)
    error("Size of vector should equal number of indeterminates.\n");
  for (j=0; j<ncols; j++)
  { entry d=labs(a->compon[j]); if (d==0) continue; /* mod 0 is noop */
    for (i=0; i<nrows; i++)
    { if ((result->elm[i][j]%=d)<0) result->elm[i][j]+=d; }
  }
  return Reduce_pol(result);
}

poly *Disjunct_mul_pol_pol(p1, p2)		
poly *p1, *p2;			

/***************************************************************
* Product of polynomials.De sets of free variables of p1 and p2*
* are disjunct.						       *
***************************************************************/

{ index r1= p1->ncols, r2=p2->ncols, n1=p1->nrows,
      n2=p2->nrows;
  entry **e1=p1->elm, **e2=p2->elm, **a;
  index i, j, s=0;
  poly *ans;
  a=(ans=mkpoly(n1*n2,r1+r2))->elm;
  for(i=0;i<n1;i++)
    for(j=0; j<n2; j++)
    { copyrow(e1[i],a[s],r1); copyrow(e2[j],a[s]+r1,r2);
      ans->coef[s]= mult(p1->coef[i],p2->coef[j]);
      setshared(ans->coef[s]);
      s++;
    }
#ifndef argumentsave
    freepol(p1);
    freepol(p2);
#endif
    return(ans);
}

poly *Mul_pol_pol(p1,p2)
    poly *p1, *p2;
{
    index ncols1 = p1->ncols, ncols2 = p2->ncols,
    nrows1 = p1->nrows, nrows2 = p2->nrows;
    index nrows = nrows1 * nrows2;
    poly *result, *garbage;
    index i,j,k = 0,l;

/***************************************************************
*  The wide polynomial is chosen p1			       *
***************************************************************/

    if (ncols1 != ncols2) error_nvars(ncols1,ncols2);

    garbage = result = mkpoly(nrows, ncols1);


/***************************************************************
*  Expand loop						       *
***************************************************************/


    for (i=0; i < nrows1; i++) {
	bigint *c = p1->coef[i];
	for (j=0; j < nrows2; j++) {
	    entry *monom = result->elm[k], *monom2 = p2->elm[j];
	    copyrow(p1->elm[i],monom,ncols1);
	    for (l=0; l<ncols2;l++) monom[l] += monom2[l];
	    result->coef[k] = mult(c,p2->coef[j]);
	    setshared(result->coef[k]);
	    k++;
	}
	/*
	result = Reduce_pol(result);
	k = result->nrows;
	if (result != garbage) error("System warning.\n");
	*/
    }


/***************************************************************
*  Sort and reduce polynomial				       *
***************************************************************/

    result = copypoly(Reduce_pol(result));
    freemem(garbage);

/***************************************************************
*  Freemem arguments					       *
***************************************************************/

#ifndef argumentsave
    freepol(p1);
    freepol(p2);
#endif

    return result;
}