how to convert a matrix in dlib to a std::vector - c++

I have a colume vector defined in dlib. How can I convert it to std::vector?
typedef dlib::matrix<double,0,1> column_vector;
column_vector starting_point(4);
starting_point = 1,2,3,4;
std::vector x = ??
Thanks

There are many ways. You could copy it via a for loop. Or use the std::vector constructor that takes iterators: std::vector<double> x(starting_point.begin(), starting_point.end()).

This would be the way you normally iterate over the matrix (doesn't matter if the matrix has only 1 column):
// loop over all the rows
for (unsigned int r = 0; r < starting_point.nr(); r += 1) {
// loop over all the columns
for (unsigned int c = 0; c < starting_point.nc(); c += 1) {
// do something here
}
}
So, why don't you iterate over your column vector and introduce each value into the new std::vector? Here is a full example:
#include <iostream>
#include <dlib/matrix.h>
typedef dlib::matrix<double,0,1> column_vector;
int main() {
column_vector starting_point(4);
starting_point = 1,2,3,4;
std::vector<double> x;
// loop over the column vector
for (unsigned int r = 0; r < starting_point.nr(); r += 1) {
x.push_back(starting_point(r,0));
}
for (std::vector<double>::iterator it = x.begin(); it != x.end(); it += 1) {
std::cout << *it << std::endl;
}
}

Related

Dynamically allocate ragged matrix

I'm trying to make a generic function which will dynamically allocate 2D structure. Number of elements in every row doesn't have to be same for all rows. Structure is represented as a container type, whose elements are again of a container type (for example a set of lists). The type of elements of that inner container can also be arbitrary. Containers only support the begin, end, and size functions. Iterator operations must be supported for all iterator types. The function should first dynamically allocate the space for storing the 2D structure by the continuous allocation procedure, and then rewrite the elements of the structure it has accepted into the dynamic structure. The function returns a double pointer through which the elements of this structure can be accessed.
#include <iostream>
#include <set>
#include <list>
#include <vector>
template < typename tip >
auto Make2DStructure(tip mat) {
using tip_objekta = typename std::decay < decltype(mat[0][0]) > ::type;
tip_objekta ** dynamic_mat = nullptr;
int rows = 0, total = 0;
for (auto i: mat) {
rows++;
for (auto j: i)
total++;
}
int columns[rows];
int k = 0;
for (auto i: mat) {
int num_of_colums = 0;
for (auto j: i)
num_of_colums++;
columns[k] = num_of_colums;
k++;
}
try {
dynamic_mat = new tip_objekta * [rows];
dynamic_mat[0] = new tip_objekta[total];
for (int i = 1; i < rows; i++)
dynamic_mat[i] = dynamic_mat[i - 1] + columns[i];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns[i]; j++)
dynamic_mat[i][j] = mat[i][j];
} catch (...) {
delete[] dynamic_mat[0];
delete[] dynamic_mat;
throw std::bad_alloc();
}
return dynamic_mat;
}
int main() {
std::vector<std::vector<int>>mat{
{1,2},
{3,4,5,6},
{7,8,9}
};
int columns[3]={2,4,3};
try {
int ** dynamic_mat = Make2DStructure(mat);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < columns[i]; j++)
std::cout << dynamic_mat[i][j] << " ";
std::cout << std::endl;
}
delete[] dynamic_mat[0];
delete[] dynamic_mat;
} catch (...) {
std::cout << "Problems with memory";
}
return 0;
}
How could I modify this to work without indexing inside Make2DStrucure()?
Also, if I used std::set<std::list<int>> instead of std::vector<std::vector<int>> in main function I would have deduction problems. How could I modify this to work for different outside and inside container?
Here's one way to accomplish what you want:
#include <iterator>
#include <type_traits>
template <typename tip>
auto Make2DStructure(tip&& mat) {
// create an alias for the value type:
using value_type = std::decay_t<decltype(*std::begin(*std::begin(mat)))>;
// allocate memory for the return value, the pointer-pointer:
value_type** rv = new value_type*[mat.size()]; // C++17: std::size(mat)
// Calculate the number of values we need to allocate space for:
size_t values = 0;
for(auto& inner: mat) values += inner.size(); // C++17: std::size(inner)
// allocate the space for the values:
value_type* data = new value_type[values];
// loop over the outer and inner container and keep the index running:
size_t idx = 0;
for(auto& inner : mat) {
// assign the outer pointer into the flat data block:
rv[idx++] = data;
for(auto& val : inner) {
// assign values in the data block:
*data++ = val;
}
}
return rv;
}
With the use of std::size where indicated, this would work with plain arrays too, not only the container classes.

