Multiply two Eigen vectors by corresponding elements - c++

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

Related

array multiplication having different sizes

I am trying to understand multiplication of two arrays which have different shapes. According to my understanding rows of d array should be equal to column of a array.
a = np.arange(3*2).reshape(3,2)
b= np.arange(3)
d = b[:,None]
c = a*d
Multiplication is a broad term in numpy. If you refer to element wise multiplication, use * if the arrays are equal shape or np.multiply in general, if you refer to matrix like multiplication, use np.matmul. Check the syntax and examples in the links.

Operations with arrays

I have to code a program using C++ at university.
They asked me to type all elements of a 3x3 matrix. And all elements have to be positive. So far so good. Each row of the matrix represents the components of a R^3 vector. So there are three vectors (Three 1D arrays). I had to calculate the modulus of each vector and successfully did it.
Here comes the problem... The next step is to get which vector the largest modulus has, and return its position. All this using a function --> int largestModulus (int Modulus[3]). How can I do that? Because it is all about comparing but this time is comparing a vector, and I only know how to compare different single elements. Thanks for your help!!
In the arguments to the function,
since you're passing and receiving a 2D matrix, the parameter must be
int largestModulus (int Modulus[][3])

How to converting a large 2D array to a sparse matrix using Eigen library?

I wish to convert a simple 2D array into a SparseMatrix, in order to improve performance and run time, since I am dealing with an array of a size around 50,000-70,000.
So far what I have:
SparseMatrix<double> sp;
sp.resize(numCells,numCells);
double Matrix[numCells,numCells];
Matrix = Map<SparseMatrix>(Matrix,numCells,numCells);
The compiler returns type mismatch value at argument 1 in template parameter list for 'template class Eigen::Map'.
I understand I am missing something here, but I can not figure it out.
Make a dense matrix and convert it into a sparse matrix:
double matrix[numCells * numCells]; // 1d array representation of your matrix
SparseMatrix<double> sp = Map<MatrixXd>(matrix,numCells,numCells).sparseView();

Return multiple vectors in OpenCL/C - Comparison of performance

I have a function f(x,y) that runs on a matrix with elements (x,y) and for each value produces two output vectors a = [a0,a1,a2,a3] and b = [b0,b1,b2,b3]. In a next step, there is a sum A over all a(x,y) and a sum B over all b(x,y). In a last step, A/B is calculated. I understand, that in OpenCL as well as in C there are 3 basic approaches to tackle this problem:
Returning a,b as one combined float8, do the summation with these vectors, explicitly formulate the division.
Return an array of 8 floats, do the summation and division on the array elements.
Return a struct holding two vectors, summation and division using vectors.
My question is: Aside from the three approaches using different data types, what's the difference regarding their memory usage and performance?

how to concatenate Vectors in Eigen?

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