I have a homework exercise. I'm almost sure it's unsolvable the way they ask it. However, I'm interested if you guys have any solution for the problem mentioned below because it seems like something that often occurs.
The description is not long, so I share it with you below:
A matrix S ∈ R n×n is skewsymmetric if it holds that S(Transpose) = −S. Derive from the class SquareMatrix from the lecture the class SkewSymmetricMatrix. Use a vector of length n(n − 1)/2 to store the matrix entries. Implement constructors, type casting and a suitable access to the coefficients.
The problem occurs while trying to provide the access, because the virtual access method defined in SquareMatrix returns a reference.
const double& SquareMatrix::operator()( int j, int k ) const
{
assert( j >= 0 && j < m );
assert( k >= 0 && k < n );
return coeff[ j + k * m ];
}
However, I can't return reference to the not stored variables. The following code is just for demonstrating my problem. In this case the j > k block would obviously not work.
const double& SkewSymmetricMatrix::operator()( int j, int k ) const
{
assert( j >= 0 && j < size() );
assert( k >= 0 && k < size() );
if( j < k )
{
const double* coeff = getCoeff();
return coeff[ j * ( j - 1 ) / 2 + k ];
}
else if ( j > k )
{
const double* coeff = getCoeff();
return -coeff[ k * ( k - 1 ) / 2 + j ];
}
else
{
return const_zero;
}
}
Do you have any suggestions that how to provide a proper access operator while trying to reduce the use of memory by
- storing less elements
- and calculating the non-stored desired elements from the actually stored ones?