Sort matrix by scheme

I need to sort negative elements of matrix by scheme below. I've tried to sort from another corner, but it is not working too. I think, that I need to put elems of matrix in 1d array. It should be sorted in Cocktail sort, but sort's type not my main problem.
My code:
int main() {
const int n = 4, m = 4;
int t, v[n*m], arr[n][m], i, j, tmp, lt, rt;
lt = 0;
rt = t;
srand(time(NULL));
for (i = 0; i < n; i++) {
for(j=0; j < m; j++) {
arr[i][j] = rand() % 100 - 50;
}
cout << endl;
}
t = 0;
for (i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
if (arr[i][j] < 0) {
v[t] = arr[i][j];
t++;
}
}
}
while(lt <= rt) {
for (i = rt; i >= lt; i--) {
if(v[i] > v[i-1]) {
swap(v[i], v[i-1]);
}
}
lt++;
for (i = lt; i <=rt; i++) {
if(v[i] > v[i-1]) {
swap(v[i], v[i-1]);
}
}
rt--;
}
for (i = 0; i < t; i++) {
cout << v[i] << " ";
}
int r = 0;
for (i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
if(arr[i][j] < 0) {
arr[i][j] = v[r];
r++;
}
}
}
}
The question sounds easy, but it is not. There is a lot of “indirection” in it, where you need to work with indices instead of values.
I shortly checked you code. It is mostly C–Code (not C++) and buggy.
Example:
int t;
rt = t;
With that you have an uninitialized variable, used as an array index. That is a fatal bug. You are also using VLA’s (Variable Length Array). This is not allowed in C++. And you are using plain C-Style arrays. This you should not do. Use std::vector, which can grow dynamically or at least std::array instead. And please give your variable more meaningful names.
I will show you one (out of the many possible) solutions, but I will use C++.
The core of the problem at hand is to find the row and column indices of the elements in the given matrix. That is not easy.
But ok, let’s start with that. If you draw a picture with the matrix and then add dotted lines over the diagonals, then you see the indices.
If the dimension of the matrix is dim then there are always dim + dim – 1 diagonals. The diagonals have first a rising number of elements and after hitting the main, longest diagonal in the middle, decreasing number of elements. So we iterate over the number of all diagonals, split by the middle diagonal, and calculate the corresponding row and column indices. This is a bit tricky, but after some time you will find out.
The resulting row and column indices will be stored in a struct. All diagonals with all row and column indices will be stored in a vector of struct. Additionally, we add the values of the original matrix cells.
Regarding the sorting. It is obviously your task to develop an own sorting algorithm. For that purpose, I created a function yourSort where you can put in your own algorithm. I simply use standard algorithms (std::sort). You may replace std::sort by your own function.
In main I put some driver code. First, we create a matrix and fill it with random values. Then we calculate the row and column indices. The entries with the negative values will be extracted and sorted. Then we copy the result back to the original matrix.
As said above, not so easy, because of the indirection with the indices and the constraint to use only negative numbers.
But anyway. Please see:
#include <iostream>
#include <vector>
#include <utility>
#include <random>
#include <algorithm>
#include <iterator>
#include <iomanip>
// Create types that are easy to understand
using RowIndex = size_t;
using ColumnIndex = size_t;
// Here we store the position (row and column) and the value of one cell in the matrix
struct PositionAndValue {
// Constructors
PositionAndValue() {};
PositionAndValue(const RowIndex r, const ColumnIndex c, const int v) : rowIndex(r), columnIndex(c), value(v) {};
// Data
RowIndex rowIndex{};
ColumnIndex columnIndex{};
int value{};
};
// Main data types
using Columns = std::vector<int>;
using Matrix = std::vector<Columns>;
using Diagonal = std::vector<PositionAndValue>;
// Fill matrix with random values. Standard function
void fillMatrixRandom(Matrix& m) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(-50, 50);
std::for_each(m.begin(), m.end(), [&](Columns &c) {std::for_each(c.begin(), c.end(), [&](int &j) { j = dis(gen);}); });
}
// Calculate the indices for all diagonals
Diagonal calculateDiagonalIndices(const Matrix& matrix) {
// The return value
Diagonal diagonalIndices{};
// Matrix dimension
const size_t MatrixDimension{ matrix.size() };
// Overall number of diagonals for this matrix
const size_t NumberOfDiagonals{ MatrixDimension + MatrixDimension - 1 };
// index of middle (longest) diagonal
const size_t MiddleDiagonal { NumberOfDiagonals / 2 + 1 };
// Counter for element index in one specific diagonal
size_t elementInDiagonal{ 0 };
for (size_t diagonalIndex = 1; diagonalIndex <= NumberOfDiagonals; ++diagonalIndex) {
// If we are above the middle diagonal
if (diagonalIndex <= MiddleDiagonal) {
// Number of elements in diagonal will increase
++elementInDiagonal;
for (size_t j = 0; j < elementInDiagonal; ++j) {
// Calculate row and column and add to result
const RowIndex row{ j };
const ColumnIndex col{ diagonalIndex - j - 1 };
diagonalIndices.emplace_back(PositionAndValue(row, col, matrix[row][col]));
}
}
else {
// We are below the middle diagonal
// Number of elements in diagonal will decrease
--elementInDiagonal;
for (size_t j = 0; j < elementInDiagonal; ++j) {
// Calculate row and column and add to result
const RowIndex row{ diagonalIndex + j - MatrixDimension };
const ColumnIndex col{ MatrixDimension - j - 1 };
diagonalIndices.emplace_back(PositionAndValue(row, col, matrix[row][col]));
}
}
}
return diagonalIndices;
}
// Simple sorting function using std algorithms
template <typename T, typename ValueType>
void yourSort(std::vector<T>& vec, ValueType T::* mPtr) {
// We will extract the negative values
std::vector<ValueType> vt{};
// Extract
std::transform(vec.begin(), vec.end(), std::back_inserter(vt), [&](const T & s) {return s.*mPtr; });
// Sort. ***** Please put here your sorting function
std::sort(vt.begin(), vt.end());
// Put back
std::for_each(vec.begin(), vec.end(), [&, i = 0U](T& s) mutable{s.*mPtr = vt[i++]; });
}
// Driver code
int main() {
// Lets use a matrix of this size
constexpr size_t MatrixDimension = 4U;
// Small lambda for printing a matrix
auto printMatrix = [](const Matrix & m) {std::for_each(m.begin(), m.end(), [](const Columns & c) {
for (int i : c) std::cout << std::setw(4) << i; std::cout << "\n"; }); std::cout << "\n"; };
// Define a matrix and fill it with random values
Matrix matrix(MatrixDimension, Columns(MatrixDimension));
fillMatrixRandom(matrix);
printMatrix(matrix);
// Calulate the indices on the diagonals
Diagonal diagonal{ calculateDiagonalIndices(matrix) };
// Extract the negatives
Diagonal negativesOnDiagonal{};
std::copy_if(diagonal.begin(), diagonal.end(), std::back_inserter(negativesOnDiagonal),
[](const PositionAndValue & pv) { return pv.value < 0; });
// Sort
yourSort(negativesOnDiagonal, &PositionAndValue::value);
// Copy back
std::for_each(negativesOnDiagonal.begin(), negativesOnDiagonal.end(),
[&matrix](const PositionAndValue & pv) { matrix[pv.rowIndex][pv.columnIndex] = pv.value; });
printMatrix(matrix);
return 0;
}

