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