CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Views: 418425
1
/*
2
* Normaliz
3
* Copyright (C) 2007-2014 Winfried Bruns, Bogdan Ichim, Christof Soeger
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*
17
* As an exception, when this program is distributed through (i) the App Store
18
* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play
19
* by Google Inc., then that store may impose any digital rights management,
20
* device limits and/or redistribution restrictions that are required by its
21
* terms of service.
22
*/
23
//---------------------------------------------------------------------------
24
#ifndef VECTOR_OPERATIONS_H
25
#define VECTOR_OPERATIONS_H
26
//---------------------------------------------------------------------------
27
28
#include <vector>
29
#include <ostream>
30
#include <list>
31
32
#include <libQnormaliz/libQnormaliz.h>
33
#include <libQnormaliz/Qinteger.h>
34
#include <libQnormaliz/Qconvert.h>
35
36
namespace libQnormaliz {
37
using std::vector;
38
39
//---------------------------------------------------------------------------
40
// Data access
41
//---------------------------------------------------------------------------
42
43
template <typename T>
44
std::ostream& operator<< (std::ostream& out, const vector<T>& vec) {
45
for (size_t i=0; i<vec.size(); ++i) {
46
out << vec[i] << " ";
47
}
48
out << std::endl;
49
return out;
50
}
51
52
//---------------------------------------------------------------------------
53
// Vector operations
54
//---------------------------------------------------------------------------
55
template<typename Number>
56
Number v_scalar_product(const vector<Number>& a,const vector<Number>& b);
57
58
//returns the scalar product of the vector a with the end of the vector b
59
template<typename Number>
60
Number v_scalar_product_unequal_vectors_end(const vector<Number>& a,const vector<Number>& b);
61
62
//returns the addition a + b, vectors must be of equal size
63
template<typename Number>
64
vector<Number> v_add(const vector<Number>& a,const vector<Number>& b);
65
template<typename Number>
66
vector<Number> v_add_overflow_check(const vector<Number>& a,const vector<Number>& b);
67
template<typename Number>
68
void v_add_result(vector<Number>& result, const size_t length, const vector<Number>& a,const vector<Number>& b);
69
70
71
72
//---------------------------------------------------------------------------
73
// Scalar operations
74
//---------------------------------------------------------------------------
75
76
//v = v * scalar
77
template<typename Number>
78
void v_scalar_multiplication(vector<Number>& v, const Number scalar){
79
size_t i,size=v.size();
80
for (i = 0; i <size; i++) {
81
v[i] *= scalar;
82
}
83
}
84
85
//---------------------------------------------------------------------------
86
87
template<typename Number>
88
void v_scalar_division(vector<Number>& v, const Number scalar){
89
size_t i,size=v.size();
90
for (i = 0; i <size; i++) {
91
v[i] /= scalar;
92
}
93
}
94
95
//---------------------------------------------------------------------------
96
// General vector operations
97
//---------------------------------------------------------------------------
98
99
//returns a new vector with the content of a extended by b
100
template<typename T>
101
vector<T> v_merge(const vector<T>& a, const T& b);
102
103
//returns a new vector with the content of a and b
104
template<typename T>
105
vector<T> v_merge(const vector<T>& a, const vector<T>& b);
106
107
//returns a new vector with the last size entries of v
108
template<typename T>
109
vector<T> v_cut_front(const vector<T>& v, size_t size);
110
111
//the input vectors must be ordered of equal size
112
//if u is different from v by just one element, it returns that element
113
//else returns 0 (the elements of u and v are >0)
114
//int v_difference_ordered_fast(const vector<size_t>& u,const vector<size_t>& v);
115
116
117
template<typename Number>
118
bool compare_last (const vector<Number>& a, const vector<Number>& b)
119
{
120
return a.back() < b.back();
121
}
122
123
//returns a key vector containing the positions of non-zero entrys of v
124
template<typename Number>
125
vector<key_t> v_non_zero_pos(const vector<Number>& v);
126
127
// counts the number of positive entries
128
template<typename Number>
129
size_t v_nr_positive(const vector<Number>& v);
130
131
// check whether the vector only contains 0
132
template<typename Number>
133
bool v_is_zero(const vector<Number>& v);
134
135
template<typename Number>
136
bool v_is_symmetric(const vector<Number>& v);
137
138
template<typename Number>
139
bool v_is_nonnegative(const vector<Number>& v);
140
141
template<typename Number>
142
Number v_max_abs(const vector<Number>& v){
143
Number tmp = 0;
144
for (size_t i=0; i<v.size(); i++){
145
if (Iabs(v[i])>tmp) tmp=Iabs(v[i]);
146
}
147
return tmp;
148
}
149
150
//---------------------------------------------------------------------------
151
// bool vector operations
152
//---------------------------------------------------------------------------
153
154
vector<bool> v_bool_andnot(const vector<bool>& a, const vector<bool>& b);
155
156
// swaps entry i and j of the vector<bool> v
157
void v_bool_entry_swap(vector<bool>& v, size_t i, size_t j);
158
159
//---------------------------------------------------------------------------
160
// Special
161
//---------------------------------------------------------------------------
162
163
// computes integral simplex containing a rational vector
164
template<typename Number>
165
void approx_simplex(const vector<Number>& q, std::list<vector<Number> >& approx,const long k);
166
167
vector<key_t> identity_key(size_t n);
168
169
//---------------------------------------------------------------------------
170
// Sorting
171
//---------------------------------------------------------------------------
172
173
template <typename T>
174
void order_by_perm(vector<T>& v, const vector<key_t>& permfix);
175
176
} // namespace
177
178
//---------------------------------------------------------------------------
179
#endif
180
//---------------------------------------------------------------------------
181
182