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