Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/math/optimization/projection.cpp
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) 2007 François du Vignaud
5
 Copyright (C) 2007 Giorgio Facchinetti
6
 Copyright (C) 2013 Peter Caspers
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <https://www.quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
#include <ql/math/optimization/projection.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    Projection::Projection(const Array& parameterValues, std::vector<bool> fixParameters)
28
0
    : fixedParameters_(parameterValues), actualParameters_(parameterValues),
29
0
      fixParameters_(std::move(fixParameters)) {
30
31
0
        if (fixParameters_.empty())
32
0
            fixParameters_ =
33
0
                std::vector<bool>(actualParameters_.size(), false);
34
35
0
        QL_REQUIRE(fixedParameters_.size() == fixParameters_.size(),
36
0
                   "fixedParameters_.size()!=parametersFreedoms_.size()");
37
0
        for (auto&& fixParameter : fixParameters_)
38
0
            if (!fixParameter)
39
0
                numberOfFreeParameters_++;
40
0
        QL_REQUIRE(numberOfFreeParameters_ > 0, "numberOfFreeParameters==0");
41
0
    }
42
43
0
    void Projection::mapFreeParameters(const Array &parameterValues) const {
44
45
0
        QL_REQUIRE(parameterValues.size() == numberOfFreeParameters_,
46
0
                   "parameterValues.size()!=numberOfFreeParameters");
47
0
        Size i = 0;
48
0
        for (Size j = 0; j < actualParameters_.size(); j++)
49
0
            if (!fixParameters_[j])
50
0
                actualParameters_[j] = parameterValues[i++];
51
52
0
    }
53
54
0
    Array Projection::project(const Array &parameters) const {
55
56
0
        QL_REQUIRE(parameters.size() == fixParameters_.size(),
57
0
                   "parameters.size()!=parametersFreedoms_.size()");
58
0
        Array projectedParameters(numberOfFreeParameters_);
59
0
        Size i = 0;
60
0
        for (Size j = 0; j < fixParameters_.size(); j++)
61
0
            if (!fixParameters_[j])
62
0
                projectedParameters[i++] = parameters[j];
63
0
        return projectedParameters;
64
65
0
    }
66
67
0
    Array Projection::include(const Array &projectedParameters) const {
68
69
0
        QL_REQUIRE(projectedParameters.size() == numberOfFreeParameters_,
70
0
                   "projectedParameters.size()!=numberOfFreeParameters");
71
0
        Array y(fixedParameters_);
72
0
        Size i = 0;
73
0
        for (Size j = 0; j < y.size(); j++)
74
0
            if (!fixParameters_[j])
75
0
                y[j] = projectedParameters[i++];
76
0
        return y;
77
78
0
    }
79
}