Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/math/matrixutilities/sparsematrix.hpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
Copyright (C) 2012 Klaus Spanderen
5
6
This file is part of QuantLib, a free-software/open-source library
7
for financial quantitative analysts and developers - http://quantlib.org/
8
9
QuantLib is free software: you can redistribute it and/or modify it
10
under the terms of the QuantLib license.  You should have received a
11
copy of the license along with this program; if not, please email
12
<quantlib-dev@lists.sf.net>. The license is also available online at
13
<http://quantlib.org/license.shtml>.
14
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
/*! \file sparsematrix.hpp
21
    \brief typedef for boost sparse matrix class
22
*/
23
24
#ifndef quantlib_sparse_matrix_hpp
25
#define quantlib_sparse_matrix_hpp
26
27
#include <ql/qldefines.hpp>
28
#include <ql/math/array.hpp>
29
30
#if defined(QL_PATCH_MSVC)
31
#pragma warning(push)
32
#pragma warning(disable:4180)
33
#pragma warning(disable:4127)
34
#endif
35
36
#if BOOST_VERSION == 106400
37
#include <boost/serialization/array_wrapper.hpp>
38
#endif
39
40
#include <boost/numeric/ublas/matrix_sparse.hpp>
41
42
#if defined(QL_PATCH_MSVC)
43
#pragma warning(pop)
44
#endif
45
46
namespace QuantLib {
47
48
    typedef boost::numeric::ublas::compressed_matrix<Real> SparseMatrix;
49
    typedef boost::numeric::ublas::matrix_reference<SparseMatrix> SparseMatrixReference;
50
51
0
    inline Array prod(const SparseMatrix& A, const Array& x) {
52
0
        QL_REQUIRE(x.size() == A.size2(),
53
0
                   "vectors and sparse matrices with different sizes ("
54
0
                   << x.size() << ", " << A.size1() << "x" << A.size2() <<
55
0
                   ") cannot be multiplied");
56
57
0
        Array b(x.size(), 0.0);
58
59
0
        for (Size i=0; i < A.filled1()-1; ++i) {
60
0
            const Size begin = A.index1_data()[i];
61
0
            const Size end   = A.index1_data()[i+1];
62
0
            Real t=0;
63
0
            for (Size j=begin; j < end; ++j) {
64
0
                t += A.value_data()[j]*x[A.index2_data()[j]];
65
0
            }
66
67
0
            b[i]=t;
68
0
        }
69
0
        return b;
70
0
    }
Unexecuted instantiation: QuantLib::prod(boost::numeric::ublas::compressed_matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, 0ul, boost::numeric::ublas::unbounded_array<unsigned long, std::__1::allocator<unsigned long> >, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > > const&, QuantLib::Array const&)
Unexecuted instantiation: QuantLib::prod(boost::numeric::ublas::compressed_matrix<double, boost::numeric::ublas::basic_row_major<unsigned long, long>, 0ul, boost::numeric::ublas::unbounded_array<unsigned long, std::__1::allocator<unsigned long> >, boost::numeric::ublas::unbounded_array<double, std::__1::allocator<double> > > const&, QuantLib::Array const&)
71
}
72
73
#endif