1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the class library */
/* SoPlex --- the Sequential object-oriented simPlex. */
/* */
/* Copyright 1996-2022 Zuse Institute Berlin */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/* You should have received a copy of the Apache-2.0 license */
/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file svectorbase.h
* @brief Sparse vectors.
*/
#ifndef _SVECTORBASE_H_
#define _SVECTORBASE_H_
#include <iostream>
#include <assert.h>
#include <math.h>
#include <cmath>
#include "soplex/stablesum.h"
namespace soplex
{
template < class R > class VectorBase;
template < class R > class SSVectorBase;
/// Sparse vector nonzero element.
/** SVectorBase keeps its nonzeros in an array of Nonzero%s providing members for saving the index and value.
*/
template < class R >
class Nonzero
{
public:
R val; ///< Value of nonzero element.
int idx; ///< Index of nonzero element.
template < class S >
Nonzero<R>& operator=(const Nonzero<S>& vec)
{
// todo: is the cast really necessary? Previous code worked without a cast
val = (R) vec.val;
idx = vec.idx;
return *this;
}
template < class S >
Nonzero<R>(const Nonzero<S>& vec)
: val(vec.val)
, idx(vec.idx)
{
}
Nonzero<R>()
: val()
, idx(0)
{
}
};
// specialized assignment operator
template <>
template < class S >
Nonzero<Real>& Nonzero<Real>::operator=(const Nonzero<S>& vec)
{
val = Real(vec.val);
idx = vec.idx;
return *this;
}
/**@brief Sparse vectors.
* @ingroup Algebra
*
* Class SVectorBase provides packed sparse vectors. Such are a sparse vectors, with a storage scheme that keeps all
* data in one contiguous block of memory. This is best suited for using them for parallel computing on a distributed
* memory multiprocessor.
*
* SVectorBase does not provide any memory management (this will be done by class DSVectorBase). This means, that the
* constructor of SVectorBase expects memory where to save the nonzeros. Further, adding nonzeros to an SVectorBase may
* fail if no more memory is available for saving them (see also DSVectorBase).
*
* When nonzeros are added to an SVectorBase, they are appended to the set of nonzeros, i.e., they recieve numbers
* size(), size()+1 ... . An SVectorBase can hold atmost max() nonzeros, where max() is given in the constructor. When
* removing nonzeros, the remaining nonzeros are renumbered. However, only the numbers greater than the number of the
* first removed nonzero are affected.
*
* The following mathematical operations are provided by class SVectorBase (SVectorBase \p a, \p b, \p c; R \p x):
*
* <TABLE>
* <TR><TD>Operation</TD><TD>Description </TD><TD></TD> </TR>
* <TR><TD>\c -= </TD><TD>subtraction </TD><TD>\c a \c -= \c b </TD></TR>
* <TR><TD>\c += </TD><TD>addition </TD><TD>\c a \c += \c b </TD></TR>
* <TR><TD>\c * </TD><TD>skalar product</TD>
* <TD>\c x = \c a \c * \c b </TD></TR>
* <TR><TD>\c *= </TD><TD>scaling </TD><TD>\c a \c *= \c x </TD></TR>
* <TR><TD>maxAbs() </TD><TD>infinity norm </TD>
* <TD>\c a.maxAbs() == \f$\|a\|_{\infty}\f$ </TD></TR>
* <TR><TD>length() </TD><TD>eucledian norm</TD>
* <TD>\c a.length() == \f$\sqrt{a^2}\f$ </TD></TR>
* <TR><TD>length2()</TD><TD>square norm </TD>
* <TD>\c a.length2() == \f$a^2\f$ </TD></TR>
* </TABLE>
*
* Operators \c += and \c -= should be used with caution, since no efficient implementation is available. One should
* think of assigning the left handside vector to a dense VectorBase first and perform the addition on it. The same
* applies to the scalar product \c *.
*
* There are two numberings of the nonzeros of an SVectorBase. First, an SVectorBase is supposed to act like a linear
* algebra VectorBase. An \em index refers to this view of an SVectorBase: operator[]() is provided which returns the
* value at the given index of the vector, i.e., 0 for all indices which are not in the set of nonzeros. The other view
* of SVectorBase%s is that of a set of nonzeros. The nonzeros are numbered from 0 to size()-1. The methods index(int
* n) and value(int n) allow to access the index and value of the \p n 'th nonzero. \p n is referred to as the \em
* number of a nonzero.
*
* @todo SVectorBase should get a new implementation. There maybe a lot of memory lost due to padding the Nonzero
* structure. A better idea seems to be class SVectorBase { int size; int used; int* idx; R* val; }; which for
* several reasons could be faster or slower. If SVectorBase is changed, also DSVectorBase and SVSet have to be
* modified.
*/
template < class R >
class SVectorBase
{
template < class S > friend class SVectorBase;
private:
// ------------------------------------------------------------------------------------------------------------------
/**@name Data */
///@{
Nonzero<R>* m_elem;
int memsize;
int memused;
///@}
public:
typedef Nonzero<R> Element;
// ------------------------------------------------------------------------------------------------------------------
/**@name Access */
///@{
/// Number of used indices.
int size() const
{
assert(m_elem != 0 || memused == 0);
return memused;
}
/// Maximal number of indices.
int max() const
{
assert(m_elem != 0 || memused == 0);
return memsize;
}
/// Dimension of the vector defined as maximal index + 1
int dim() const
{
const Nonzero<R>* e = m_elem;
int d = -1;
int n = size();
while(n--)
{
d = (d > e->idx) ? d : e->idx;
e++;
}
return d + 1;
}
/// Position of index \p i.
/** @return Finds the position of the first index \p i in the index set. If no such index \p i is found,
* -1 is returned. Otherwise, index(pos(i)) == i holds.
*/
int pos(int i) const
{
if(m_elem != 0)
{
int n = size();
for(int p = 0; p < n; ++p)
{
if(m_elem[p].idx == i)
{
assert(index(p) == i);
return p;
}
}
}
return -1;
}
/// Value to index \p i.
R operator[](int i) const
{
int n = pos(i);
if(n >= 0)
return m_elem[n].val;
return 0;
}
/// Reference to the \p n 'th nonzero element.
Nonzero<R>& element(int n)
{
assert(n >= 0);
assert(n < max());
return m_elem[n];
}
/// The \p n 'th nonzero element.
const Nonzero<R>& element(int n) const
{
assert(n >= 0);
assert(n < size());
return m_elem[n];
}
/// Reference to index of \p n 'th nonzero.
int& index(int n)
{
assert(n >= 0);
assert(n < size());
return m_elem[n].idx;
}
/// Index of \p n 'th nonzero.
int index(int n) const
{
assert(n >= 0);
assert(n < size());
return m_elem[n].idx;
}
/// Reference to value of \p n 'th nonzero.
R& value(int n)
{
assert(n >= 0);
assert(n < size());
return m_elem[n].val;
}
/// Value of \p n 'th nonzero.
const R& value(int n) const
{
assert(n >= 0);
assert(n < size());
return m_elem[n].val;
}
/// Append one nonzero \p (i,v).
void add(int i, const R& v)
{
assert(m_elem != 0);
assert(size() < max());
if(v != 0.0)
{
int n = size();
m_elem[n].idx = i;
m_elem[n].val = v;
set_size(n + 1);
assert(size() <= max());
}
}
/// Append one uninitialized nonzero.
void add(int i)
{
assert(m_elem != 0);
assert(size() < max());
int n = size();
m_elem[n].idx = i;
set_size(n + 1);
assert(size() <= max());
}
/// Append nonzeros of \p sv.
void add(const SVectorBase& sv)
{
add(sv.size(), sv.m_elem);
}
/// Append \p n nonzeros.
void add(int n, const int i[], const R v[])
{
assert(n + size() <= max());
if(n <= 0)
return;
int newnnz = 0;
Nonzero<R>* e = m_elem + size();
while(n--)
{
if(*v != 0.0)
{
assert(e != nullptr);
e->idx = *i;
e->val = *v;
e++;
++newnnz;
}
i++;
v++;
}
set_size(size() + newnnz);
}
/// Append \p n nonzeros.
template < class S >
void add(int n, const int i[], const S v[])
{
assert(n + size() <= max());
if(n <= 0)
return;
int newnnz = 0;
Nonzero<R>* e = m_elem + size();
while(n--)
{
if(*v != R(0.0))
{
e->idx = *i;
e->val = *v;
e++;
++newnnz;
}
i++;
v++;
}
set_size(size() + newnnz);
}
/// Append \p n nonzeros.
void add(int n, const Nonzero<R> e[])
{
assert(n + size() <= max());
if(n <= 0)
return;
int newnnz = 0;
Nonzero<R>* ee = m_elem + size();
while(n--)
{
if(e->val != 0.0)
{
*ee++ = *e;
++newnnz;
}
e++;
}
set_size(size() + newnnz);
}
/// Remove nonzeros \p n thru \p m.
void remove(int n, int m)
{
assert(n <= m);
assert(m < size());
assert(n >= 0);
++m;
int cpy = m - n;
cpy = (size() - m >= cpy) ? cpy : size() - m;
Nonzero<R>* e = &m_elem[size() - 1];
Nonzero<R>* r = &m_elem[n];
set_size(size() - cpy);
do
{
*r++ = *e--;
}
while(--cpy);
}
/// Remove \p n 'th nonzero.
void remove(int n)
{
assert(n >= 0);
assert(n < size());
int newsize = size() - 1;
set_size(newsize);
if(n < newsize)
m_elem[n] = m_elem[newsize];
}
/// Remove all indices.
void clear()
{
set_size(0);
}
/// Sort nonzeros to increasing indices.
void sort()
{
if(m_elem != 0)
{
Nonzero<R> dummy;
Nonzero<R>* w;
Nonzero<R>* l;
Nonzero<R>* s = &(m_elem[0]);
Nonzero<R>* e = s + size();
for(l = s, w = s + 1; w < e; l = w, ++w)
{
if(l->idx > w->idx)
{
dummy = *w;
do
{
l[1] = *l;
if(l-- == s)
break;
}
while(l->idx > dummy.idx);
l[1] = dummy;
}
}
}
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Arithmetic operations */
///@{
/// Maximum absolute value, i.e., infinity norm.
R maxAbs() const
{
R maxi = 0;
for(int i = size() - 1; i >= 0; --i)
{
if(spxAbs(m_elem[i].val) > maxi)
maxi = spxAbs(m_elem[i].val);
}
assert(maxi >= 0);
return maxi;
}
/// Minimum absolute value.
R minAbs() const
{
R mini = R(infinity);
for(int i = size() - 1; i >= 0; --i)
{
if(spxAbs(m_elem[i].val) < mini)
mini = spxAbs(m_elem[i].val);
}
assert(mini >= 0);
return mini;
}
/// Floating point approximation of euclidian norm (without any approximation guarantee).
R length() const
{
return std::sqrt(R(length2()));
}
/// Squared norm.
R length2() const
{
R x = 0;
int n = size();
const Nonzero<R>* e = m_elem;
while(n--)
{
x += e->val * e->val;
e++;
}
return x;
}
/// Scaling.
SVectorBase<R>& operator*=(const R& x)
{
int n = size();
Nonzero<R>* e = m_elem;
assert(x != 0);
while(n--)
{
e->val *= x;
e++;
}
return *this;
}
/// Inner product.
R operator*(const VectorBase<R>& w) const;
/// inner product for sparse vectors
template < class S >
R operator*(const SVectorBase<S>& w) const
{
StableSum<R> x;
int n = size();
int m = w.size();
if(n == 0 || m == 0)
return x;
int i = 0;
int j = 0;
Element* e = m_elem;
typename SVectorBase<S>::Element wj = w.element(j);
while(true)
{
if(e->idx == wj.idx)
{
x += e->val * wj.val;
i++;
j++;
if(i == n || j == m)
break;
e++;
wj = w.element(j);
}
else if(e->idx < wj.idx)
{
i++;
if(i == n)
break;
e++;
}
else
{
j++;
if(j == m)
break;
wj = w.element(j);
}
}
return x;
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Constructions, destruction, and assignment */
///@{
/// Default constructor.
/** The constructor expects one memory block where to store the nonzero elements. This must be passed to the
* constructor, where the \em number of Nonzero%s needs that fit into the memory must be given and a pointer to the
* beginning of the memory block. Once this memory has been passed, it shall not be modified until the SVectorBase
* is no longer used.
*/
explicit SVectorBase<R>(int n = 0, Nonzero<R>* p_mem = 0)
{
setMem(n, p_mem);
}
SVectorBase<R>(const SVectorBase<R>& sv) = default;
/// Assignment operator.
template < class S >
SVectorBase<R>& operator=(const VectorBase<S>& vec);
/// Assignment operator.
SVectorBase<R>& operator=(const SVectorBase<R>& sv)
{
if(this != &sv)
{
assert(max() >= sv.size());
int i = sv.size();
int nnz = 0;
Nonzero<R>* e = m_elem;
const Nonzero<R>* s = sv.m_elem;
while(i--)
{
assert(e != 0);
if(s->val != 0.0)
{
*e++ = *s;
++nnz;
}
++s;
}
set_size(nnz);
}
return *this;
}
/// move assignement operator.
SVectorBase<R>& operator=(const SVectorBase<R>&& sv)
{
if(this != &sv)
{
this->m_elem = sv.m_elem;
this->memsize = sv.memsize;
this->memused = sv.memused;
}
return *this;
}
/// Assignment operator.
template < class S >
SVectorBase<R>& operator=(const SVectorBase<S>& sv)
{
if(this != (const SVectorBase<R>*)(&sv))
{
assert(max() >= sv.size());
int i = sv.size();
int nnz = 0;
Nonzero<R>* e = m_elem;
const Nonzero<S>* s = sv.m_elem;
while(i--)
{
assert(e != 0);
if(s->val != 0.0)
{
*e++ = *s;
++nnz;
}
++s;
}
set_size(nnz);
}
return *this;
}
/// scale and assign
SVectorBase<Real>& scaleAssign(int scaleExp, const SVectorBase<Real>& sv)
{
if(this != &sv)
{
assert(max() >= sv.size());
for(int i = 0; i < sv.size(); ++i)
{
m_elem[i].val = spxLdexp(sv.value(i), scaleExp);
m_elem[i].idx = sv.index(i);
}
assert(isConsistent());
}
return *this;
}
/// scale and assign
SVectorBase<Real>& scaleAssign(const int* scaleExp, const SVectorBase<Real>& sv,
bool negateExp = false)
{
if(this != &sv)
{
assert(max() >= sv.size());
if(negateExp)
{
for(int i = 0; i < sv.size(); ++i)
{
m_elem[i].val = spxLdexp(sv.value(i), -scaleExp[sv.index(i)]);
m_elem[i].idx = sv.index(i);
}
}
else
{
for(int i = 0; i < sv.size(); ++i)
{
m_elem[i].val = spxLdexp(sv.value(i), scaleExp[sv.index(i)]);
m_elem[i].idx = sv.index(i);
}
}
assert(isConsistent());
}
return *this;
}
/// Assignment operator.
template < class S >
SVectorBase<R>& assignArray(const S* rowValues, const int* rowIndices, int rowSize)
{
assert(max() >= rowSize);
int i;
for(i = 0; i < rowSize && i < max(); i++)
{
m_elem[i].val = rowValues[i];
m_elem[i].idx = rowIndices[i];
}
set_size(i);
return *this;
}
/// Assignment operator.
template < class S >
SVectorBase<R>& operator=(const SSVectorBase<S>& sv);
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Memory */
///@{
/// get pointer to internal memory.
Nonzero<R>* mem() const
{
return m_elem;
}
/// Set size of the vector.
void set_size(int s)
{
assert(m_elem != 0 || s == 0);
memused = s;
}
/// Set the maximum number of nonzeros in the vector.
void set_max(int m)
{
assert(m_elem != 0 || m == 0);
memsize = m;
}
/// Set the memory area where the nonzeros will be stored.
void setMem(int n, Nonzero<R>* elmem)
{
assert(n >= 0);
assert(n == 0 || elmem != 0);
m_elem = elmem;
set_size(0);
set_max(n);
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Utilities */
///@{
/// Consistency check.
bool isConsistent() const
{
#ifdef ENABLE_CONSISTENCY_CHECKS
if(m_elem != 0)
{
const int my_size = size();
const int my_max = max();
if(my_size < 0 || my_max < 0 || my_size > my_max)
return MSGinconsistent("SVectorBase");
for(int i = 1; i < my_size; ++i)
{
for(int j = 0; j < i; ++j)
{
// allow trailing zeros
if(m_elem[i].idx == m_elem[j].idx && m_elem[i].val != 0)
return MSGinconsistent("SVectorBase");
}
}
}
#endif
return true;
}
///@}
};
/// specialization for inner product for sparse vectors
template <>
template < class S >
Real SVectorBase<Real>::operator*(const SVectorBase<S>& w) const
{
StableSum<Real> x;
int n = size();
int m = w.size();
if(n == 0 || m == 0)
return Real(0);
int i = 0;
int j = 0;
SVectorBase<Real>::Element* e = m_elem;
typename SVectorBase<S>::Element wj = w.element(j);
while(true)
{
if(e->idx == wj.idx)
{
x += e->val * Real(wj.val);
i++;
j++;
if(i == n || j == m)
break;
e++;
wj = w.element(j);
}
else if(e->idx < wj.idx)
{
i++;
if(i == n)
break;
e++;
}
else
{
j++;
if(j == m)
break;
wj = w.element(j);
}
}
return x;
}
} // namespace soplex
#endif // _SVECTORBASE_H_