Thread overview
static if on types like double, float
Jun 14, 2006
sclytrack
Jun 14, 2006
Oskar Linde
Jun 14, 2006
Daniel Keep
Jun 15, 2006
Daniel Keep
Jun 15, 2006
Don Clugston
June 14, 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));
}
}
}


June 14, 2006
sclytrack@pi.be wrote:

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

See the is-expression:

http://www.digitalmars.com/d/expression.html#IsExpression

example:

static if (is (T == double)) {
	something();
} else {
	something_else();
}

/Oskar
June 14, 2006

sclytrack@pi.be wrote:
> 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.)
> 
> [snip]

# static if( is( T == float ) )
# 	/* stuff */
# else static if( is( T == double ) )
# 	/* more stuff */
# else
# {
# 	pragma(msg, "It's floats, doubles or nothing!");
# 	static assert(false);
# }

You might be able to use ``static assert(false, "...");`` to show a message, maybe not.  I know you can pass the message to the non-static version, but I can't remember if the static one supports that.

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/
June 14, 2006
"Daniel Keep" <daniel.keep.lists@gmail.com> wrote in message news:e6p2l6$r1h$1@digitaldaemon.com...

> You might be able to use ``static assert(false, "...");`` to show a message, maybe not.  I know you can pass the message to the non-static version, but I can't remember if the static one supports that.

Indeed you can!


June 15, 2006

Jarrett Billingsley wrote:
> "Daniel Keep" <daniel.keep.lists@gmail.com> wrote in message news:e6p2l6$r1h$1@digitaldaemon.com...
> 
>> You might be able to use ``static assert(false, "...");`` to show a message, maybe not.  I know you can pass the message to the non-static version, but I can't remember if the static one supports that.
> 
> Indeed you can!

Oh good.  When Walter does one of these unexpected feature adds, it's hard to remember what he's actually implemented, and what people have been saying should be implemented.

Kinda like the old "was I dreaming that, or did it really happen?" problem :)

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/
June 15, 2006
Jarrett Billingsley wrote:
> "Daniel Keep" <daniel.keep.lists@gmail.com> wrote in message news:e6p2l6$r1h$1@digitaldaemon.com...
> 
>> You might be able to use ``static assert(false, "...");`` to show a
>> message, maybe not.  I know you can pass the message to the non-static
>> version, but I can't remember if the static one supports that.
> 
> Indeed you can! 

I've been trying to work out what is allowed in the message. You can concatenate char [] literals, and use .mangleof, but AFAICT, templates inside the message are not instantiated. (I'm trying to work out if it's possible to dump a template 'call stack' in the message).