It's possible to convert an Eigen::Map to a Matrix by assignment:
vector<float> v = { 1, 2, 3, 4 };
auto m_map = Eigen::Map<Eigen::Matrix<float, 2, 2, Eigen::RowMajor>>(&v[0]);
Eigen::MatrixXf m = m_map;
cout << m << endl;
This produces:
1 2
3 4
If I try to do something similar with a Tensor:
vector<float> v = { 1, 2, 3, 4 };
auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
Eigen::Tensor<float, 2> t = mapped_t;
I simply get the compiler error YOU_MADE_A_PROGRAMMING_MISTAKE. Is there any way to convert a TensorMap to a Tensor?
Well, Eigen::RowMajor is not the default for Eigen::Tensor which means you are not assigning to the same type which means YOU_MADE_A_PROGRAMMING_MISTAKE. You have to explicitly request swapping the layout.
#include <vector>
#include <unsupported/Eigen/CXX11/Tensor>
int main()
{
std::vector<float> v = { 1, 2, 3, 4 };
auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
Eigen::Tensor<float, 2> t = Eigen::TensorLayoutSwapOp<Eigen::Tensor<float, 2, Eigen::RowMajor>>(mapped_t);
}
Using C++14 you could write a nice instantiator function for that.
#include <type_traits>
#include <vector>
#include <unsupported/Eigen/CXX11/Tensor>
namespace Eigen {
template < typename T >
decltype(auto) TensorLayoutSwap(T&& t)
{
return Eigen::TensorLayoutSwapOp<typename std::remove_reference<T>::type>(t);
}
}
int main()
{
std::vector<float> v = { 1, 2, 3, 4 };
auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
Eigen::Tensor<float, 2> t = Eigen::TensorLayoutSwap(mapped_t);
}
Related
I am having trouble swapping rows inside a Matrix (nested array) according to the value of a specific column, I am making a console application that displays the results of a group stage in the football world cup so first I insert the results of the games to update the statistics of each team (rows) and generate the input array, but finally, I need to sort the rows according to the number of points of each team (the last position in each row). I need to create a function to generate the output array.
Input array:
inputArray[4][7] {
{0, 2, 1, 1, 3, -2, 2},
{1, 1, 1, 3, 3, 0, 4},
{2, 0, 1, 3, 1, 2, 6},
{1, 1, 1, 2, 2, 0, 4},
}
Output array of the function:
outputArray[4][7] {
{2, 0, 1, 3, 1, 2, 6},
{1, 1, 1, 3, 3, 0, 4},
{1, 1, 1, 2, 2, 0, 4},
{0, 2, 1, 1, 3, -2, 2},
}
The solution is straightforward. But my guess is that this is not what you want.
And, I think that the last value in the row is not the sum . . .
But let us first look at one potential solution:
#include <iostream>
#include <algorithm>
#include <array>
constexpr size_t NumberOfRows = 4u;
constexpr size_t NumberOfColumns = 7u;
using Columns = std::array<int, NumberOfColumns>;
using Array = std::array<Columns,NumberOfRows>;
int main() {
Array array{{ {0, 2, 1, 1, 3, -2, 2},
{1, 1, 1, 3, 3, 0, 4},
{2, 0, 1, 3, 1, 2, 6},
{1, 1, 1, 2, 2, 0, 4} }};
std::sort(std::begin(array), std::end(array), [](const Columns& c1, const Columns& c2) {return c1[6] < c2[6]; });
for (const Columns& c : array) {
for (const int i : c) std::cout << i << '\t';
std::cout << '\n';
}
}
If you want the array to be dynamic, then you may use a std::vector instead. You can then resize the number of rows and then number of columns.
#include <iostream>
#include <algorithm>
#include <vector>
constexpr size_t NumberOfRows = 4u;
constexpr size_t NumberOfColumns = 7u;
using Columns = std::vector<int>;
using Array = std::vector<Columns>;
int main() {
Array array{ {0, 2, 1, 1, 3, -2, 2},
{1, 1, 1, 3, 3, 0, 4},
{2, 0, 1, 3, 1, 2, 6},
{1, 1, 1, 2, 2, 0, 4} };
std::sort(std::begin(array), std::end(array), [](const Columns& c1, const Columns& c2) {return c1[6] < c2[6]; });
for (const Columns& c : array) {
for (const int i : c) std::cout << i << '\t';
std::cout << '\n';
}
}
But I still think that this is the wrong design. Becuase the last value in a row is the sum of other values. It is dependent, can be calculated, and there is no need to store ist.
See the following better design:
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
struct Result {
std::vector<int> values{};
int sum() const { return std::accumulate(values.begin(), values.end(), 0); }
friend std::ostream& operator << (std::ostream& os, const Result& r) {
for (const int i : r.values) os << i << '\t';
return os << "--> " << r.sum();;
}
};
struct Series {
std::vector<Result> results{};
friend std::ostream& operator << (std::ostream& os, const Series& s) {
for (const Result r : s.results) os << r << '\n';
return os;
}
};
int main() {
Series series{{
{{0, 2, 1, 1, 3,-2}},
{{1, 1, 1, 3, 3, 0}},
{{2, 0, 1, 3, 1, 2}},
{{ 1, 1, 1, 2, 2, 0}}
}};
std::sort(series.results.begin(), series.results.end(), [](const Result& r1, const Result& r2) {return r1.sum() < r2.sum(); });
std::cout << series;
}
But you did not give enough information to give a good answer.
There are a function called inner_product, but I failed miserably in use that. I'll need to use this function several times for different matrices and vectors. Bellow my current code:
std::vector<vector<int>> matrix_a = {{0, 0},
{0, 1},
{1, 0},
{1, 1}};
std::vector<float> vector_b = {0.5, 0.8};
dot_produt(matrix_a, vettor_b);
float dot_produt(vector<vector<int>> e, vector<float> p){
return std::inner_product(std::begin(e), std::end(e), std::begin(p), 0.0);
}
The process is like:
(0.5 * 0) + (0.8 * 0) + (0.5 * 0) + (0.8 * 1)... ...
Expected output:
2.6
Error:
no match for 'operator*' (operand types are 'std::vector<int>' and 'float')
__init = __init + (*__first1 * *__first2);
You are trying to use pointers to begin and end of a vector of vectors, inner_product requires pointers to beginning and end of a vector.
Also, vectors have their own iterators, you can use them instead of std::begin and std::end.
Live demo
#include <iostream>
#include <numeric>
#include <vector>
//passing vectors by reference avoids unnecessary copies
double dot_produt(const std::vector<std::vector<int>> &e, const std::vector<float> &p)
{
double result = 0;
for (auto& v : e) //range based loop
result += std::inner_product(v.begin(), v.end(), p.begin(), 0.0);
return result;
}
int main()
{
std::vector<std::vector<int>> matrix_a = {{0, 0},
{0, 1},
{1, 0},
{1, 1}};
std::vector<float> vector_b = {0.5, 0.8};
std::cout << dot_produt(matrix_a, vector_b); //test print
}
Output:
2.6
I have a vector of digits, for example {3, 6, 0, 1, 8}
I need to covert it to an integer using every digit of a vector consistently.
So the number i'll get is 36018.
Possible solution:
std::vector<int> values = {1, 3, 4, 5};
int res = 0, s = values.size();
for(int num : values) res += num * pow(10, --s);
I want to know if there is some more "elegant", or short maybe, way to do this using stl algorithms.
You could use std::accumulate
std::vector<int> values = {1, 3, 4, 5};
int result = std::accumulate(values.begin(), values.end(), 0, [](int acc, int val){
return 10 * acc + val;
});
std::cout << result << std::endl; // 1345
A regular for loop is easier to read and therefore IMO is the more elegant choice:
int num = 0;
for (int d : values) {
num = num * 10 + d;
}
With C++20-Ranges or range-v3 it can be made quite readable
#include <iostream>
#include <vector>
#include <range/v3/all.hpp>
int main() {
std::vector<int> values{1, 2, 3};
auto powers_of_10 = ranges::view::generate([n = 1]() mutable {
auto res = n;
n *= 10;
return res;
});
auto num = ranges::inner_product(powers_of_10, values | ranges::view::reverse, 0);
std::cout << num << '\n';
}
The idea here is to produce a range of powers of 10 [1, 10, 100, ...] and then to simply calculate the dot product with the reversed input vector.
It could have been even more expressive if there were a iterate_view that iteratively applies a function to a value.
I want to copy values from one vector to other one that will be stored in a specific order and the second vector will contain more elements than the first one.
For example:
vector<int> temp;
temp.push_back(2);
temp.push_back(0);
temp.push_back(1);
int size1 = temp.size();
int size2 = 4;
vector<int> temp2(size1 * size2);
And now I would like to fill temp2 like that: {2, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1}.
Is it possible to do this using only algorithms (e.g. fill)?
Yes, it is possible using std::generate_n algorithm:
int main() {
std::vector<int> base{1, 0, 2};
const int factor = 4;
std::vector<int> out{};
std::generate_n(std::back_inserter(out), base.size() * factor,
[&base, counter=0]() mutable {
return base[counter++ / factor];
});
for(const auto i : out) {
std::cout << i << ' ';
}
}
This code prints: 1 1 1 1 0 0 0 0 2 2 2 2
The key is the lambda used in std::generate_n. It operates on internal counter to know which values, based on base vector (and accessed depending on factor and counter values), to generate.
No, this is quite a specific use case, but you can trivially implement it yourself.
#include <vector>
#include <iostream>
std::vector<int> Elongate(const std::vector<int>& src, const size_t factor)
{
std::vector<int> result;
result.reserve(src.size() * factor);
for (const auto& el : src)
result.insert(result.end(), factor, el);
return result;
}
int main()
{
std::vector<int> temp{2, 0, 1};
std::vector<int> real = Elongate(temp, 4);
for (const auto& el : real)
std::cerr << el << ' ';
std::cerr << '\n';
}
(live demo)
I need to minimize H in following equation:
Where H is 3x3 Matrix.
Pn is 3x1 matrix (point).
Euclidean() gives distance between 2 points.
Dn is the actual distance.
I have one initial estimate of H and m points(P0 to Pm)
I need optimize value of H such that for all m points error is minimized.
(All the values in the expression are known)
How can I implement this using opencv or dlib (or using boost/NLopt).
Although the documentation of find_optimal_parameters function of dlib library was really not enough, there is a unit test that you can find on github which shows how to use the function.
I saw the other question you've asked and seems that the solution was something different than in this question. However, here is an example, how to use the library (this is the first time I'm hearing about it) to calculate what you need or something very close to that. Probably you will need to change the DistanceQuality() function (by replacing the existing loop with two nested ones) and I'll let you do it yourself.
Please note, that everything all over the code is hardcoded, no error handling is done and the testing is done right in the main() function. There's lots of work to be done, though you can find the code working for illustration purposes.
Here we go:
#include <iostream>
#include <dlib/optimization.h>
#include <dlib/optimization/find_optimal_parameters.h>
using namespace dlib;
typedef matrix<double, 3, 1> MyPoint;
std::vector<MyPoint> points;
std::vector<double> distances;
double MyDistance(MyPoint point1, MyPoint point2)
{
double sum = 0;
for (int i = 0; i < 3; i++)
{
sum += (point1(i, 0) - point2(i, 0)) * (point1(i, 0) - point2(i, 0));
}
return sqrt(sum);
}
double DistanceQuality(const matrix<double, 3, 3>& H)
{
double sum = 0;
for (int i = 0; i < points.size() - 1; i++)
{
auto proj1 = H*points[i];
auto proj2 = H*points[i+1];
sum += abs(MyDistance(proj1, proj2) - distances[i]);
}
return sum;
}
matrix<double, 3, 3> VecToMatrix(matrix<double, 0, 1> vec)
{
matrix<double, 3, 3> matrix;
for (int i = 0; i < 9; i++)
{
matrix(i / 3, i % 3) = vec(i);
}
return matrix;
}
double test_function(matrix<double, 0, 1> H)
{
matrix<double, 3, 3> newH = VecToMatrix(H);
auto result = DistanceQuality(newH);
return result;
}
int main()
{
matrix<double, 3, 1> p1;
matrix<double, 3, 1> p2;
matrix<double, 3, 1> p3;
p1 = { 1, 1, 1 };
p2 = { 2, 2, 3 };
p3 = { 3, 1.6, 7};
points.push_back(p1);
points.push_back(p2);
points.push_back(p3);
double d1 = 2.44949;
double d2 = 4.142463;
distances.push_back(d1);
distances.push_back(d2);
matrix<double, 0, 1> H;
H = { 3, 1, 1,
1, 1, 6,
1, 4, 1 };
matrix<double, 0, 1> H_min;
matrix<double, 0, 1> H_max;
H_min = { 0.5, 0.6, 0.5,
0.5, 0.7, 0.5,
0.8, 0.3, 0.5, };
H_max = { 10, 10, 10,
10, 10, 10,
10, 10, 10, };
dlib::find_optimal_parameters(4, 0.001, 1000, H, H_min, H_max, test_function);
std::cout << "new H: " << std::endl << VecToMatrix(H) << std::endl;
return 0;
}
Hope you can adapt the parameters for you specific case.