85 {
86 if (physical.rows() != center.cols() || physical.cols() == 0 ||
87 !physical.allFinite())
88 return false;
89 const bool same_problem =
90 solver_ && separation_ == separation && equal(center_, center) &&
91 equal(gradient_, gradient) && equal(physical_, physical);
92 Eigen::VectorXd axis;
93 if (same_problem && equal(orientation, accepted_orientation_)) {
94
95
96 axis = accepted_axis_;
97 } else {
98 axis = physical.transpose() * orientation.row(0).transpose();
99 const double norm = axis.norm();
100 if (!(norm > 0.0) || !std::isfinite(norm)) return false;
101 axis /= norm;
102 }
103 if (!same_problem) {
104 center_ = center;
105 gradient_ = gradient;
106 physical_ = physical;
107 separation_ = separation;
108 gpr::Observation seed;
109 seed.
R = geometry(axis);
110 if (initial_endpoint && initial_endpoint->
R.isApprox(seed.
R)) {
111 seed = *initial_endpoint;
113 return false;
114 }
115 if (!
start(center, gradient, separation, physical, memory, seed))
116 return false;
117 }
118
119 struct Context {
120 SphereRotation& rotation;
122 gpr::Observation last;
123 bool failed = false;
124 } context{*
this,
evaluate, accepted_};
125 const auto callback = [](
void*
user,
const DLManagedTensorVersioned*
x,
126 double* value,
127 DLManagedTensorVersioned* g) noexcept {
128 auto& ctx = *
static_cast<Context*
>(
user);
129 auto& state = ctx.rotation;
130 try {
131 const auto* xp = reinterpret_cast<const double*>(
132 static_cast<const uint8_t*
>(
x->dl_tensor.data) +
133 x->dl_tensor.byte_offset);
134 auto* gp = reinterpret_cast<double*>(
135 static_cast<uint8_t*>(g->dl_tensor.data) +
136 g->dl_tensor.byte_offset);
137 const Eigen::Map<const Eigen::VectorXd> u(
138 xp, state.physical_.cols());
139 gpr::Observation image;
140 image.
R = state.geometry(u);
141 if (equal(image.
R, state.accepted_.R)) {
142 image = state.accepted_;
143 } else if (!ctx.evaluate(image)) {
144 ctx.failed = true;
145 return RGMIN_INTERNAL_ERROR;
146 }
147 if (!state.valid(image)) {
148 ctx.failed = true;
149 return RGMIN_INTERNAL_ERROR;
150 }
151 const double linear =
152 state.gradient_.dot((image.
R - state.reference_.R).row(0));
153 *value = (image.
E(0, 0) - state.reference_.E(0, 0) - linear) /
154 (state.separation_ * state.separation_);
155 Eigen::Map<Eigen::VectorXd> output(gp, state.physical_.cols());
156 output = state.physical_.transpose() *
157 (image.
G.row(0).transpose() - state.gradient_) /
158 state.separation_;
159 ctx.last = std::move(image);
160 return RGMIN_SUCCESS;
161 } catch (...) {
162 ctx.failed = true;
163 return RGMIN_INTERNAL_ERROR;
164 }
165 };
166 using Tensor = std::unique_ptr<DLManagedTensorVersioned,
167 decltype(&rgmin_tensor_free)>;
168 Tensor tensor(rgmin_tensor_borrow_cpu_f64(axis.data(), axis.size()),
169 rgmin_tensor_free);
170 rgmin_report_t report{};
171 if (!tensor ||
172 rgmin_solver_step_fg(solver_.get(), callback, &context,
173 tensor.get(), &report) != RGMIN_SUCCESS ||
174 context.failed || !axis.allFinite())
175 return false;
177 if (equal(position, context.last.R)) {
178 endpoint = std::move(context.last);
179 } else if (equal(position, accepted_.R)) {
180 endpoint = accepted_;
181 } else {
182 endpoint.
R = position;
183 if (!
evaluate(endpoint) || !valid(endpoint))
return false;
184 }
185 rotated = (physical_ * axis).transpose();
186 accepted_axis_ = axis;
187 accepted_orientation_ = rotated;
188 accepted_ = endpoint;
189 return true;
190 }
bool start(const gpr::Coord ¢er, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const gpr::Observation &endpoint, const gpr::Observation *previous=nullptr)
eon_relax_band_t eon_relax_surface_fn void * user
VecchiaResult evaluate(const Eigen::MatrixXd &points, const Eigen::VectorXd &y, double lengthscale, double noise, int max_neighbours)