问题
having a Eigen::MatrixBase & data, are there any way to get if it is a float or double matrix? I need to create an new complex matrix of the same size and type as the MatrixBase.
If it is MatrixXf then i need to create MatrixXcf, and if MatrixXd i need MatrixXcD.?
template <typename A>
int dowork(const Eigen::MatrixBase<A>& data)
回答1:
That's pretty simple, just use the A::RealScalar typedef to build your complex type:
template <typename A>
int dowork(const Eigen::MatrixBase<A>& data) {
typedef Matrix<std::complex<typename A::RealScalar, Dynamic, Dynamic> MatCplx;
...
回答2:
Im not 100 percent sure I understand the question being asked, but I think you asking to having another matrix allocated of the same time type after some condition is met? Do you want this new matrix to not allow data types that don't match the type?
If not, because you are using template classes you have a lot of freedom and can just use general template data types. Also look into representing the matrix using vector formats for ease of use. Maybe even sparse format stuff like so
// Local variables used.
int a = 0;
int b = 0;
int endN, endM;
// Iterates through matrix checking when last matrix value is reached for end point.
while (endN != sizeN && endM != sizeM) {
if (a == sizeN) {
b++;
a = 0;
endM = b;
} else {
if (Matrix[a][b] != 0) {
// Stores non-zero matrix values in queue SpareseFormat.
SparseFormat.push(a);
SparseFormat.push(b);
SparseFormat.push(Matrix[a][b]);
}
endN = a;
a++;
}
}
Sorry if I totally didn't understand your question. It has been a long night :P
来源:https://stackoverflow.com/questions/15148978/possible-to-get-type-of-eigenmatrixbaset