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.
Related
Consider the following code:
#include <Eigen/Core>
using Matrix = Eigen::Matrix<float, 2, 2>;
Matrix func1(const Matrix& mat) { return mat + 0.5; }
Matrix func2(const Matrix& mat) { return mat / 0.5; }
func1() does not compile; you need to replace mat with mat.array() in the function body to fix it ([1]). However, func2() does compile as-is.
My question has to do with why the API is designed this way. Why is addition-with-scalar and division-by-scalar treated differently? What problems would arise if the following method is added to the Matrix class, and why haven't those problems arisen already for the operator/ method?:
auto operator+(Scalar s) const { return this->array() + s; }
From a mathematics perspective, a scalar added to a matrix "should" be the same as adding the scalar only to the diagonal. That is, a math text would usually use M + 0.5 to mean M + 0.5I, for I the identity matrix. There are many ways to justify this. For example, you can appeal to the analogy I = 1, or you can appeal to the desire to say Mx + 0.5x = (M + 0.5)x whenever x is a vector, etc.
Alternatively, you could take M + 0.5 to add 0.5 to every element. This is what you think is right if you don't think of matrices from a "linear algebra mindset" and treat them as just collections (arrays) of numbers, where it is natural to just "broadcast" scalar operations.
Since there are multiple "obvious" ways to handle + between a scalar and a matrix, where someone expecting one may be blindsided by the other, it is a good idea to do as Eigen does and ban such expressions. You are then forced to signify what you want in a less ambiguous way.
The natural definition of / from an algebra perspective coincides with the array perspective, so no reason to ban it.
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 been trying to reconstruct the input data fed to my RBM program written in C++ with the aid of Eigen library. But in order to keep the matrix elements of the reconstructed matrix into some specific range, i need to apply a sigmoid function to those.
When i do so i get a conversion error and i don't know the way round it.
Here is my Sigmoid function computed in an header file:
double sigmoid(double x)
{
return 1.0 / (1.0 + exp(-x));
}
And here is how i compute the reconstruction:
MatrixXd V;
double well[36];
Map<MatrixXd>( well, V.rows(), V.cols() ) = V;
V = sigmoid(H * result3Eigen.transpose() + onesmat*result2Eigen.transpose());
At last here the error message i get when compiling the code:
error C2664:'utils::sigmoid':cannot convert parameter 1 from 'Eigen::MatrixXd'
to 'double'
Thank you for any hints in solving the issue.
If you want to apply a function to each element of an Eigen matrix, you can use the unaryExpr function:
V = my_matrix.unaryExpr(&sigmoid);
This will run the sigmoid function on each element of the Eigen matrix my_matrix, and then return another matrix as the result.
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
I am attempting to implement a complex-valued matrix equation in OpenCV. I've prototyped in MATLAB which works fine. Starting off with the equation (exact MATLAB implementation):
kernel = exp(1i .* k .* Circ3D) .* z ./ (1i .* lambda .* Circ3D .* Circ3D)
In which
1i = complex number
k = constant (float)
Circ3D = real-valued matrix of known size
lambda = constant (float)
.* = element-wise multiplication
./ = element-wise division
The result is a complex-valued matrix. I succeeded in generating the necessary Circ3D matrix as a CV_32F, however the multiplication by the complex number i is giving me trouble. From the OpenCV documentation I understand that a complex matrix is simply a two-channel matrix (CV_32FC2).
The real trouble comes from how to define i. I've tried several options, among which defining i as
cv::Vec2d complex = cv::Vec2d(0,1);
and then multiplying by the matrix
kernel = complex * Circ3D
But this doesn't work (although I didn't expect it to). I suspect I need to do something with std::complex but I have no idea what (http://docs.opencv.org/modules/core/doc/basic_structures.html).
Thanks in advance for any help.
Edit: Just after writing this post I did make some progress, by defining i as follows:
std::complex<float> complex(0,1)
I am then able to assign complex values as follows:
kernel.at<std::complex<float>>(i,j) = cv::exp(complex * k * Circ3D.at<float>(i,j)) * ...
z / (complex * lambda * pow(Circ3D.at<float>(i,j),2));
However, this works in a loop, which makes the procedure incredibly slow. Any way to do it in one go?
OpenCV treats std::complex just like the simple pair of numbers (see example in the documentation). No special rules on arithmetic operations are applied. You overcome this by multiplying std::complex directly. So basically, this is simple: you either chose automatic complex arithmetic (as you are doing now), or automatic vectorization (when using OpenCV functions on matrices).
I think, in your case you should carry all the complex arithmetic by yourself. Store matrix of complex values C{ai + b} as two matrices A{a} and B{b}. Implement exponentiation by yourself. Multiplication on scalars and addition shouldn't be a problem.
There is also the function mulSpectrums, which lets you do element wise multiplication of complex matrices. So if K is your kernel matrix and I is some complex matrix, that is, CV_32FC2 (float two channel) you can do the following to compute the element wise multiplication,
// Make K a complex matrix
cv::Mat Ktmp[] = {cv::Mat_<float>(K), cv::Mat::zeros(K.size(), CV_32FC1)};
cv::Mat Kc;
cv::merge(Ktmp,2,Kc);
// Do matrix multiplication
cv::mulSpectrums(Kc,I,I,0);