Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r51673 - in trunk/libs/numeric/ublas/test: . common
From: guwi17_at_[hidden]
Date: 2009-03-09 18:32:05


Author: guwi17
Date: 2009-03-09 18:32:04 EDT (Mon, 09 Mar 2009)
New Revision: 51673
URL: http://svn.boost.org/trac/boost/changeset/51673

Log:
added unit test for LU decomposition

Added:
   trunk/libs/numeric/ublas/test/common/testhelper.hpp (contents, props changed)
   trunk/libs/numeric/ublas/test/test_lu.cpp (contents, props changed)
Text files modified:
   trunk/libs/numeric/ublas/test/CMakeLists.txt | 3 +++
   trunk/libs/numeric/ublas/test/Jamfile.v2 | 19 +++++++++++--------
   2 files changed, 14 insertions(+), 8 deletions(-)

Modified: trunk/libs/numeric/ublas/test/CMakeLists.txt
==============================================================================
--- trunk/libs/numeric/ublas/test/CMakeLists.txt (original)
+++ trunk/libs/numeric/ublas/test/CMakeLists.txt 2009-03-09 18:32:04 EDT (Mon, 09 Mar 2009)
@@ -57,6 +57,8 @@
 
 boost_test_run(placement_new)
 
+boost_test_run(test_lu)
+
 
 SET(test_compile_flags "-DEXTERNAL")
 #-- Intel Compiler flags
@@ -69,3 +71,4 @@
 ENDIF (APPLE)
 
 boost_test_compile(concepts COMPILE_FLAGS "-DEXTERNAL")
+

Modified: trunk/libs/numeric/ublas/test/Jamfile.v2
==============================================================================
--- trunk/libs/numeric/ublas/test/Jamfile.v2 (original)
+++ trunk/libs/numeric/ublas/test/Jamfile.v2 2009-03-09 18:32:04 EDT (Mon, 09 Mar 2009)
@@ -33,6 +33,7 @@
                         USE_MAP_ARRAY USE_STD_MAP
             USE_MAPPED_VECTOR USE_COMPRESSED_VECTOR USE_COORDINATE_VECTOR
             USE_MAPPED_MATRIX USE_COMPRESSED_MATRIX USE_COORDINATE_MATRIX
+ USE_GENERALIZED_VECTOR_OF_VECTOR
                         ;
 # Generalize VofV still failing
 # USE_GENERALIZED_VECTOR_OF_VECTOR
@@ -89,14 +90,14 @@
             <define>$(UBLAS_TESTSET)
       ]
 # Test commented out, just like in V1 Jamfile
-# [ run test7.cpp
-# test71.cpp
-# test72.cpp
-# test73.cpp
-# : : :
-# <define>BOOST_UBLAS_USE_INTERVAL
-# <define>$(UBLAS_TESTSET)
-# ]
+ [ run test7.cpp
+ test71.cpp
+ test72.cpp
+ test73.cpp
+ : : :
+ <define>BOOST_UBLAS_USE_INTERVAL
+ <define>$(UBLAS_TESTSET)
+ ]
 
       [ run placement_new.cpp
       ]
@@ -106,4 +107,6 @@
             <toolset>intel-linux:<cxxflags>"-Xc"
                         <toolset>darwin:<cxxflags>"-fabi-version=0"
       ]
+ [ run test_lu.cpp
+ ]
     ;

