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