SoPlex
Loading...
Searching...
No Matches
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 const int n = vec.size();
67
68 clear();
69
70 for(int i = 0; i < n; ++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
153{
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 }
202
203 return *this;
204}
205
206
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);
243
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;
261
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;
334}
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)
399 {
400 j = vec.index(i);
401 x = v[j] + xx * vec.value(i);
402
403 if(isNotZero(x, this->getEpsilon()))
404 {
405 if(v[j] == 0)
406 addIdx(j);
407
408 v[j] = x;
409 }
410 else
411 {
412 if(v[j] != 0)
413 adjust = true;
414
415 v[j] = 0;
416 }
417 }
418
419 if(adjust)
420 {
421 int* iptr = idx;
422 int* iiptr = idx;
423 int* endptr = idx + num;
424
425 for(; iptr < endptr; ++iptr)
426 {
427 x = v[*iptr];
428
429 if(isNotZero(x, this->getEpsilon()))
430 *iiptr++ = *iptr;
431 }
432
433 num = int(iiptr - idx);
434 }
435 }
436 else
438
439 assert(isConsistent());
440
441 return *this;
442}
443
444
445/// Assigns pair wise vector product of setup x and setup y to SSVectorBase.
446template < class R >
447template < class S, class T >
448inline
450 const SSVectorBase<T>& y)
451{
452 assert(this != (const SSVectorBase<R>*)(&x));
453 assert(this != (const SSVectorBase<R>*)(&y));
454 assert(dim() == x.dim());
455 assert(x.dim() == y.dim());
456 assert(x.isSetup());
457 assert(y.isSetup());
458 const int n = x.size() - 1;
459 const int m = y.size() - 1;
460 int i = 0;
461 int j = 0;
462
463 clear();
464 setupStatus = false;
465
466 /* both x and y non-zero vectors? */
467 if(m >= 0 && n >= 0)
468 {
469 int xi = x.index(i);
470 int yj = y.index(j);
471
472 while(i < n && j < m)
473 {
474 if(xi == yj)
475 {
476 VectorBase<R>::val[xi] = R(x.val[xi]) * R(y.val[xi]);
477 xi = x.index(++i);
478 yj = y.index(++j);
479 }
480 else if(xi < yj)
481 xi = x.index(++i);
482 else
483 yj = y.index(++j);
484 }
485
486 /* check (possible) remaining indices */
487
488 while(i < n && xi != yj)
489 xi = x.index(++i);
490
491 while(j < m && xi != yj)
492 yj = y.index(++j);
493
494 if(xi == yj)
495 VectorBase<R>::val[xi] = R(x.val[xi]) * R(y.val[xi]);
496 }
497
498 setup();
499
500 assert(isConsistent());
501
502 return *this;
503}
504
505
506
507/// Assigns \f$x^T \cdot A\f$ to SSVectorBase.
508template < class R >
509template < class S, class T >
510inline
512{
513 assert(this != (const SSVectorBase<R>*)(&x));
514 assert(A.num() == dim());
515
516 R y;
517
518 clear();
519
520 for(int i = dim() - 1; i >= 0; --i)
521 {
522 y = A[i] * x;
523
524 if(isNotZero(y, this->getEpsilon()))
525 {
526 VectorBase<R>::val[i] = y;
528 }
529 }
530
531 assert(isConsistent());
532
533 return *this;
534}
536
537
538/// Assigns SSVectorBase to \f$A \cdot x\f$ for a setup \p x.
539#define SOPLEX_SHORTPRODUCT_FACTOR 0.5
540template < class R >
541template < class S, class T >
542inline
544 const SSVectorBase<T>& x,
545 Timer* timeSparse, Timer* timeFull,
546 int& nCallsSparse, int& nCallsFull
547 )
548{
549 assert(A.num() == x.dim());
550 assert(x.isSetup());
551 clear();
552
553 if(x.size() == 1)
554 {
555 if(timeSparse != nullptr)
556 timeSparse->start();
557
558 assign2product1(A, x);
559 setupStatus = true;
560
561 if(timeSparse != nullptr)
562 timeSparse->stop();
563
564 ++nCallsSparse;
565 }
566 else if(isSetup()
567 && (double(x.size()) * A.memSize() <= SOPLEX_SHORTPRODUCT_FACTOR * dim() * A.num()))
568 {
569 if(timeSparse != nullptr)
570 timeSparse->start();
571
573 setupStatus = true;
574
575 if(timeSparse != nullptr)
576 timeSparse->stop();
577
578 ++nCallsSparse;
579 }
580 else
581 {
582 if(timeFull != nullptr)
583 timeFull->start();
584
585 assign2productFull(A, x);
586 setupStatus = false;
587
588 if(timeFull != nullptr)
589 timeFull->stop();
590
591 ++nCallsFull;
592 }
593
594 assert(isConsistent());
595
596 return *this;
597}
598
599
600
601/// Assignment helper.
602template < class R >
603template < class S, class T >
604inline
606{
607 assert(x.isSetup());
608 assert(x.size() == 1);
609
610 // get the nonzero value of x and the corresponding vector in A:
611 const int nzidx = x.idx[0];
612 const T nzval = x.val[nzidx];
613 const SVectorBase<S>& Ai = A[nzidx];
614
615 // compute A[nzidx] * nzval:
616 if(isZero(nzval, this->getEpsilon()) || Ai.size() == 0)
617 clear(); // this := zero vector
618 else
619 {
620 num = Ai.size();
621
622 for(int j = num - 1; j >= 0; --j)
623 {
624 const Nonzero<S>& Aij = Ai.element(j);
625 idx[j] = Aij.idx;
626 VectorBase<R>::val[Aij.idx] = nzval * Aij.val;
627 }
628 }
629
630 assert(isConsistent());
631
632 return *this;
633}
634
635
636
637/// Assignment helper.
638template < class R >
639template < class S, class T >
640inline
642 const SSVectorBase<T>& x)
643{
644 assert(this != (const SSVectorBase<R>*)(&x));
645 assert(x.isSetup());
646
647 clear();
648
649 if(x.size() == 0) // x can be setup but have size 0 => this := zero vector
650 return *this;
651
652 // compute x[0] * A[0]
653 int curidx = x.idx[0];
654 const T x0 = x.val[curidx];
655 const SVectorBase<S>& A0 = A[curidx];
656 const int xsize = x.size();
657 int Aisize;
658
659 // If x[0] == 0, do nothing.
660 if(isNotZero(x0, this->getEpsilon()))
661 {
662 Aisize = A0.size();
663
664 for(int j = 0; j < Aisize; ++j)
665 {
666 const Nonzero<S>& elt = A0.element(j);
667 const R product = x0 * elt.val;
668
669 // count only non-zero values; not 'isNotZero(product, epsilon)'
670 if(product != 0)
671 {
672 assert(num < len);
673 idx[num++] = elt.idx;
674 VectorBase<R>::val[elt.idx] = product;
675 }
676 }
677 }
678
679 // Compute the other x[i] * A[i] and add them to the existing vector.
680 for(int i = 1; i < xsize; ++i)
681 {
682 curidx = x.idx[i];
683 const T xi = x.val[curidx];
684 const SVectorBase<S>& Ai = A[curidx];
685
686 Aisize = Ai.size();
687
688 // Compute x[i] * A[i] and add it to the existing vector.
689 for(int j = 0; j < Aisize; ++j)
690 {
691 const Nonzero<S>& elt = Ai.element(j);
692 const R oldval = VectorBase<R>::val[elt.idx];
693 const R newval = oldval + xi * elt.val;
694
695 // If the value was positive zero, the position is still unused, so increase the counter.
696 if(isPlusZero(oldval))
697 {
698 assert(num < len);
699 idx[num++] = elt.idx;
700 }
701
702 // If the value becomes exactly 0, mark the index as used by setting negative zero.
703 VectorBase<R>::val[elt.idx] = (newval != 0) ? newval : -R(0);
704 }
705 }
706
707 // Clean up by shifting all nonzeros (w.r.t. epsilon) to the front of idx,
708 // zeroing all values which are nearly 0, and setting #num# appropriately.
709 int nz_counter = 0;
710
711 for(int i = 0; i < num; ++i)
712 {
713 curidx = idx[i];
714
715 if(isZero(VectorBase<R>::val[curidx], this->getEpsilon()))
716 VectorBase<R>::val[curidx] = +R(0);
717 else
718 idx[nz_counter++] = curidx;
719 }
720
721 num = nz_counter;
722
723 assert(isConsistent());
724
725 return *this;
726}
727
728
729
730/// Assignment helper.
731template < class R >
732template < class S, class T >
733inline
735 const SSVectorBase<T>& x)
736{
737 assert(this != (const SSVectorBase<R>*)(&x));
738 assert(x.isSetup());
739
740 if(x.size() == 0) // x can be setup but have size 0 => this := zero vector
741 {
742 clear();
743 return *this;
744 }
745
746 const int xsize = x.size();
747 bool A_is_zero = true;
748 int Aisize;
749
750 for(int i = 0; i < xsize; ++i)
751 {
752 const int curidx = x.idx[i];
753 const T xi = x.val[curidx];
754 const SVectorBase<S>& Ai = A[curidx];
755 Aisize = Ai.size();
756
757 if(A_is_zero && Aisize > 0)
758 A_is_zero = false;
759
760 for(int j = 0; j < Aisize; ++j)
761 {
762 const Nonzero<S>& elt = Ai.element(j);
763 VectorBase<R>::val[elt.idx] += xi * elt.val;
764 }
765 }
766
767 if(A_is_zero)
768 clear(); // case x != 0 but A == 0
769
770 return *this;
771}
772
773
774
775/// Assigns SSVectorBase to \f$A \cdot x\f$ thereby setting up \p x.
776template < class R >
777template < class S, class T >
778inline
780{
781 assert(this != (const SSVectorBase<R>*)(&x));
782 assert(!x.isSetup());
783
784 if(x.dim() == 0)
785 {
786 // x == 0 => this := zero vector
787 clear();
788 x.num = 0;
789 }
790 else
791 {
792 // x is not setup, so walk through its value vector
793 const int end = x.dim();
794 int nzcount = 0;
795
796 for(int i = 0; i < end; ++i)
797 {
798 // advance to the next element != 0
799 if(x.val[i] != 0)
800 {
801 // If x[i] is really nonzero, compute A[i] * x[i] and adapt x.idx,
802 // otherwise set x[i] to 0.
803 if(isNotZero(x.val[i], this->getEpsilon()))
804 {
805 const T xval = x.val[i];
806 const SVectorBase<S>& Ai = A[i];
807 x.idx[ nzcount++ ] = i;
808
809 for(int j = Ai.size() - 1; j >= 0; --j)
810 {
811 const Nonzero<S>& elt = Ai.element(j);
812 VectorBase<R>::val[elt.idx] += xval * elt.val;
813 }
814 }
815 else
816 x.val[i] = 0;
817 }
818 }
819
820 x.num = nzcount;
821 setupStatus = false;
822 }
823
824 x.setupStatus = true;
825
826 assert(isConsistent());
827
828 return *this;
829}
830
831
832
833/// Assigns only the elements of \p rhs.
834template < class R >
835template < class S >
836inline
838{
839 assert(rhs.dim() <= VectorBase<R>::dim());
840 const int s = rhs.size();
841 num = 0;
842
843 for(int i = 0; i < s; ++i)
844 {
845 int k = rhs.index(i);
846 S v = rhs.value(i);
847
848 if(isZero(v, this->getEpsilon()))
849 VectorBase<R>::val[k] = 0;
850 else
851 {
852 VectorBase<R>::val[k] = v;
853 idx[num++] = k;
854 }
855 }
856
857 setupStatus = true;
858
859 assert(isConsistent());
860
861 return *this;
862}
863
864
865
866/// Assigns only the elements of \p rhs.
867template < >
868template < >
869inline
871{
872 assert(rhs.dim() <= VectorBase<Rational>::dim());
873 const int s = rhs.size();
874 num = 0;
875
876 for(int i = 0; i < s; ++i)
877 {
878 int k = rhs.index(i);
879 const Rational& v = rhs.value(i);
880
881 if(v == 0)
883 else
884 {
886 idx[num++] = k;
887 }
888 }
889
890 setupStatus = true;
891
892 assert(isConsistent());
893
894 return *this;
895}
896
897
898
899/// Assignment operator.
900template < class R >
901template < class S >
902inline
904{
905 clear();
906
907 return assign(rhs);
908}
909
910
911
912// ---------------------------------------------------------------------------------------------------------------------
913// Methods of SVectorBase
914// ---------------------------------------------------------------------------------------------------------------------
915
916
917
918/// Assignment operator.
919template < class R >
920template < class S >
921inline
923{
924 const int d = vec.dim();
925 int n = 0;
926 Nonzero<R>* e = m_elem;
927
928 clear();
929
930 for(int i = 0; i < d; ++i)
931 {
932 if(vec[i] != 0)
933 {
934 assert(n < max());
935
936 e->idx = i;
937 e->val = vec[i];
938 ++e;
939 ++n;
940 }
941 }
943 set_size(n);
944
945 return *this;
946}
947
948
949
950/// Assignment operator (specialization for Real).
951template <>
952template < class S >
953inline
955{
956 const int d = vec.dim();
957 int n = 0;
959
960 clear();
961
962 for(int i = 0; i < d; ++i)
963 {
964 if(vec[i] != 0)
965 {
966 assert(n < max());
967
968 e->idx = i;
969 e->val = Real(vec[i]);
970 ++e;
971 ++n;
972 }
973 }
974
975 set_size(n);
976
977 return *this;
978}
979
980
981
982/// Assignment operator.
983template < class R >
984template < class S >
985inline
987{
988 assert(sv.isSetup());
989 const int n = sv.size();
990 assert(n <= max());
991 int nnz = 0;
992 int idx;
993
994 Nonzero<R>* e = m_elem;
995
996 for(int i = 0; i < n; ++i)
997 {
998 idx = sv.index(i);
999
1000 if(sv[idx] != 0)
1001 {
1002 e->idx = idx;
1003 e->val = sv[idx];
1004 ++e;
1005 ++nnz;
1006 }
1007 }
1008
1009 set_size(nnz);
1010
1011 return *this;
1012}
1013
1014
1015
1016/// Inner product.
1017template < class R >
1018inline
1020{
1021 StableSum<R> x;
1022 const Nonzero<R>* e = m_elem;
1023
1024 for(int i = size() - 1; i >= 0; --i)
1025 {
1026 x += e->val * w[e->idx];
1027 ++e;
1028 }
1029
1030 return x;
1031}
1032
1033
1034
1035// ---------------------------------------------------------------------------------------------------------------------
1036// Methods of DSVectorBase
1037// ---------------------------------------------------------------------------------------------------------------------
1038
1039
1040
1041/// Copy constructor.
1042template < class R >
1043template < class S >
1044inline
1046 : theelem(0)
1047{
1048 allocMem((vec.dim() < 1) ? 2 : vec.dim());
1049 *this = vec;
1050
1051 assert(isConsistent());
1052}
1053
1054
1055
1056/// Copy constructor.
1057template < class R >
1058template < class S >
1059inline
1061 : theelem(0)
1062{
1063 allocMem(old.size() < 1 ? 2 : old.size());
1065
1066 assert(isConsistent());
1067}
1068
1069
1070
1071/// Assignment operator.
1072template < class R >
1073template < class S >
1074inline
1076{
1077 assert(this != (const DSVectorBase<R>*)(&vec));
1078
1080 setMax(vec.dim());
1082
1083 assert(isConsistent());
1084
1085 return *this;
1086}
1087
1088
1089
1090/// Assignment operator.
1091template < class R >
1092template < class S >
1093inline
1095{
1096 assert(this != &vec);
1097
1099 makeMem(vec.size());
1101
1102 return *this;
1103}
1104
1105
1106
1107// ---------------------------------------------------------------------------------------------------------------------
1108// Operators
1109// ---------------------------------------------------------------------------------------------------------------------
1110
1111
1112
1113/// Output operator.
1114template < class R >
1115inline
1116std::ostream& operator<<(std::ostream& s, const VectorBase<R>& vec)
1117{
1118 const int d = vec.dim() - 1;
1119 int i;
1120
1121 s << '(';
1122
1123 for(i = 0; i < d; ++i)
1124 s << vec[i] << ", ";
1125
1126 s << vec[i] << ')';
1127
1128 return s;
1129}
1130
1131
1132
1133/// Subtraction.
1134template < class R >
1135inline
1137{
1138 const int d = w.dim();
1139 VectorBase<R> res(d);
1140
1141 for(int i = 0; i < d; ++i)
1142 res[i] = -w[i];
1143
1144 res += v;
1145
1146 return res;
1147}
1148
1149
1150
1151
1152/// Scaling.
1153template < class R >
1154inline
1156{
1157 const int n = v.size();
1158 DSVectorBase<R> res(n);
1159
1160 for(int i = 0; i < n; ++i)
1161 res.add(v.index(i), v.value(i) * x);
1162
1163 return res;
1164}
1165
1166
1167
1168/// Scaling.
1169template < class R >
1170inline
1172{
1173 return v * x;
1174}
1175
1176
1177
1178template < class R >
1179inline
1180std::istream& operator>>(std::istream& s, VectorBase<R>& vec)
1181{
1182 char c;
1183 R val;
1184 int i = 0;
1185
1186 while(s.get(c).good())
1187 {
1188 if(c != ' ' && c != '\t' && c != '\n')
1189 break;
1190 }
1191
1192 if(c != '(')
1193 s.putback(c);
1194 else
1195 {
1196 do
1197 {
1198 s >> val;
1199
1200 if(i >= vec.dim() - 1)
1201 vec.reDim(i + 16);
1202
1203 vec[i++] = val;
1204
1205 while(s.get(c).good())
1206 {
1207 if(c != ' ' && c != '\t' && c != '\n')
1208 break;
1209 }
1210
1211 if(c != ',')
1212 {
1213 if(c != ')')
1214 s.putback(c);
1215
1216 break;
1217 }
1218 }
1219 while(s.good());
1220 }
1221
1222 vec.reDim(i);
1223
1224 return s;
1225}
1226
1227
1228
1229/// Output operator.
1230template < class R >
1231inline
1232std::ostream& operator<<(std::ostream& os, const SVectorBase<R>& v)
1233{
1234 const int n = v.size();
1235
1236 for(int i = 0, j = 0; i < n; ++i)
1237 {
1238 if(j)
1239 {
1240 if(v.value(i) < 0)
1241 os << " - " << -v.value(i);
1242 else
1243 os << " + " << v.value(i);
1244 }
1245 else
1246 os << v.value(i);
1247
1248 os << " x" << v.index(i);
1249 j = 1;
1250
1251 if((i + 1) % 4 == 0)
1252 os << "\n\t";
1253 }
1254
1255 return os;
1256}
1257
1258
1259
1260/// Output operator.
1261template < class R >
1262inline
1263std::ostream& operator<<(std::ostream& os, const SVSetBase<R>& s)
1264{
1265 for(int i = 0; i < s.num(); ++i)
1266 os << s[i] << "\n";
1267
1268 return os;
1269}
1270}
1271
1272/* reset the SOPLEX_DEBUG flag to its original value */
1273#undef SOPLEX_DEBUG
1274#ifdef SOPLEX_DEBUG_BASEVECTORS
1275#define SOPLEX_DEBUG
1276#undef SOPLEX_DEBUG_BASEVECTORS
1277#endif
1278
1279#endif // _BASEVECTORS_H_
#define SOPLEX_SHORTPRODUCT_FACTOR
Assigns SSVectorBase to for a setup x.
Dynamic sparse vectors.
void makeMem(int n)
Ensure there is room for n new nonzeros.
bool isConsistent() const
Consistency check.
void allocMem(int n)
Allocate memory for n nonzeros.
DSVectorBase(int n=8)
Default constructor.
void add(const SVectorBase< R > &vec)
Append nonzeros of vec.
void setMax(int newmax=1)
Reset nonzero memory to >= newmax.
DSVectorBase< R > & operator=(const SVectorBase< S > &vec)
Assignment operator.
Nonzero< R > * theelem
Memory.
void addIdx(int i)
appends index i.
Definition idxset.h:174
int max() const
returns the maximal number of indices which can be stored in IdxSet.
Definition idxset.h:138
int num
number of used indices
Definition idxset.h:72
int * idx
array of indices
Definition idxset.h:74
int len
length of array idx
Definition idxset.h:73
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.
SSVectorBase< R > & assign2productShort(const SVSetBase< S > &A, const SSVectorBase< T > &x)
Assignment helper.
SSVectorBase< R > & assign2product1(const SVSetBase< S > &A, const SSVectorBase< T > &x)
Assignment helper.
bool setupStatus
Is the SSVectorBase set up?
SSVectorBase< R > & multAdd(S xx, const SVectorBase< T > &vec)
Addition of a scaled vector.
SSVectorBase< R > & assign(const SVectorBase< S > &rhs)
Assigns only the elements of rhs.
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.
SSVectorBase< R > & operator-=(const VectorBase< S > &vec)
Subtraction.
bool isConsistent() const
consistency check.
SSVectorBase(int p_dim, std::shared_ptr< Tolerances > tol=nullptr)
Default constructor.
SSVectorBase< R > & assign2product(const SSVectorBase< S > &x, const SVSetBase< T > &A)
Assigns to SSVectorBase.
SSVectorBase< R > & assignPWproduct4setup(const SSVectorBase< S > &x, const SSVectorBase< T > &y)
Assigns pair wise vector product to SSVectorBase.
SSVectorBase< R > & operator=(const SSVectorBase< S > &rhs)
Assignment operator.
int index(int n) const
Returns index of the n 'th nonzero element.
int dim() const
Dimension of VectorBase.
SSVectorBase< R > & operator+=(const VectorBase< S > &vec)
Addition.
void clear()
Clears vector.
SSVectorBase< R > & assign2productFull(const SVSetBase< S > &A, const SSVectorBase< T > &x)
Assignment helper.
SSVectorBase< R > & assign2productAndSetup(const SVSetBase< S > &A, SSVectorBase< T > &x)
Assigns SSVectorBase to thereby setting up x.
int size() const
Returns the number of nonzeros.
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.
Nonzero< R > & element(int n)
Reference to the n 'th nonzero element.
R operator*(const VectorBase< R > &w) const
Inner product.
int & index(int n)
Reference to index of n 'th nonzero.
friend class SVectorBase
R & value(int n)
Reference to value of n 'th nonzero.
int dim() const
Dimension of the vector defined as maximal index + 1.
SVectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
void clear()
Remove all indices.
Nonzero< R > * m_elem
int size() const
Number of used indices.
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
std::vector< R >::const_iterator end() const
Definition vectorbase.h:520
friend class VectorBase
Definition vectorbase.h:91
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
const std::vector< R > & vec()
Return underlying std::vector.
Definition vectorbase.h:296
void clear()
Set vector to contain all-zeros (keeping the same length).
Definition vectorbase.h:308
VectorBase< R > & multSub(const S &x, const SVectorBase< T > &vec)
Subtraction of scaled vector.
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.
std::istream & operator>>(std::istream &s, VectorBase< R > &vec)
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
VectorBase< R > operator-(const SVectorBase< R > &v, const VectorBase< R > &w)
Subtraction.
double Real
Definition spxdefines.h:269
DSVectorBase< R > operator*(const SVectorBase< R > &v, R x)
Scaling.
bool isPlusZero(R x)
detects whether value is positive zero
Definition spxdefines.h:312
SPxDefaultRT & operator=(const SPxDefaultRT &rhs)
assignment operator
Rational number types.
Debugging, floating point type and parameter definitions.
Semi sparse vector.
Sparse vectors.
Set of sparse vectors.
Timer class.
Dense vector.