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-2019 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  StableSum<R> x;
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  StableSum<R> x;
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 
426  if(isNotZero(x, epsilon))
427  v[j] = x;
428  else
429  {
430  adjust = true;
431  v[j] = SOPLEX_VECTOR_MARKER;
432  }
433  }
434  else
435  {
436  x = xx * vec.value(i);
437 
438  if(isNotZero(x, epsilon))
439  {
440  v[j] = x;
441  addIdx(j);
442  }
443  }
444  }
445 
446  if(adjust)
447  {
448  int* iptr = idx;
449  int* iiptr = idx;
450  int* endptr = idx + num;
451 
452  for(; iptr < endptr; ++iptr)
453  {
454  x = v[*iptr];
455 
456  if(isNotZero(x, epsilon))
457  *iiptr++ = *iptr;
458  else
459  v[*iptr] = 0;
460  }
461 
462  num = int(iiptr - idx);
463  }
464  }
465  else
466  VectorBase<R>::multAdd(xx, vec);
467 
468  assert(isConsistent());
469 
470  return *this;
471 }
472 
473 
474 /// Assigns pair wise vector product of setup x and setup y to SSVectorBase.
475 template < class R >
476 template < class S, class T >
477 inline
479  const SSVectorBase<T>& y)
480 {
481  assert(dim() == x.dim());
482  assert(x.dim() == y.dim());
483  assert(x.isSetup());
484  assert(y.isSetup());
485 
486  clear();
487  setupStatus = false;
488 
489  int i = 0;
490  int j = 0;
491  int n = x.size() - 1;
492  int m = y.size() - 1;
493 
494  /* both x and y non-zero vectors? */
495  if(m >= 0 && n >= 0)
496  {
497  int xi = x.index(i);
498  int yj = y.index(j);
499 
500  while(i < n && j < m)
501  {
502  if(xi == yj)
503  {
504  VectorBase<R>::val[xi] = R(x.val[xi]) * R(y.val[xi]);
505  xi = x.index(++i);
506  yj = y.index(++j);
507  }
508  else if(xi < yj)
509  xi = x.index(++i);
510  else
511  yj = y.index(++j);
512  }
513 
514  /* check (possible) remaining indices */
515 
516  while(i < n && xi != yj)
517  xi = x.index(++i);
518 
519  while(j < m && xi != yj)
520  yj = y.index(++j);
521 
522  if(xi == yj)
523  VectorBase<R>::val[xi] = R(x.val[xi]) * R(y.val[xi]);
524  }
525 
526  setup();
527 
528  assert(isConsistent());
529 
530  return *this;
531 }
532 
533 
534 
535 /// Assigns \f$x^T \cdot A\f$ to SSVectorBase.
536 template < class R >
537 template < class S, class T >
538 inline
540 {
541  assert(A.num() == dim());
542 
543  R y;
544 
545  clear();
546 
547  for(int i = dim() - 1; i >= 0; --i)
548  {
549  y = A[i] * x;
550 
551  if(isNotZero(y, epsilon))
552  {
553  VectorBase<R>::val[i] = y;
554  IdxSet::addIdx(i);
555  }
556  }
557 
558  assert(isConsistent());
559 
560  return *this;
561 }
562 
563 
564 
565 /// Assigns SSVectorBase to \f$A \cdot x\f$ for a setup \p x.
566 #define shortProductFactor 0.5
567 template < class R >
568 template < class S, class T >
569 inline
571  const SSVectorBase<T>& x)
572 {
573  assert(A.num() == x.dim());
574  assert(x.isSetup());
575  clear();
576 
577  if(x.size() == 1)
578  {
579  assign2product1(A, x);
580  setupStatus = true;
581  }
582  else if(isSetup() && (double(x.size()) * A.memSize() <= shortProductFactor * dim() * A.num()))
583  {
584  assign2productShort(A, x);
585  setupStatus = true;
586  }
587  else
588  {
589  assign2productFull(A, x);
590  setupStatus = false;
591  }
592 
593  assert(isConsistent());
594 
595  return *this;
596 }
597 
598 
599 
600 /// Assignment helper.
601 template < class R >
602 template < class S, class T >
603 inline
605 {
606  assert(x.isSetup());
607  assert(x.size() == 1);
608 
609  // get the nonzero value of x and the corresponding vector in A:
610  const int nzidx = x.idx[0];
611  const T nzval = x.val[nzidx];
612  const SVectorBase<S>& Ai = A[nzidx];
613 
614  // compute A[nzidx] * nzval:
615  if(isZero(nzval, epsilon) || Ai.size() == 0)
616  clear(); // this := zero vector
617  else
618  {
619  num = Ai.size();
620 
621  for(int j = num - 1; j >= 0; --j)
622  {
623  const Nonzero<S>& Aij = Ai.element(j);
624  idx[j] = Aij.idx;
625  VectorBase<R>::val[Aij.idx] = nzval * Aij.val;
626  }
627  }
628 
629  assert(isConsistent());
630 
631  return *this;
632 }
633 
634 
635 
636 /// Assignment helper.
637 template < class R >
638 template < class S, class T >
639 inline
641  const SSVectorBase<T>& x)
642 {
643  assert(x.isSetup());
644 
645  if(x.size() == 0) // x can be setup but have size 0 => this := zero vector
646  {
647  clear();
648  return *this;
649  }
650 
651  // compute x[0] * A[0]
652  int curidx = x.idx[0];
653  const T x0 = x.val[curidx];
654  const SVectorBase<S>& A0 = A[curidx];
655  int nonzero_idx = 0;
656  int xsize = x.size();
657  int Aisize;
658 
659  num = A0.size();
660 
661  if(isZero(x0, epsilon) || num == 0)
662  {
663  // A[0] == 0 or x[0] == 0 => this := zero vector
664  clear();
665  }
666  else
667  {
668  for(int j = 0; j < num; ++j)
669  {
670  const Nonzero<S>& elt = A0.element(j);
671  const R product = x0 * elt.val;
672 
673  // store the value in any case
674  idx[nonzero_idx] = elt.idx;
675  VectorBase<R>::val[elt.idx] = product;
676 
677  // count only non-zero values; not 'isNotZero(product, epsilon)'
678  if(product != 0)
679  ++nonzero_idx;
680  }
681  }
682 
683  // Compute the other x[i] * A[i] and add them to the existing vector.
684  for(int i = 1; i < xsize; ++i)
685  {
686  curidx = x.idx[i];
687  const T xi = x.val[curidx];
688  const SVectorBase<S>& Ai = A[curidx];
689 
690  // If A[i] == 0 or x[i] == 0, do nothing.
691  Aisize = Ai.size();
692 
693  if(isNotZero(xi, epsilon) || Aisize == 0)
694  {
695  // Compute x[i] * A[i] and add it to the existing vector.
696  for(int j = 0; j < Aisize; ++j)
697  {
698  const Nonzero<S>& elt = Ai.element(j);
699  idx[nonzero_idx] = elt.idx;
700  R oldval = VectorBase<R>::val[elt.idx];
701 
702  // An old value of exactly 0 means the position is still unused.
703  // It will be used now (either by a new nonzero or by a SOPLEX_VECTOR_MARKER),
704  // so increase the counter. If oldval != 0, we just
705  // change an existing NZ-element, so don't increase the counter.
706  if(oldval == 0)
707  ++nonzero_idx;
708 
709  // Add the current product x[i] * A[i][j]; if oldval was
710  // SOPLEX_VECTOR_MARKER before, it does not hurt because SOPLEX_VECTOR_MARKER is really small.
711  oldval += xi * elt.val;
712 
713  // If the new value is exactly 0, mark the index as used
714  // by setting a value which is nearly 0; otherwise, store
715  // the value. Values below epsilon will be removed later.
716  if(oldval == 0)
718  else
719  VectorBase<R>::val[elt.idx] = oldval;
720  }
721  }
722  }
723 
724  // Clean up by shifting all nonzeros (w.r.t. epsilon) to the front of idx,
725  // zeroing all values which are nearly 0, and setting #num# appropriately.
726  int nz_counter = 0;
727 
728  for(int i = 0; i < nonzero_idx; ++i)
729  {
730  curidx = idx[i];
731 
732  if(isZero(VectorBase<R>::val[curidx], epsilon))
733  VectorBase<R>::val[curidx] = 0;
734  else
735  {
736  idx[nz_counter] = curidx;
737  ++nz_counter;
738  }
739 
740  num = nz_counter;
741  }
742 
743  assert(isConsistent());
744 
745  return *this;
746 }
747 
748 
749 
750 /// Assignment helper.
751 template < class R >
752 template < class S, class T >
753 inline
755  const SSVectorBase<T>& x)
756 {
757  assert(x.isSetup());
758 
759  if(x.size() == 0) // x can be setup but have size 0 => this := zero vector
760  {
761  clear();
762  return *this;
763  }
764 
765  bool A_is_zero = true;
766  int xsize = x.size();
767  int Aisize;
768 
769  for(int i = 0; i < xsize; ++i)
770  {
771  const int curidx = x.idx[i];
772  const T xi = x.val[curidx];
773  const SVectorBase<S>& Ai = A[curidx];
774  Aisize = Ai.size();
775 
776  if(A_is_zero && Aisize > 0)
777  A_is_zero = false;
778 
779  for(int j = 0; j < Aisize; ++j)
780  {
781  const Nonzero<S>& elt = Ai.element(j);
782  VectorBase<R>::val[elt.idx] += xi * elt.val;
783  }
784  }
785 
786  if(A_is_zero)
787  clear(); // case x != 0 but A == 0
788 
789  return *this;
790 }
791 
792 
793 
794 /// Assigns SSVectorBase to \f$A \cdot x\f$ thereby setting up \p x.
795 template < class R >
796 template < class S, class T >
797 inline
799 {
800  if(x.isSetup())
801  return assign2product4setup(A, x);
802 
803  if(x.dim() == 0)
804  {
805  // x == 0 => this := zero vector
806  clear();
807  x.num = 0;
808  }
809  else
810  {
811  // x is not setup, so walk through its value vector
812  int nzcount = 0;
813  int end = x.dim();
814 
815  for(int i = 0; i < end; ++i)
816  {
817  // advance to the next element != 0
818  T& xval = x.val[i];
819 
820  if(xval != 0)
821  {
822  // If x[i] is really nonzero, compute A[i] * x[i] and adapt x.idx,
823  // otherwise set x[i] to 0.
824  if(isNotZero(xval, epsilon))
825  {
826  const SVectorBase<S>& Ai = A[i];
827  x.idx[ nzcount++ ] = i;
828 
829  for(int j = Ai.size() - 1; j >= 0; --j)
830  {
831  const Nonzero<S>& elt = Ai.element(j);
832  VectorBase<R>::val[elt.idx] += xval * elt.val;
833  }
834  }
835  else
836  xval = 0;
837  }
838  }
839 
840  x.num = nzcount;
841  setupStatus = false;
842  }
843 
844  x.setupStatus = true;
845 
846  assert(isConsistent());
847 
848  return *this;
849 }
850 
851 
852 
853 /// Assigns only the elements of \p rhs.
854 template < class R >
855 template < class S >
856 inline
858 {
859  assert(rhs.dim() <= VectorBase<R>::dim());
860 
861  int s = rhs.size();
862  num = 0;
863 
864  for(int i = 0; i < s; ++i)
865  {
866  int k = rhs.index(i);
867  S v = rhs.value(i);
868 
869  if(isZero(v, epsilon))
870  VectorBase<R>::val[k] = 0;
871  else
872  {
873  VectorBase<R>::val[k] = v;
874  idx[num++] = k;
875  }
876  }
877 
878  setupStatus = true;
879 
880  assert(isConsistent());
881 
882  return *this;
883 }
884 
885 
886 
887 /// Assigns only the elements of \p rhs.
888 template < >
889 template < >
890 inline
892 {
893  assert(rhs.dim() <= VectorBase<Rational>::dim());
894 
895  int s = rhs.size();
896  num = 0;
897 
898  for(int i = 0; i < s; ++i)
899  {
900  int k = rhs.index(i);
901  const Rational& v = rhs.value(i);
902 
903  if(v == 0)
905  else
906  {
908  idx[num++] = k;
909  }
910  }
911 
912  setupStatus = true;
913 
914  assert(isConsistent());
915 
916  return *this;
917 }
918 
919 
920 
921 /// Assignment operator.
922 template < class R >
923 template < class S >
924 inline
926 {
927  clear();
928 
929  return assign(rhs);
930 }
931 
932 
933 
934 // ---------------------------------------------------------------------------------------------------------------------
935 // Methods of SVectorBase
936 // ---------------------------------------------------------------------------------------------------------------------
937 
938 
939 
940 /// Assignment operator.
941 template < class R >
942 template < class S >
943 inline
945 {
946  int n = 0;
947  Nonzero<R>* e = m_elem;
948 
949  clear();
950 
951  for(int i = vec.dim() - 1; i >= 0; --i)
952  {
953  if(vec[i] != 0)
954  {
955  assert(n < max());
956 
957  e->idx = i;
958  e->val = vec[i];
959  ++e;
960  ++n;
961  }
962  }
963 
964  set_size(n);
965 
966  return *this;
967 }
968 
969 
970 
971 /// Assignment operator (specialization for Real).
972 template <>
973 template < class S >
974 inline
976 {
977  int n = 0;
978  Nonzero<Real>* e = m_elem;
979 
980  clear();
981 
982  for(int i = vec.dim() - 1; i >= 0; --i)
983  {
984  if(vec[i] != 0)
985  {
986  assert(n < max());
987 
988  e->idx = i;
989  e->val = Real(vec[i]);
990  ++e;
991  ++n;
992  }
993  }
994 
995  set_size(n);
996 
997  return *this;
998 }
999 
1000 
1001 
1002 /// Assignment operator.
1003 template < class R >
1004 template < class S >
1005 inline
1007 {
1008  assert(sv.isSetup());
1009  assert(max() >= sv.size());
1010 
1011  int nnz = 0;
1012  int idx;
1013 
1014  Nonzero<R>* e = m_elem;
1015 
1016  for(int i = 0; i < nnz; ++i)
1017  {
1018  idx = sv.index(i);
1019 
1020  if(sv.value(idx) != 0.0)
1021  {
1022  e->idx = idx;
1023  e->val = sv[idx];
1024  ++e;
1025  ++nnz;
1026  }
1027  }
1028 
1029  set_size(nnz);
1030 
1031  return *this;
1032 }
1033 
1034 
1035 
1036 /// Inner product.
1037 template < class R >
1038 inline
1040 {
1041  StableSum<R> x;
1042  Nonzero<R>* e = m_elem;
1043 
1044  for(int i = size() - 1; i >= 0; --i)
1045  {
1046  x += e->val * w[e->idx];
1047  e++;
1048  }
1049 
1050  return x;
1051 }
1052 
1053 
1054 
1055 // ---------------------------------------------------------------------------------------------------------------------
1056 // Methods of DSVectorBase
1057 // ---------------------------------------------------------------------------------------------------------------------
1058 
1059 
1060 
1061 /// Copy constructor.
1062 template < class R >
1063 template < class S >
1064 inline
1066  : theelem(0)
1067 {
1068  allocMem((vec.dim() < 1) ? 2 : vec.dim());
1069  *this = vec;
1070 
1071  assert(isConsistent());
1072 }
1073 
1074 
1075 
1076 /// Copy constructor.
1077 template < class R >
1078 template < class S >
1079 inline
1081  : theelem(0)
1082 {
1083  allocMem(old.size() < 1 ? 2 : old.size());
1085 
1086  assert(isConsistent());
1087 }
1088 
1089 
1090 
1091 /// Assignment operator.
1092 template < class R >
1093 template < class S >
1094 inline
1096 {
1097  assert(this != (const DSVectorBase<R>*)(&vec));
1098 
1100  setMax(vec.dim());
1102 
1103  assert(isConsistent());
1104 
1105  return *this;
1106 }
1107 
1108 
1109 
1110 /// Assignment operator.
1111 template < class R >
1112 template < class S >
1113 inline
1115 {
1116  assert(this != &vec);
1117 
1119  makeMem(vec.size());
1121 
1122  return *this;
1123 }
1124 
1125 
1126 
1127 // ---------------------------------------------------------------------------------------------------------------------
1128 // Operators
1129 // ---------------------------------------------------------------------------------------------------------------------
1130 
1131 
1132 
1133 /// Output operator.
1134 template < class R >
1135 inline
1136 std::ostream& operator<<(std::ostream& s, const VectorBase<R>& vec)
1137 {
1138  int i;
1139 
1140  s << '(';
1141 
1142  for(i = 0; i < vec.dim() - 1; ++i)
1143  s << vec[i] << ", ";
1144 
1145  s << vec[i] << ')';
1146 
1147  return s;
1148 }
1149 
1150 
1151 
1152 /// Negation.
1153 template < class R >
1154 inline
1156 {
1157  DVectorBase<R> res(vec.dim());
1158 
1159  for(int i = 0; i < res.dim(); ++i)
1160  res[i] = -vec[i];
1161 
1162  return res;
1163 }
1164 
1165 
1166 
1167 /// Addition.
1168 template < class R >
1169 inline
1171 {
1172  assert(v.dim() == w.dim());
1173 
1174  DVectorBase<R> res(v.dim());
1175 
1176  for(int i = 0; i < res.dim(); ++i)
1177  res[i] = v[i] + w[i];
1178 
1179  return res;
1180 }
1181 
1182 
1183 
1184 /// Addition.
1185 template < class R >
1186 inline
1188 {
1189  DVectorBase<R> res(v);
1190 
1191  res += w;
1192 
1193  return res;
1194 }
1195 
1196 
1197 
1198 /// Addition.
1199 template < class R >
1200 inline
1202 {
1203  return w + v;
1204 }
1205 
1206 
1207 
1208 /// Subtraction.
1209 template < class R >
1210 inline
1212 {
1213  assert(v.dim() == w.dim());
1214 
1215  DVectorBase<R> res(v.dim());
1216 
1217  for(int i = 0; i < res.dim(); ++i)
1218  res[i] = v[i] - w[i];
1219 
1220  return res;
1221 }
1222 
1223 
1224 
1225 /// Subtraction.
1226 template < class R >
1227 inline
1229 {
1230  DVectorBase<R> res(v);
1231 
1232  res -= w;
1233 
1234  return res;
1235 }
1236 
1237 
1238 
1239 /// Subtraction.
1240 template < class R >
1241 inline
1243 {
1244  DVectorBase<R> res(w.dim());
1245 
1246  for(int i = 0; i < res.dim(); ++i)
1247  res[i] = -w[i];
1248 
1249  res += v;
1250 
1251  return res;
1252 }
1253 
1254 
1255 
1256 /// Scaling.
1257 template < class R >
1258 inline
1260 {
1261  DVectorBase<R> res(v.dim());
1262 
1263  for(int i = 0; i < res.dim(); ++i)
1264  res[i] = x * v[i];
1265 
1266  return res;
1267 }
1268 
1269 
1270 
1271 /// Scaling.
1272 template < class R >
1273 inline
1275 {
1276  return v * x;
1277 }
1278 
1279 
1280 
1281 /// Scaling.
1282 template < class R >
1283 inline
1285 {
1286  DSVectorBase<R> res(v.size());
1287 
1288  for(int i = 0; i < v.size(); ++i)
1289  res.add(v.index(i), v.value(i) * x);
1290 
1291  return res;
1292 }
1293 
1294 
1295 
1296 /// Scaling.
1297 template < class R >
1298 inline
1300 {
1301  return v * x;
1302 }
1303 
1304 
1305 
1306 /// Input operator.
1307 template < class R >
1308 inline
1309 std::istream& operator>>(std::istream& s, DVectorBase<R>& vec)
1310 {
1311  char c;
1312  R val;
1313  int i = 0;
1314 
1315  while(s.get(c).good())
1316  {
1317  if(c != ' ' && c != '\t' && c != '\n')
1318  break;
1319  }
1320 
1321  if(c != '(')
1322  s.putback(c);
1323  else
1324  {
1325  do
1326  {
1327  s >> val;
1328 
1329  if(i >= vec.dim() - 1)
1330  vec.reDim(i + 16);
1331 
1332  vec[i++] = val;
1333 
1334  while(s.get(c).good())
1335  {
1336  if(c != ' ' && c != '\t' && c != '\n')
1337  break;
1338  }
1339 
1340  if(c != ',')
1341  {
1342  if(c != ')')
1343  s.putback(c);
1344 
1345  break;
1346  }
1347  }
1348  while(s.good());
1349  }
1350 
1351  vec.reDim(i);
1352 
1353  return s;
1354 }
1355 
1356 
1357 
1358 /// Output operator.
1359 template < class R >
1360 inline
1361 std::ostream& operator<<(std::ostream& os, const SVectorBase<R>& v)
1362 {
1363  for(int i = 0, j = 0; i < v.size(); ++i)
1364  {
1365  if(j)
1366  {
1367  if(v.value(i) < 0)
1368  os << " - " << -v.value(i);
1369  else
1370  os << " + " << v.value(i);
1371  }
1372  else
1373  os << v.value(i);
1374 
1375  os << " x" << v.index(i);
1376  j = 1;
1377 
1378  if((i + 1) % 4 == 0)
1379  os << "\n\t";
1380  }
1381 
1382  return os;
1383 }
1384 
1385 
1386 
1387 /// Output operator.
1388 template < class R >
1389 inline
1390 std::ostream& operator<<(std::ostream& os, const SVSetBase<R>& s)
1391 {
1392  for(int i = 0; i < s.num(); ++i)
1393  os << s[i] << "\n";
1394 
1395  return os;
1396 }
1397 }
1398 
1399 /* reset the SOPLEX_DEBUG flag to its original value */
1400 #undef SOPLEX_DEBUG
1401 #ifdef SOPLEX_DEBUG_BASEVECTORS
1402 #define SOPLEX_DEBUG
1403 #undef SOPLEX_DEBUG_BASEVECTORS
1404 #endif
1405 
1406 #endif // _BASEVECTORS_H_
DVectorBase< R > & operator=(const VectorBase< R > &vec)
Assignment operator.
Definition: dvectorbase.h:159
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:754
int * idx
array of indices
Definition: idxset.h:65
bool isSetup() const
Returns setup status.
Definition: ssvectorbase.h:121
Sparse vector nonzero element.
Definition: svectorbase.h:36
void reDim(int newdim, const bool setZero=true)
Resets DVectorBase&#39;s dimension to newdim.
Definition: dvectorbase.h:253
std::istream & operator>>(std::istream &s, DVectorBase< R > &vec)
Input operator.
Definition: basevectors.h:1309
VectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: vectorbase.h:112
SSVectorBase< R > & assign(const SVectorBase< S > &rhs)
Assigns only the elements of rhs.
Definition: basevectors.h:857
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:153
SSVectorBase< R > & operator-=(const VectorBase< S > &vec)
Subtraction.
Definition: ssvectorbase.h:377
Nonzero< R > & element(int n)
Reference to the n &#39;th nonzero element.
Definition: svectorbase.h:217
Set of sparse vectors.
SSVectorBase< R > & assign2product1(const SVSetBase< S > &A, const SSVectorBase< T > &x)
Assignment helper.
Definition: basevectors.h:604
#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:819
R & value(int n)
Reference to value of n &#39;th nonzero.
Definition: svectorbase.h:253
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:60
int idx
Index of nonzero element.
Definition: svectorbase.h:41
VectorBase< R > & operator+=(const VectorBase< S > &vec)
Addition.
Definition: vectorbase.h:273
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:218
int dim() const
Dimension of VectorBase.
Definition: ssvectorbase.h:563
SSVectorBase< R > & assign2productAndSetup(const SVSetBase< S > &A, SSVectorBase< T > &x)
Assigns SSVectorBase to thereby setting up x.
Definition: basevectors.h:798
DVectorBase< R > operator-(const SVectorBase< R > &v, const VectorBase< R > &w)
Subtraction.
Definition: basevectors.h:1242
double Real
Definition: spxdefines.h:218
DVectorBase< R > operator+(const SVectorBase< R > &v, const VectorBase< R > &w)
Addition.
Definition: basevectors.h:1201
SSVectorBase< R > & assign2product(const SSVectorBase< S > &x, const SVSetBase< T > &A)
Assigns to SSVectorBase.
Definition: basevectors.h:539
int & index(int n)
Reference to index of n &#39;th nonzero.
Definition: svectorbase.h:235
DVectorBase< R > operator*(const VectorBase< R > &v, R x)
Scaling.
Definition: basevectors.h:1259
Wrapper for GMP types.
R * val
Values of vector.
Definition: vectorbase.h:88
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:175
const int * indexMem() const
Returns array indices.
Definition: ssvectorbase.h:294
Debugging, floating point type and parameter definitions.
VectorBase< R > & multAdd(const S &x, const VectorBase< T > &vec)
Addition of scaled vector.
Definition: vectorbase.h:412
int dim() const
Dimension of vector.
Definition: vectorbase.h:217
int size() const
Returns the number of nonzeros.
Definition: ssvectorbase.h:200
Everything should be within this namespace.
int dim() const
Dimension of the vector defined as maximal index + 1.
Definition: svectorbase.h:167
Dense vector.
SSVectorBase< R > & operator+=(const VectorBase< S > &vec)
Addition.
Definition: ssvectorbase.h:340
VectorBase< R > & operator-=(const VectorBase< S > &vec)
Subtraction.
Definition: vectorbase.h:294
Sparse vectors.
R operator*(const VectorBase< R > &vec) const
Inner product.
Definition: vectorbase.h:338
Semi sparse vector.
R val
Value of nonzero element.
Definition: svectorbase.h:40
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:478
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:566
SSVectorBase< R > & assign2productShort(const SVSetBase< S > &A, const SSVectorBase< T > &x)
Assignment helper.
Definition: basevectors.h:640
int num() const
Current number of SVectorBases.
Definition: svsetbase.h:759
DSVectorBase< R > operator*(R x, const SVectorBase< R > &v)
Scaling.
Definition: basevectors.h:1299
Sparse vector set.Class SVSetBase provides a set of sparse vectors SVectorBase. All SVectorBases in a...
Definition: ssvectorbase.h:34
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:183
Dynamic dense vectors.
SSVectorBase< R > & assign2product4setup(const SVSetBase< S > &A, const SSVectorBase< T > &x)
Assigns SSVectorBase to for a setup x.
Definition: basevectors.h:570