34 const F& objectiveFunction) {
35 Real W = 0.5*(3.0-std::sqrt(5.0));
36 Real x = W*low+(1-W)*high;
37 if (mid > low && mid < high)
40 Real midValue = objectiveFunction(x);
43 while (high-low > tolerance && iterations < maxIt) {
44 if (x - low > high -x) {
45 Real tentativeNewMid = W*low+(1-W)*x;
46 Real tentativeNewMidValue =
47 objectiveFunction(tentativeNewMid);
49 if (tentativeNewMidValue < midValue) {
52 midValue = tentativeNewMidValue;
54 low = tentativeNewMid;
57 Real tentativeNewMid = W*x+(1-W)*high;
58 Real tentativeNewMidValue =
59 objectiveFunction(tentativeNewMid);
61 if (tentativeNewMidValue < midValue) {
64 midValue = tentativeNewMidValue;
66 high = tentativeNewMid;
82 : r_(
r),
s_(
s), alpha_(
alpha), z1_(z1), z2_(z2), z3_(z3), zweight_(zweight)
85 QL_REQUIRE(
r > 0,
"sphere must have positive radius");
94 if (cylinderInside > 0.0) {
127 y3= std::sqrt(
r_*
r_ - y1*y1-y2*y2);
136 Real x2 = x2sq >= 0.0 ?
Real(std::sqrt(x2sq)) : 0.0;
137 Real x3= std::sqrt(
r_*
r_ - x1*x1-x2*x2);
151 Real distance = std::sqrt( z1moved*z1moved +
z2_*
z2_);
153 Real y1moved = z1moved*scale;
156 Real residual =
r_*
r_ - y1*y1 -y2*y2;
157 if (residual >=0.0) {
158 y3 = std::sqrt(residual);
171 y2 = std::sqrt(
r_*
r_ -y1*y1);
188 std::vector<Real>
y(3);
191 "intersection empty so no solution");
193 if (maxIterations ==0)
196 optimizer.
findClosest(maxIterations, tolerance,
y[0],
y[1],
y[2]);
void findClosest(Size maxIterations, Real tolerance, Real &y1, Real &y2, Real &y3) const
Real objectiveFunction(Real x2) const
SphereCylinderOptimizer(Real r, Real s, Real alpha, Real z1, Real z2, Real z3, Real zweight=1.0)
bool isIntersectionNonEmpty() const
bool findByProjection(Real &y1, Real &y2, Real &y3) const
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
unsigned QL_INTEGER Natural
positive integer
std::size_t Size
size of a container
std::vector< Real > sphereCylinderOptimizerClosest(Real r, Real s, Real alpha, Real z1, Real z2, Real z3, Natural maxIterations, Real tolerance, Real zweight)
ext::shared_ptr< YieldTermStructure > r
Find closest point of the intersection of a sphere and cylinder to a given point.