Linear Algebra Module User’s Guide : Chapter 3 Vectors and Matrices : Type Conversion
Type Conversion
Two kinds of type conversion are possible between Rogue Wave matrix classes:
Conversion between matrices with the same shape but different precisions; for example, from a RWBandMat<float> to a RWBandMat<double>.
Conversion between matrices with the same precision but different shapes; for example, from a RWSymMat<DComplex> to an RWGenMat<DComplex>.
We discuss these two kinds of conversion in the following sections.
Converting Precisions
Converting between matrices of the same shape but different precisions is much the same as converting between scalar types. Both follow similar rules in C++. The following program gives an example of type conversions that are possible using tridiagonal matrices:
 
#include <rw/lapack/trdgmat.h>
 
int main()
{
RWTriDiagMat<float> Afloat(5,5); // 1
RWTriDiagMat<double> Adouble = Afloat; // 2
RWTriDiagMat<DComplex> Acomplex = Adouble; // 3
 
return 0;
}
//1 This line creates a 5 x 5 tridiagonal matrix of floats.
//2 This line demonstrates that this matrix can be converted automatically to a matrix of doubles.
//3 This line shows that this matrix can be converted to a matrix of complex.
In each of the conversions above, no information is lost. However, conversions in the opposite direction are narrowing and may cause a loss of precision. These conversions are possible, but you must explicitly use a function provided for this purpose. For example:
 
#include <rw/lapack/trdgmat.h>
 
int main()
{
RWTriDiagMat<DComplex> Acomplex(5,5); // 1
RWTriDiagMat<double> Adouble = real(Acomplex); // 2
RWTriDiagMat<float> Afloat= toFloat(Adouble); // 3
 
return 0;
}
//1 This line defines a 5 x 5 complex tridiagonal matrix.
//2 Takes the real part of the complex matrix and stores it as a matrix of doubles.
//3 Converts a matrix of doubles to a matrix of floats.
Type conversions between RWGenMat<T> matrices is different due to their template definition. Automatic conversions are not generally available. For a complete discussion about type conversions for general matrices, see the Essential Math Module User’s Guide.
Converting Shapes
In addition to converting from one matrix precision to another, you can convert between matrix shapes. Automatic type conversion can be used to convert any matrix shape to a general matrix of the same precision. Furthermore, symmetric banded, hermitian banded, and tridiagonal matrices can be converted automatically to banded matrices.
You can convert matrices in the opposite direction, as you did precisions, by using explicit functions. For example, the function toSymMat() extracts the symmetric part of a matrix. This function is often used in situations where you have a general matrix that you know is symmetric and you want to recognize this explicitly.