Scippy

SoPlex

Sequential object-oriented simPlex

basevectors.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996 Roland Wunderling */
7 /* 1996-2016 Konrad-Zuse-Zentrum */
8 /* fuer Informationstechnik Berlin */
9 /* */
10 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
11 /* */
12 /* You should have received a copy of the ZIB Academic License */
13 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
14 /* */
15 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
16 
17 /**@file basevectors.h
18  * @brief Collection of dense, sparse, and semi-sparse vectors.
19  */
20 #ifndef _BASEVECTORS_H_
21 #define _BASEVECTORS_H_
22 
23 /* undefine SOPLEX_DEBUG flag from including files; if SOPLEX_DEBUG should be defined in this file, do so below */
24 #ifdef SOPLEX_DEBUG
25 #define SOPLEX_DEBUG_BASEVECTORS
26 #undef SOPLEX_DEBUG
27 #endif
28 
29 #include "spxdefines.h"
30 #include "rational.h"
31 #include "vectorbase.h"
32 #include "dvectorbase.h"
33 #include "ssvectorbase.h"
34 #include "svectorbase.h"
35 #include "dsvectorbase.h"
36 #include "unitvectorbase.h"
37 #include "svsetbase.h"
38 
39 #define SOPLEX_VECTOR_MARKER 1e-100
40 
41 namespace soplex
42 {
43 
44 // ---------------------------------------------------------------------------------------------------------------------
45 // Methods of VectorBase
46 // ---------------------------------------------------------------------------------------------------------------------
47 
48 /// Assignment operator.
49 /** Assigning an SVectorBase to a VectorBase using operator=() will set all values to 0 except the nonzeros of \p vec.
50  * This is diffent in method assign().
51  */
52 
53 
54 
55 template < class R >
56 template < class S >
57 inline
59 {
60  clear();
61 
62  for( int i = 0; i < vec.size(); ++i )
63  {
64  assert(vec.index(i) < dim());
65  val[vec.index(i)] = vec.value(i);
66  }
67 
68  assert(isConsistent());
69 
70  return *this;
71 }
72 
73 
74 
75 /// Assign values of \p vec.
76 /** Assigns all nonzeros of \p vec to the vector. All other values remain unchanged. */
77 template < class R >
78 template < class S >
79 inline
81 {
82  for( int i = vec.size() - 1; i >= 0; --i )
83  val[vec.index(i)] = vec.value(i);
84 
85  assert(isConsistent());
86 
87  return *this;
88 }
89 
90 
91 
92 /// Assignment operator.
93 /** Assigning an SSVectorBase to a VectorBase using operator=() will set all values to 0 except the nonzeros of \p vec.
94  * This is diffent in method assign().
95  */
96 template < class R >
97 template < class S >
98 inline
100 {
101  if( vec.isSetup() )
102  {
103  clear();
104  assign(vec);
105  }
106  else
107  operator=(static_cast<const VectorBase<R>&>(vec));
108 
109  return *this;
110 }
111 
112 
113 
114 /// Assign values of \p vec.
115 /** Assigns all nonzeros of \p vec to the vector. All other values remain unchanged. */
116 template < class R >
117 template < class S >
118 inline
120 {
121  assert(vec.dim() <= dim());
122 
123  if (vec.isSetup())
124  {
125  const int* idx = vec.indexMem();
126 
127  for(int i = vec.size() - 1; i >= 0; --i)
128  {
129  val[*idx] = vec.val[*idx];
130  idx++;
131  }
132  }
133  else
134  operator=(static_cast<const VectorBase<R>&>(vec));
135 
136  return *this;
137 }
138 
139 
140 
141 /// Addition.
142 template < class R >
143 template < class S >
144 inline
146 {
147  for( int i = vec.size() - 1; i >= 0; --i )
148  {
149  assert(vec.index(i) >= 0);
150  assert(vec.index(i) < dim());
151  val[vec.index(i)] += vec.value(i);
152  }
153 
154  return *this;
155 }
156 
157 
158 
159 /// Addition.
160 template < class R >
161 template < class S >
162 inline
164 {
165  assert(dim() == vec.dim());
166 
167  if ( vec.isSetup() )
168  {
169  for( int i = vec.size() - 1; i >= 0 ; --i )
170  val[vec.index(i)] += vec.value(i);
171  }
172  else
173  {
174  for( int i = dim() - 1; i >= 0; --i )
175  val[i] += vec[i];
176  }
177 
178  return *this;
179 }
180 
181 
182 
183 /// Subtraction.
184 template < class R >
185 template < class S >
186 inline
188 {
189  for( int i = vec.size() - 1; i >= 0; --i )
190  {
191  assert(vec.index(i) >= 0);
192  assert(vec.index(i) < dim());
193  val[vec.index(i)] -= vec.value(i);
194  }
195 
196  return *this;
197 }
198 
199 
200 
201 /// Subtraction.
202 template < class R >
203 template < class S >
204 inline
206 {
207  assert(dim() == vec.dim());
208 
209  if ( vec.isSetup() )
210  {
211  for( int i = vec.size() - 1; i >= 0; --i )
212  val[vec.index(i)] -= vec.value(i);
213  }
214  else
215  {
216  for( int i = dim() - 1; i >= 0; --i )
217  val[i] -= vec[i];
218  }
219 
220  return *this;
221 }
222 
223 
224 
225 /// Inner product.
226 template < class R >
227 inline
229 {
230  assert(dim() >= vec.dim());
231 
232  R x(0);
233 
234  for( int i = vec.size() - 1; i >= 0; --i )
235  x += val[vec.index(i)] * vec.value(i);
236 
237  return x;
238 }
239 
240 
241 
242 /// Inner product.
243 template < class R >
244 inline
246 {
247  assert(dim() == vec.dim());
248 
249  if( vec.isSetup() )
250  {
251  const int* idx = vec.indexMem();
252 
253  R x(0);
254 
255  for( int i = vec.size() - 1; i >= 0; --i )
256  {
257  x += val[*idx] * vec.val[*idx];
258  idx++;
259  }
260 
261  return x;
262  }
263  else
264  return operator*(static_cast<const VectorBase<R>&>(vec));
265 }
266 
267 
268 
269 /// Addition of scaled vector.
270 template < class R >
271 template < class S, class T >
272 inline
274 {
275  for( int i = vec.size() - 1; i >= 0; --i )
276  {
277  assert(vec.index(i) < dim());
278  val[vec.index(i)] += x * vec.value(i);
279  }
280 
281  return *this;
282 }
283 
284 
285 
286 /// Subtraction of scaled vector.
287 template < class R >
288 template < class S, class T >
289 inline
291 {
292  for( int i = vec.size() - 1; i >= 0; --i )
293  {
294  assert(vec.index(i) < dim());
295  val[vec.index(i)] -= x * vec.value(i);
296  }
297 
298  return *this;
299 }
300 
301 
302 
303 #ifndef SOPLEX_LEGACY
304 /// Addition of scaled vector, specialization for rationals
305 template <>
306 template <>
307 inline
309 {
310  for( int i = vec.size() - 1; i >= 0; --i )
311  {
312  assert(vec.index(i) < dim());
313  val[vec.index(i)].addProduct(x, vec.value(i));
314  }
315 
316  return *this;
317 }
318 
319 
320 
321 /// Subtraction of scaled vector, specialization for rationals
322 template <>
323 template <>
324 inline
326 {
327  for( int i = vec.size() - 1; i >= 0; --i )
328  {
329  assert(vec.index(i) < dim());
330  val[vec.index(i)].subProduct(x, vec.value(i));
331  }
332 
333  return *this;
334 }
335 #endif
336 
337 
338 
339 /// Addition of scaled vector.
340 template < class R >
341 template < class S, class T >
342 inline
344 {
345  assert(vec.dim() <= dim());
346 
347  if( vec.isSetup() )
348  {
349  const int* idx = vec.indexMem();
350 
351  for( int i = vec.size() - 1; i>= 0; --i )
352  val[idx[i]] += x * vec[idx[i]];
353  }
354  else
355  {
356  assert(vec.dim() == dim());
357 
358  for( int i = dim() - 1; i >= 0; --i )
359  val[i] += x * vec.val[i];
360  }
361 
362  return *this;
363 }
364 
365 
366 
367 // ---------------------------------------------------------------------------------------------------------------------
368 // Methods of DVectorBase
369 // ---------------------------------------------------------------------------------------------------------------------
370 
371 
372 
373 /// Assignment operator.
374 template < class R >
375 template < class S >
376 inline
378 {
379  if( vec.dim() != VectorBase<R>::dim() )
380  reDim(vec.dim());
381 
383 
384  assert(isConsistent());
385 
386  return *this;
387 }
388 
389 
390 
391 // ---------------------------------------------------------------------------------------------------------------------
392 // Methods of SSVectorBase
393 // ---------------------------------------------------------------------------------------------------------------------
394 
395 
396 
397 /// Addition.
398 template < class R >
399 template < class S >
400 inline
402 {
404 
405  if( isSetup() )
406  {
407  setupStatus = false;
408  setup();
409  }
410 
411  return *this;
412 }
413 
414 
415 
416 /// Subtraction.
417 template < class R >
418 template < class S >
419 inline
421 {
423 
424  if( isSetup() )
425  {
426  setupStatus = false;
427  setup();
428  }
429 
430  return *this;
431 }
432 
433 
434 
435 /// Addition of a scaled vector.
436 ///@todo SSVectorBase::multAdd() should be rewritten without pointer arithmetic.
437 template < class R >
438 template < class S, class T >
439 inline
441 {
442  if( isSetup() )
443  {
444  R* v = VectorBase<R>::val;
445  R x;
446  bool adjust = false;
447  int j;
448 
449  for( int i = vec.size() - 1; i >= 0; --i )
450  {
451  j = vec.index(i);
452 
453  if( v[j] != 0 )
454  {
455  x = v[j] + xx * vec.value(i);
456  if( isNotZero(x, epsilon) )
457  v[j] = x;
458  else
459  {
460  adjust = true;
461  v[j] = SOPLEX_VECTOR_MARKER;
462  }
463  }
464  else
465  {
466  x = xx * vec.value(i);
467  if( isNotZero(x, epsilon) )
468  {
469  v[j] = x;
470  addIdx(j);
471  }
472  }
473  }
474 
475  if( adjust )
476  {
477  int* iptr = idx;
478  int* iiptr = idx;
479  int* endptr = idx + num;
480 
481  for( ; iptr < endptr; ++iptr )
482  {
483  x = v[*iptr];
484  if( isNotZero(x, epsilon) )
485  *iiptr++ = *iptr;
486  else
487  v[*iptr] = 0;
488  }
489 
490  num = int(iiptr - idx);
491  }
492  }
493  else
494  VectorBase<R>::multAdd(xx, vec);
495 
496  assert(isConsistent());
497 
498  return *this;
499 }
500 
501 
502 
503 /// Assigns \f$x^T \cdot A\f$ to SSVectorBase.
504 template < class R >
505 template < class S, class T >
506 inline
508 {
509  assert(A.num() == dim());
510 
511  R y;
512 
513  clear();
514 
515  for( int i = dim() - 1; i >= 0; --i )
516  {
517  y = A[i] * x;
518 
519  if( isNotZero(y, epsilon) )
520  {
521  VectorBase<R>::val[i] = y;
522  IdxSet::addIdx(i);
523  }
524  }
525 
526  assert(isConsistent());
527 
528  return *this;
529 }
530 
531 
532 
533 /// Assigns SSVectorBase to \f$A \cdot x\f$ for a setup \p x.
534 #define shortProductFactor 0.5
535 template < class R >
536 template < class S, class T >
537 inline
539 {
540  assert(A.num() == x.dim());
541  assert(x.isSetup());
542  clear();
543 
544  if( x.size() == 1 )
545  {
546  assign2product1(A, x);
547  setupStatus = true;
548  }
549  else if( isSetup() && (double(x.size()) * A.memSize() <= shortProductFactor * dim() * A.num()) )
550  {
551  assign2productShort(A, x);
552  setupStatus = true;
553  }
554  else
555  {
556  assign2productFull(A, x);
557  setupStatus = false;
558  }
559 
560  assert(isConsistent());
561 
562  return *this;
563 }
564 
565 
566 
567 /// Assignment helper.
568 template < class R >
569 template < class S, class T >
570 inline
572 {
573  assert(x.isSetup());
574  assert(x.size() == 1);
575 
576  // get the nonzero value of x and the corresponding vector in A:
577  const int nzidx = x.idx[0];
578  const T nzval = x.val[nzidx];
579  const SVectorBase<S>& Ai = A[nzidx];
580 
581  // compute A[nzidx] * nzval:
582  if( isZero(nzval, epsilon) || Ai.size() == 0 )
583  clear(); // this := zero vector
584  else
585  {
586  num = Ai.size();
587  for( int j = num - 1; j >= 0; --j )
588  {
589  const Nonzero<S>& Aij = Ai.element(j);
590  idx[j] = Aij.idx;
591  VectorBase<R>::val[Aij.idx] = nzval * Aij.val;
592  }
593  }
594 
595  assert(isConsistent());
596 
597  return *this;
598 }
599 
600 
601 
602 /// Assignment helper.
603 template < class R >
604 template < class S, class T >
605 inline
607 {
608  assert(x.isSetup());
609 
610  if( x.size() == 0 ) // x can be setup but have size 0 => this := zero vector
611  {
612  clear();
613  return *this;
614  }
615 
616  // compute x[0] * A[0]
617  int curidx = x.idx[0];
618  const T x0 = x.val[curidx];
619  const SVectorBase<S>& A0 = A[curidx];
620  int nonzero_idx = 0;
621  int xsize = x.size();
622  int Aisize;
623 
624  num = A0.size();
625  if( isZero(x0, epsilon) || num == 0 )
626  {
627  // A[0] == 0 or x[0] == 0 => this := zero vector
628  clear();
629  }
630  else
631  {
632  for( int j = 0; j < num; ++j )
633  {
634  const Nonzero<S>& elt = A0.element(j);
635  const R product = x0 * elt.val;
636 
637  // store the value in any case
638  idx[nonzero_idx] = elt.idx;
639  VectorBase<R>::val[elt.idx] = product;
640 
641  // count only non-zero values; not 'isNotZero(product, epsilon)'
642  if( product != 0 )
643  ++nonzero_idx;
644  }
645  }
646 
647  // Compute the other x[i] * A[i] and add them to the existing vector.
648  for( int i = 1; i < xsize; ++i )
649  {
650  curidx = x.idx[i];
651  const T xi = x.val[curidx];
652  const SVectorBase<S>& Ai = A[curidx];
653 
654  // If A[i] == 0 or x[i] == 0, do nothing.
655  Aisize = Ai.size();
656  if ( isNotZero(xi, epsilon) || Aisize == 0 )
657  {
658  // Compute x[i] * A[i] and add it to the existing vector.
659  for( int j = 0; j < Aisize; ++j )
660  {
661  const Nonzero<S>& elt = Ai.element(j);
662  idx[nonzero_idx] = elt.idx;
663  R oldval = VectorBase<R>::val[elt.idx];
664 
665  // An old value of exactly 0 means the position is still unused.
666  // It will be used now (either by a new nonzero or by a SOPLEX_VECTOR_MARKER),
667  // so increase the counter. If oldval != 0, we just
668  // change an existing NZ-element, so don't increase the counter.
669  if( oldval == 0 )
670  ++nonzero_idx;
671 
672  // Add the current product x[i] * A[i][j]; if oldval was
673  // SOPLEX_VECTOR_MARKER before, it does not hurt because SOPLEX_VECTOR_MARKER is really small.
674  oldval += xi * elt.val;
675 
676  // If the new value is exactly 0, mark the index as used
677  // by setting a value which is nearly 0; otherwise, store
678  // the value. Values below epsilon will be removed later.
679  if( oldval == 0 )
681  else
682  VectorBase<R>::val[elt.idx] = oldval;
683  }
684  }
685  }
686 
687  // Clean up by shifting all nonzeros (w.r.t. epsilon) to the front of idx,
688  // zeroing all values which are nearly 0, and setting #num# appropriately.
689  int nz_counter = 0;
690 
691  for( int i = 0; i < nonzero_idx; ++i )
692  {
693  curidx = idx[i];
694 
695  if( isZero( VectorBase<R>::val[curidx], epsilon ) )
696  VectorBase<R>::val[curidx] = 0;
697  else
698  {
699  idx[nz_counter] = curidx;
700  ++nz_counter;
701  }
702 
703  num = nz_counter;
704  }
705 
706  assert(isConsistent());
707 
708  return *this;
709 }
710 
711 
712 
713 /// Assignment helper.
714 template < class R >
715 template < class S, class T >
716 inline
718 {
719  assert(x.isSetup());
720 
721  if( x.size() == 0 ) // x can be setup but have size 0 => this := zero vector
722  {
723  clear();
724  return *this;
725  }
726 
727  bool A_is_zero = true;
728  int xsize = x.size();
729  int Aisize;
730 
731  for( int i = 0; i < xsize; ++i )
732  {
733  const int curidx = x.idx[i];
734  const T xi = x.val[curidx];
735  const SVectorBase<S>& Ai = A[curidx];
736  Aisize = Ai.size();
737 
738  if( A_is_zero && Aisize > 0 )
739  A_is_zero = false;
740 
741  for( int j = 0; j < Aisize; ++j )
742  {
743  const Nonzero<S>& elt = Ai.element(j);
744  VectorBase<R>::val[elt.idx] += xi * elt.val;
745  }
746  }
747 
748  if( A_is_zero )
749  clear(); // case x != 0 but A == 0
750 
751  return *this;
752 }
753 
754 
755 
756 /// Assigns SSVectorBase to \f$A \cdot x\f$ thereby setting up \p x.
757 template < class R >
758 template < class S, class T >
759 inline
761 {
762  if( x.isSetup() )
763  return assign2product4setup(A, x);
764 
765  if( x.dim() == 0 )
766  { // x == 0 => this := zero vector
767  clear();
768  x.num = 0;
769  }
770  else
771  {
772  // x is not setup, so walk through its value vector
773  int nzcount = 0;
774  int end = x.dim();
775 
776  for( int i = 0; i < end; ++i )
777  {
778  // advance to the next element != 0
779  T& xval = x.val[i];
780 
781  if( xval != 0 )
782  {
783  // If x[i] is really nonzero, compute A[i] * x[i] and adapt x.idx,
784  // otherwise set x[i] to 0.
785  if( isNotZero(xval, epsilon) )
786  {
787  const SVectorBase<S>& Ai = A[i];
788  x.idx[ nzcount++ ] = i;
789 
790  for( int j = Ai.size() - 1; j >= 0; --j )
791  {
792  const Nonzero<S>& elt = Ai.element(j);
793  VectorBase<R>::val[elt.idx] += xval * elt.val;
794  }
795  }
796  else
797  xval = 0;
798  }
799  }
800 
801  x.num = nzcount;
802  setupStatus = false;
803  }
804 
805  x.setupStatus = true;
806 
807  assert(isConsistent());
808 
809  return *this;
810 }
811 
812 
813 
814 /// Assigns only the elements of \p rhs.
815 template < class R >
816 template < class S >
817 inline
819 {
820  assert(rhs.dim() <= VectorBase<R>::dim());
821 
822  int s = rhs.size();
823  num = 0;
824 
825  for( int i = 0; i < s; ++i )
826  {
827  int k = rhs.index(i);
828  S v = rhs.value(i);
829 
830  if( isZero(v, epsilon) )
831  VectorBase<R>::val[k] = 0;
832  else
833  {
834  VectorBase<R>::val[k] = v;
835  idx[num++] = k;
836  }
837  }
838 
839  setupStatus = true;
840 
841  assert(isConsistent());
842 
843  return *this;
844 }
845 
846 
847 
848 /// Assigns only the elements of \p rhs.
849 template < >
850 template < >
851 inline
853 {
854  assert(rhs.dim() <= VectorBase<Rational>::dim());
855 
856  int s = rhs.size();
857  num = 0;
858 
859  for( int i = 0; i < s; ++i )
860  {
861  int k = rhs.index(i);
862  const Rational& v = rhs.value(i);
863 
864  if( v == 0 )
866  else
867  {
869  idx[num++] = k;
870  }
871  }
872 
873  setupStatus = true;
874 
875  assert(isConsistent());
876 
877  return *this;
878 }
879 
880 
881 
882 /// Assignment operator.
883 template < class R >
884 template < class S >
885 inline
887 {
888  clear();
889 
890  return assign(rhs);
891 }
892 
893 
894 
895 // ---------------------------------------------------------------------------------------------------------------------
896 // Methods of SVectorBase
897 // ---------------------------------------------------------------------------------------------------------------------
898 
899 
900 
901 /// Assignment operator.
902 template < class R >
903 template < class S >
904 inline
906 {
907  int n = 0;
908  Nonzero<R> *e = m_elem;
909 
910  clear();
911 
912  for( int i = vec.dim() - 1; i >= 0; --i )
913  {
914  if( vec[i] != 0 )
915  {
916  assert(n < max());
917 
918  e->idx = i;
919  e->val = vec[i];
920  ++e;
921  ++n;
922  }
923  }
924 
925  set_size(n);
926 
927  return *this;
928 }
929 
930 
931 
932 /// Assignment operator (specialization for Real).
933 template <>
934 template < class S >
935 inline
937 {
938  int n = 0;
939  Nonzero<Real> *e = m_elem;
940 
941  clear();
942 
943  for( int i = vec.dim() - 1; i >= 0; --i )
944  {
945  if( vec[i] != 0 )
946  {
947  assert(n < max());
948 
949  e->idx = i;
950  e->val = Real(vec[i]);
951  ++e;
952  ++n;
953  }
954  }
955 
956  set_size(n);
957 
958  return *this;
959 }
960 
961 
962 
963 /// Assignment operator.
964 template < class R >
965 template < class S >
966 inline
968 {
969  assert(max() >= sv.size());
970 
971  set_size(sv.size());
972 
973  Nonzero<R> *e = m_elem;
974 
975  for( int i = size() - 1; i >= 0; --i )
976  {
977  e->idx = sv.index(i);
978  e->val = sv[e->idx];
979  ++e;
980  }
981 
982  return *this;
983 }
984 
985 
986 
987 /// Inner product.
988 template < class R >
989 inline
991 {
992  R x = 0;
993  Nonzero<R>* e = m_elem;
994 
995  for( int i = size() - 1; i >= 0; --i )
996  {
997  x += e->val * w[e->idx];
998  e++;
999  }
1000 
1001  return x;
1002 }
1003 
1004 
1005 
1006 // ---------------------------------------------------------------------------------------------------------------------
1007 // Methods of DSVectorBase
1008 // ---------------------------------------------------------------------------------------------------------------------
1009 
1010 
1011 
1012 /// Copy constructor.
1013 template < class R >
1014 template < class S >
1015 inline
1017  : theelem(0)
1018 {
1019  allocMem((vec.dim() < 1) ? 2 : vec.dim());
1020  *this = vec;
1021 
1022  assert(isConsistent());
1023 }
1024 
1025 
1026 
1027 /// Copy constructor.
1028 template < class R >
1029 template < class S >
1030 inline
1032  : theelem(0)
1033 {
1034  allocMem(old.size() < 1 ? 2 : old.size());
1036 
1037  assert(isConsistent());
1038 }
1039 
1040 
1041 
1042 /// Assignment operator.
1043 template < class R >
1044 template < class S >
1045 inline
1047 {
1048  assert(this != (const DSVectorBase<R>*)(&vec));
1049 
1051  setMax(vec.dim());
1053 
1054  assert(isConsistent());
1055 
1056  return *this;
1057 }
1058 
1059 
1060 
1061 /// Assignment operator.
1062 template < class R >
1063 template < class S >
1064 inline
1066 {
1067  assert(this != &vec);
1068 
1070  makeMem(vec.size());
1072 
1073  return *this;
1074 }
1075 
1076 
1077 
1078 // ---------------------------------------------------------------------------------------------------------------------
1079 // Operators
1080 // ---------------------------------------------------------------------------------------------------------------------
1081 
1082 
1083 
1084 /// Output operator.
1085 template < class R >
1086 inline
1087 std::ostream& operator<<(std::ostream& s, const VectorBase<R>& vec)
1088 {
1089  int i;
1090 
1091  s << '(';
1092 
1093  for( i = 0; i < vec.dim() - 1; ++i )
1094  s << vec[i] << ", ";
1095 
1096  s << vec[i] << ')';
1097 
1098  return s;
1099 }
1100 
1101 
1102 
1103 /// Negation.
1104 template < class R >
1105 inline
1107 {
1108  DVectorBase<R> res(vec.dim());
1109 
1110  for( int i = 0; i < res.dim(); ++i )
1111  res[i] = -vec[i];
1112 
1113  return res;
1114 }
1115 
1116 
1117 
1118 /// Addition.
1119 template < class R >
1120 inline
1122 {
1123  assert(v.dim() == w.dim());
1124 
1125  DVectorBase<R> res(v.dim());
1126 
1127  for( int i = 0; i < res.dim(); ++i )
1128  res[i] = v[i] + w[i];
1129 
1130  return res;
1131 }
1132 
1133 
1134 
1135 /// Addition.
1136 template < class R >
1137 inline
1139 {
1140  DVectorBase<R> res(v);
1141 
1142  res += w;
1143 
1144  return res;
1145 }
1146 
1147 
1148 
1149 /// Addition.
1150 template < class R >
1151 inline
1153 {
1154  return w + v;
1155 }
1156 
1157 
1158 
1159 /// Subtraction.
1160 template < class R >
1161 inline
1163 {
1164  assert(v.dim() == w.dim());
1165 
1166  DVectorBase<R> res(v.dim());
1167 
1168  for( int i = 0; i < res.dim(); ++i )
1169  res[i] = v[i] - w[i];
1170 
1171  return res;
1172 }
1173 
1174 
1175 
1176 /// Subtraction.
1177 template < class R >
1178 inline
1180 {
1181  DVectorBase<R> res(v);
1182 
1183  res -= w;
1184 
1185  return res;
1186 }
1187 
1188 
1189 
1190 /// Subtraction.
1191 template < class R >
1192 inline
1194 {
1195  DVectorBase<R> res(w.dim());
1196 
1197  for( int i = 0; i < res.dim(); ++i )
1198  res[i] = -w[i];
1199 
1200  res += v;
1201 
1202  return res;
1203 }
1204 
1205 
1206 
1207 /// Scaling.
1208 template < class R >
1209 inline
1211 {
1212  DVectorBase<R> res(v.dim());
1213 
1214  for( int i = 0; i < res.dim(); ++i )
1215  res[i] = x * v[i];
1216 
1217  return res;
1218 }
1219 
1220 
1221 
1222 /// Scaling.
1223 template < class R >
1224 inline
1226 {
1227  return v * x;
1228 }
1229 
1230 
1231 
1232 /// Scaling.
1233 template < class R >
1234 inline
1236 {
1237  DSVectorBase<R> res(v.size());
1238 
1239  for( int i = 0; i < v.size(); ++i )
1240  res.add(v.index(i), v.value(i) * x);
1241 
1242  return res;
1243 }
1244 
1245 
1246 
1247 /// Scaling.
1248 template < class R >
1249 inline
1251 {
1252  return v * x;
1253 }
1254 
1255 
1256 
1257 /// Input operator.
1258 template < class R >
1259 inline
1260 std::istream& operator>>(std::istream& s, DVectorBase<R>& vec)
1261 {
1262  char c;
1263  R val;
1264  int i = 0;
1265 
1266  while( s.get(c).good() )
1267  {
1268  if( c != ' ' && c != '\t' && c != '\n' )
1269  break;
1270  }
1271 
1272  if( c != '(' )
1273  s.putback(c);
1274  else
1275  {
1276  do
1277  {
1278  s >> val;
1279 
1280  if( i >= vec.dim() - 1 )
1281  vec.reDim(i + 16);
1282  vec[i++] = val;
1283 
1284  while( s.get(c).good() )
1285  {
1286  if( c != ' ' && c != '\t' && c != '\n' )
1287  break;
1288  }
1289 
1290  if( c != ',' )
1291  {
1292  if (c != ')')
1293  s.putback(c);
1294  break;
1295  }
1296  }
1297  while( s.good() );
1298  }
1299 
1300  vec.reDim(i);
1301 
1302  return s;
1303 }
1304 
1305 
1306 
1307 /// Output operator.
1308 template < class R >
1309 inline
1310 std::ostream& operator<<(std::ostream& os, const SVectorBase<R>& v)
1311 {
1312  for( int i = 0, j = 0; i < v.size(); ++i )
1313  {
1314  if( j )
1315  {
1316  if( v.value(i) < 0 )
1317  os << " - " << -v.value(i);
1318  else
1319  os << " + " << v.value(i);
1320  }
1321  else
1322  os << v.value(i);
1323 
1324  os << " x" << v.index(i);
1325  j = 1;
1326 
1327  if( (i + 1) % 4 == 0 )
1328  os << "\n\t";
1329  }
1330 
1331  return os;
1332 }
1333 
1334 
1335 
1336 /// Output operator.
1337 template < class R >
1338 inline
1339 std::ostream& operator<<(std::ostream& os, const SVSetBase<R>& s)
1340 {
1341  for( int i = 0; i < s.num(); ++i )
1342  os << s[i] << "\n";
1343 
1344  return os;
1345 }
1346 }
1347 
1348 /* reset the SOPLEX_DEBUG flag to its original value */
1349 #undef SOPLEX_DEBUG
1350 #ifdef SOPLEX_DEBUG_BASEVECTORS
1351 #define SOPLEX_DEBUG
1352 #undef SOPLEX_DEBUG_BASEVECTORS
1353 #endif
1354 
1355 #endif // _BASEVECTORS_H_
bool isNotZero(Real a, Real eps=Param::epsilon())
returns true iff |a| > eps
Definition: spxdefines.h:413
int * idx
array of indices
Definition: idxset.h:65
void makeMem(int n)
Ensure there is room for n new nonzeros.
Definition: dsvectorbase.h:71
Sparse vector nonzero element.
Definition: svectorbase.h:35
void reDim(int newdim, const bool setZero=true)
Resets DVectorBase&#39;s dimension to newdim.
Definition: dvectorbase.h:249
DVectorBase< R > operator+(const VectorBase< R > &v, const VectorBase< R > &w)
Addition.
Definition: basevectors.h:1121
DSVectorBase< R > & operator=(const SVectorBase< S > &vec)
Assignment operator.
Definition: dsvectorbase.h:149
std::istream & operator>>(std::istream &s, DVectorBase< R > &vec)
Input operator.
Definition: basevectors.h:1260
SVectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: basevectors.h:905
void allocMem(int n)
Allocate memory for n nonzeros.
Definition: dsvectorbase.h:62
VectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: vectorbase.h:111
Dynamic dense vectors.Class DVectorBase is a derived class of VectorBase adding automatic memory mana...
Definition: dvectorbase.h:48
Dense vector.Class VectorBase provides dense linear algebra vectors. It does not provide memory manag...
Definition: dsvectorbase.h:28
Nonzero< R > & element(int n)
Reference to the n &#39;th nonzero element.
Definition: svectorbase.h:218
Set of sparse vectors.
#define SOPLEX_VECTOR_MARKER
Definition: basevectors.h:39
Dynamic sparse vectors.Class DSVectorBase implements dynamic sparse vectors, i.e. SVectorBases with a...
Definition: dsvectorbase.h:42
int size() const
Number of used indices.
Definition: svectorbase.h:152
R & value(int n)
Reference to value of n &#39;th nonzero.
Definition: svectorbase.h:254
int memSize() const
Used nonzero memory.
Definition: svsetbase.h:806
bool isConsistent() const
Consistency check.
Definition: dsvectorbase.h:289
Wrapper for GMP type mpq_class.We wrap mpq_class so that we can replace it by a double type if GMP is...
Definition: rational.h:45
Dynamic sparse vectors.
bool setupStatus
Is the SSVectorBase set up?
Definition: ssvectorbase.h:59
int idx
Index of nonzero element.
Definition: svectorbase.h:40
VectorBase< R > & operator+=(const VectorBase< S > &vec)
Addition.
Definition: vectorbase.h:230
Semi sparse vector.This class implements semi-sparse vectors. Such are DVectorBases where the indices...
Definition: dsvectorbase.h:29
void add(const SVectorBase< S > &vec)
Append nonzeros of sv.
Definition: dsvectorbase.h:216
Nonzero< R > * theelem
Memory.
Definition: dsvectorbase.h:53
int num() const
Current number of SVectorBases.
Definition: svsetbase.h:746
int dim() const
Dimension of the vector defined as maximal index + 1.
Definition: svectorbase.h:166
double Real
SOPLEX_DEBUG.
Definition: spxdefines.h:200
int index(int n) const
Returns index of the n &#39;th nonzero element.
Definition: ssvectorbase.h:174
int & index(int n)
Reference to index of n &#39;th nonzero.
Definition: svectorbase.h:236
DVectorBase< R > operator-(const VectorBase< R > &vec)
Negation.
Definition: basevectors.h:1106
DVectorBase< R > operator*(const VectorBase< R > &v, R x)
Scaling.
Definition: basevectors.h:1210
Wrapper for GMP types.
R operator*(const VectorBase< R > &w) const
Inner product.
Definition: basevectors.h:990
R * val
Values of vector.
Definition: vectorbase.h:87
int dim() const
Dimension of vector.
Definition: vectorbase.h:174
DSVectorBase(int n=8)
Default constructor.
Definition: dsvectorbase.h:94
Debugging, floating point type and parameter definitions.
bool isSetup() const
Returns setup status.
Definition: ssvectorbase.h:120
VectorBase< R > & multAdd(const S &x, const VectorBase< T > &vec)
Addition of scaled vector.
Definition: vectorbase.h:369
Everything should be within this namespace.
Dense vector.
void clear()
Remove all indices.
Definition: svectorbase.h:396
VectorBase< R > & operator-=(const VectorBase< S > &vec)
Subtraction.
Definition: vectorbase.h:251
Sparse vectors.
Semi sparse vector.
R operator*(const VectorBase< R > &vec) const
Inner product.
Definition: vectorbase.h:295
R val
Value of nonzero element.
Definition: svectorbase.h:39
int dim() const
Dimension of VectorBase.
Definition: ssvectorbase.h:532
const int * indexMem() const
Returns array indices.
Definition: ssvectorbase.h:282
Sparse vectors.Class SVectorBase provides packed sparse vectors. Such are a sparse vectors...
Definition: dvectorbase.h:31
bool isZero(Real a, Real eps=Param::epsilon())
returns true iff |a| <= eps
Definition: spxdefines.h:407
VectorBase< R > & assign(const SVectorBase< S > &vec)
Assign values of vec.
Definition: basevectors.h:80
int num
number of used indices
Definition: idxset.h:63
#define shortProductFactor
Assigns SSVectorBase to for a setup x.
Definition: basevectors.h:534
R value(int n) const
Returns value of the n &#39;th nonzero element.
Definition: ssvectorbase.h:182
DSVectorBase< R > operator*(R x, const SVectorBase< R > &v)
Scaling.
Definition: basevectors.h:1250
Sparse vector set.Class SVSetBase provides a set of sparse vectors SVectorBase. All SVectorBases in a...
Definition: ssvectorbase.h:33
VectorBase< R > & multSub(const S &x, const SVectorBase< T > &vec)
Subtraction of scaled vector.
Definition: basevectors.h:290
int size() const
Returns the number of nonzeros.
Definition: ssvectorbase.h:199
Dynamic dense vectors.
void setMax(int newmax=1)
Reset nonzero memory to >= newmax.
Definition: dsvectorbase.h:248