Loading...
Searching...
No Matches
gpr::quantum::CartesianNaturalCubicPath Class Reference

#include <gpr/quantum/ConstrainedImaginaryTimePath.h>

Public Types

using V = Eigen::VectorXd

Public Member Functions

 CartesianNaturalCubicPath (const std::vector< V > &path)
std::pair< V, V > evaluate (double r) const
std::pair< V, V > derivatives (double r) const

Detailed Description

Definition at line 115 of file ConstrainedImaginaryTimePath.h.

Member Typedef Documentation

◆ V

Definition at line 117 of file ConstrainedImaginaryTimePath.h.

Constructor & Destructor Documentation

◆ CartesianNaturalCubicPath()

gpr::quantum::CartesianNaturalCubicPath::CartesianNaturalCubicPath ( const std::vector< V > & path)
inlineexplicit

Definition at line 125 of file ConstrainedImaginaryTimePath.h.

126 : values_(path)
127 {
128 if (path.size() < 3)
129 throw std::invalid_argument(
130 "Cartesian spline requires at least three path nodes");
131 knots_.resize(path.size());
132 knots_[0] = 0.;
133 for (std::size_t j = 1; j < path.size(); ++j) {
134 if (path[j].size() != path.front().size() || !path[j].allFinite())
135 throw std::invalid_argument(
136 "Finite consistent Cartesian path coordinates required");
137 const double length = (path[j] - path[j - 1]).norm();
138 if (!(length > 0) || !std::isfinite(length))
139 throw std::invalid_argument(
140 "Distinct Cartesian path nodes required");
141 knots_[j] = knots_[j - 1] + length;
142 }
143 const double total = knots_.back();
144 if (!(total > 0) || !std::isfinite(total))
145 throw std::invalid_argument(
146 "Finite Cartesian path length required");
147 for (double& knot: knots_)
148 knot /= total;
149 second_.assign(path.size(), V::Zero(path.front().size()));
150 const Eigen::Index interior =
151 static_cast<Eigen::Index>(path.size() - 2);
152 Eigen::MatrixXd matrix = Eigen::MatrixXd::Zero(interior, interior);
153 Eigen::MatrixXd rhs =
154 Eigen::MatrixXd::Zero(interior, path.front().size());
155 for (Eigen::Index i = 0; i < interior; ++i) {
156 const std::size_t j = static_cast<std::size_t>(i + 1);
157 const double left = knots_[j] - knots_[j - 1],
158 right = knots_[j + 1] - knots_[j];
159 matrix(i, i) = 2 * (left + right);
160 if (i) matrix(i, i - 1) = left;
161 if (i + 1 < interior) matrix(i, i + 1) = right;
162 rhs.row(i) = 6 * ((path[j + 1] - path[j]) / right -
163 (path[j] - path[j - 1]) / left)
164 .transpose();
165 }
166 const Eigen::MatrixXd curvature = matrix.ldlt().solve(rhs);
167 if (!curvature.allFinite())
168 throw std::runtime_error("Cartesian natural spline solve failed");
169 for (Eigen::Index i = 0; i < interior; ++i)
170 second_[static_cast<std::size_t>(i + 1)] =
171 curvature.row(i).transpose();
172 }

Member Function Documentation

◆ derivatives()

std::pair< V, V > gpr::quantum::CartesianNaturalCubicPath::derivatives ( double r) const
inline

Definition at line 194 of file ConstrainedImaginaryTimePath.h.

195 {
196 if (!std::isfinite(r))
197 throw std::invalid_argument(
198 "Finite Cartesian path coordinate required");
199 auto upper = std::upper_bound(knots_.begin(), knots_.end(), r);
200 std::size_t i =
201 upper == knots_.begin() ? 0
202 : upper == knots_.end()
203 ? knots_.size() - 2
204 : static_cast<std::size_t>(upper - knots_.begin() - 1);
205 const double h = knots_[i + 1] - knots_[i], z = r - knots_[i];
206 const V b = (values_[i + 1] - values_[i]) / h -
207 h * (2 * second_[i] + second_[i + 1]) / 6.;
208 const V c = second_[i] / 2.;
209 const V d = (second_[i + 1] - second_[i]) / (6 * h);
210 return {b + z * (2 * c + 3 * z * d), 2 * c + 6 * z * d};
211 }
@ z
Definition Enums.h:28

◆ evaluate()

std::pair< V, V > gpr::quantum::CartesianNaturalCubicPath::evaluate ( double r) const
inline

Definition at line 174 of file ConstrainedImaginaryTimePath.h.

175 {
176 if (!std::isfinite(r))
177 throw std::invalid_argument(
178 "Finite Cartesian path coordinate required");
179 auto upper = std::upper_bound(knots_.begin(), knots_.end(), r);
180 std::size_t i =
181 upper == knots_.begin() ? 0
182 : upper == knots_.end()
183 ? knots_.size() - 2
184 : static_cast<std::size_t>(upper - knots_.begin() - 1);
185 const double h = knots_[i + 1] - knots_[i], z = r - knots_[i];
186 const V b = (values_[i + 1] - values_[i]) / h -
187 h * (2 * second_[i] + second_[i + 1]) / 6.;
188 const V c = second_[i] / 2.;
189 const V d = (second_[i + 1] - second_[i]) / (6 * h);
190 return {values_[i] + z * (b + z * (c + z * d)),
191 b + z * (2 * c + 3 * z * d)};
192 }

The documentation for this class was generated from the following file: