I've got some serious problem with basic scalar multiplication of 2 vectors in Eigen lib.
When I have two vectors....both are the rows of matrix..so... I must second .transpose();, I must to get one number..so how its possible that I can access to result of this vectors in 1 iterator 2,3,4,5...??????!!!!!
q_c=matVk.row(ks);
lk=(matVk.row(i).transpose());
vectors multiply :
hore= q_c * lk;
the result is totally different than have to be.....and the values in vectors are ok,,i checked it,,I try everything to solve this....I try more specific init.: of vectors: same result
VectorXd hore(1);
VectorXd lk(k);
VectorXd q_c(k);
The following are the typedefs defined in the documentation:
typedef Matrix< double , 1, Dynamic > RowVectorXd
typedef Matrix< double , Dynamic , 1> VectorXd
If you multiply a column vector to a column vector, you will get the result what you are getting as question details are not much clear. But I guess this is the mistake you are making. Try this instead:
VectorXd hore(1);
VectorXd lk(k);
RowVectorXd q_c(k);
hore = q_c*lk
Related
Is there a fast way to make conversion below?
use "std::vector<std::pair<double, double>> dataVector" to initialize "Eigen::MatrixXd rtData(dataVector.size(), 2)"
Assuming sizeof(std::pair<int,int>) == 2*sizeof(int) (i.e., std::pair is not adding any padding) you can use an Eigen::Map of a row-major integer matrix, then cast it to double. Something like this:
typedef Eigen::Matrix<int, Eigen::Dynamic, 2, Eigen::RowMajor> MatrixX2i_r;
Eigen::MatrixXd rtData
= Eigen::Map<MatrixX2i_r const>(&(dataVector[0].first), dataVector.size(), 2).cast<double>();
You can also use that expression directly without storing it into a MatrixXd (conversion will happen on the fly, every time the expression is used -- so if you use rtData multiple times, it could still be better to store it once).
I am assuming you wish to create a two column matrix from bunch of pairs stacked in a vector.
As far I can see in the documentation MatrixXd is typedef of Matrix<double,dynamic,dynamic> so first potential issue is that you wish to fill matrix of double by int values.
Since MatrixXd is merely a typedef I don't think there is a suitable constructor in Eigen library for you to do quick conversion.
Simply write a code or a function that iterates through your vector and populates the matrix. In C++14:
int itt=0;
for (const auto& [first, second] : dataVector)
{
rtData(itt,0) = first;
rtData(itt,1) = second;
itt++;
}
That is a neat for loop that can go through vector of pairs, you still need the index of the elements, as Eigen matrices do not have push_back methods as STL.
Note again that I would advise MatrixXi if you are populating it with integers and not doubles.
I have two Eigen vectors (vectorOne and vectorTwo) of my defined type( see below for my type).
typedef Matrix<double, 50, 1> myVector;
I want a third vector vectorThree that will have multiplication of vectorOne and vectorTwo. But I want to multiply each element by corresponding element - i.e. vectorOne(i, 0) by vectorTwo (i, 0) so that I have something like below for all i.
vectorThree (i, 0) = vectorOne(i, 0) * vectorTwo(i, 0)
I saw this and tried vectorOne.array() * vectorTwo.array() but it did not work.
I know I can do that using a for loop and iterating over all elements. But is there a more efficient or built in Eigen function for that?
You should be able to cast matrices to arrays via .array() and multiply it here. It would return an array expression though, so maybe it is not what you want.
From Eigen documentation:
First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays are fundamentally different from matrices, is when you multiply two together. Matrices interpret multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two arrays can be multiplied if and only if they have the same dimensions.
Otherwise you can use .cwiseProduct of matrix to get matrix as result.
https://eigen.tuxfamily.org/dox/group__QuickRefPage.html#matrixonly
I have two vectorXd in my program and I like to concatenate them into one vector, so that the second one's values goes after the first one, I found this for matrix but it doesn't seem to work on Vectors:
Eigen how to concatenate matrix along a specific dimension?
Like so, assuming you have vec1 and vec2 already:
VectorXd vec_joined(vec1.size() + vec2.size());
vec_joined << vec1, vec2;
(Note that the vector types are simply typedefs of matrix types constrained to have only one column.)
Further reading: Advanced initialization
I am trying to solve a simple least square of type Ax = b. The c++ eigen library offers several functionalities regarding this and I have seen some kind of solutions here: Solving system Ax=b in linear least squares fashion with complex elements and lower-triangular square A matrix and here: Least Squares Solution of Linear Algerbraic Equation Ax = By in Eigen C++
What I want to do is that using dynamic version of the matrix A and b. The elements of matrix A are floating points in my case and has 3 columns, but the number of data items (i.e. rows) will be dynamic (inside a loop).
It will be helpful to have a short code snippet of basic declaration of A, b and filling out values.
If you need dynamic matrices/vectors, just use:
MatrixXd m1(5,7); // double
VectorXd v1(23); // double
MatrixXf m2(3,5); // floating
VectorXf v2(12); // floating
Those variables will all be saved in heap.
If you need square matrices or vectors with fixed size (but be careful, they aren't dynamic!) use the following syntax:
Matrix3d m3; // double, size 3x3
Vector3d v3; // double, size 1x3
Matrix4d m4; // double, size 4x4
Vector4d v4; // double, size 1x4
I have a 2D Eigen Array where each item in the array is a 3-element Eigen Vector (e.g. a velocity field over a surface).
I want to multiply each each element of the 2D array with a 3-element Eigen RowVector, effectively taking the dot product.
Eigen::Array<Eigen::Vector3d, Eigen::Dynamic, Eigen::Dynamic> velField(5, 5);
Eigen::Vector3d n;
// ... initialisation of n and velField not shown
Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> result(5, 5);
result = n.transpose() * velField;
This gives a compile error YOU MIXED DIFFERENT NUMERIC TYPES. However, if I don't assign the result, but just compute it:
n.transpose() * velField;
it compiles. What is the correct return type for 'result' - or how else can I fix this?
EDIT
The same problem can be observed in this simpler case (multiplying by a double):
Eigen::Array<Eigen::Vector3d, Eigen::Dynamic, Eigen::Dynamic> velField(5, 5);
// ... initialisation of velField not shown
Eigen::Array<Eigen::Vector3d, Eigen::Dynamic, Eigen::Dynamic> result(5, 5);
result = 3.0 * velField;
However the same code works when using std::complex<double> instead of Eigen::Vector3d:
Eigen::Array<std::complex<double>, Eigen::Dynamic, Eigen::Dynamic> velField(5, 5);
// ... initialisation of velField not shown
Eigen::Array<std::complex<double>, Eigen::Dynamic, Eigen::Dynamic> result(5, 5);
result = 3.0 * velField;
I wonder why it works for std::complex but not with Eigen::Vector3d. Both types define the operator * with a double.
After further reading, I found an answer:
According to the reference documentation of the matrix class, the first template parameter _Scalar is:
_Scalar: matrix_tparam_scalar Numeric type, e.g. float, double, int or std::complex<float>.
User defined sclar types are supported as well.
It links to this page. It lists the requirements for the "custom scalar types". In the above example Eigen::NumTraits for <Vector3d> is missing. It can't be implemented properly for Vector3d, so one should only store types that represent scalars inside an Eigen::Matrix/Eigen::Array.
Update:
The line without assignment n.transpose() * velField; works because of lazy evalutation. It also works when doing this (in C++11):
auto result = n.transpose() * velField;
But it has done no calculation at that point (check the type of result in the debugger). As soon as you use result it will fail the same way than in your first example.
This is not allowed. I'd recommend to store velField as a Matrix, i.e., a 1D array of 3D vectors, and then use standard linear algebra operators.