My Project  debian-1:4.1.1-p2+ds-4build4
ntupel.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 
5 /*
6 * ABSTRACT: n-tupel of coeffcients
7 */
8 
9 
10 
11 #include "misc/auxiliary.h"
12 #include "misc/mylimits.h"
13 
14 #include "reporter/reporter.h"
15 #include "omalloc/omalloc.h"
16 
17 #include "coeffs/numbers.h"
18 #include "coeffs/coeffs.h"
19 #include "coeffs/mpr_complex.h"
20 
21 #include "coeffs/ntupel.h"
22 #include "coeffs/modulop.h"
23 #include "coeffs/longrat.h"
24 
26 typedef number *number_array;
27 
28 static void nnCoeffWrite(const coeffs r, BOOLEAN b)
29 {
30  int i=0;
31  coeffs_array d=(coeffs_array)r->data;
32  loop
33  {
34  d[i]->cfCoeffWrite(d[i],b);
35  i++;
36  if (d[i]==NULL) break;
37  PrintS(" x ");
38  }
39 }
40 
41 
42 static BOOLEAN nnGreaterZero (number k, const coeffs r)
43 {
44  int i=0;
45  coeffs_array d=(coeffs_array)r->data;
46  loop
47  {
48  if (d[i]->type==n_R) return d[i]->cfGreaterZero(k,d[i]);
49  if (d[i]->type==n_long_R) return d[i]->cfGreaterZero(k,d[i]);
50  i++;
51  if (d[i]==NULL) return d[i-1]->cfGreaterZero(k,d[i-1]);
52  }
53 }
54 
55 static number nnMult (number a,number b, const coeffs r)
56 {
59  coeffs_array d=(coeffs_array)r->data;
60  int i=0;
61  loop
62  {
63  i++;
64  if (d[i]==NULL) break;
65  }
66  number *C=(number*)omAlloc(i*sizeof(number));
67  i=0;
68  loop
69  {
70  C[i]=d[i]->cfMult(A[i],B[i],d[i]);
71  i++;
72  if (d[i]==NULL) return (number)C;
73  }
74 }
75 
76 /*2
77 * create a number from int
78 */
79 static number nnInit (long l, const coeffs r)
80 {
81  coeffs_array d=(coeffs_array)r->data;
82  int i=0;
83  loop
84  {
85  i++;
86  if (d[i]==NULL) break;
87  }
88  number *C=(number*)omAlloc(i*sizeof(number));
89  i=0;
90  loop
91  {
92  C[i]=d[i]->cfInit(l,d[i]);
93  i++;
94  if (d[i]==NULL) return (number)C;
95  }
96 }
97 
98 /*2
99 * convert a number to int
100 */
101 static long nnInt(number &n, const coeffs r)
102 {
103  int i=0;
104  coeffs_array d=(coeffs_array)r->data;
105  loop
106  {
107  if (d[i]->type==n_R) return d[i]->cfInt(n,d[i]);
108  if (d[i]->type==n_long_R) return d[i]->cfInt(n,d[i]);
109  i++;
110  if (d[i]==NULL) return d[i-1]->cfInt(n,d[i-1]);
111  }
112 }
113 
114 static int nnSize(number n, const coeffs r)
115 {
116  int s=0;
117  int i=0;
118  coeffs_array d=(coeffs_array)r->data;
119  loop
120  {
121  if (d[i]->type==n_R) s+=d[i]->cfSize(n,d[i]);
122  else if (d[i]->type==n_long_R) s+=d[i]->cfSize(n,d[i]);
123  i++;
124  if (d[i]==NULL)
125  {
126  if (s==0) return d[i-1]->cfSize(n,d[i-1]);
127  else return s;
128  }
129  }
130 }
131 
132 static number nnCopy (number a, const coeffs r)
133 {
135  coeffs_array d=(coeffs_array)r->data;
136  int i=0;
137  loop
138  {
139  i++;
140  if (d[i]==NULL) break;
141  }
142  number *C=(number*)omAlloc(i*sizeof(number));
143  i=0;
144  loop
145  {
146  C[i]=d[i]->cfCopy(A[i],d[i]);
147  i++;
148  if (d[i]==NULL) return (number)C;
149  }
150 }
151 
152 static number nnAdd (number a, number b, const coeffs r)
153 {
156  coeffs_array d=(coeffs_array)r->data;
157  int i=0;
158  loop
159  {
160  i++;
161  if (d[i]==NULL) break;
162  }
163  number *C=(number*)omAlloc(i*sizeof(number));
164  i=0;
165  loop
166  {
167  C[i]=d[i]->cfAdd(A[i],B[i],d[i]);
168  i++;
169  if (d[i]==NULL) return (number)C;
170  }
171 }
172 
173 static number nnSub (number a, number b, const coeffs r)
174 {
177  coeffs_array d=(coeffs_array)r->data;
178  int i=0;
179  loop
180  {
181  i++;
182  if (d[i]==NULL) break;
183  }
184  number *C=(number*)omAlloc(i*sizeof(number));
185  i=0;
186  loop
187  {
188  C[i]=d[i]->cfSub(A[i],B[i],d[i]);
189  i++;
190  if (d[i]==NULL) return (number)C;
191  }
192 }
193 
194 static BOOLEAN nnIsZero (number a, const coeffs r)
195 {
197  coeffs_array d=(coeffs_array)r->data;
198  int i=0;
199  loop
200  {
201  if (!d[i]->cfIsZero(A[i],d[i])) return FALSE;
202  i++;
203  if (d[i]==NULL) return TRUE;
204  }
205 }
206 
207 static BOOLEAN nnIsOne (number a, const coeffs r)
208 {
209  int i=0;
211  coeffs_array d=(coeffs_array)r->data;
212  loop
213  {
214  if (!d[i]->cfIsOne(A[i],d[i])) return FALSE;
215  i++;
216  if (d[i]==NULL) return TRUE;
217  }
218 }
219 
220 static BOOLEAN nnIsMOne (number a, const coeffs r)
221 {
222  int i=0;
224  coeffs_array d=(coeffs_array)r->data;
225  loop
226  {
227  if (!d[i]->cfIsMOne(A[i],d[i])) return FALSE;
228  i++;
229  if (d[i]==NULL) return TRUE;
230  }
231 }
232 
233 static number nnDiv (number a,number b, const coeffs r)
234 {
235  if (nnIsZero(b,r))
236  {
237  WerrorS(nDivBy0);
238  return nnInit(0,r);
239  }
242  coeffs_array d=(coeffs_array)r->data;
243  int i=0;
244  loop
245  {
246  i++;
247  if (d[i]==NULL) break;
248  }
249  number *C=(number*)omAlloc(i*sizeof(number));
250  i=0;
251  loop
252  {
253  C[i]=d[i]->cfDiv(A[i],B[i],d[i]);
254  i++;
255  if (d[i]==NULL) return (number)C;
256  }
257 }
258 
259 static number nnInvers (number a, const coeffs r)
260 {
261  if (nnIsZero(a,r))
262  {
263  WerrorS(nDivBy0);
264  return nnInit(0,r);
265  }
267  coeffs_array d=(coeffs_array)r->data;
268  int i=0;
269  loop
270  {
271  i++;
272  if (d[i]==NULL) break;
273  }
274  number *C=(number*)omAlloc(i*sizeof(number));
275  i=0;
276  loop
277  {
278  C[i]=d[i]->cfInvers(A[i],d[i]);
279  i++;
280  if (d[i]==NULL) return (number)C;
281  }
282 }
283 
284 static number nnInpNeg (number a, const coeffs r)
285 {
287  coeffs_array d=(coeffs_array)r->data;
288  int i=0;
289  loop
290  {
291  i++;
292  if (d[i]==NULL) break;
293  }
294  i=0;
295  loop
296  {
297  A[i]=d[i]->cfInpNeg(A[i],d[i]);
298  i++;
299  if (d[i]==NULL) return (number)A;
300  }
301 }
302 
303 static BOOLEAN nnGreater (number a,number b, const coeffs r)
304 {
307  coeffs_array d=(coeffs_array)r->data;
308  int i=0;
309  loop
310  {
311  if (d[i]->type==n_R) return d[i]->cfGreater(A[i],B[i],d[i]);
312  else if (d[i]->type==n_long_R) return d[i]->cfGreater(A[i],B[i],d[i]);
313  i++;
314  if (d[i]==NULL)
315  {
316  return d[i-1]->cfGreater(A[i],B[i],d[i-1]);
317  }
318  }
319 }
320 
321 static BOOLEAN nnEqual (number a,number b, const coeffs r)
322 {
323  int i=0;
326  coeffs_array d=(coeffs_array)r->data;
327  loop
328  {
329  if (!d[i]->cfEqual(A[i],B[i],d[i])) return FALSE;
330  i++;
331  if (d[i]==NULL) return TRUE;
332  }
333 }
334 
335 static void nnWriteShort (number a, const coeffs r)
336 {
338  coeffs_array d=(coeffs_array)r->data;
339  int i=0;
340  loop
341  {
342  if (d[i]->type==n_R) { d[i]->cfWriteLong(A[i],d[i]); return; }
343  else if (d[i]->type==n_long_R) {d[i]->cfWriteLong(A[i],d[i]); return; }
344  i++;
345  if (d[i]==NULL) { d[i-1]->cfWriteLong(A[i-1],d[i-1]); return; }
346  }
347 }
348 
349 static void nnWriteLong (number a, const coeffs r)
350 {
352  coeffs_array d=(coeffs_array)r->data;
353  int i=0;
354  StringSetS("(");
355  loop
356  {
357  d[i]->cfWriteLong(A[i],d[i]);
358  i++;
359  if (d[i]==NULL) {StringAppendS(")"); return; }
360  else StringAppendS(",");
361  }
362 }
363 
364 #if 0
365 void nrPower (number a, int i, number * result, const coeffs r)
366 {
367  assume( getCoeffType(r) == n_R );
368 
369  if (i==0)
370  {
371  *result = nf(nf(1.0).F()).N();
372  return;
373  }
374  if (i==1)
375  {
376  *result = nf(nf(a).F()).N();
377  return;
378  }
379  nrPower(a,i-1,result,r);
380  *result = nf(nf(a).F() * nf(*result).F()).N();
381 }
382 #endif
383 
384 static const char * nnRead (const char *s, number *a, const coeffs r)
385 {
386  // reading R/longR part, setting everything else to random
387  coeffs_array d=(coeffs_array)r->data;
388  int i=0;
389  loop
390  {
391  i++;
392  if (d[i]==NULL) break;
393  }
394  number *C=(number*)omAlloc0(i*sizeof(number));
395  *a=(number)C;
396  int is_zero=2; /*1: read 0, 2: read <> 0 */
397  int reader=-1;
398  i=0;
399  loop
400  {
401  if ((d[i]->type==n_R)
402  || (d[i]->type==n_long_R))
403  {
404  s=d[i]->cfRead(s,&(C[i]),d[i]);
405  is_zero=d[i]->cfIsZero(C[i],d[i]);
406  reader=i;
407  break;
408  }
409  i++;
410  if (d[i]==NULL)
411  {
412  s=d[i-1]->cfRead(s,&(C[i-1]),d[i-1]);
413  is_zero=d[i-1]->cfIsZero(C[i-1],d[i-1]);
414  reader=i-1;
415  break;
416  }
417  }
418  if (is_zero==0)
419  { // seting random values!=0 for Z/p
420  i=0;
421  loop
422  {
423  if (i!=reader)
424  {
425  if (d[i]->type==n_Zp)
426  {
427  int l=0;
428  do
429  {
430  l=siRand();
431  } while(l%(d[i]->ch)==0);
432  C[i]=d[i]->cfInit(l,d[i]);
433  }
434  else if ((d[i]->type==n_R)
435  || (d[i]->type==n_long_R))
436  {
437  nMapFunc nMap=d[i]->cfSetMap(d[reader],d[i]);
438  C[i]=nMap(C[reader],d[reader],d[i]);
439  }
440  else
441  {
442  WerrorS("reading is not suppiorted for such compinations of coeffs");
443  break;
444  }
445  }
446  i++;
447  if (d[i]==NULL) break;
448  }
449  }
450  else if (is_zero==1)
451  { // read a zero
452  i=0;
453  loop
454  {
455  C[i]=d[i]->cfInit(0,d[i]);
456  i++;
457  if (d[i]==NULL) break;
458  }
459  }
460  else if (reader==-1)/* not R or long_R*/
461  {
462  s=d[0]->cfRead(s,&(C[0]),d[0]);
463  i=1;
464  loop
465  {
466  if (d[i]==NULL) break;
467  i++;
468  {
469  nMapFunc nMap=d[i]->cfSetMap(d[0],d[i]);
470  C[i]=nMap(C[0],d[0],d[i]);
471  }
472  }
473  }
474  else
475  {
476  WerrorS("nnRead: should not happen");
477  }
478  return s;
479 }
480 
481 #ifdef LDEBUG
482 /*2
483 * test valid numbers: not implemented yet
484 */
485 BOOLEAN nnDBTest(number a, const char *f, const int l, const coeffs r)
486 {
487  BOOLEAN bo=FALSE;
489  coeffs_array d=(coeffs_array)r->data;
490  int i=0;
491  loop
492  {
493  bo&=d[i]->cfDBTest(A[i],f,l,d[i]);
494  i++;
495  if (d[i]==NULL) return bo;
496  }
497 }
498 #endif
499 
500 static number nnMap(number from, const coeffs aRing, const coeffs r)
501 {
502  coeffs_array d=(coeffs_array)r->data;
503  int i=0;
504  loop
505  {
506  i++;
507  if (d[i]==NULL) break;
508  }
509  number *C=(number*)omAlloc(i*sizeof(number));
510  nMapFunc nMap;
511  i=0;
512  loop
513  {
514  nMap=d[i]->cfSetMap(aRing,d[i]);
515  if (nMap==NULL) Werror("no map for compoment %d",i);
516  else C[i]=nMap(from,aRing,d[i]);
517  i++;
518  if (d[i]==NULL) return (number)C;
519  }
520 }
521 
522 nMapFunc nnSetMap(const coeffs src, const coeffs dst)
523 {
524  return nnMap;
525 }
526 
527 static char* nnCoeffString(const coeffs r)
528 {
529  #define SI_BUF_SIZE 1024
530  char buf[SI_BUF_SIZE];
531  coeffs_array d=(coeffs_array)r->data;
532  int i=0;
533  buf[0]='\0';
534  loop
535  {
536  char *s=d[i]->cfCoeffName(d[i]);
537  strncat(buf,s, SI_BUF_SIZE-strlen(buf)-strlen(s)-1);
538  i++;
539  if (d[i]==NULL) break;
540  strncat(buf," x ", SI_BUF_SIZE-strlen(buf)-4);
541  }
542  return omStrDup(buf);
543 }
544 
547 static char* nnCoeffName(const coeffs r)
548 {
549  coeffs_array d=(coeffs_array)r->data;
550  int i=0;
551  nnCoeffName_buf[0]='\0';
552  loop
553  {
554  char *s=d[i]->cfCoeffName(d[i]);
555  strncat(nnCoeffName_buf,s, SI_BUF_SIZE-strlen(nnCoeffName_buf)-strlen(s)-1);
556  i++;
557  if (d[i]==NULL) break;
558  strncat(nnCoeffName_buf," x ", SI_BUF_SIZE-strlen(nnCoeffName_buf)-4);
559  }
560  return nnCoeffName_buf;
561 }
562 
563 static void nnDelete (number * a, const coeffs r)
564 {
565  if (*a!=NULL)
566  {
567  coeffs_array d=(coeffs_array)r->data;
569  int i=0;
570  loop
571  {
572  d[i]->cfDelete(&(A[i]),d[i]);
573  i++;
574  if (d[i]==NULL) break;
575  }
576  omFreeSize(d,i*sizeof(number));
577  *a=NULL;
578  }
579 }
580 
581 
582 static number nnChineseRemainder(number *x, number *q,int rl, BOOLEAN sym, CFArray &inv_cache,const coeffs CF)
583 {
584  WerrorS("not yet");
585  return NULL;
586 }
587 
588 
590 {
591  n->data=p;
592  n->type=n_nTupel;
593  n->is_field=TRUE;
594  n->is_domain=TRUE;
595 
596  n->ch = 0;
597  n->cfCoeffString = nnCoeffString;
598  n->cfCoeffName = nnCoeffName;
599 
600  n->cfInit = nnInit;
601  n->cfDelete = nnDelete;
602  n->cfInt = nnInt;
603  n->cfAdd = nnAdd;
604  n->cfSub = nnSub;
605  n->cfMult = nnMult;
606  n->cfDiv = nnDiv;
607  n->cfExactDiv= nnDiv;
608  n->cfInpNeg = nnInpNeg;
609  n->cfInvers= nnInvers;
610  n->cfCopy = nnCopy;
611  n->cfGreater = nnGreater;
612  n->cfEqual = nnEqual;
613  n->cfIsZero = nnIsZero;
614  n->cfIsOne = nnIsOne;
615  n->cfIsMOne = nnIsMOne;
616  n->cfGreaterZero = nnGreaterZero;
617  n->cfWriteShort = nnWriteShort;
618  n->cfWriteLong = nnWriteLong;
619  n->cfRead = nnRead;
620  //n->cfPower = nnPower;
621  n->cfSetMap = nnSetMap;
622  n->cfCoeffWrite = nnCoeffWrite;
623 
624  n->cfChineseRemainder=nnChineseRemainder;
625  n->cfSize = nnSize;
626 #ifdef LDEBUG
627  n->cfDBTest=nnDBTest;
628 #endif
629  //n->nCoeffIsEqual = ndCoeffIsEqual;
630  return FALSE;
631 }
All the auxiliary stuff.
int BOOLEAN
Definition: auxiliary.h:85
#define TRUE
Definition: auxiliary.h:98
#define FALSE
Definition: auxiliary.h:94
int l
Definition: cfEzgcd.cc:93
int i
Definition: cfEzgcd.cc:125
int k
Definition: cfEzgcd.cc:92
Variable x
Definition: cfModGcd.cc:4023
int p
Definition: cfModGcd.cc:4019
CanonicalForm b
Definition: cfModGcd.cc:4044
FILE * f
Definition: checklibs.c:9
Coefficient rings, fields and other domains suitable for Singular polynomials.
@ n_R
single prescision (6,6) real numbers
Definition: coeffs.h:32
@ n_long_R
real floating point (GMP) numbers
Definition: coeffs.h:34
@ n_Zp
\F{p < 2^31}
Definition: coeffs.h:30
@ n_nTupel
n-tupel of cf: ZZ/p1,...
Definition: coeffs.h:43
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
Definition: coeffs.h:422
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition: coeffs.h:74
return result
Definition: facAbsBiFact.cc:76
const CanonicalForm int s
Definition: facAbsFact.cc:55
b *CanonicalForm B
Definition: facBivar.cc:52
void WerrorS(const char *s)
Definition: feFopen.cc:24
if(yy_init)
Definition: libparse.cc:1418
#define assume(x)
Definition: mod2.h:390
The main handler for Singular numbers which are suitable for Singular polynomials.
static char * nnCoeffName(const coeffs r)
Definition: ntupel.cc:547
static number nnMap(number from, const coeffs aRing, const coeffs r)
Definition: ntupel.cc:500
static BOOLEAN nnIsZero(number a, const coeffs r)
Definition: ntupel.cc:194
coeffs * coeffs_array
Definition: ntupel.cc:25
static number nnChineseRemainder(number *x, number *q, int rl, BOOLEAN sym, CFArray &inv_cache, const coeffs CF)
Definition: ntupel.cc:582
static BOOLEAN nnIsMOne(number a, const coeffs r)
Definition: ntupel.cc:220
static number nnCopy(number a, const coeffs r)
Definition: ntupel.cc:132
static number nnDiv(number a, number b, const coeffs r)
Definition: ntupel.cc:233
static number nnAdd(number a, number b, const coeffs r)
Definition: ntupel.cc:152
#define SI_BUF_SIZE
static void nnDelete(number *a, const coeffs r)
Definition: ntupel.cc:563
number * number_array
Definition: ntupel.cc:26
static number nnInpNeg(number a, const coeffs r)
Definition: ntupel.cc:284
static void nnWriteShort(number a, const coeffs r)
Definition: ntupel.cc:335
static BOOLEAN nnEqual(number a, number b, const coeffs r)
Definition: ntupel.cc:321
static number nnInit(long l, const coeffs r)
Definition: ntupel.cc:79
static long nnInt(number &n, const coeffs r)
Definition: ntupel.cc:101
static si_char_SI_BUF_SIZE nnCoeffName_buf
Definition: ntupel.cc:546
static number nnMult(number a, number b, const coeffs r)
Definition: ntupel.cc:55
static BOOLEAN nnGreaterZero(number k, const coeffs r)
Definition: ntupel.cc:42
BOOLEAN nnInitChar(coeffs n, void *p)
Initialize r.
Definition: ntupel.cc:589
static BOOLEAN nnGreater(number a, number b, const coeffs r)
Definition: ntupel.cc:303
static number nnInvers(number a, const coeffs r)
Definition: ntupel.cc:259
static number nnSub(number a, number b, const coeffs r)
Definition: ntupel.cc:173
static void nnCoeffWrite(const coeffs r, BOOLEAN b)
Definition: ntupel.cc:28
static BOOLEAN nnIsOne(number a, const coeffs r)
Definition: ntupel.cc:207
static int nnSize(number n, const coeffs r)
Definition: ntupel.cc:114
char si_char_SI_BUF_SIZE[SI_BUF_SIZE]
Definition: ntupel.cc:545
static void nnWriteLong(number a, const coeffs r)
Definition: ntupel.cc:349
static const char * nnRead(const char *s, number *a, const coeffs r)
Definition: ntupel.cc:384
static char * nnCoeffString(const coeffs r)
Definition: ntupel.cc:527
nMapFunc nnSetMap(const coeffs src, const coeffs dst)
Definition: ntupel.cc:522
BOOLEAN nnDBTest(number a, const char *f, const int l, const coeffs r)
Definition: ntupel.cc:485
const char *const nDivBy0
Definition: numbers.h:89
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define NULL
Definition: omList.c:10
void StringSetS(const char *st)
Definition: reporter.cc:128
void StringAppendS(const char *st)
Definition: reporter.cc:107
void PrintS(const char *s)
Definition: reporter.cc:284
void Werror(const char *fmt,...)
Definition: reporter.cc:189
int status int void * buf
Definition: si_signals.h:59
int siRand()
Definition: sirandom.c:41
#define A
Definition: sirandom.c:23
#define loop
Definition: structs.h:78
Definition: gnumpfl.cc:28
SI_FLOAT F() const
Definition: gnumpfl.cc:33
number N() const
Definition: gnumpfl.cc:34