multiplication of 5 different size matrix in c ++ - c++

I want to multiply 5 matrix ( all are not of same size) in c++, so what i can do?
will i have to use loop 5 times or is there any simple method like as in matlab?
sizes of matrix are
1st : 1x4
2nd : 4x4
3rd : 4x4
4th : 4x4
5th : 4x1

You could use boost linear algebra library which defines both matrix and vector types and has multiplication with operator *.
matrix<int> m1(1,4);
matrix<int> m2(4,4);
matrix<int> m3(4,4);
matrix<int> m4(4,1);
//... initialize your matrices here
matrix<int> result = m1*m2*m3*m4;

Write a function that performs the matrix multiplication for matrices of arbitrary size (with a sanity check).
Use the function four times, once for each multiplication.
Alternatively, you can define the * operator for the type matrix that you defined yourself, so that you could just write a*b*c*d

Related

Performing Matrix Vector Product using Eigen library in C++

How do I perform a matrix vector product using the Eigen library in C++ with a "for loop", when the date type of my variables are in the form Eigen::MatrixXd and Eigen::VectorXd?
NOTE: I don't want to use the in-built * operator of directly multiplying a matrix with vector, I want to use a for loop and perform the element wise operation myself
When I try to do the element wise multiplication on Eigen::MatrixXd and Eigen::VectorXd, I get an error.
I have no idea what problem you have encountered. Here is an example that may help.
// mat: 3x4 vec_h: 4x1
Eigen::MatrixXd mat = Eigen::MatrixXd::Ones(3,4);
Eigen::VectorXd vec_h = Eigen::VectorXd::Random(4);
// mat.row(i): 1x4
// mat.row(i).transpose(): 4x1
// cwiseProduct() element-wise multiplication
// add .eval() because we are changing the element value in place.
for (int i = 0; i < mat.rows(); ++i) {
mat.row(i)=mat.row(i).transpose().cwiseProduct(vec_h).eval();
}
In case you are looking for more information, here's the link Eigen: The Array class and coefficient-wise operations

Multiply two Eigen vectors by corresponding elements

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

Eigen C++ matrix multiplication between MatrixXd and VectorXd

So below is my code and as you can see I am trying to multiply a square matrix MatrixXd with vectorXd.
When I try this, I get the following error:
"invalid operands to binary expression ('const typename ProductReturnType > >, Matrix >::Type' (aka 'const GeneralProduct
I do not know what is going wrong. I am sorry if this is a bad question, but please help!
void calcMinPortfolio(int num_ofStocks, Eigen::MatrixXd& covMatrix, Eigen::VectorXd& weights){
Eigen::MatrixXd identityMat;
identityMat.resize(num_ofStocks, num_ofStocks);
identityMat.Identity();
weights = (covMatrix.inverse() * identityMat) / (identityMat.transpose() * covMatrix.inverse() * identityMat);
}
There are 2 problems:
1 - The way you construct the identity, you cannot use .Identity() on a dynamic matrix (only on compile-size specified size). Simply use the one-liner
Eigen::MatrixXd identityMat = Eigen::MatrixXd::Identity(num_ofStocks, num_ofStocks);
2 - The denominator is a general matrix product (a type), so you cannot divide a matrix by it. What do you mean by division of a matrix by another one? Use .inverse() for the denominator if this is what you want. Next, multiplying by identities doesn't make any sense unless covMatrix is a scalar, which is not.
PS: even if the denominator is a a size 1 x 1 matrix, it is still a matrix type, and not a scalar, so you cannot divide a matrix by it. If you want to divide it by the scalar represented by the matrix, then use your_expresion(0) to extract the scalar from the 1 x 1 matrix.

Linear least square equation solving using c++ eigen library (dynamic allocation)

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

Boost Matrix Dynamic Size Expansion

I am writing a c++ program which uses boost library for matrix operations. I have a need were by i have to dynamically expand the size of the initial matrix.
Example:
if my matrix size was:
matrix<float> m(3,3);
and later my matrix will expand and i'll need a 4*4 matrix. The naive approach i could think of is allocate a new matrix with size 4,4 and and copy all elements of 3*3 matrix to it. Isn't there any better way of doing this in boost?
Please, consider using the resize() function: "The existing elements of the matrix are preseved (sic) when specified."
Here is an example code from Boost.
This is one of ways.
matrix<int> A; // Matrix size would be zero by zero
A.resize(2, 3); // Matrix size became 2 by 3
Why not just create a matrix using the no arg constructor and call the resize method as needed?
http://www.boost.org/doc/libs/1_47_0/libs/numeric/ublas/doc/matrix.htm