Added: trunk/libs/numeric/ublas/test/common/testhelper.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/numeric/ublas/test/common/testhelper.hpp 2009-03-09 18:32:04 EDT (Mon, 09 Mar 2009)
@@ -0,0 +1,57 @@
+// Copyright 2008 Gunter Winkler <guwi17_at_[hidden]>
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+
+#ifndef _HPP_TESTHELPER_
+#define _HPP_TESTHELPER_
+
+#include <utility>
+
+static unsigned _success_counter = 0;
+static unsigned _fail_counter = 0;
+
+static inline
+void assertTrue(const char* message, bool condition) {
+#ifndef NOMESSAGES
+ std::cout << message;
+#endif
+ if ( condition ) {
+ ++ _success_counter;
+ std::cout << "1\n"; // success
+ } else {
+ ++ _fail_counter;
+ std::cout << "0\n"; // failed
+ }
+}
+
+static
+std::pair<unsigned, unsigned> getResults() {
+ return std::make_pair(_success_counter, _fail_counter);
+}
+
+template < class M1, class M2 >
+bool compare( const boost::numeric::ublas::matrix_expression<M1> & m1,
+ const boost::numeric::ublas::matrix_expression<M2> & m2 ) {
+ size_t size1 = (std::min)(m1().size1(), m2().size1());
+ size_t size2 = (std::min)(m1().size2(), m2().size2());
+ for (size_t i=0; i < size1; ++i) {
+ for (size_t j=0; j < size2; ++j) {
+ if ( m1()(i,j) != m2()(i,j) ) return false;
+ }
+ }
+ return true;
+}
+
+template < class M1, class M2 >
+bool compare( const boost::numeric::ublas::vector_expression<M1> & m1,
+ const boost::numeric::ublas::vector_expression<M2> & m2 ) {
+ size_t size = (std::min)(m1().size(), m2().size());
+ for (size_t i=0; i < size; ++i) {
+ if ( m1()(i) != m2()(i) ) return false;
+ }
+ return true;
+}
+
+#endif

Added: trunk/libs/numeric/ublas/test/test_lu.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/numeric/ublas/test/test_lu.cpp 2009-03-09 18:32:04 EDT (Mon, 09 Mar 2009)
@@ -0,0 +1,65 @@
+// switch automatic singular check off
+#define BOOST_UBLAS_TYPE_CHECK 0
+
+#include <boost/numeric/ublas/io.hpp>
+#include <boost/numeric/ublas/lu.hpp>
+#include <boost/cstdlib.hpp>
+
+#include "common/testhelper.hpp"
+
+#include <iostream>
+#include <sstream>
+
+using namespace boost::numeric::ublas;
+using std::string;
+
+static const string matrix_IN = "[3,3]((1,2,2),(2,3,3),(3,4,6))\0";
+static const string matrix_LU = "[3,3]((3,4,6),(3.33333343e-01,6.66666627e-01,0),(6.66666687e-01,4.99999911e-01,-1))\0";
+static const string matrix_INV= "[3,3]((-3,2,-7.94728621e-08),(1.50000012,0,-5.00000060e-01),(4.99999911e-01,-1,5.00000060e-01))\0";
+static const string matrix_PM = "[3](2,2,2)";
+
+int main () {
+
+ typedef float TYPE;
+
+ typedef matrix<TYPE> MATRIX;
+
+ MATRIX A;
+ MATRIX LU;
+ MATRIX INV;
+
+ {
+ std::istringstream is(matrix_IN);
+ is >> A;
+ }
+ {
+ std::istringstream is(matrix_LU);
+ is >> LU;
+ }
+ {
+ std::istringstream is(matrix_INV);
+ is >> INV;
+ }
+ permutation_matrix<>::vector_type temp;
+ {
+ std::istringstream is(matrix_PM);
+ is >> temp;
+ }
+ permutation_matrix<> PM(temp);
+
+ permutation_matrix<> pm(3);
+
+ int result = lu_factorize<MATRIX, permutation_matrix<> >(A, pm);
+
+ assertTrue("factorization completed: ", 0 == result);
+ assertTrue("LU factors are correct: ", compare(A, LU));
+ assertTrue("permutation is correct: ", compare(pm, PM));
+
+ MATRIX B = identity_matrix<TYPE>(A.size2());
+
+ lu_substitute(A, pm, B);
+
+ assertTrue("inverse is correct: ", compare(B, INV));
+
+ return (getResults().second > 0) ? boost::exit_failure : boost::exit_success;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk