source: git/IntegerProgramming/binomial.cc @ cc4553

spielwiese
Last change on this file since cc4553 was ce3dda, checked in by Hans Schönemann <hannes@…>, 18 years ago
*hannes: solaris port git-svn-id: file:///usr/local/Singular/svn/trunk@8917 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 42.6 KB
Line 
1// binomial.cc
2
3// implementation of class binomial
4
5#ifndef BINOMIAL_CC
6#define BINOMIAL_CC
7
8#include "binomial__term_ordering.h"
9
10///////////////////////// constructors and destructor //////////////////////
11
12// For a better overview, the constructor code is separated for
13// NO_SUPPORT_DRIVEN_METHODS and SUPPORT_DRIVEN_METHODS.
14
15#ifdef NO_SUPPORT_DRIVEN_METHODS
16
17binomial::binomial(const short& number_of_variables)
18    :_number_of_variables(number_of_variables)
19{
20  exponent_vector=new Integer[_number_of_variables];
21}
22
23
24
25
26binomial::binomial(const short& number_of_variables,const Integer* exponents)
27    :_number_of_variables(number_of_variables)
28{
29
30  // range check for rarely used constructors
31  if(_number_of_variables<=0)
32  {
33    cerr<<"\nWARNING: binomial::binomial(const short&, const Integer*):\n"
34      "argument out of range"<<endl;
35    exponent_vector=NULL;
36    // to avoid problems when deleting
37    return;
38  }
39
40  // initialization
41  exponent_vector=new Integer[_number_of_variables];
42  for(short i=0;i<_number_of_variables;i++)
43    exponent_vector[i]=exponents[i];
44}
45
46
47
48
49binomial::binomial(const short& number_of_variables,const Integer* exponents,
50                   const term_ordering& w)
51    :_number_of_variables(number_of_variables)
52{
53
54  // range check for rarely used constructors
55  if(_number_of_variables<=0)
56  {
57    cerr<<"\nWARNING: binomial::binomial(const short&, const Integer*):\n"
58      "argument out of range"<<endl;
59    exponent_vector=NULL;
60    // to avoid problems when deleting
61    return;
62  }
63
64  exponent_vector=new Integer[_number_of_variables];
65
66  // determine head and tail
67  if(w.compare_to_zero(exponents)>=0)
68    for(short i=0;i<_number_of_variables;i++)
69      exponent_vector[i]=exponents[i];
70  else
71    for(short i=0;i<_number_of_variables;i++)
72      exponent_vector[i]=-exponents[i];
73
74}
75
76
77
78
79binomial::binomial(const binomial& b)
80    :_number_of_variables(b._number_of_variables)
81{
82  exponent_vector=new Integer[_number_of_variables];
83  for(short i=0;i<_number_of_variables;i++)
84    exponent_vector[i]=b.exponent_vector[i];
85}
86
87
88
89
90#endif  // NO_SUPPORT_DRIVEN_METHODS
91
92
93
94
95#ifdef SUPPORT_DRIVEN_METHODS
96
97
98
99
100binomial::binomial(const short& number_of_variables)
101    :_number_of_variables(number_of_variables),head_support(0),tail_support(0)
102{
103  exponent_vector=new Integer[_number_of_variables];
104}
105
106
107
108
109binomial::binomial(const short& number_of_variables, const Integer* exponents)
110    :_number_of_variables(number_of_variables),head_support(0),tail_support(0)
111{
112
113  // range check for rarely used constructors
114  if(_number_of_variables<=0)
115  {
116    exponent_vector=NULL;
117    // to avoid problems when deleting
118    cerr<<"\nWARNING: binomial::binomial(const short&, const Integer*):\n"
119      "argument out of range"<<endl;
120    return;
121  }
122
123  exponent_vector=new Integer[_number_of_variables];
124
125  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
126  // number of bits of a long int
127
128
129  for(short i=0;i<_number_of_variables;i++)
130  {
131
132#ifdef SUPPORT_VARIABLES_FIRST
133
134    Integer actual_entry=exponents[i];
135    exponent_vector[i]=actual_entry;
136
137#endif  // SUPPORT_VARIABLES_FIRST
138
139#ifdef SUPPORT_VARIABLES_LAST
140
141    short j=_number_of_variables-1-i;
142    Integer actual_entry=exponents[j];
143    exponent_vector[j]=actual_entry;
144
145#endif  // SUPPORT_VARIABLES_LAST
146
147    if(i<size_of_support_vectors)
148      // variable i is considered in the support vectors
149      if(actual_entry>0)
150        head_support|=(1<<i);
151        // bit i of head_support is set to 1 (counting from 0)
152      else
153        if(actual_entry<0)
154          tail_support|=(1<<i);
155    // bit i of tail_support is set to 1
156  }
157
158}
159
160
161
162
163binomial::binomial(const short& number_of_variables, const Integer* exponents,
164                   const term_ordering& w)
165    :_number_of_variables(number_of_variables),head_support(0),tail_support(0)
166{
167  // range check for rarely used constructors
168  if(_number_of_variables<=0)
169  {
170    cerr<<"\nWARNING: binomial::binomial(const short&, const Integer*):\n"
171      "argument out of range"<<endl;
172    exponent_vector=NULL;
173    // to avoid problems when deleting
174    return;
175  }
176
177
178  exponent_vector=new Integer[_number_of_variables];
179
180  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
181  // number of bits of a long int
182
183  // determine head and tail
184  short sign;
185  if(w.compare_to_zero(exponents)>=0)
186    sign=1;
187  else
188    sign=-1;
189
190
191  for(short i=0;i<_number_of_variables;i++)
192  {
193
194#ifdef SUPPORT_VARIABLES_FIRST
195
196    Integer actual_entry=sign*exponents[i];
197    exponent_vector[i]=actual_entry;
198
199#endif  // SUPPORT_VARIABLES_FIRST
200
201#ifdef SUPPORT_VARIABLES_LAST
202
203    short j=_number_of_variables-1-i;
204    Integer actual_entry=sign*exponents[j];
205    exponent_vector[j]=actual_entry;
206
207#endif  // SUPPORT_VARIABLES_LAST
208
209    if(i<size_of_support_vectors)
210      // variable i is considered in the support vectors
211      if(actual_entry>0)
212        head_support|=(1<<i);
213        // bit i of head_support is set to 1 (counting from 0)
214      else
215        if(actual_entry<0)
216          tail_support|=(1<<i);
217    // bit i of tail_support is set to 1
218  }
219
220}
221
222
223
224
225binomial::binomial(const binomial& b)
226    :_number_of_variables(b._number_of_variables),
227     head_support(b.head_support),tail_support(b.tail_support)
228{
229  exponent_vector=new Integer[_number_of_variables];
230  for(short i=0;i<_number_of_variables;i++)
231    exponent_vector[i]=b.exponent_vector[i];
232}
233
234
235
236
237#endif  // SUPPORT_DRIVEN_METHODS
238
239
240
241
242binomial::~binomial()
243{
244  delete[] exponent_vector;
245}
246
247
248
249
250/////////////////// object information /////////////////////////////////////
251
252
253
254
255short binomial::number_of_variables() const
256{
257  return _number_of_variables;
258}
259
260
261
262
263short binomial::error_status() const
264{
265  if(_number_of_variables<0)
266    return _number_of_variables;
267  return 0;
268}
269
270
271
272
273//////////////////// assignment and access operators ////////////////////////
274
275
276
277
278binomial& binomial::operator=(const binomial& b)
279{
280
281  if(&b==this)
282    return *this;
283
284
285#ifdef SUPPORT_DRIVEN_METHODS
286
287  head_support=b.head_support;
288  tail_support=b.tail_support;
289
290#endif  // SUPPORT_DRIVEN_METHODS
291
292  if(_number_of_variables!=b._number_of_variables)
293  {
294    delete[] exponent_vector;
295    _number_of_variables=b._number_of_variables;
296
297    if(_number_of_variables<=0)
298    {
299      cerr<<"\nWARNING: binomial& binomial::operator=(const binomial&):\n"
300        "assigment from corrupt binomial"<<endl;
301      exponent_vector=NULL;
302      return (*this);
303    }
304
305    exponent_vector=new Integer[_number_of_variables];
306  }
307
308  for(short i=0;i<_number_of_variables;i++)
309    exponent_vector[i]=b.exponent_vector[i];
310
311  return(*this);
312}
313
314
315
316
317Integer binomial::operator[](const short& i) const
318{
319  return exponent_vector[i];
320}
321
322
323
324
325//////////////////// comparison operators ///////////////////////////////////
326
327
328
329
330BOOLEAN binomial::operator==(const binomial& b) const
331{
332  if(this == &b)
333    return(TRUE);
334
335#ifdef SUPPORT_DRIVEN_METHODS
336
337  if(head_support!=b.head_support)
338    return(FALSE);
339  if(tail_support!=b.tail_support)
340    return(FALSE);
341
342#endif  // SUPPORT_DRIVEN_METHODS
343
344  for(short i=0;i<_number_of_variables;i++)
345    if(exponent_vector[i]!=b.exponent_vector[i])
346      return(FALSE);
347  return(TRUE);
348}
349
350
351
352
353BOOLEAN binomial::operator!=(const binomial& b) const
354{
355  if(this == &b)
356    return(FALSE);
357
358#ifdef SUPPORT_DRIVEN_METHODS
359
360  if(head_support!=b.head_support)
361    return(TRUE);
362  if(tail_support!=b.tail_support)
363    return(TRUE);
364
365#endif  // SUPPORT_DRIVEN_METHODS
366
367  for(short i=0;i<_number_of_variables;i++)
368    if(exponent_vector[i]!=b.exponent_vector[i])
369      return(TRUE);
370  return(FALSE);
371}
372
373
374
375
376// operators for efficient comparisons with the zero binomial (comp_value=0)
377
378BOOLEAN binomial::operator==(const Integer comp_value) const
379{
380
381#ifdef SUPPORT_DRIVEN_METHODS
382
383  if(comp_value==0)
384  {
385    if(head_support!=0)
386      return(FALSE);
387    if(tail_support!=0)
388      return(FALSE);
389  }
390
391#endif  // SUPPORT_DRIVEN_METHODS
392
393  for(short i=0;i<_number_of_variables;i++)
394    if(exponent_vector[i]!=comp_value)
395      return(FALSE);
396  return(TRUE);
397}
398
399
400
401
402BOOLEAN binomial::operator!=(const Integer comp_value) const
403{
404
405#ifdef SUPPORT_DRIVEN_METHODS
406
407  if(comp_value==0)
408  {
409    if(head_support!=0)
410      return(TRUE);
411    if(tail_support!=0)
412      return(TRUE);
413  }
414
415#endif  // SUPPORT_DRIVEN_METHODS
416
417  for(short i=0;i<_number_of_variables;i++)
418    if(exponent_vector[i]!=comp_value)
419      return(TRUE);
420  return(FALSE);
421}
422
423
424
425
426BOOLEAN binomial::operator<=(const Integer comp_value) const
427{
428
429#ifdef SUPPORT_DRIVEN_METHODS
430
431  if(comp_value==0)
432    if(head_support!=0)
433      return(FALSE);
434
435#endif  // SUPPORT_DRIVEN_METHODS
436
437  for(short i=0;i<_number_of_variables;i++)
438    if(exponent_vector[i]>comp_value)
439      return(FALSE);
440  return(TRUE);
441}
442
443
444
445
446BOOLEAN binomial::operator>=(const Integer comp_value) const
447{
448
449#ifdef SUPPORT_DRIVEN_METHODS
450
451  if(comp_value==0)
452    if(tail_support!=0)
453      return(FALSE);
454
455#endif
456
457  for(short i=0;i<_number_of_variables;i++)
458    if(exponent_vector[i]<comp_value)
459      return(FALSE);
460  return(TRUE);
461}
462
463
464
465
466////////////// basic routines for Buchbergers's algorithm //////////////////
467
468
469
470
471Integer binomial::head_reductions_by(const binomial& b) const
472// Returns the number of possible reductions of the actual binomialŽs head
473// by the binomial b. This is the minimum of the quotients
474// exponent_vector[i]/b.exponent_vector[i]
475// where exponent_vector[i]>0 and b.exponent_vector[i]>0
476// (0 if there are no such quotients).
477// A negative return value means b=0 or head(b)=1.
478{
479
480
481#ifdef NO_SUPPORT_DRIVEN_METHODS
482
483  Integer result=-1;
484  Integer new_result=-1;
485  // -1 stands for infinitely many reductions
486
487  for(short i=0;i<_number_of_variables;i++)
488    // explicit sign tests for all components
489  {
490    Integer actual_b_component=b.exponent_vector[i];
491
492    if(actual_b_component>0)
493      // else variable i is not involved in the head of b
494    {
495      Integer actual_component=exponent_vector[i];
496
497      if(actual_component<actual_b_component)
498        return 0;
499
500      new_result=(Integer) (actual_component/actual_b_component);
501
502      // new_result>=1
503      if((new_result<result) || (result==-1))
504        // new (or first) minimum
505        result=new_result;
506    }
507 }
508
509#endif  // NO_SUPPORT_DRIVEN_METHODS
510
511
512#ifdef SUPPORT_DRIVEN_METHODS
513
514  if((head_support&b.head_support)!=b.head_support)
515    // head support of b not contained in head support, no reduction possible
516    return 0;
517
518
519  Integer result=-1;
520  Integer new_result=-1;
521  // -1 stands for infinitely many reductions
522
523
524  short size_of_support_vectors=CHAR_BIT*sizeof(long);
525  // number of bits of a long int
526  if(size_of_support_vectors>_number_of_variables)
527    size_of_support_vectors=_number_of_variables;
528    // number of components of the support vectors
529
530
531#ifdef SUPPORT_VARIABLES_FIRST
532
533  for(short i=0;i<size_of_support_vectors;i++)
534    // test support variables
535
536    if(b.head_support&(1<<i))
537      // bit i of b.head_support is 1
538    {
539      new_result=(Integer) (exponent_vector[i]/b.exponent_vector[i]);
540      // remember that exponent_vector[i]>0 !
541      // (head support contains that of b)
542
543      if(new_result==0)
544        // exponent_vector[i]<b.exponent_vector[i]
545        return 0;
546
547      // new_result>=1
548      if((new_result<result) || (result==-1))
549        // new (or first) minimum
550        result=new_result;
551    }
552
553
554  for(short i=size_of_support_vectors;i<_number_of_variables;i++)
555    // test non-support variables
556    // from now on we need explicit sign tests
557  {
558    Integer actual_b_component=b.exponent_vector[i];
559
560    if(actual_b_component>0)
561      // else variable i is not involved in the head of b
562    {
563      Integer actual_component=exponent_vector[i];
564
565      if(actual_component<actual_b_component)
566        return 0;
567
568      new_result=(Integer) (actual_component/actual_b_component);
569
570      // new_result>=1
571      if((new_result<result) || (result==-1))
572        // new (or first) minimum
573        result=new_result;
574    }
575  }
576
577#endif  // SUPPORT_VARIABLES_FIRST
578
579
580#ifdef SUPPORT_VARIABLES_LAST
581
582  for(short i=0;i<size_of_support_vectors;i++)
583    // test support variables
584
585    if(b.head_support&(1<<i))
586      // bit i of b.head_support is 1
587    {
588      short j=_number_of_variables-1-i;
589      new_result=(Integer) (exponent_vector[j]/ b.exponent_vector[j]);
590      // remember that exponent_vector[_number_of_variables-1-i]>0 !
591      // (head support contains that of b)
592
593      if(new_result==0)
594        // exponent_vector[_number_of_variables-1-i]
595        // <b.exponent_vector[_number_of_variables-1-i]
596        return 0;
597
598      // new_result>=1
599      if((new_result<result) || (result==-1))
600        // new (or first) minimum
601        result=new_result;
602    }
603
604
605  for(short i=size_of_support_vectors;i<_number_of_variables;i++)
606    // test non-support variables
607    // from now on we need explicit sign tests
608  {
609    short j=_number_of_variables-1-i;
610    Integer actual_b_component=b.exponent_vector[j];
611
612    if(actual_b_component>0)
613      // else variable number_of_variables-1-i is not involved in the head of b
614    {
615      Integer actual_component=exponent_vector[j];
616
617      if(actual_component<actual_b_component)
618        return 0;
619
620      new_result=(Integer) (actual_component/actual_b_component);
621
622      // new_result>=1
623      if((new_result<result) || (result==-1))
624        // new (or first) minimum
625        result=new_result;
626    }
627  }
628
629#endif  // SUPPORT_VARIABLES_LAST
630
631
632#endif  // SUPPORT_DRIVEN_METHODS
633
634
635  return(result);
636}
637
638
639
640
641Integer binomial::tail_reductions_by(const binomial& b) const
642// Returns the number of possible reductions of the actual binomialŽs tail
643// by the binomial b. This is the minimum of the quotients
644// - exponent_vector[i]/b.exponent_vector[i]
645// where exponent_vector[i]<0 and b.exponent_vector[i]>0
646// (0 if there are no such quotients).
647// A negative return value means b=0 or head(b)=1.
648{
649
650
651#ifdef NO_SUPPORT_DRIVEN_METHODS
652
653  Integer result=-1;
654  Integer new_result=-1;
655  // -1 stands for infinitely many reductions
656
657  for(short i=0;i<_number_of_variables;i++)
658    // explicit sign tests for all components
659  {
660    Integer actual_b_component=b.exponent_vector[i];
661
662    if(actual_b_component>0)
663      // else variable i is not involved in the head of b
664    {
665      Integer actual_component=-exponent_vector[i];
666
667      if(actual_component<actual_b_component)
668        return 0;
669
670      new_result=(Integer) (actual_component/actual_b_component);
671
672      // new_result>=1
673      if((new_result<result) || (result==-1))
674        // new (or first) minimum
675        result=new_result;
676    }
677 }
678
679#endif  // NO_SUPPORT_DRIVEN_METHODS
680
681
682#ifdef SUPPORT_DRIVEN_METHODS
683
684  if((tail_support&b.head_support)!=b.head_support)
685    // head support of b not contained in tail support, no reduction possible
686    return 0;
687
688
689  Integer result=-1;
690  Integer new_result=-1;
691  // -1 stands for infinitely many reductions
692
693
694  short size_of_support_vectors=CHAR_BIT*sizeof(long);
695  // number of bits of a long int
696  if(size_of_support_vectors>_number_of_variables)
697    size_of_support_vectors=_number_of_variables;
698    // number of components of the support vectors
699
700
701#ifdef SUPPORT_VARIABLES_FIRST
702
703  for(short i=0;i<size_of_support_vectors;i++)
704    // test support variables
705
706    if(b.head_support&(1<<i))
707      // bit i of b.head_support is 1
708    {
709      new_result=(Integer) (-exponent_vector[i]/b.exponent_vector[i]);
710      // remember that exponent_vector[i]<0 !
711      // (tail support contains the head support of b)
712
713      if(new_result==0)
714        // -exponent_vector[i]<b.exponent_vector[i]
715        return 0;
716
717      // new_result>=1
718      if((new_result<result) || (result==-1))
719        // new (or first) minimum
720        result=new_result;
721    }
722
723
724  for(short i=size_of_support_vectors;i<_number_of_variables;i++)
725    // test non-support variables
726    // from now on we need explicit sign tests
727  {
728    Integer actual_b_component=b.exponent_vector[i];
729
730    if(actual_b_component>0)
731      // else variable i is not involved in the head of b
732    {
733      Integer actual_component=-exponent_vector[i];
734
735      if(actual_component<actual_b_component)
736        return 0;
737
738      new_result=(Integer) (actual_component/actual_b_component);
739
740      // new_result>=1
741      if((new_result<result) || (result==-1))
742        // new (or first) minimum
743        result=new_result;
744    }
745  }
746
747#endif  // SUPPORT_VARIABLES_FIRST
748
749
750#ifdef SUPPORT_VARIABLES_LAST
751
752  for(short i=0;i<size_of_support_vectors;i++)
753    // test support variables
754
755    if(b.head_support&(1<<i))
756      // bit i of b.head_support is 1
757    {
758      short j=_number_of_variables-1-i;
759      new_result=(Integer) (-exponent_vector[j] / b.exponent_vector[j]);
760      // remember that exponent_vector[_number_of_variables-1-i]<0 !
761      // (tail support contains the head support of b)
762
763      if(new_result==0)
764        // -exponent_vector[_number_of_variables-1-i]
765        // <b.exponent_vector[_number_of_variables-1-i]
766        return 0;
767
768      // new_result>=1
769      if((new_result<result) || (result==-1))
770        // new (or first) minimum
771        result=new_result;
772    }
773
774
775  for(short i=size_of_support_vectors;i<_number_of_variables;i++)
776    // test non-support variables
777    // from now on we need explicit sign tests
778  {
779    short j=_number_of_variables-1-i;
780    Integer actual_b_component=b.exponent_vector[j];
781
782    if(actual_b_component>0)
783      // else variable number_of_variables-1-i is not involved in the head of b
784    {
785      Integer actual_component=-exponent_vector[j];
786
787      if(actual_component<actual_b_component)
788        return 0;
789
790      new_result=(Integer) (actual_component/actual_b_component);
791
792      // new_result>=1
793      if((new_result<result) || (result==-1))
794        // new (or first) minimum
795        result=new_result;
796    }
797  }
798
799#endif  // SUPPORT_VARIABLES_LAST
800
801
802#endif  // SUPPORT_DRIVEN_METHODS
803
804
805  return(result);
806}
807
808
809
810
811int binomial::reduce_head_by(const binomial& b, const term_ordering& w)
812{
813  Integer reduction_number=head_reductions_by(b);
814  if(reduction_number<=0)
815    return 0;
816
817  for(short i=0;i<_number_of_variables;i++)
818    exponent_vector[i]-=(reduction_number * b.exponent_vector[i]);
819  // multiple reduction
820  // reduction corresponds to subtraction of vectors
821
822  short sign=w.compare_to_zero(exponent_vector);
823
824
825#ifdef NO_SUPPORT_DRIVEN_METHODS
826
827  if(sign==0)
828    // binomial reduced to zero
829    return 2;
830
831  for(short i=0;i<_number_of_variables;i++)
832    exponent_vector[i]*=sign;
833
834#endif  // NO_SUPPORT_DRIVEN_METHODS
835
836
837#ifdef SUPPORT_DRIVEN_METHODS
838
839  head_support=0;
840  tail_support=0;
841
842  if(sign==0)
843    // binomial reduced to zero
844    return 2;
845
846  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
847
848
849  // recompute the support vectors
850
851#ifdef SUPPORT_VARIABLES_FIRST
852
853  for(short i=0;i<_number_of_variables;i++)
854  {
855
856    Integer& actual_entry=exponent_vector[i];
857    // to avoid unnecessary pointer arithmetic
858
859    actual_entry*=sign;
860
861    if(i<size_of_support_vectors)
862      if(actual_entry>0)
863        head_support|=(1<<i);
864      else
865        if(actual_entry<0)
866          tail_support|=(1<<i);
867  }
868
869#endif  // SUPPORT_VARIABLES_FIRST
870
871
872#ifdef SUPPORT_VARIABLES_LAST
873
874  for(short i=0;i<_number_of_variables;i++)
875  {
876    Integer& actual_entry=exponent_vector[_number_of_variables-1-i];
877    // to avoid unneccessary pointer arithmetic
878
879    actual_entry*=sign;
880
881    if(i<size_of_support_vectors)
882      if(actual_entry>0)
883        head_support|=(1<<i);
884      else
885        if(actual_entry<0)
886          tail_support|=(1<<i);
887  }
888
889#endif  // SUPPORT_VARIABLES_LAST
890
891
892#endif  // SUPPORT_DRIVEN_METHODS
893
894  return 1;
895}
896
897
898
899
900int binomial::reduce_tail_by(const binomial& b, const term_ordering& w)
901{
902  Integer reduction_number=tail_reductions_by(b);
903  if(reduction_number<=0)
904    return 0;
905
906  for(short i=0;i<_number_of_variables;i++)
907    exponent_vector[i]+=(reduction_number * b.exponent_vector[i]);
908  // multiple reduction
909  // reduction corresponds to addition of vectors
910
911  // a tail reduction does not require a sign check
912
913
914#ifdef SUPPORT_DRIVEN_METHODS
915
916  head_support=0;
917  tail_support=0;
918
919  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
920
921
922  // recompute the support vectors
923
924#ifdef SUPPORT_VARIABLES_FIRST
925
926  for(short i=0;i<_number_of_variables;i++)
927  {
928
929    Integer& actual_entry=exponent_vector[i];
930    // to avoid unnecessary pointer arithmetic
931
932    if(i<size_of_support_vectors)
933      if(actual_entry>0)
934        head_support|=(1<<i);
935      else
936        if(actual_entry<0)
937          tail_support|=(1<<i);
938  }
939
940#endif  // SUPPORT_VARIABLES_FIRST
941
942
943#ifdef SUPPORT_VARIABLES_LAST
944
945  for(short i=0;i<_number_of_variables;i++)
946  {
947    Integer& actual_entry=exponent_vector[_number_of_variables-1-i];
948    // to avoid unneccessary pointer arithmetic
949
950    if(i<size_of_support_vectors)
951      if(actual_entry>0)
952        head_support|=(1<<i);
953      else
954        if(actual_entry<0)
955          tail_support|=(1<<i);
956  }
957
958#endif  // SUPPORT_VARIABLES_LAST
959
960
961#endif  // SUPPORT_DRIVEN_METHODS
962
963  return 1;
964}
965
966
967
968
969binomial& S_binomial(const binomial& a, const binomial& b,
970                     const term_ordering& w)
971{
972  binomial* S_bin=new binomial(a._number_of_variables);
973  binomial& result=*S_bin;
974  // Note that we allocate memory for the result binomial. We often use
975  // pointers or references as argument and return types because the
976  // generating binomials of an ideal are kept in lists. For the performance
977  // of Buchberger's algorithm it it very important to avoid local copies
978  // of binomials, so a lot of attention is paid on the choice of argument
979  // and return types. As this choice is done in order to improve performance,
980  // it might be a bad choice with respect to code reuse (there are some
981  // dangerous constructions).
982
983  for(short i=0;i<result._number_of_variables;i++)
984    result.exponent_vector[i]=a.exponent_vector[i]-b.exponent_vector[i];
985  // The S-binomial corresponds to the vector difference.
986
987  // compute head and tail
988  short sign=w.compare_to_zero(result.exponent_vector);
989
990
991#ifdef NO_SUPPORT_DRIVEN_METHODS
992
993  if(sign==0)
994    // binomial reduced to zero
995    return result;
996
997  for(short i=0;i<result._number_of_variables;i++)
998    result.exponent_vector[i]*=sign;
999
1000#endif  // NO_SUPPORT_DRIVEN_METHODS
1001
1002
1003#ifdef SUPPORT_DRIVEN_METHODS
1004
1005  result.head_support=0;
1006  result.tail_support=0;
1007
1008  if(sign==0)
1009    // binomial reduced to zero
1010    return result;
1011
1012  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1013
1014
1015  // recompute the support vectors
1016
1017#ifdef SUPPORT_VARIABLES_FIRST
1018
1019  for(short i=0;i<result._number_of_variables;i++)
1020  {
1021
1022    Integer& actual_entry=result.exponent_vector[i];
1023    // to avoid unnecessary pointer arithmetic
1024
1025    actual_entry*=sign;
1026
1027    if(i<size_of_support_vectors)
1028      if(actual_entry>0)
1029        result.head_support|=(1<<i);
1030      else
1031        if(actual_entry<0)
1032          result.tail_support|=(1<<i);
1033  }
1034
1035#endif  // SUPPORT_VARIABLES_FIRST
1036
1037
1038#ifdef SUPPORT_VARIABLES_LAST
1039
1040  for(short i=0;i<result._number_of_variables;i++)
1041  {
1042    Integer& actual_entry=result.exponent_vector
1043      [result._number_of_variables-1-i];
1044    // to avoid unneccessary pointer arithmetic
1045
1046    actual_entry*=sign;
1047
1048    if(i<size_of_support_vectors)
1049      if(actual_entry>0)
1050        result.head_support|=(1<<i);
1051      else
1052        if(actual_entry<0)
1053          result.tail_support|=(1<<i);
1054  }
1055
1056#endif  // SUPPORT_VARIABLES_LAST
1057
1058
1059#endif  // SUPPORT_DRIVEN_METHODS
1060
1061
1062  return result;
1063}
1064
1065
1066
1067
1068///////////// criteria for unnecessary S-pairs ///////////////////////////////
1069
1070// The criteria are programmed in a way that tries to minimize pointer
1071// arithmetic. Therefore the code may appear a little bit inflated.
1072
1073
1074
1075
1076BOOLEAN relatively_prime(const binomial& a, const binomial& b)
1077{
1078
1079#ifdef NO_SUPPORT_DRIVEN_METHODS
1080
1081  // look at all variables
1082  for(short i=0;i<a._number_of_variables;i++)
1083    if((a.exponent_vector[i]>0) && (b.exponent_vector[i]>0))
1084      return FALSE;
1085
1086  return TRUE;
1087
1088#endif  // NO_SUPPORT_DRIVEN_METHODS
1089
1090
1091#ifdef SUPPORT_DRIVEN_METHODS
1092
1093  if((a.head_support & b.head_support)!=0)
1094  // common support variable in the heads
1095    return FALSE;
1096
1097  // no common support variable in the heads, look at remaining variables
1098  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1099
1100
1101#ifdef SUPPORT_VARIABLES_FIRST
1102
1103  for(short i=size_of_support_vectors;i<a._number_of_variables;i++)
1104    if((a.exponent_vector[i]>0) && (b.exponent_vector[i]>0))
1105      return FALSE;
1106
1107  return TRUE;
1108
1109#endif  // SUPPORT_VARIABLES_FIRST
1110
1111
1112#ifdef SUPPORT_VARIABLES_LAST
1113
1114  for(short i=a._number_of_variables-1-size_of_support_vectors;i>=0;i--)
1115    if((a.exponent_vector[i]>0) && (b.exponent_vector[i]>0))
1116      return FALSE;
1117
1118  return TRUE;
1119
1120#endif  // SUPPORT_VARIABLES_LAST
1121
1122
1123#endif  // SUPPORT_DRIVEN_METHODS
1124
1125}
1126
1127
1128
1129
1130BOOLEAN M(const binomial& a, const binomial& b, const binomial& c)
1131// Returns TRUE iff lcm(head(a),head(c)) divides properly lcm(head(b),head(c)).
1132// This is checked by comparing the positive components of the exponent
1133// vectors.
1134{
1135
1136
1137#ifdef SUPPORT_DRIVEN_METHODS
1138
1139  long b_or_c=b.head_support|c.head_support;
1140
1141  if((a.head_support|b_or_c) != b_or_c)
1142    return FALSE;
1143  // The support of lcm(head(a),head(c)) equals the union of the head supports
1144  // of a and c. The above condition verifies if the support of
1145  // lcm(head(a),head(c)) is contained in the support of lcm(head(b),head(c))
1146  // by checking if head a involves a variable that is not involved in
1147  // head(b) or head(c).
1148
1149#endif  // SUPPORT_DRIVEN_METHODS
1150
1151
1152  BOOLEAN properly=FALSE;
1153
1154  for(short i=0;i<a._number_of_variables;i++)
1155  {
1156    Integer a_exponent=a.exponent_vector[i];
1157    Integer b_exponent=b.exponent_vector[i];
1158    Integer c_exponent=c.exponent_vector[i];
1159    Integer m1=MAXIMUM(a_exponent,c_exponent);
1160    Integer m2=MAXIMUM(b_exponent,c_exponent);
1161
1162    if(m1>0)
1163    {
1164      if(m1>m2)
1165        return FALSE;
1166      if(m1<m2)
1167        properly=TRUE;
1168    }
1169  }
1170
1171  return properly;
1172}
1173
1174
1175
1176
1177BOOLEAN F(const binomial& a, const binomial& b, const binomial& c)
1178// verifies if lcm(head(a),head(c))=lcm(head(b),head(c))
1179{
1180
1181#ifdef SUPPORT_DRIVEN_METHODS
1182
1183  if((a.head_support|c.head_support)!=(b.head_support|c.head_support))
1184    return FALSE;
1185  // The above condition verifies if the support of lcm(head(a),head(c))
1186  // equals the support of lcm(head(b),head(c)).
1187
1188#endif  // SUPPORT_DRIVEN_METHODS
1189
1190  for(short i=0;i<a._number_of_variables;i++)
1191  {
1192    Integer a_exponent=a.exponent_vector[i];
1193    Integer b_exponent=b.exponent_vector[i];
1194    Integer c_exponent=c.exponent_vector[i];
1195    Integer m1=MAXIMUM(a_exponent,c_exponent);
1196    Integer m2=MAXIMUM(b_exponent,c_exponent);
1197
1198    if((m1!=m2) && (m1>0 || m2>0))
1199      return FALSE;
1200  }
1201
1202  return TRUE;
1203}
1204
1205
1206
1207
1208BOOLEAN B(const binomial& a, const binomial& b, const binomial& c)
1209// verifies if head(a) divides lcm(head(b),head(c)) and
1210// lcm(head(a),head(b))!=lcm(head(b),head(c))!=lcm(head(a),head(c))
1211{
1212
1213#ifdef SUPPORT_DRIVEN_METHODS
1214
1215  long a_or_b=a.head_support|b.head_support;
1216  long a_or_c=a.head_support|c.head_support;
1217  long b_or_c=b.head_support|c.head_support;
1218
1219  if((a.head_support & b_or_c)!=a.head_support)
1220    return FALSE;
1221  // The above condition verifies if the support of head(a) is contained in
1222  // the support of lcm(head(b),head(c)).
1223
1224  if( (a_or_c != b_or_c) && (a_or_b != b_or_c))
1225    // Then the inequality conditions are guaranteed...
1226  {
1227    for(short i=0;i<a._number_of_variables;i++)
1228    {
1229      Integer b_exponent=b.exponent_vector[i];
1230      Integer c_exponent=c.exponent_vector[i];
1231
1232      if(a.exponent_vector[i]>MAXIMUM(b_exponent,c_exponent))
1233        return FALSE;
1234    }
1235
1236    return (TRUE);
1237  }
1238
1239
1240  if(a_or_b != b_or_c)
1241    // Then the first inequality conditions is guaranteed...
1242    // Verifie only the second.
1243  {
1244    BOOLEAN not_equal=FALSE;
1245
1246    for(short i=0;i<a._number_of_variables;i++)
1247    {
1248      Integer a_exponent=a.exponent_vector[i];
1249      Integer b_exponent=b.exponent_vector[i];
1250      Integer c_exponent=c.exponent_vector[i];
1251      Integer m=MAXIMUM(b_exponent, c_exponent);
1252
1253      if(a_exponent>m)
1254        return FALSE;
1255
1256      if(MAXIMUM(a_exponent,c_exponent) != m)
1257         not_equal=TRUE;
1258    }
1259    return(not_equal);
1260  }
1261
1262
1263  if( a_or_c != b_or_c )
1264    // Then the second inequality conditions is guaranteed...
1265    // Verifie only the first.
1266  {
1267    BOOLEAN not_equal=FALSE;
1268
1269    for(short i=0;i<a._number_of_variables;i++)
1270    {
1271      Integer a_exponent=a.exponent_vector[i];
1272      Integer b_exponent=b.exponent_vector[i];
1273      Integer c_exponent=c.exponent_vector[i];
1274      Integer m=MAXIMUM(b_exponent, c_exponent);
1275
1276      if(a_exponent > m)
1277        return FALSE;
1278
1279      if(MAXIMUM(a_exponent,b_exponent) != m)
1280        not_equal=TRUE;
1281    }
1282    return(not_equal);
1283  }
1284
1285#endif  // SUPPORT_DRIVEN_METHODS
1286
1287
1288  BOOLEAN not_equal_1=FALSE;
1289  BOOLEAN not_equal_2=FALSE;
1290
1291  for(short i=0;i<a._number_of_variables;i++)
1292  {
1293    Integer a_exponent=a.exponent_vector[i];
1294    Integer b_exponent=b.exponent_vector[i];
1295    Integer c_exponent=c.exponent_vector[i];
1296    Integer m=MAXIMUM(b_exponent, c_exponent);
1297
1298    if(a_exponent > m)
1299      return FALSE;
1300
1301    if(MAXIMUM(a_exponent,b_exponent) != m)
1302      not_equal_1=TRUE;
1303    if(MAXIMUM(a_exponent,c_exponent) != m)
1304      not_equal_2=TRUE;
1305  }
1306
1307  return (not_equal_1 && not_equal_2);
1308
1309}
1310
1311
1312
1313
1314BOOLEAN second_crit(const binomial& a, const binomial& b,
1315                    const binomial& c)
1316// verifies if head(a) divides lcm(head(b),head(c))
1317{
1318
1319#ifdef SUPPORT_DRIVEN_METHODS
1320
1321  if((a.head_support & (b.head_support|c.head_support))!=a.head_support)
1322    return FALSE;
1323  // The above condition verifies if the support of head(a) is contained in
1324  // the support of lcm(head(b),head(c))
1325
1326#endif  // SUPPORT_DRIVEN_METHODS.
1327
1328  for(short i=0;i<a._number_of_variables;i++)
1329  {
1330    Integer b_exponent=b.exponent_vector[i];
1331    Integer c_exponent=c.exponent_vector[i];
1332
1333    if(a.exponent_vector[i]>MAXIMUM(b_exponent,c_exponent))
1334      return FALSE;
1335  }
1336
1337  return (TRUE);
1338}
1339
1340
1341
1342
1343//////// special routines needed by the IP-algorithms ///////////////////////
1344
1345
1346
1347
1348BOOLEAN binomial::involves_elimination_variables(const term_ordering& w)
1349{
1350// The use of support information would require the distinction of various
1351// cases here (relation between the number of variables to eliminate
1352// and the number of support variables) and be quite difficult.
1353// It is doubtful if this would improve performance.
1354// As this function is not used in BuchbergerŽs algorithm (and therefore
1355// rather rarely), I renounce to implement this.
1356
1357  for(short i=0;i<w.number_of_elimination_variables();i++)
1358    // elimination variables are always the last ones
1359    if(exponent_vector[_number_of_variables-1-i]!=0)
1360      return TRUE;
1361
1362  return FALSE;
1363}
1364
1365
1366
1367
1368BOOLEAN binomial::drop_elimination_variables(const term_ordering& w)
1369{
1370  _number_of_variables-=w.number_of_elimination_variables();
1371  // dangerous (no compatibility check)!!
1372
1373  // copy components of interest to save memory
1374  // the leading term has to be recomputed!!
1375
1376  Integer *aux=exponent_vector;
1377  exponent_vector=new Integer[_number_of_variables];
1378
1379  if(w.weight(aux)>=0)
1380    for(short i=0;i<_number_of_variables;i++)
1381      exponent_vector[i]=aux[i];
1382  else
1383    for(short i=0;i<_number_of_variables;i++)
1384      exponent_vector[i]=-aux[i];
1385
1386  delete[] aux;
1387
1388
1389#ifdef SUPPORT_DRIVEN_METHODS
1390
1391  // Recompute head and tail.
1392  // Normally, this routine is only called for binomials that do not involve
1393  // the variables to eliminate. But if SUPPORT_VARIABLES_LAST is enabled,
1394  // the support changes in spite of this. Therefore, the support is
1395  // recomputed... For the same reasons as mentionned in the preceeding
1396  // routine, the existing support information is not used.
1397
1398  head_support=0;
1399  tail_support=0;
1400  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1401  if(size_of_support_vectors>_number_of_variables)
1402    size_of_support_vectors=_number_of_variables;
1403
1404
1405#ifdef SUPPORT_VARIABLES_FIRST
1406
1407  for(short i=0;i<size_of_support_vectors;i++)
1408  {
1409    Integer actual_entry=exponent_vector[i];
1410    if(actual_entry>0)
1411      head_support|=(1<<i);
1412    else
1413      if(actual_entry[i]<0)
1414        tail_support|=(1<<i);
1415  }
1416
1417#endif  // SUPPORT_VARIABLES_FIRST
1418
1419
1420#ifdef SUPPORT_VARIABLES_LAST
1421
1422  for(short i=0;i<size_of_support_vectors;i++)
1423  {
1424    Integer actual_entry=exponent_vector[_number_of_variables-1-i];
1425    if(actual_entry>0)
1426      head_support|=(1<<i);
1427    else
1428      if(actual_entry<0)
1429        tail_support|=(1<<i);
1430  }
1431
1432#endif  // SUPPORT_VARIABLES_LAST
1433
1434
1435#endif  // SUPPORT_DRIVEN_METHODS
1436  return TRUE;
1437
1438}
1439
1440
1441
1442
1443BOOLEAN binomial::drop_last_weighted_variable(const term_ordering& w)
1444{
1445  _number_of_variables--;
1446  // dangerous!!
1447
1448  // copy components of interest to save memory
1449  // the leading term has to be recomputed!!
1450
1451  Integer *aux=exponent_vector;
1452  exponent_vector=new Integer[_number_of_variables];
1453
1454  short last_weighted_variable=w.number_of_weighted_variables()-1;
1455  aux[last_weighted_variable]=0;
1456  // set last component to zero, so it cannot influence the weight
1457
1458  if(w.weight(aux)>=0)
1459  {
1460    for(short i=0;i<last_weighted_variable;i++)
1461      exponent_vector[i]=aux[i];
1462    for(short i=last_weighted_variable;i<_number_of_variables;i++)
1463      exponent_vector[i]=aux[i+1];
1464  }
1465  else
1466  {
1467    for(short i=0;i<last_weighted_variable;i++)
1468      exponent_vector[i]=-aux[i];
1469     for(short i=last_weighted_variable;i<_number_of_variables;i++)
1470      exponent_vector[i]=-aux[i+1];
1471  }
1472
1473  delete[] aux;
1474
1475
1476#ifdef SUPPORT_DRIVEN_METHODS
1477
1478  // Recompute head and tail.
1479  // Normally, this routine is only called for binomials that do not involve
1480  // the variable to be dropped. But if SUPPORT_VARIABLES_LAST is enabled,
1481  // the support changes in spite of this. Therefore, the support is
1482  // recomputed... For the same reasons as mentionned in the preceeding
1483  // routines, the existing support information is not used.
1484
1485  head_support=0;
1486  tail_support=0;
1487  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1488  if(size_of_support_vectors>_number_of_variables)
1489    size_of_support_vectors=_number_of_variables;
1490
1491
1492#ifdef SUPPORT_VARIABLES_FIRST
1493
1494  for(short i=0;i<size_of_support_vectors;i++)
1495  {
1496    Integer actual_entry=exponent_vector[i];
1497    if(actual_entry>0)
1498      head_support|=(1<<i);
1499    else
1500      if(actual_entry<0)
1501        tail_support|=(1<<i);
1502  }
1503
1504#endif  // SUPPORT_VARIABLES_FIRST
1505
1506
1507#ifdef SUPPORT_VARIABLES_LAST
1508
1509  for(short i=0;i<size_of_support_vectors;i++)
1510  {
1511    Integer actual_entry=exponent_vector[_number_of_variables-1-i];
1512    if(actual_entry>0)
1513      head_support|=(1<<i);
1514    else
1515      if(actual_entry<0)
1516        tail_support|=(1<<i);
1517  }
1518
1519#endif  // SUPPORT_VARIABLES_LAST
1520
1521#endif  // SUPPORT_DRIVEN_METHODS
1522  return TRUE;
1523}
1524
1525
1526
1527
1528int binomial::adapt_to_term_ordering(const term_ordering& w)
1529{
1530
1531  if(w.compare_to_zero(exponent_vector)<0)
1532  {
1533    // then exchange head and tail
1534    for(short i=0;i<_number_of_variables;i++)
1535      exponent_vector[i]*=(-1);
1536
1537
1538#ifdef SUPPORT_DRIVEN_METHODS
1539
1540    unsigned long swap=head_support;
1541    head_support=tail_support;
1542    tail_support=swap;
1543
1544#endif
1545
1546
1547    return -1;
1548    // binomial changed
1549  }
1550
1551  else
1552    return 1;
1553    // binomial unchanged
1554}
1555
1556
1557
1558
1559binomial& binomial::swap_variables(const short& i, const short& j)
1560{
1561
1562
1563#ifdef SUPPORT_DRIVEN_METHODS
1564
1565  // First adjust head_support and tail_support.
1566
1567  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1568  if(size_of_support_vectors>_number_of_variables)
1569    size_of_support_vectors=_number_of_variables;
1570
1571
1572#ifdef SUPPORT_VARIABLES_FIRST
1573
1574  if(i<size_of_support_vectors)
1575    // else i is no support variable
1576  {
1577    if(exponent_vector[j]>0)
1578    // bit i will be 1 in the new head_support, 0 in the new tail_support
1579    {
1580      head_support|=(1<<i);
1581      // bit i is set to 1
1582
1583      tail_support&=~(1<<i);
1584      // bit i is set to 0
1585      // (in the complement ~(1<<i) all bits are 1 except from bit i)
1586    }
1587
1588    if(exponent_vector[j]==0)
1589    // bit i will be 0 in the new head_support, 0 in the new tail_support
1590    {
1591      head_support&=~(1<<i);
1592      // bit i is set to 0
1593
1594      tail_support&=~(1<<i);
1595      // bit i is set to 0
1596    }
1597
1598    if(exponent_vector[j]<0)
1599    // bit i will be 0 in the new head_support, 1 in the new tail_support
1600    {
1601      head_support&=~(1<<i);
1602      // bit i is set to 0
1603
1604      tail_support|=(1<<i);
1605      // bit i is set to 1
1606    }
1607  }
1608
1609
1610  if(j<size_of_support_vectors)
1611    // else j is no support variable
1612  {
1613    if(exponent_vector[i]>0)
1614    // bit j will be 1 in the new head_support, 0 in the new tail_support
1615    {
1616      head_support|=(1<<j);
1617      // bit j is set to 1
1618
1619      tail_support&=~(1<<j);
1620      // bit j is set to 0
1621      // (in the complement ~(1<<j) all bits are 1 except from bit j)
1622    }
1623
1624    if(exponent_vector[i]==0)
1625    // bit j will be 0 in the new head_support, 0 in the new tail_support
1626    {
1627      head_support&=~(1<<j);
1628      // bit j is set to 0
1629
1630      tail_support&=~(1<<j);
1631      // bit j is set to 0
1632    }
1633
1634    if(exponent_vector[i]<0)
1635    // bit j will be 0 in the new head_support, 1 in the new tail_support
1636    {
1637      head_support&=~(1<<j);
1638      // bit j is set to 0
1639
1640      tail_support|=(1<<j);
1641      // bit j is set to 1
1642    }
1643  }
1644
1645#endif  // SUPPORT_VARIABLES_FIRST
1646
1647
1648#ifdef SUPPORT_VARIABLES_LAST
1649
1650  // Using SUPPORT_VARIABLES_LAST, bit k of the support vectors
1651  // corresponds to exponent_vector[_number_of_variables-1-k],
1652  // hence bit _number_of_variables-1-i to exponent_vector[i].
1653
1654  if(i>=_number_of_variables-size_of_support_vectors)
1655    // else i is no support variable
1656  {
1657    if(exponent_vector[j]>0)
1658    // bit _number_of_variables-1-i will be 1 in the new head_support,
1659    // 0 in the new tail_support
1660    {
1661      short k=_number_of_variables-1-i;
1662
1663      head_support|=(1<<k);
1664      // bit _number_of_variables-1-i is set to 1
1665
1666      tail_support&=~(1<<k);
1667      // bit _number_of_variables-1-i is set to 0
1668      // (in the complement ~(1<<(_number_of_variables-1-i)) all bits are 1
1669      // except from bit _number_of_variables-1-i)
1670    }
1671
1672    if(exponent_vector[j]==0)
1673    // bit _number_of_variables-1-i will be 0 in the new head_support,
1674    // 0 in the new tail_support
1675    {
1676      short k=_number_of_variables-1-i;
1677
1678      head_support&=~(1<<k);
1679      // bit _number_of_variables-1-i is set to 0
1680
1681      tail_support&=~(1<<k);
1682      // bit _number_of_variables-1-i is set to 0
1683    }
1684
1685    if(exponent_vector[j]<0)
1686    // bit _number_of_variables-1-i will be 0 in the new head_support,
1687     // 1 in the new tail_support
1688    {
1689      short k=_number_of_variables-1-i;
1690
1691      head_support&=~(1<<k);
1692      // bit _number_of_variables-1-i is set to 0
1693
1694      tail_support|=(1<<k);
1695      // bit _number_of_variables-1-i is set to 1
1696    }
1697  }
1698
1699
1700  if(j>=_number_of_variables-size_of_support_vectors)
1701      // else j is no support variable
1702  {
1703   if(exponent_vector[i]>0)
1704    // bit _number_of_variables-1-j will be 1 in the new head_support,
1705    // 0 in the new tail_support
1706    {
1707      short k=_number_of_variables-1-j;
1708
1709      head_support|=(1<<k);
1710      // bit _number_of_variables-1-j is set to 1
1711
1712      tail_support&=~(1<<k);
1713      // bit _number_of_variables-1-j is set to 0
1714      // (in the complement ~(1<<(_number_of_variables-1-j)) all bits are 1
1715      // except from bit _number_of_variables-1-j)
1716    }
1717
1718    if(exponent_vector[i]==0)
1719    // bit _number_of_variables-1-j will be 0 in the new head_support,
1720    // 0 in the new tail_support
1721    {
1722      short k=_number_of_variables-1-j;
1723
1724      head_support&=~(1<<k);
1725      // bit _number_of_variables-1-j is set to 0
1726
1727      tail_support&=~(1<<k);
1728      // bit _number_of_variables-1-j is set to 0
1729    }
1730
1731    if(exponent_vector[i]<0)
1732    // bit _number_of_variables-1-j will be 0 in the new head_support,
1733     // 1 in the new tail_support
1734    {
1735      short k=_number_of_variables-1-j;
1736
1737      head_support&=~(1<<k);
1738      // bit _number_of_variables-1-j is set to 0
1739
1740      tail_support|=(1<<k);
1741      // bit _number_of_variables-1-j is set to 1
1742    }
1743  }
1744
1745#endif  // SUPPORT_VARIABLES_LAST
1746
1747#endif  // SUPPORT_DRIVEN_METHODS
1748
1749
1750// Now swap the components.
1751
1752  Integer swap=exponent_vector[j];
1753  exponent_vector[j]=exponent_vector[i];
1754  exponent_vector[i]=swap;
1755
1756  return *this;
1757
1758}
1759
1760binomial& binomial::flip_variable(const short& i)
1761{
1762
1763  if(exponent_vector[i]==0)
1764    // binomial does not involve variable to flip
1765    return *this;
1766
1767
1768#ifdef SUPPORT_DRIVEN_METHODS
1769
1770// First adjust head_support and tail_support.
1771
1772  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1773  if(size_of_support_vectors>_number_of_variables)
1774    size_of_support_vectors=_number_of_variables;
1775
1776
1777#ifdef SUPPORT_VARIABLES_FIRST
1778
1779  if(i<size_of_support_vectors)
1780    // else i is no support variable
1781  {
1782    if(exponent_vector[i]>0)
1783      // variable i will be moved from head to tail
1784    {
1785      head_support&=~(1<<i);
1786      // bit i is set to 0
1787
1788      tail_support|=(1<<i);
1789      // bit i is set to 1
1790    }
1791
1792    else
1793      // variable i will be moved from tail to head
1794      // remember that exponent_vector[i]!=0
1795    {
1796      tail_support&=~(1<<i);
1797      // bit i is set to 0
1798
1799      head_support|=(1<<i);
1800      // bit i is set to 1
1801    }
1802  }
1803#endif  // SUPPORT_VARIABLES_FIRST
1804
1805#ifdef SUPPORT_VARIABLES_LAST
1806
1807  // Using SUPPORT_VARIABLES_LAST, bit k of the support vectors
1808  // corresponds to exponent_vector[_number_of_variables-1-k],
1809  // hence bit _number_of_variables-1-i to exponent_vector[i].
1810
1811  if(i>=_number_of_variables-size_of_support_vectors)
1812    // else i is no support variable
1813  {
1814    if(exponent_vector[i]>0)
1815    // variable i will be moved from head to tail
1816    {
1817      short k=_number_of_variables-1-i;
1818
1819      head_support&=~(1<<k);
1820      // bit _number_of_variables-1-i is set to 0
1821
1822      tail_support|=(1<<k);
1823      // bit _number_of_variables-1-i is set to 1
1824
1825     }
1826
1827    else
1828    // variable i will be moved from tail to head
1829    {
1830      short k=_number_of_variables-1-i;
1831
1832      tail_support&=~(1<<k);
1833      // bit _number_of_variables-1-i is set to 0
1834
1835      head_support|=(1<<k);
1836      // bit _number_of_variables-1-i is set to 1
1837
1838    }
1839  }
1840#endif  // SUPPORT_VARIABLES_LAST
1841
1842
1843#endif  // SUPPORT_DRIVEN_METHODS
1844
1845  // Now flip the variable.
1846
1847  exponent_vector[i]*=-1;
1848
1849}
1850
1851////////////////////////// output /////////////////////////////////////////
1852
1853void binomial::print() const
1854{
1855  printf("(");
1856  for(short i=0;i<_number_of_variables-1;i++)
1857    printf("%6d,",exponent_vector[i]);
1858  printf("%6d)\n",exponent_vector[_number_of_variables-1]);
1859}
1860
1861void binomial::print_all() const
1862{
1863  print();
1864
1865#ifdef SUPPORT_DRIVEN_METHODS
1866
1867  printf("head: %ld, tail %ld\n",head_support,tail_support);
1868
1869#endif  // SUPPORT_DRIVEN_METHODS
1870}
1871
1872void binomial::print(FILE* output) const
1873{
1874  fprintf(output,"(");
1875  for(short i=0;i<_number_of_variables-1;i++)
1876    fprintf(output,"%6d,",exponent_vector[i]);
1877  fprintf(output,"%6d)\n",exponent_vector[_number_of_variables-1]);
1878}
1879
1880void binomial::print_all(FILE* output) const
1881{
1882  print(output);
1883
1884#ifdef SUPPORT_DRIVEN_METHODS
1885
1886  fprintf(output,"head: %ld, tail %ld\n",head_support,tail_support);
1887
1888#endif  // SUPPORT_DRIVEN_METHODS
1889}
1890
1891void binomial::print(ofstream& output) const
1892{
1893  output<<"(";
1894  for(short i=0;i<_number_of_variables-1;i++)
1895    output<<setw(6)<<exponent_vector[i]<<",";
1896  output<<setw(6)<<exponent_vector[_number_of_variables-1]<<")"<<endl;
1897}
1898
1899void binomial::print_all(ofstream& output) const
1900{
1901  print(output);
1902
1903#ifdef SUPPORT_DRIVEN_METHODS
1904
1905  output<<"head: "<<setw(16)<<head_support<<", tail: "<<setw(16)
1906        <<tail_support<<endl;
1907
1908#endif  // SUPPORT_DRIVEN_METHODS
1909}
1910
1911void binomial::format_print(ofstream& output) const
1912{
1913  for(short i=0;i<_number_of_variables;i++)
1914    output<<setw(6)<<exponent_vector[i];
1915  output<<endl;
1916}
1917
1918#endif  // BINOMIAL_CC
Note: See TracBrowser for help on using the repository browser.