A
Andrew Morgan
Hi
I have a matrix class:
template<class Type> class TMatrix
{
//...
TMatrix& T();
//...
}
The transposition function above does not execute in place, ie it
returns a new object. This allows expressions like:
TMatrix<single> A,L,D;
L = ...
D = ...
A = L.T()*D*L;
L.T() is not L and therefore it makes sense to return a new object. If
L.T() were executed in-place, the final product D*L would in fact be
D*L.T() which is incorrect.
However, there are situations where the transpose is required in an
assignment, and
L = L.T();
seems clumsy and inefficient with all the constructors and destructors
getting in on the action (unless good return by value optimizations are
being generated).
Should I do something like:
TMatrix& T(bool inplace = false);
which does not break:
A = L.T()*D*L;
but allows:
L.T(true);
and even:
A = L.T(true)*D*L.T();
as a pathological example. The difference between this expression and
the first version being that L is ends in a transposed state (first
transpose in-place, the second not).
The code may be something like:
template<class Type> TMatrix<Type>& TMatrix<Type>::T(bool inplace)
{
if(inplace)
{
// Transpose in-place.
//...
return *this;
}
/* Create temporary and transpose in-place. Admittedly, a bit lazy
since the transpose code could be re-done below, but on the other hand
one does not want two code fragments doing the same job. */
return TMatrix<Type>(this).T(true);
}
Since a temporary has to be created, maybe
L = L.T();
is not so inefficient after all?
Any comments or suggestions?
Thanks
Andrew
I have a matrix class:
template<class Type> class TMatrix
{
//...
TMatrix& T();
//...
}
The transposition function above does not execute in place, ie it
returns a new object. This allows expressions like:
TMatrix<single> A,L,D;
L = ...
D = ...
A = L.T()*D*L;
L.T() is not L and therefore it makes sense to return a new object. If
L.T() were executed in-place, the final product D*L would in fact be
D*L.T() which is incorrect.
However, there are situations where the transpose is required in an
assignment, and
L = L.T();
seems clumsy and inefficient with all the constructors and destructors
getting in on the action (unless good return by value optimizations are
being generated).
Should I do something like:
TMatrix& T(bool inplace = false);
which does not break:
A = L.T()*D*L;
but allows:
L.T(true);
and even:
A = L.T(true)*D*L.T();
as a pathological example. The difference between this expression and
the first version being that L is ends in a transposed state (first
transpose in-place, the second not).
The code may be something like:
template<class Type> TMatrix<Type>& TMatrix<Type>::T(bool inplace)
{
if(inplace)
{
// Transpose in-place.
//...
return *this;
}
/* Create temporary and transpose in-place. Admittedly, a bit lazy
since the transpose code could be re-done below, but on the other hand
one does not want two code fragments doing the same job. */
return TMatrix<Type>(this).T(true);
}
Since a temporary has to be created, maybe
L = L.T();
is not so inefficient after all?
Any comments or suggestions?
Thanks
Andrew