# static if on types like double, float

sclytrack at pi.be sclytrack at pi.be
Wed Jun 14 06:15:53 PDT 2006

```I have the following class, called SingularValueDecomposition, it should only
accept T of type float and double. Currently, it uses dgesvd for double, but for
float it must call sgesvd.

1. How can I do a static if on a type? (Thanks in advance.)

class SingularValueDecomposition(T)
{
protected:
MatrixRectangular!(T, true) _matrix;

MatrixRectangular!(T, true) _uMatrix;
MatrixDiagonal!(T, true) _sMatrix;
MatrixRectangular!(T, true) _vTMatrix;
public:
this( MatrixRectangular!(T,true) matrix)
{
_matrix = matrix;
//		MxN=(MxM) (MxN) (NxN)
_uMatrix = new MatrixRectangular!(T, true) (_matrix.rowCount, _matrix.rowCount);
_sMatrix = new MatrixDiagonal!(T, true) (_matrix.rowCount, _matrix.columnCount);
_vTMatrix = new MatrixRectangular!(T, true) (_matrix.columnCount,
_matrix.columnCount);
}

MatrixRectangular!(T, true) uMatrix()
{
return _uMatrix;
}

MatrixDiagonal!(T, true) sMatrix()
{
return _sMatrix;
}

MatrixRectangular!(T, true) vTMatrix()
{
return _vTMatrix;
}

MatrixRectangular!(T, true) recompose()
{
return _uMatrix * _sMatrix * _vTMatrix;
}

void decompose()		//decompose, recompose
{

T bestLWork;

char jobu='A';
char jobvt='A';
int m = _matrix.rowCount;
int n = _matrix.columnCount;
T * a = &_matrix.data[0];
int lda = _matrix.columnCount;
T * s= &_sMatrix.data[0];		//min(m,n)
T * u = &_uMatrix.data[0];		//LDU, M for 'A'
int ldu = _uMatrix.rowCount;		//leading dimensions of u
T * vt = &_vTMatrix.data[0];		//N x N for 'A'
int ldvt = _vTMatrix.rowCount;		//N for 'A'
//		float * work;		//
int lwork = -1; //dimensions of array work	//Two Calls Find Optimal LWORK
int info;  //0 success, <0 -i argument had illegal value

//sgesvd_(&jobu, &jobvt, &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, &bestLWork,
&lwork, &info);
dgesvd_(&jobu, &jobvt, &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, &bestLWork,
&lwork, &info);

writefln( "Info:      "~std.string.toString(info));
writefln( "bestLWork: "~std.string.toString(bestLWork));

if (info == 0)
{
lwork = cast(int) bestLWork;	//float to int
T [] workSpace = new T[lwork];
T * work = &workSpace[0];		//manner, don't have to pin them with fixed (C#)

//sgesvd_(&jobu, &jobvt, &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork,
&info);
dgesvd_(&jobu, &jobvt, &m, &n, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork,
&info);

writefln( "Info:      "~std.string.toString(info));
writefln( "bestLWork: "~std.string.toString(bestLWork));
}
}
}

```