Pair of vectors instead of a vector<pair>?
Tag : cpp , By : user179271
Date : March 29 2020, 07:55 AM
I hope this helps . Maybe you are looking something like flat_(multi)map/set from Boost.Container ?
|
Vector of pairs or pair of vectors?
Tag : cpp , By : Jonathan Bernard
Date : March 29 2020, 07:55 AM
may help you . In my code I'm dealing with a set of measurements (a vector of float), in which each element has two associated uncertainties (say +up/-down). Let's say I want to dump on screen these values, something like , This:
|
sorting vector of pair of vectors
Tag : cpp , By : Andrew L.
Date : March 29 2020, 07:55 AM
this one helps. You may have a particularly complicated data structure with a super specific and arbitrarily ordering that you want to define on it, but no matter. The nice thing about std::sort is that it's completely accommodating: std::sort(A.begin(), A.end(), MyComparator);
typedef pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > Elem;
bool MyComparator(const Elem& a, const Elem& b) {
// return true if a should go before b in the sort
// step 1 is apparently to do a vector comparison
for (size_t i = 0; i < a.second.second.size(); ++i) {
if (i == b.second.second.size()) {
return false;
}
else if (a[i] < b[i]) {
return true;
}
else if (a[i] > b[i]) {
return false;
}
}
if (a.second.second.size() < b.second.second.size()) {
return true;
}
// if we got here, the two vectors compare equal
// so onto the next steps 2, 3, ...
// etc.
}
|
Is it possible to get scipy.integrate.odeint to work with a pair of vectors (or vector of vectors)
Date : March 29 2020, 07:55 AM
this one helps. As Warren mentioned, the answer is that odeint does require 1-D arrays. The trick is to set up the function passed to odeint so that it converts the 1-D array passed in as the second in the vector form desired - in this case, 2 3-D vectors, perform the calculations in vector form, then reshape the result back as a 1-D array. The answer demonstrates the technique, using the convenient numpy reshape function. def position_and_velocity (x, t, params):
# x = (S, V) as vectors
# Ordinary differential equation - velocity of an object in frictionless free-fall.
G = params
g = np.array ([0, 0, -G])
# convert the 6 element vector to 2 3 element vectors of displacement and velocity
# to use vector formulation of the math
s,v = x.reshape (2,3)
acceleration = np.array ([v * t, g])
# reshape the two vector results back into one for odeint
return np.reshape (acceleration, 6)
s = np.array ([0, 0, 5])
v = np.array ([40, 10, 0])
SV0 = np.array ([s, v])
# pass reshaped displacement and velocity vector to odeint
soln = odeint (position_and_velocity, np.reshape (SV0, 6), t, args = (G,))
|
Working with a vector of pair vectors?
Tag : cpp , By : Tom Smith
Date : March 29 2020, 07:55 AM
seems to work fine I've been search around Google but I didn't find what I need. I'm trying to create a vector that allows me to add 3 (and after I'll need to store 4) variables, access and sort them. , First to access to the different elements: for (auto& x :chromosomes)
cout <<x.first<<": "<<x.second.first<<" "<<x.second.second<<endl;
sort(chromosomes.begin(), chromosomes.end(),
[](auto &x, auto &y) { return x.second.first<y.second.first;});
sort(chromosomes.begin(), chromosomes.end(),
[](auto &x, auto &y) { return x.second.first<y.second.first
|| (x.second.first==y.second.first
&& x.second.second<y.second.second );});
struct Chromosome {
string name;
int WSA;
double fault_percent;
};
vector <Chromosome> chromosomes;
sort(chromosomes.begin(), chromosomes.end(),
[](auto &x, auto &y) { return x.WSA<y.WSA
|| (x.WSA==y.WSA && x.fault_percent<y.fault_percent );});
|