|
Boost : |
From: David Cameron (dave.bc_at_[hidden])
Date: 2005-11-02 03:04:04
I've been writing some code to serialize and deserialize
boost::numeric::ublas::matrixs. While writing unit tests for it I
found out about the boost::numeric::ublas::equals function in
matrix_assign.hpp. I was relieved, because I had previously been using
a templated function to compare matrices.
Unfortunately, it doesn't work for me with matrix<int>s. Consider the
following cppunit test code:
void testMatrixEqualsOperator() {
using namespace boost::numeric::ublas;
matrix<double> test;
test.resize(3,3);
CPPUNIT_ASSERT( equals(test,test) );
matrix<int> test1;
test.resize(4,4);
CPPUNIT_ASSERT( equals(test1,test1) );
}
The first four lines work. Adding the next three lines causes the unit
tests to fail with an Abort. This is not the standard way CppUnit
records a failed test, it must be something failing inside the call to
equals().
Previous messages referring to the equals() function argued that it
wasn't ready for use because there was rarely a need to compare
matrices in real applications. I would argue that it should be working
as intended, if only to allow for proper unit testing of code that
uses matrices.
Here is the function I have been using instead. It works well for me
with doubles and ints. Although, I imagine it is not particularly
fast. E is for Element.
template<class E>
bool areMatricesEqual(
boost::numeric::ublas::matrix<E> lhs,
boost::numeric::ublas::matrix<E> rhs)
{
using boost::numeric::ublas;
if (&lhs == &rhs)
return true;
if (lhs.size1() != rhs.size1())
return false;
if (lhs.size2() != rhs.size2())
return false;
typename matrix<E>::iterator1 l(lhs.begin1());
typename matrix<E>::iterator1 r(rhs.begin1());
while (l != lhs.end1()) {
if (*l != *r)
return false;
++l;
++r;
}
return true;
}
I am using gcc-3.3 on ubuntu-5.04 running on an AthlonXP. I'd be happy
to provide any more info to help track down the bug. Assuming it is
one.
-- David Cameron University of British Columbia http://bar.psych.ubc.ca/People/Dave.html
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk