source: git/IntegerProgramming/binomial.cc @ f3a8c2e

fieker-DuValspielwiese
Last change on this file since f3a8c2e was 6ba162, checked in by Hans Schönemann <hannes@…>, 24 years ago
This commit was generated by cvs2svn to compensate for changes in r4282, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@4283 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
1437}
1438
1439
1440
1441
1442BOOLEAN binomial::drop_last_weighted_variable(const term_ordering& w)
1443{
1444  _number_of_variables--;
1445  // dangerous!!
1446
1447  // copy components of interest to save memory
1448  // the leading term has to be recomputed!!
1449
1450  Integer *aux=exponent_vector;
1451  exponent_vector=new Integer[_number_of_variables];
1452
1453  short last_weighted_variable=w.number_of_weighted_variables()-1;
1454  aux[last_weighted_variable]=0;
1455  // set last component to zero, so it cannot influence the weight
1456
1457  if(w.weight(aux)>=0)
1458  {
1459    for(short i=0;i<last_weighted_variable;i++)
1460      exponent_vector[i]=aux[i];
1461    for(short i=last_weighted_variable;i<_number_of_variables;i++)
1462      exponent_vector[i]=aux[i+1];
1463  }
1464  else
1465  {
1466    for(short i=0;i<last_weighted_variable;i++)
1467      exponent_vector[i]=-aux[i];
1468     for(short i=last_weighted_variable;i<_number_of_variables;i++)
1469      exponent_vector[i]=-aux[i+1];
1470  }
1471
1472  delete[] aux;
1473
1474
1475#ifdef SUPPORT_DRIVEN_METHODS
1476
1477  // Recompute head and tail.
1478  // Normally, this routine is only called for binomials that do not involve
1479  // the variable to be dropped. But if SUPPORT_VARIABLES_LAST is enabled,
1480  // the support changes in spite of this. Therefore, the support is
1481  // recomputed... For the same reasons as mentionned in the preceeding
1482  // routines, the existing support information is not used.
1483
1484  head_support=0;
1485  tail_support=0;
1486  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1487  if(size_of_support_vectors>_number_of_variables)
1488    size_of_support_vectors=_number_of_variables;
1489
1490
1491#ifdef SUPPORT_VARIABLES_FIRST
1492
1493  for(short i=0;i<size_of_support_vectors;i++)
1494  {
1495    Integer actual_entry=exponent_vector[i];
1496    if(actual_entry>0)
1497      head_support|=(1<<i);
1498    else
1499      if(actual_entry<0)
1500        tail_support|=(1<<i);
1501  }
1502
1503#endif  // SUPPORT_VARIABLES_FIRST
1504
1505
1506#ifdef SUPPORT_VARIABLES_LAST
1507
1508  for(short i=0;i<size_of_support_vectors;i++)
1509  {
1510    Integer actual_entry=exponent_vector[_number_of_variables-1-i];
1511    if(actual_entry>0)
1512      head_support|=(1<<i);
1513    else
1514      if(actual_entry<0)
1515        tail_support|=(1<<i);
1516  }
1517
1518#endif  // SUPPORT_VARIABLES_LAST
1519
1520#endif  // SUPPORT_DRIVEN_METHODS
1521
1522}
1523
1524
1525
1526
1527int binomial::adapt_to_term_ordering(const term_ordering& w)
1528{
1529
1530  if(w.compare_to_zero(exponent_vector)<0)
1531  {
1532    // then exchange head and tail
1533    for(short i=0;i<_number_of_variables;i++)
1534      exponent_vector[i]*=(-1);
1535
1536
1537#ifdef SUPPORT_DRIVEN_METHODS
1538
1539    unsigned long swap=head_support;
1540    head_support=tail_support;
1541    tail_support=swap;
1542
1543#endif
1544
1545
1546    return -1;
1547    // binomial changed
1548  }
1549
1550  else
1551    return 1;
1552    // binomial unchanged
1553}
1554
1555
1556
1557
1558binomial& binomial::swap_variables(const short& i, const short& j)
1559{
1560
1561
1562#ifdef SUPPORT_DRIVEN_METHODS
1563
1564  // First adjust head_support and tail_support.
1565
1566  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1567  if(size_of_support_vectors>_number_of_variables)
1568    size_of_support_vectors=_number_of_variables;
1569
1570
1571#ifdef SUPPORT_VARIABLES_FIRST
1572
1573  if(i<size_of_support_vectors)
1574    // else i is no support variable
1575  {
1576    if(exponent_vector[j]>0)
1577    // bit i will be 1 in the new head_support, 0 in the new tail_support
1578    {
1579      head_support|=(1<<i);
1580      // bit i is set to 1
1581
1582      tail_support&=~(1<<i);
1583      // bit i is set to 0
1584      // (in the complement ~(1<<i) all bits are 1 except from bit i)
1585    }
1586
1587    if(exponent_vector[j]==0)
1588    // bit i will be 0 in the new head_support, 0 in the new tail_support
1589    {
1590      head_support&=~(1<<i);
1591      // bit i is set to 0
1592
1593      tail_support&=~(1<<i);
1594      // bit i is set to 0
1595    }
1596
1597    if(exponent_vector[j]<0)
1598    // bit i will be 0 in the new head_support, 1 in the new tail_support
1599    {
1600      head_support&=~(1<<i);
1601      // bit i is set to 0
1602
1603      tail_support|=(1<<i);
1604      // bit i is set to 1
1605    }
1606  }
1607
1608
1609  if(j<size_of_support_vectors)
1610    // else j is no support variable
1611  {
1612    if(exponent_vector[i]>0)
1613    // bit j will be 1 in the new head_support, 0 in the new tail_support
1614    {
1615      head_support|=(1<<j);
1616      // bit j is set to 1
1617
1618      tail_support&=~(1<<j);
1619      // bit j is set to 0
1620      // (in the complement ~(1<<j) all bits are 1 except from bit j)
1621    }
1622
1623    if(exponent_vector[i]==0)
1624    // bit j will be 0 in the new head_support, 0 in the new tail_support
1625    {
1626      head_support&=~(1<<j);
1627      // bit j is set to 0
1628
1629      tail_support&=~(1<<j);
1630      // bit j is set to 0
1631    }
1632
1633    if(exponent_vector[i]<0)
1634    // bit j will be 0 in the new head_support, 1 in the new tail_support
1635    {
1636      head_support&=~(1<<j);
1637      // bit j is set to 0
1638
1639      tail_support|=(1<<j);
1640      // bit j is set to 1
1641    }
1642  }
1643
1644#endif  // SUPPORT_VARIABLES_FIRST
1645
1646
1647#ifdef SUPPORT_VARIABLES_LAST
1648
1649  // Using SUPPORT_VARIABLES_LAST, bit k of the support vectors
1650  // corresponds to exponent_vector[_number_of_variables-1-k],
1651  // hence bit _number_of_variables-1-i to exponent_vector[i].
1652
1653  if(i>=_number_of_variables-size_of_support_vectors)
1654    // else i is no support variable
1655  {
1656    if(exponent_vector[j]>0)
1657    // bit _number_of_variables-1-i will be 1 in the new head_support,
1658    // 0 in the new tail_support
1659    {
1660      short k=_number_of_variables-1-i;
1661
1662      head_support|=(1<<k);
1663      // bit _number_of_variables-1-i is set to 1
1664
1665      tail_support&=~(1<<k);
1666      // bit _number_of_variables-1-i is set to 0
1667      // (in the complement ~(1<<(_number_of_variables-1-i)) all bits are 1
1668      // except from bit _number_of_variables-1-i)
1669    }
1670
1671    if(exponent_vector[j]==0)
1672    // bit _number_of_variables-1-i will be 0 in the new head_support,
1673    // 0 in the new tail_support
1674    {
1675      short k=_number_of_variables-1-i;
1676
1677      head_support&=~(1<<k);
1678      // bit _number_of_variables-1-i is set to 0
1679
1680      tail_support&=~(1<<k);
1681      // bit _number_of_variables-1-i is set to 0
1682    }
1683
1684    if(exponent_vector[j]<0)
1685    // bit _number_of_variables-1-i will be 0 in the new head_support,
1686     // 1 in the new tail_support
1687    {
1688      short k=_number_of_variables-1-i;
1689
1690      head_support&=~(1<<k);
1691      // bit _number_of_variables-1-i is set to 0
1692
1693      tail_support|=(1<<k);
1694      // bit _number_of_variables-1-i is set to 1
1695    }
1696  }
1697
1698
1699  if(j>=_number_of_variables-size_of_support_vectors)
1700      // else j is no support variable
1701  {
1702   if(exponent_vector[i]>0)
1703    // bit _number_of_variables-1-j will be 1 in the new head_support,
1704    // 0 in the new tail_support
1705    {
1706      short k=_number_of_variables-1-j;
1707
1708      head_support|=(1<<k);
1709      // bit _number_of_variables-1-j is set to 1
1710
1711      tail_support&=~(1<<k);
1712      // bit _number_of_variables-1-j is set to 0
1713      // (in the complement ~(1<<(_number_of_variables-1-j)) all bits are 1
1714      // except from bit _number_of_variables-1-j)
1715    }
1716
1717    if(exponent_vector[i]==0)
1718    // bit _number_of_variables-1-j will be 0 in the new head_support,
1719    // 0 in the new tail_support
1720    {
1721      short k=_number_of_variables-1-j;
1722
1723      head_support&=~(1<<k);
1724      // bit _number_of_variables-1-j is set to 0
1725
1726      tail_support&=~(1<<k);
1727      // bit _number_of_variables-1-j is set to 0
1728    }
1729
1730    if(exponent_vector[i]<0)
1731    // bit _number_of_variables-1-j will be 0 in the new head_support,
1732     // 1 in the new tail_support
1733    {
1734      short k=_number_of_variables-1-j;
1735
1736      head_support&=~(1<<k);
1737      // bit _number_of_variables-1-j is set to 0
1738
1739      tail_support|=(1<<k);
1740      // bit _number_of_variables-1-j is set to 1
1741    }
1742  }
1743
1744#endif  // SUPPORT_VARIABLES_LAST
1745
1746#endif  // SUPPORT_DRIVEN_METHODS
1747
1748
1749// Now swap the components.
1750
1751  Integer swap=exponent_vector[j];
1752  exponent_vector[j]=exponent_vector[i];
1753  exponent_vector[i]=swap;
1754
1755  return *this;
1756
1757}
1758
1759binomial& binomial::flip_variable(const short& i)
1760{
1761
1762  if(exponent_vector[i]==0)
1763    // binomial does not involve variable to flip
1764    return *this;
1765
1766
1767#ifdef SUPPORT_DRIVEN_METHODS
1768
1769// First adjust head_support and tail_support.
1770
1771  short size_of_support_vectors=CHAR_BIT*sizeof(unsigned long);
1772  if(size_of_support_vectors>_number_of_variables)
1773    size_of_support_vectors=_number_of_variables;
1774
1775
1776#ifdef SUPPORT_VARIABLES_FIRST
1777
1778  if(i<size_of_support_vectors)
1779    // else i is no support variable
1780  {
1781    if(exponent_vector[i]>0)
1782      // variable i will be moved from head to tail
1783    {
1784      head_support&=~(1<<i);
1785      // bit i is set to 0
1786
1787      tail_support|=(1<<i);
1788      // bit i is set to 1
1789    }
1790
1791    else
1792      // variable i will be moved from tail to head
1793      // remember that exponent_vector[i]!=0
1794    {
1795      tail_support&=~(1<<i);
1796      // bit i is set to 0
1797
1798      head_support|=(1<<i);
1799      // bit i is set to 1
1800    }
1801  }
1802#endif  // SUPPORT_VARIABLES_FIRST
1803
1804#ifdef SUPPORT_VARIABLES_LAST
1805
1806  // Using SUPPORT_VARIABLES_LAST, bit k of the support vectors
1807  // corresponds to exponent_vector[_number_of_variables-1-k],
1808  // hence bit _number_of_variables-1-i to exponent_vector[i].
1809
1810  if(i>=_number_of_variables-size_of_support_vectors)
1811    // else i is no support variable
1812  {
1813    if(exponent_vector[i]>0)
1814    // variable i will be moved from head to tail
1815    {
1816      short k=_number_of_variables-1-i;
1817
1818      head_support&=~(1<<k);
1819      // bit _number_of_variables-1-i is set to 0
1820
1821      tail_support|=(1<<k);
1822      // bit _number_of_variables-1-i is set to 1
1823
1824     }
1825
1826    else
1827    // variable i will be moved from tail to head
1828    {
1829      short k=_number_of_variables-1-i;
1830
1831      tail_support&=~(1<<k);
1832      // bit _number_of_variables-1-i is set to 0
1833
1834      head_support|=(1<<k);
1835      // bit _number_of_variables-1-i is set to 1
1836
1837    }
1838  }
1839#endif  // SUPPORT_VARIABLES_LAST
1840
1841
1842#endif  // SUPPORT_DRIVEN_METHODS
1843
1844  // Now flip the variable.
1845
1846  exponent_vector[i]*=-1;
1847
1848}
1849
1850////////////////////////// output /////////////////////////////////////////
1851
1852void binomial::print() const
1853{
1854  printf("(");
1855  for(short i=0;i<_number_of_variables-1;i++)
1856    printf("%6d,",exponent_vector[i]);
1857  printf("%6d)\n",exponent_vector[_number_of_variables-1]);
1858}
1859
1860void binomial::print_all() const
1861{
1862  print();
1863
1864#ifdef SUPPORT_DRIVEN_METHODS
1865
1866  printf("head: %ld, tail %ld\n",head_support,tail_support);
1867
1868#endif  // SUPPORT_DRIVEN_METHODS
1869}
1870
1871void binomial::print(FILE* output) const
1872{
1873  fprintf(output,"(");
1874  for(short i=0;i<_number_of_variables-1;i++)
1875    fprintf(output,"%6d,",exponent_vector[i]);
1876  fprintf(output,"%6d)\n",exponent_vector[_number_of_variables-1]);
1877}
1878
1879void binomial::print_all(FILE* output) const
1880{
1881  print(output);
1882
1883#ifdef SUPPORT_DRIVEN_METHODS
1884
1885  fprintf(output,"head: %ld, tail %ld\n",head_support,tail_support);
1886
1887#endif  // SUPPORT_DRIVEN_METHODS
1888}
1889
1890void binomial::print(ofstream& output) const
1891{
1892  output<<"(";
1893  for(short i=0;i<_number_of_variables-1;i++)
1894    output<<setw(6)<<exponent_vector[i]<<",";
1895  output<<setw(6)<<exponent_vector[_number_of_variables-1]<<")"<<endl;
1896}
1897
1898void binomial::print_all(ofstream& output) const
1899{
1900  print(output);
1901
1902#ifdef SUPPORT_DRIVEN_METHODS
1903
1904  output<<"head: "<<setw(16)<<head_support<<", tail: "<<setw(16)
1905        <<tail_support<<endl;
1906
1907#endif  // SUPPORT_DRIVEN_METHODS
1908}
1909
1910void binomial::format_print(ofstream& output) const
1911{
1912  for(short i=0;i<_number_of_variables;i++)
1913    output<<setw(6)<<exponent_vector[i];
1914  output<<endl;
1915}
1916
1917#endif  // BINOMIAL_CC
Note: See TracBrowser for help on using the repository browser.