/src/quantlib/ql/math/matrix.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2007, 2008 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 | | <https://www.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 matrix.hpp |
21 | | \brief matrix used in linear algebra. |
22 | | */ |
23 | | |
24 | | #include <ql/math/matrix.hpp> |
25 | | #if defined(QL_PATCH_MSVC) |
26 | | #pragma warning(push) |
27 | | #pragma warning(disable:4180) |
28 | | #pragma warning(disable:4127) |
29 | | #endif |
30 | | |
31 | | #if BOOST_VERSION == 106400 |
32 | | #include <boost/serialization/array_wrapper.hpp> |
33 | | #endif |
34 | | #include <boost/numeric/ublas/vector_proxy.hpp> |
35 | | #include <boost/numeric/ublas/triangular.hpp> |
36 | | #include <boost/numeric/ublas/lu.hpp> |
37 | | |
38 | | #if defined(QL_PATCH_MSVC) |
39 | | #pragma warning(pop) |
40 | | #endif |
41 | | |
42 | | namespace QuantLib { |
43 | | |
44 | 0 | Matrix inverse(const Matrix& m) { |
45 | 0 | QL_REQUIRE(m.rows() == m.columns(), "matrix is not square"); |
46 | | |
47 | 0 | boost::numeric::ublas::matrix<Real> a(m.rows(), m.columns()); |
48 | |
|
49 | 0 | std::copy(m.begin(), m.end(), a.data().begin()); |
50 | |
|
51 | 0 | boost::numeric::ublas::permutation_matrix<Size> pert(m.rows()); |
52 | | |
53 | | // lu decomposition |
54 | 0 | Size singular = 1; |
55 | 0 | try { |
56 | 0 | singular = lu_factorize(a, pert); |
57 | 0 | } catch (const boost::numeric::ublas::internal_logic& e) { |
58 | 0 | QL_FAIL("lu_factorize error: " << e.what()); |
59 | 0 | } catch (const boost::numeric::ublas::external_logic& e) { |
60 | 0 | QL_FAIL("lu_factorize error: " << e.what()); |
61 | 0 | } |
62 | 0 | QL_REQUIRE(singular == 0, "singular matrix given"); |
63 | | |
64 | 0 | boost::numeric::ublas::matrix<Real> |
65 | 0 | inverse = boost::numeric::ublas::identity_matrix<Real>(m.rows()); |
66 | | |
67 | | // backsubstitution |
68 | 0 | try { |
69 | 0 | boost::numeric::ublas::lu_substitute(a, pert, inverse); |
70 | 0 | } catch (const boost::numeric::ublas::internal_logic& e) { |
71 | 0 | QL_FAIL("lu_substitute error: " << e.what()); |
72 | 0 | } |
73 | | |
74 | 0 | Matrix retVal(m.rows(), m.columns()); |
75 | 0 | std::copy(inverse.data().begin(), inverse.data().end(), |
76 | 0 | retVal.begin()); |
77 | |
|
78 | 0 | return retVal; |
79 | 0 | } |
80 | | |
81 | 0 | Real determinant(const Matrix& m) { |
82 | 0 | QL_REQUIRE(m.rows() == m.columns(), "matrix is not square"); |
83 | | |
84 | 0 | boost::numeric::ublas::matrix<Real> a(m.rows(), m.columns()); |
85 | 0 | std::copy(m.begin(), m.end(), a.data().begin()); |
86 | | |
87 | | |
88 | | // lu decomposition |
89 | 0 | boost::numeric::ublas::permutation_matrix<Size> pert(m.rows()); |
90 | 0 | /* const Size singular = */ lu_factorize(a, pert); |
91 | |
|
92 | 0 | Real retVal = 1.0; |
93 | |
|
94 | 0 | for (Size i=0; i < m.rows(); ++i) { |
95 | 0 | if (pert[i] != i) |
96 | 0 | retVal *= -a(i,i); |
97 | 0 | else |
98 | 0 | retVal *= a(i,i); |
99 | 0 | } |
100 | 0 | return retVal; |
101 | 0 | } |
102 | | |
103 | | } |