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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* 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 vectorbase.h
* @brief Dense vector.
*/
#ifndef _VECTORBASE_H_
#define _VECTORBASE_H_
#include <assert.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include "vector"
#include "algorithm"
#include "soplex/spxdefines.h"
#include "soplex/stablesum.h"
#include "soplex/rational.h"
namespace soplex
{
template < class R > class SVectorBase;
template < class R > class SSVectorBase;
/**@brief Dense vector.
* @ingroup Algebra
*
* Class VectorBase provides dense linear algebra vectors. Internally, VectorBase wraps std::vector.
*
* After construction, the values of a VectorBase can be accessed with the subscript operator[](). Safety is provided by
* qchecking of array bound when accessing elements with the subscript operator[]() (only when compiled without \c
* -DNDEBUG).
*
* A VectorBase is distinguished from a simple array of %Reals or %Rationals by providing a set of mathematical
* operations.
*
* The following mathematical operations are provided by class VectorBase (VectorBase \p a, \p b; 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>scalar 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>minAbs() </TD><TD> </TD>
* <TD>\c a.minAbs() == \f$\min |a_i|\f$ </TD></TR>
*
* <TR><TD>length() </TD><TD>euclidian 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>
* <TR><TD>multAdd(\c x,\c b)</TD><TD>add scaled vector</TD>
* <TD> \c a += \c x * \c b </TD></TR>
* </TABLE>
*
* When using any of these operations, the vectors involved must be of the same dimension. Also an SVectorBase \c b is
* allowed if it does not contain nonzeros with index greater than the dimension of \c a.q
*/
template < class R >
class VectorBase
{
// VectorBase is a friend of VectorBase of different template type. This is so
// that we can do conversions.
template <typename S>
friend class VectorBase;
protected:
// ------------------------------------------------------------------------------------------------------------------
/**@name Data */
///@{
/// Values of vector.
std::vector<R> val;
///@}
public:
// ------------------------------------------------------------------------------------------------------------------
/**@name Construction and assignment */
///@{
/// Constructor.
/** There is no default constructor since the storage for a VectorBase must be provided externally. Storage must be
* passed as a memory block val at construction. It must be large enough to fit at least dimen values.
*/
// Default constructor
VectorBase<R>()
{
// Default constructor
;
}
// Construct from pointer, copies the values into the VectorBase
VectorBase<R>(int dimen, R* p_val)
{
val.assign(p_val, p_val + dimen);
}
// do not convert int to empty vectorbase
explicit VectorBase<R>(int p_dimen)
{
val.resize(p_dimen);
}
// Constructing an element (usually involving casting Real to Rational and
// vice versa.)
template <typename S>
VectorBase<R>(const VectorBase<S>& vec)
{
this->operator=(vec);
}
// The move constructor
VectorBase<R>(const VectorBase<R>&& vec)noexcept: val(std::move(vec.val))
{
}
// Copy constructor
VectorBase<R>(const VectorBase<R>& vec): val(vec.val)
{
}
/// Assignment operator.
// Supports assignment from a Rational vector to Real and vice versa
template < class S >
VectorBase<R>& operator=(const VectorBase<S>& vec)
{
if((VectorBase<S>*)this != &vec)
{
val.clear();
val.reserve(vec.dim());
for(auto& v : vec.val)
{
val.push_back(R(v));
}
}
return *this;
}
/// Assignment operator.
VectorBase<R>& operator=(const VectorBase<R>& vec)
{
if(this != &vec)
{
val.reserve(vec.dim());
val = vec.val;
}
return *this;
}
/// Move assignment operator
VectorBase<R>& operator=(const VectorBase<R>&& vec)
{
val = std::move(vec.val);
return *this;
}
/// scale and assign
VectorBase<R>& scaleAssign(int scaleExp, const VectorBase<R>& vec)
{
if(this != &vec)
{
assert(dim() == vec.dim());
auto dimen = dim();
for(decltype(dimen) i = 0 ; i < dimen; i++)
val[i] = spxLdexp(vec[i], scaleExp);
}
return *this;
}
/// scale and assign
VectorBase<R>& scaleAssign(const int* scaleExp, const VectorBase<R>& vec, bool negateExp = false)
{
if(this != &vec)
{
assert(dim() == vec.dim());
if(negateExp)
{
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
val[i] = spxLdexp(vec[i], -scaleExp[i]);
}
else
{
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
val[i] = spxLdexp(vec[i], scaleExp[i]);
}
}
return *this;
}
/// Assignment operator.
/** Assigning an SVectorBase to a VectorBase using operator=() will set all values to 0 except the nonzeros of \p vec.
* This is diffent in method assign().
*/
template < class S >
VectorBase<R>& operator=(const SVectorBase<S>& vec);
/// Assignment operator.
/** Assigning an SSVectorBase to a VectorBase using operator=() will set all values to 0 except the nonzeros of \p
* vec. This is diffent in method assign().
*/
/**@todo do we need this also in non-template version, because SSVectorBase can be automatically cast to VectorBase? */
template < class S >
VectorBase<R>& operator=(const SSVectorBase<S>& vec);
/// Assign values of \p vec.
/** Assigns all nonzeros of \p vec to the vector. All other values remain unchanged. */
template < class S >
VectorBase<R>& assign(const SVectorBase<S>& vec);
/// Assign values of \p vec.
/** Assigns all nonzeros of \p vec to the vector. All other values remain unchanged. */
template < class S >
VectorBase<R>& assign(const SSVectorBase<S>& vec);
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Access */
///@{
/// Dimension of vector.
int dim() const
{
return int(val.size());
}
/// Return \p n 'th value by reference.
R& operator[](int n)
{
assert(n >= 0 && n < dim());
return val[n];
}
/// Return \p n 'th value.
const R& operator[](int n) const
{
assert(n >= 0 && n < dim());
return val[n];
}
/// Equality operator.
friend bool operator==(const VectorBase<R>& vec1, const VectorBase<R>& vec2)
{
return (vec1.val == vec2.val);
}
/// Return underlying std::vector.
const std::vector<R>& vec()
{
return val;
}
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Arithmetic operations */
///@{
/// Set vector to contain all-zeros (keeping the same length)
void clear()
{
for(auto& v : val)
v = 0;
}
/// Addition.
template < class S >
VectorBase<R>& operator+=(const VectorBase<S>& vec)
{
assert(dim() == vec.dim());
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
val[i] += vec[i];
return *this;
}
/// Addition.
template < class S >
VectorBase<R>& operator+=(const SVectorBase<S>& vec);
/// Addition.
template < class S >
VectorBase<R>& operator+=(const SSVectorBase<S>& vec);
/// Subtraction.
template < class S >
VectorBase<R>& operator-=(const VectorBase<S>& vec)
{
assert(dim() == vec.dim());
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
val[i] -= vec[i];
return *this;
}
/// Subtraction.
template < class S >
VectorBase<R>& operator-=(const SVectorBase<S>& vec);
/// Subtraction.
template < class S >
VectorBase<R>& operator-=(const SSVectorBase<S>& vec);
/// Scaling.
template < class S >
VectorBase<R>& operator*=(const S& x)
{
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
val[i] *= x;
return *this;
}
/// Division.
template < class S >
VectorBase<R>& operator/=(const S& x)
{
assert(x != 0);
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
val[i] /= x;
return *this;
}
/// Inner product.
R operator*(const VectorBase<R>& vec) const
{
StableSum<R> x;
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
x += val[i] * vec.val[i];
return x;
}
/// Inner product.
R operator*(const SVectorBase<R>& vec) const;
/// Inner product.
R operator*(const SSVectorBase<R>& vec) const;
/// Maximum absolute value, i.e., infinity norm.
R maxAbs() const
{
assert(dim() > 0);
// A helper function for the std::max_element. Because we compare the absolute value.
auto absCmpr = [](R a, R b)
{
return (spxAbs(a) < spxAbs(b));
};
auto maxReference = std::max_element(val.begin(), val.end(), absCmpr);
R maxi = spxAbs(*maxReference);
assert(maxi >= 0.0);
return maxi;
}
/// Minimum absolute value.
R minAbs() const
{
assert(dim() > 0);
// A helper function for the std::min_element. Because we compare the absolute value.
auto absCmpr = [](R a, R b)
{
return (spxAbs(a) < spxAbs(b));
};
auto minReference = std::min_element(val.begin(), val.end(), absCmpr);
R mini = spxAbs(*minReference);
assert(mini >= 0.0);
return mini;
}
/// Floating point approximation of euclidian norm (without any approximation guarantee).
R length() const
{
return spxSqrt(length2());
}
/// Squared norm.
R length2() const
{
return (*this) * (*this);
}
/// Addition of scaled vector.
template < class S, class T >
VectorBase<R>& multAdd(const S& x, const VectorBase<T>& vec)
{
assert(vec.dim() == dim());
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
val[i] += x * vec.val[i];
return *this;
}
/// Addition of scaled vector.
template < class S, class T >
VectorBase<R>& multAdd(const S& x, const SVectorBase<T>& vec);
/// Subtraction of scaled vector.
template < class S, class T >
VectorBase<R>& multSub(const S& x, const SVectorBase<T>& vec);
/// Addition of scaled vector.
template < class S, class T >
VectorBase<R>& multAdd(const S& x, const SSVectorBase<T>& vec);
///@}
// ------------------------------------------------------------------------------------------------------------------
/**@name Utilities */
///@{
/// Conversion to C-style pointer.
/** This function serves for using a VectorBase in an C-style function. It returns a pointer to the first value of
* the array.
*
* @todo check whether this non-const c-style access should indeed be public
*/
R* get_ptr()
{
return val.data();
}
/// Conversion to C-style pointer.
/** This function serves for using a VectorBase in an C-style function. It returns a pointer to the first value of
* the array.
*/
const R* get_const_ptr() const
{
return val.data();
}
// Provides access to the iterators of std::vector<R> val
typename std::vector<R>::const_iterator begin() const
{
return val.begin();
}
typename std::vector<R>::iterator begin()
{
return val.begin();
}
// Provides access to the iterators of std::vector<R> val
typename std::vector<R>::const_iterator end() const
{
return val.end();
}
typename std::vector<R>::iterator end()
{
return val.end();
}
// Functions from VectorBase
// This used to be VectorBase's way of having std::vector's capacity. This
// represents the maximum number of elements the std::vector can have without,
// needing any more resizing. Bigger than size, mostly.
int memSize() const
{
return int(val.capacity());
}
/// Resets \ref soplex::VectorBase "VectorBase"'s dimension to \p newdim.
void reDim(int newdim, const bool setZero = true)
{
if(setZero && newdim > dim())
{
// Inserts 0 to the rest of the vectors.
//
// TODO: Is this important after the change of raw pointers to
// std::vector. This is just a waste of operations, I think.
val.insert(val.end(), newdim - VectorBase<R>::dim(), 0);
}
else
{
val.resize(newdim);
}
}
/// Resets \ref soplex::VectorBase "VectorBase"'s memory size to \p newsize.
void reSize(int newsize)
{
assert(newsize > VectorBase<R>::dim());
// Problem: This is not a conventional resize for std::vector. This only
// updates the capacity, i.e., by pushing elements to the vector after this,
// there will not be any (internal) resizes.
val.reserve(newsize);
}
// For operations such as vec1 - vec2
const VectorBase<R> operator-(const VectorBase<R>& vec) const
{
assert(vec.dim() == dim());
VectorBase<R> res;
res.val.reserve(dim());
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
{
res.val.push_back(val[i] - vec[i]);
}
return res;
}
// Addition
const VectorBase<R> operator+(const VectorBase<R>& v) const
{
assert(v.dim() == dim());
VectorBase<R> res;
res.val.reserve(dim());
auto dimen = dim();
for(decltype(dimen) i = 0; i < dimen; i++)
{
res.val.push_back(val[i] + v[i]);
}
return res;
}
// The negation operator. e.g. -vec1;
friend VectorBase<R> operator-(const VectorBase<R>& vec)
{
VectorBase<R> res;
res.val.reserve(vec.dim());
for(auto& v : vec.val)
{
res.val.push_back(-(v));
}
return res;
}
///@}
/// Consistency check.
bool isConsistent() const
{
return true;
}
};
/// Inner product.
template<>
inline
Rational VectorBase<Rational>::operator*(const VectorBase<Rational>& vec) const
{
assert(vec.dim() == dim());
if(dim() <= 0 || vec.dim() <= 0)
return Rational();
Rational x = val[0];
x *= vec.val[0];
auto dimen = dim();
for(decltype(dimen) i = 1; i < dimen; i++)
x += val[i] * vec.val[i];
return x;
}
} // namespace soplex
#endif // _VECTORBASE_H_