|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r75560 - trunk/libs/numeric/ublas/test
From: david.bellot_at_[hidden]
Date: 2011-11-19 18:27:00
Author: david.bellot
Date: 2011-11-19 18:26:59 EST (Sat, 19 Nov 2011)
New Revision: 75560
URL: http://svn.boost.org/trac/boost/changeset/75560
Log:
added new tests
Added:
trunk/libs/numeric/ublas/test/test_inplace_solve.cpp (contents, props changed)
trunk/libs/numeric/ublas/test/test_triangular.cpp (contents, props changed)
Text files modified:
trunk/libs/numeric/ublas/test/Jamfile.v2 | 10 +++++++---
trunk/libs/numeric/ublas/test/README | 2 +-
2 files changed, 8 insertions(+), 4 deletions(-)
Modified: trunk/libs/numeric/ublas/test/Jamfile.v2
==============================================================================
--- trunk/libs/numeric/ublas/test/Jamfile.v2 (original)
+++ trunk/libs/numeric/ublas/test/Jamfile.v2 2011-11-19 18:26:59 EST (Sat, 19 Nov 2011)
@@ -1,4 +1,4 @@
-# Copyright (c) 2004 Michael Stevens
+# Copyright (c) 2004-2011 Michael Stevens, David Bellot
# Use, modification and distribution are subject to 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)
@@ -132,6 +132,10 @@
]
[ run test_complex_norms.cpp
]
- [ run test_assignment.cpp
- ]
+ [ run test_assignment.cpp
+ ]
+ [ run test_triangular.cpp
+ ]
+ [ run test_inplace_solve.cpp
+ ]
;
Modified: trunk/libs/numeric/ublas/test/README
==============================================================================
--- trunk/libs/numeric/ublas/test/README (original)
+++ trunk/libs/numeric/ublas/test/README 2011-11-19 18:26:59 EST (Sat, 19 Nov 2011)
@@ -1,4 +1,4 @@
-Copyright (c) 2000-2004 Joerg Walter, Mathias Koch
+Copyright (c) 2000-2011 Joerg Walter, Mathias Koch, David Bellot
Distributed under the Boost Software License, Version 1.0. (See
accompanying file LICENSE_1_0.txt or copy at
Added: trunk/libs/numeric/ublas/test/test_inplace_solve.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/numeric/ublas/test/test_inplace_solve.cpp 2011-11-19 18:26:59 EST (Sat, 19 Nov 2011)
@@ -0,0 +1,115 @@
+#include <iostream>
+
+#include <boost/numeric/ublas/vector.hpp>
+#include <boost/numeric/ublas/matrix.hpp>
+#include <boost/numeric/ublas/matrix_sparse.hpp>
+#include <boost/numeric/ublas/triangular.hpp.new>
+#include <boost/numeric/ublas/io.hpp>
+
+
+#include <sys/times.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+
+#include "utils.hpp"
+
+namespace ublas = boost::numeric::ublas;
+
+static const double TOL(1.0e-5); ///< Used for comparing two real numbers.
+static const int n(10); ///< defines the test matrix size
+
+template<class mat, class vec>
+double diff(const mat& A, const vec& x, const vec& b) {
+ return ublas::norm_2(prod(A, x) - b);
+}
+
+// efficiently fill matrix depending on majority
+template<class mat>
+void fill_matrix(mat& A, ublas::column_major_tag) {
+ for (int i=0; i<n; ++i) {
+ if (i-1>=0) {
+ A(i-1, i) = -1;
+ }
+ A(i, i) = 1;
+ if (i+1<n) {
+ A(i+1, i) = -2;
+ }
+ }
+}
+template<class mat>
+void fill_matrix(mat& A, ublas::row_major_tag) {
+ for (int i=0; i<n; ++i) {
+ if (i-1>=0) {
+ A(i, i-1) = -1;
+ }
+ A(i, i) = 1;
+ if (i+1<n) {
+ A(i, i+1) = -2;
+ }
+ }
+}
+
+template<class mat>
+BOOST_UBLAS_TEST_DEF ( test_inplace_solve )
+{
+ mat A(n, n);
+ A.clear();
+ fill_matrix(A, typename mat::orientation_category());
+
+ ublas::vector<double> b(n, 1.0);
+
+ // The test matrix is not triangular, but is interpreted that way by
+ // inplace_solve using the lower_tag/upper_tags. For checking, the
+ // triangular_adaptor makes A triangular for comparison.
+ {
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(A, x, ublas::lower_tag());
+ BOOST_UBLAS_TEST_CHECK(diff(ublas::triangular_adaptor<mat, ublas::lower>(A), x, b) < TOL);
+ }
+ {
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(A, x, ublas::upper_tag());
+ BOOST_UBLAS_TEST_CHECK(diff (ublas::triangular_adaptor<mat, ublas::upper>(A), x, b) < TOL);
+ }
+ {
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(x, A, ublas::lower_tag());
+ BOOST_UBLAS_TEST_CHECK(diff (trans(ublas::triangular_adaptor<mat, ublas::lower>(A)), x, b) < TOL);
+ }
+ {
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(x, A, ublas::upper_tag());
+ BOOST_UBLAS_TEST_CHECK(diff (trans(ublas::triangular_adaptor<mat, ublas::upper>(A)), x , b) < TOL);
+ }
+}
+
+int main() {
+ const int n=10;
+
+ // typedefs are needed as macros do not work with "," in template arguments
+ typedef ublas::compressed_matrix<double, ublas::row_major> commat_doub_rowmaj;
+ typedef ublas::compressed_matrix<double, ublas::column_major> commat_doub_colmaj;
+ typedef ublas::matrix<double, ublas::row_major> mat_doub_rowmaj;
+ typedef ublas::matrix<double, ublas::column_major> mat_doub_colmaj;
+ typedef ublas::mapped_matrix<double, ublas::row_major> mapmat_doub_rowmaj;
+ typedef ublas::mapped_matrix<double, ublas::column_major> mapmat_doub_colmaj;
+ typedef ublas::coordinate_matrix<double, ublas::row_major> cormat_doub_rowmaj;
+ typedef ublas::coordinate_matrix<double, ublas::column_major> cormat_doub_colmaj;
+ typedef ublas::mapped_vector_of_mapped_vector<double, ublas::row_major> mvmv_doub_rowmaj;
+ typedef ublas::mapped_vector_of_mapped_vector<double, ublas::column_major> mvmv_doub_colmaj;
+
+ BOOST_UBLAS_TEST_BEGIN();
+
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<commat_doub_rowmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<commat_doub_colmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<mat_doub_rowmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<mat_doub_colmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<mapmat_doub_rowmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<mapmat_doub_colmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<cormat_doub_rowmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<cormat_doub_colmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<mvmv_doub_rowmaj> );
+ BOOST_UBLAS_TEST_DO( test_inplace_solve<mvmv_doub_colmaj> );
+
+ BOOST_UBLAS_TEST_END();
+}
Added: trunk/libs/numeric/ublas/test/test_triangular.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/numeric/ublas/test/test_triangular.cpp 2011-11-19 18:26:59 EST (Sat, 19 Nov 2011)
@@ -0,0 +1,139 @@
+#include <iostream>
+
+#include <boost/numeric/ublas/vector.hpp>
+#include <boost/numeric/ublas/matrix.hpp>
+#include <boost/numeric/ublas/matrix_sparse.hpp>
+#include <boost/numeric/ublas/triangular.hpp>
+#include <boost/numeric/ublas/io.hpp>
+
+
+#include <sys/times.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+
+
+namespace ublas = boost::numeric::ublas;
+
+template<class mat, class vec>
+double diff(const mat& A, const vec& x, const vec& b) {
+ vec temp(prod(A, x) - b);
+ double result = 0;
+ for (typename vec::size_type i=0; i<temp.size(); ++i) {
+ result += temp(i)*temp(i);
+ }
+ return sqrt(result);
+}
+
+template<class mat, class vec>
+double diff(const vec& x, const mat& A, const vec& b) {
+ return diff(trans(A), x, b);
+}
+
+namespace ublas = boost::numeric::ublas;
+
+
+int main() {
+ const int n=10000;
+#if 1
+ ublas::compressed_matrix<double, ublas::row_major> mat_row_upp(n, n);
+ ublas::compressed_matrix<double, ublas::column_major> mat_col_upp(n, n);
+ ublas::compressed_matrix<double, ublas::row_major> mat_row_low(n, n);
+ ublas::compressed_matrix<double, ublas::column_major> mat_col_low(n, n);
+#else
+ ublas::matrix<double, ublas::row_major> mat_row_upp(n, n, 0);
+ ublas::matrix<double, ublas::column_major> mat_col_upp(n, n, 0);
+ ublas::matrix<double, ublas::row_major> mat_row_low(n, n, 0);
+ ublas::matrix<double, ublas::column_major> mat_col_low(n, n, 0);
+#endif
+ ublas::vector<double> b(n, 1);
+
+ std::cerr << "Constructing..." << std::endl;
+ for (int i=0; i<n; ++i) {
+ b(i) = rand() % 10;
+ double main = -10 + rand() % 20 ;
+ if (main == 0) main+=1;
+ double side = -10 + rand() % 20 ;
+ if (i-1>=0) {
+ mat_row_low(i, i-1) = side;
+ }
+ mat_row_low(i, i) = main;
+
+ mat_col_low(i, i) = main;
+ if (i+1<n) {
+ mat_col_low(i+1, i) = side;
+ }
+
+ mat_row_upp(i, i) = main;
+ if (i+1<n) {
+ mat_row_upp(i, i+1) = side;
+ }
+
+ if (i-1>=0) {
+ mat_col_upp(i-1, i) = side;
+ }
+ mat_col_upp(i, i) = main;
+ }
+
+ std::cerr << "Starting..." << std::endl;
+ {
+ clock_t start = clock();
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(mat_col_low, x, ublas::lower_tag());
+ std::cerr << "Col_low x: " << clock()-start << std::endl;
+ std::cerr << "delta: " << diff(mat_col_low, x, b) << "\n";
+ }
+ {
+ clock_t start = clock();
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(mat_row_low, x, ublas::lower_tag());
+ std::cerr << "Row_low x: " << clock()-start << std::endl;
+ std::cerr << "delta: " << diff(mat_row_low, x, b) << "\n";
+ }
+
+ {
+ clock_t start = clock();
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(mat_col_upp, x, ublas::upper_tag());
+ std::cerr << "col_upp x: " << clock()-start << std::endl;
+ std::cerr << "delta: " << diff(mat_col_upp, x, b) << "\n";
+ }
+ {
+ clock_t start = clock();
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(mat_row_upp, x, ublas::upper_tag());
+ std::cerr << "row_upp x: " << clock()-start << std::endl;
+ std::cerr << "delta: " << diff(mat_row_upp, x, b) << "\n";
+ }
+
+// {
+// clock_t start = clock();
+// ublas::vector<double> x(b);
+// ublas::inplace_solve(x, mat_col_low, ublas::lower_tag());
+// std::cerr << "x col_low: " << clock()-start << std::endl;
+// std::cerr << "delta: " << diff(x, mat_col_low, b) << "\n";
+// }
+ {
+ clock_t start = clock();
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(x, mat_row_low, ublas::lower_tag());
+ std::cerr << "x row_low: " << clock()-start << std::endl;
+ std::cerr << "delta: " << diff(x, mat_row_low, b) << "\n";
+ }
+
+// {
+// clock_t start = clock();
+// ublas::vector<double> x(b);
+// ublas::inplace_solve(x, mat_col_upp, ublas::upper_tag());
+// std::cerr << "x col_upp: " << clock()-start << std::endl;
+// std::cerr << "delta: " << diff(x, mat_col_upp, b) << "\n";
+// }
+ {
+ clock_t start = clock();
+ ublas::vector<double> x(b);
+ ublas::inplace_solve(x, mat_row_upp, ublas::upper_tag());
+ std::cerr << "x row_upp: " << clock()-start << std::endl;
+ std::cerr << "delta: " << diff(x, mat_row_upp, b) << "\n";
+ }
+
+
+}
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