c++ - Finding position of an element in vector of vectors -
let a
vector of vectors of type double
, i.e. vector<vector<double> > a
, b
same integers, i.e. vector<vector<int> > b
. assume size of a
, b
same (and sizes of every nested vectors equal well).
i check if j
contained in i
-th vector of b
using std::find
, write std::find(b[i].begin(), b[i].end(), j) != b[i].end()
. now, if true
return value in a
corresponding [i][position_of_j_found_in_b[i]]
. how can accomplish this?
my code follows:
class sparsematrix { private: vector<vector<double> > entries_; vector<vector<int> > columnindices_; public: sparsematrix(); sparsematrix(vector<vector<double> >,vector<vector<int> >); ~sparsematrix(); // getters vector<vector<double> > getentries(); vector<vector<int> > getcolindices(); double operator()(const unsigned int&, const unsigned int&); vector<int> size(); }; sparsematrix::sparsematrix() { }; sparsematrix::sparsematrix(vector<vector<double> > values, vector<vector<int> > colind) : entries_(values), columnindices_(colind) { }; sparsematrix::~sparsematrix() { }; vector<vector<double> > sparsematrix::getentries() { return entries_; } vector<vector<int> > sparsematrix::getcolindices() { return columnindices_; } double sparsematrix::operator()(const unsigned int i, const unsigned int j) { assert(i <= (*this).size()[0] && j <= (*this).size()[1]); auto = find(columnindices_[i].begin(),columnindices_[i].end(), j); if(it != columnindices_[i].end()) return entries_[i][it - columnindices_[i].begin()]; return 0.0; } vector<int> sparsematrix::size() // returns dimensions of matrix { vector<int> dim(2); // stores dimensions of matrix dim[0] = columnindices_.size(); // numbers of rows in matrix int temp = 0; vector<vector<int> >::iterator i; for(i=columnindices_.begin(); != columnindices_.end(); ++i) { if(*max_element((*i).begin(),(*i).end()) > temp) // if maximal element in i-th vector of columnindices_ greater current (temp) temp = *max_element((*i).begin(),(*i).end()); // update current biggest new 1 } dim[1] = temp; return dim; }
and main()
function is
int main(int argc, char const *argv[]) { vector<vector<double> > values {{3.0, 1.0}, {2.0}, {5.0, 4.0}}; vector<vector<int> > columns {{1,3}, {1}, {2,3}}; sparsematrix a(values,columns); cout << "matrix has dimensions " << a.size()[0] << "x" << a.size()[1] << "." << endl; // works fine cout << a.getentries()[0][0] << " " << a(0,0) << endl; // expect same output... return 0; }
here solution solution of original problem:
double foo(const vector<vector<double> >& x, const vector<vector<int> >& y, unsigned int i, unsigned int j) { auto = find(y[i].begin(),y[i].end(), j); if (it != y[i].end()) return x[i][it - y[i].begin()]; return 0.0; }
p.s. pass vector reference because optimization, same reason pass unsigned int value.
here example of sparsematrix implementation:
#include <cassert> #include <vector> #include <tuple> #include <iostream> #include <cmath> #include <algorithm> class sparsematrix final { public: explicit sparsematrix(const std::vector<double>& full_matrix) { static constexpr double eps = 1e-16; const size_t n = std::sqrt(full_matrix.size()); assert((n * n) == full_matrix.size()); (size_t = 0; < n; ++i) (size_t j = 0; j < n; ++j) if (std::fabs(full_matrix[i * n + j]) > eps) data_.push_back(std::make_tuple(full_matrix[i * n + j], i, j)); } double operator()(size_t i, size_t j) const { auto = std::find_if(data_.begin(), data_.end(), [&i, &j](const std::tuple<double, size_t, size_t>& elm) { return std::get<1>(elm) == && std::get<2>(elm) == j; }); return == data_.end() ? 0.0 : std::get<0>(*it); } private: std::vector<std::tuple<double, size_t, size_t>> data_; }; int main() { sparsematrix a({3.0, 0, 1.0, 2.0, 0, 0, 0, 5.0, 4.0}); std::cout << a(0, 0) << '\n'; }
note sparsematrix study projects, every day usage, best way use existing classes linear algebra c++.
Comments
Post a Comment