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