File: test_inplace_solve.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (123 lines) | stat: -rw-r--r-- 4,218 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#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 "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() {

    // typedefs are needed as macros do not work with "," in template arguments

    BOOST_UBLAS_TEST_BEGIN();

#ifdef USE_MATRIX
    typedef ublas::matrix<double, ublas::row_major>     mat_doub_rowmaj;
    typedef ublas::matrix<double, ublas::column_major>  mat_doub_colmaj;
    BOOST_UBLAS_TEST_DO( test_inplace_solve<mat_doub_rowmaj> );
    BOOST_UBLAS_TEST_DO( test_inplace_solve<mat_doub_colmaj> );
#endif

#ifdef USE_COMPRESSED_MATRIX
    typedef ublas::compressed_matrix<double, ublas::row_major>  commat_doub_rowmaj;
        typedef ublas::compressed_matrix<double, ublas::column_major>  commat_doub_colmaj;
    BOOST_UBLAS_TEST_DO( test_inplace_solve<commat_doub_rowmaj> );
    BOOST_UBLAS_TEST_DO( test_inplace_solve<commat_doub_colmaj> );
#endif

#ifdef USE_MAPPED_MATRIX
    typedef ublas::mapped_matrix<double, ublas::row_major>      mapmat_doub_rowmaj;
    typedef ublas::mapped_matrix<double, ublas::column_major>   mapmat_doub_colmaj;
    BOOST_UBLAS_TEST_DO( test_inplace_solve<mapmat_doub_rowmaj> );
    BOOST_UBLAS_TEST_DO( test_inplace_solve<mapmat_doub_colmaj> );
#endif

#ifdef USE_COORDINATE_MATRIX
    typedef ublas::coordinate_matrix<double, ublas::row_major>     cormat_doub_rowmaj;
    typedef ublas::coordinate_matrix<double, ublas::column_major>  cormat_doub_colmaj;
    BOOST_UBLAS_TEST_DO( test_inplace_solve<cormat_doub_rowmaj> );
    BOOST_UBLAS_TEST_DO( test_inplace_solve<cormat_doub_colmaj> );
#endif

#ifdef USE_MAPPED_VECTOR_OF_MAPPED_VECTOR
    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_DO( test_inplace_solve<mvmv_doub_rowmaj> );
    BOOST_UBLAS_TEST_DO( test_inplace_solve<mvmv_doub_colmaj> );
#endif

    BOOST_UBLAS_TEST_END();
}