Way to replace one vector with another

I'm sorting 2 vectors using a vector of index.
the 2 vectors have not the same size. One vector (keys) is size X and the other (descriptors) is size X*128 (one key is parametrized by 128 values).
In order to create the vector of index, I generated a vector of unsigned, and used the iota function to put in this vector [0,1,2,...,X]
then I use the sort function to sort these index depending scale of a key (keys[i].s).
After that, I generate another vector in which I copy the values using the vector of index for both descriptors and keys (calling them descriptors_tmp, and keys_tmp), and then I want to make the first keys vector equal to the keys_tmp, and the same for descriptors equals to descriptors_tmp.
My question are :
-Is there a way to make that without making any copy. Since I don't need the previous version of keys and descriptors, I could just make the vector point on the other vector (something like *keys = *keys_tmp) ?
-Is there an easier way to achieve what I'm trying to achieve?
My code :
void _siftMatch::getIdxOfSorting(std::vector<unsigned>& idx_scale_order)
{
//keys[i].s is the scale and I sort depending decreasing scale
auto cmp_scale = [this](int i, int j) {
return keys[i].s > keys[j].s;
};
std::sort(idx_scale_order.begin(), idx_scale_order.end(), cmp_scale);
}
void _siftMatch::sort() {
//vector containing the index of sorted
std::vector<unsigned>idx_scale_order;
idx_scale_order.resize(keys.size());
//Generate [0,1,...,X]
std::iota(idx_scale_order.begin(), idx_scale_order.end(), 0);
//Sort the vector
getIdxOfSorting(idx_scale_order);
std::vector<float> descriptors_tmp;
std::vector<SiftGPU::SiftKeypoint> keys_tmp;
for (int i = 0; i < idx_scale_order.size(); ++i) {
keys_tmp.push_back(keys[idx_scale_order[i]]);
for (int j = 0; j < 128; ++j)
descriptors_tmp.push_back(descriptors[idx_scale_order[i] * 128 + j]);
}
//This is here that I want to put descriptors_tmp and keys_tmp in descriptors and keys
//descriptors.swap(descriptors_tmp.data);
}
Is there a way to make that without making any copy
This example of sorting 3 arrays according to one of the arrays, using a 4th generated array of indices that are sorted according to one of the 3 arrays may help. The key part of this is the in place reordering of all 4 arrays according to the array of indices. You'll need to modify this for your situation. I'm not sure why you are converting the array of indices to an array of numeric strings (via the itoa calls), using the indices directly works better for the example in this answer.
// sort 3 vectors according to one of them
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
int main()
{
std::vector <int> A; // ages
std::vector <std::string> N; // names
std::vector <int> Z; // zip codes
std::vector <size_t> I; // indices
int tA;
std::string tN;
int tZ;
A.push_back(37);
N.push_back("Ted");
Z.push_back(54211);
A.push_back(21);
N.push_back("John");
Z.push_back(53421);
A.push_back(31);
N.push_back("Fred");
Z.push_back(52422);
A.push_back(21);
N.push_back("Sam");
Z.push_back(51422);
// display the vectors
for(size_t i = 0; i < A.size(); i++)
std::cout << std::setw(6) << N[i]
<< std::setw(8) << Z[i]
<< std::setw(4) << A[i] << std::endl;
std::cout << std::endl;
// initialize the vector of indices
for(size_t i = 0; i < A.size(); i++)
I.push_back(i);
// sort I according to A
std::stable_sort(I.begin(), I.end(),
[&A](size_t i, size_t j) {return
A[i] < A[j];});
// reorder A, N, Z in place also restore I
// time complexity is O(n)
for(size_t i = 0; i < A.size(); i++){
size_t j, k;
if(i != I[i]){
tA = A[i];
tN = N[i];
tZ = Z[i];
k = i;
while(i != (j = I[k])){
A[k] = A[j];
N[k] = N[j];
Z[k] = Z[j];
I[k] = k;
k = j;
}
A[k] = tA;
N[k] = tN;
Z[k] = tZ;
I[k] = k;
}
}
// display the sorted vectors
for(size_t i = 0; i < A.size(); i++)
std::cout << std::setw(6) << N[i]
<< std::setw(8) << Z[i]
<< std::setw(4) << A[i] << std::endl;
return 0;
}

