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