Two dimensional array read from file as argument

I want to read Graph from file and start function to get max flow but i get some error when i pass this graph as function argument. What am I doing wrong?
int main() {
fstream file;
file.open( "macierz.txt", ios::in );
int n = 7;
int graph[n][n];
int v;
for(int i = 0; i < n; i++){
for(int j = 0 ; j < n; j++){
file >> v;
graph[i][j] = v;
}
}
cout << "Ford-Fulkerson -MATRIX- The maximum possible flow: " << fordFulkersonMatrix(graph, 0, 6) << endl;
function :
int fordFulkersonMatrix(int graph[7][7], int start, int target) {
int u, v;
int rGraph[7][7];
int parent[7];
int max_flow = 0;
for (u = 0; u < 7; u++) {
for (v = 0; v < 7; v++)
{
rGraph[u][v] = graph[u][v];
}
}
Error:
main.cpp|200|error: cannot convert 'int (*)[(((unsigned int)(((int)n) + -0x000000001)) + 1)]' to 'const int (*)[7]' for argument '1' to 'int fordFulkersonMatrix(const int (*)[7], int, int)'|
If you program in Java, for sure you will like the C++ STL interfaces:
// or even better - use Eigen as Peter K mentioned
using MyCustomMatrix = std::vector<std::vector<int> >;
// input by const ref to prevent std::vector copy
int fordFulkersonMatrix(const MyCustomMatrix& input, int start, int target) {
// do whatever you need to do
// you can access your matrix elements like this:
int elem34 = input[3][4]; // only if your matrix if big enough, of course
// you can iterare using C++11 range loops
for (const auto& row : input) {
for(auto elem : row) {
std::cout << "My elem: " << elem << std::endl;
}
}
// and you can access your matrix dim information like this:
int numRows = input.size();
if (numRows > 0) {
int numCols = input[0].size();
}
return 0;
}
Later in the code, you can construct your matrix like that:
MyCustomMatrix mat;
mat.push_back(MyCustomMatrix::value_type()); // add one row
mat[0].push_back(69); // add one elem to the first row
fordFulkersonMatrix(mat, 0, 6);
Expected output:
My elem: 69
This is of course very lazy solution, but maybe you don't need anything more. For some serious linear algebra operations consider using specialized libraries (like already mentioned Eigen, or boost uBLAS).

Passing an array by reference as a function parameter in C++

How can I pass an array, declared in main method by reference as a parameter to a function? Also the compiler wants dimentions, but when i give them with variables, the compiler gives errors of invalid integer dimensions of the array, here's the code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int scanV(int Row, int value, int M, int (&tableValues)[])
{
for (int i = 0; i <= M; ++i)
{
if (tableValues[Row - 1][i] == 1)
{
if (i + value <= M)
{
tableValues[Row][i+value] == 1;
}
if (i - value >= 0)
{
tableValues[Row][i-value] = 1;
}
}
}
}
int main()
{
int C, B, M;
cin>>C;
int integers[C];
for (int i = 1; i < C; ++i)
{
cin>>integers[i];
}
cin>>B;
cin>>M;
integers[0] = B;
int tableValues[C][M + 1];
tableValues[0][B] = 1;
for (int i = 1; i < C; ++i)
{
scanV(i, integers[i], M, tableValues);
}
return 0;
}
One simple solution is to use vectors. Consider this simple example:
#include <iostream>
#include <vector>
void f (std::vector<std::vector<int> > &v)
{
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
v[i][j] = i * 3 + j;
}
int main()
{
std::vector<std::vector<int> > v (3, std::vector<int> (3, 0));
f (v);
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
std::cout << v[i][j] << ' ';
}
In main, a 2D vector of ints (3x3) is created. The constructor shows 3 elements, all initialized with a vector of ints, which are in turn created with 3 elements initialized to 0.
Then, the vector is passed by reference to the function f, which assigns increasing values. When the vector is printed in main, it shows:
0 1 2 3 4 5 6 7 8
As you can see, their use is very similar to normal arrays, but they are actually contained, and provide easy access to a new level of programming using the STL.
In C++11, their use becomes even more familiar. You can assign vectors as follows:
std::vector<int> v0 = {2, 5};
std::vector<std::vector<int> > v1 { {1,2,3} , {4,5,6} , {7,8,9} };
Note that for vectors of multiple dimensions it's a good idea to encapsulate it in a matrix class of some sort with an underlying 1D vector type instead.
Edit:
Here's an example of initializing a 1D and 2D vector to specified elements. As seen above, this is easy in C++11, but if you have an array already, it's still pretty quick.
int a [5] = {1,2,3,4,5}; //normal
std::vector<int> v1 (a, a +5); //create using beginning and ending addresses of a
int b[3][3] = { {1,2,3} , {4,5,6} , {7,8,9} }; //normal
std::vector<std::vector<int> > v2; //empty vector
for (int i = 0; i < 3; ++i) //3 in first dimension
v2.push_back (std::vector<int> (b [i], b [i] + 3)); //push a vector with the appropriate 3 elements of b onto the back of v2
For going through one element at a time, you can do this:
std::vector<std::vector<int> > v (3, std::vector<int> (3));
for (int i = 0; i < v.size(); ++i) //v.size() == 3
for (int j = 0; j < v [i].size(); ++j)
adjustElement (v [i][j]); //replace with what you need
std::vectors are the way to go in C++ as variable-length arrays (such as int integers[C];) are forbidden.
I recommend typedefing these to make your code easier to read:
#include <vector>
typedef std::vector<int> row_t;
typedef std::vector<row_t> table_t;
Then you can declare:
void scanV(int Row, int value, int M, table_t& tableValues)
I've made this void since it doesn't return anything.
Your variables in int main() then become:
row_t integers;
table_t tableValues;
Watch out for this line:
tableValues[Row][i+value] == 1;
You probably meant to assign, rather than to check equivalence!
And please try to pick some more meaningful variable names than B, M, C...