24#ifndef FPGRIDSPLINE_HPP_INCLUDED
25#define FPGRIDSPLINE_HPP_INCLUDED
27#include "fitpack_config.h"
30#include "fxArrays.hpp"
33#include "fitpack_gridded_spline_c.h"
45static_assert(
sizeof(fitpack_gridded_spline_c) ==
sizeof(fitpack_fitter_c),
46 "C descriptor layout mismatch: fitpack_gridded_spline_c vs fitpack_fitter_c");
107 if (
this != &other) {
166 return "fpGridSpline";
199 return static_cast<fpFitter&
>(*this);
202 return static_cast<const fpFitter&
>(*this);
212 virtual void new_points(int32_t n, int32_t xg_n1, int32_t xg_n2, std::vector<double>&
xg, std::vector<double>&
z, int32_t* m =
nullptr,
bool* row_major =
nullptr, int32_t* order =
nullptr) {
213 fitpack_gridded_spline_c_new_points(
as<fitpack_gridded_spline_c>(), xg_n1, xg_n2,
xg.data(),
static_cast<int32_t
>(
z.size()),
z.data(), n, m, row_major, order);
220 virtual int32_t
new_fit(int32_t n, int32_t xg_n1, int32_t xg_n2, std::vector<double>&
xg, std::vector<double>&
z, int32_t* m =
nullptr,
bool* row_major =
nullptr, int32_t* order =
nullptr,
double*
smoothing =
nullptr) {
221 return fitpack_gridded_spline_c_new_fit(
as<fitpack_gridded_spline_c>(), xg_n1, xg_n2,
xg.data(),
static_cast<int32_t
>(
z.size()),
z.data(), n, m, row_major, order,
smoothing);
225 virtual int32_t
fit(
double*
smoothing =
nullptr, int32_t* order =
nullptr) {
240 virtual std::vector<double>
eval_ongrid(int32_t xg_n1, int32_t xg_n2, std::vector<double>&
xg, std::vector<int32_t>& m, int32_t n_result, int32_t* ierr =
nullptr) {
241 int32_t n =
static_cast<int32_t
>(m.size());
242 std::vector<double> result(n_result);
251 virtual double eval(std::vector<double>& x, int32_t* ierr =
nullptr)
const {
252 int32_t n =
static_cast<int32_t
>(x.size());
260 virtual std::vector<double>
eval(int32_t xp_n1, int32_t xp_n2, std::vector<double>& xp, int32_t* ierr =
nullptr) {
261 std::vector<double> result(xp_n2);
262 int32_t n_result = 0;
263 fitpack_gridded_spline_c_grid_eval_many(
as<fitpack_gridded_spline_c>(), xp_n1, xp_n2, xp.data(), ierr, result.data(), &n_result, xp_n2);
264 result.resize(n_result);
272 virtual std::vector<double>
dfdx_ongrid(int32_t xg_n1, int32_t xg_n2, std::vector<double>&
xg, std::vector<int32_t>& m, std::vector<int32_t>& nu, int32_t n_result, int32_t* ierr =
nullptr) {
273 int32_t n =
static_cast<int32_t
>(m.size());
274 std::vector<double> result(n_result);
275 fitpack_gridded_spline_c_dfdx_ongrid(
as<fitpack_gridded_spline_c>(), xg_n1, xg_n2,
xg.data(), n, m.data(), nu.data(), ierr, result.data(), n_result);
283 virtual double dfdx(std::vector<double>& x, std::vector<int32_t>& nu, int32_t* ierr =
nullptr) {
284 int32_t n =
static_cast<int32_t
>(x.size());
292 virtual std::vector<double>
dfdx(int32_t xp_n1, int32_t xp_n2, std::vector<double>& xp, std::vector<int32_t>& nu, int32_t* ierr =
nullptr) {
293 int32_t n =
static_cast<int32_t
>(nu.size());
294 std::vector<double> result(xp_n2);
295 int32_t n_result = 0;
296 fitpack_gridded_spline_c_grid_derivatives_many(
as<fitpack_gridded_spline_c>(), xp_n1, xp_n2, xp.data(), n, nu.data(), ierr, result.data(), &n_result, xp_n2);
297 result.resize(n_result);
305 virtual double integral(std::vector<double>& lower, std::vector<double>& upper)
const {
306 int32_t n =
static_cast<int32_t
>(lower.size());
312 fitpack_gridded_spline_c result_c;
321 int32_t n =
static_cast<int32_t
>(nu.size());
322 fitpack_gridded_spline_c result_c;
335 void comm_pack(std::vector<double>& buffer)
const override {
336 int32_t n =
static_cast<int32_t
>(buffer.size());
345 int32_t n =
static_cast<int32_t
>(buffer.size());
350 double mse()
const override {
362 int32_t n =
static_cast<int32_t
>(buffer.size());
371 int32_t n =
static_cast<int32_t
>(buffer.size());
393 double* raw =
nullptr;
394 int64_t extents[2] = {0, 0};
396 const int64_t total = extents[0] * extents[1];
397 if (raw ==
nullptr || total <= 0)
return {};
398 const double* first =
reinterpret_cast<const double*
>(raw);
399 return std::vector<double>(first, first + total);
407 double* raw =
nullptr;
408 int64_t extents[2] = {0, 0};
410 return { extents[0], extents[1] };
426 fxArray<double>
t()
const {
427 double* raw =
nullptr;
428 int64_t extents[2] = {0, 0};
431 for (
int k = 0; k < 2; ++k) {
433 bounds[2 * k + 1] =
static_cast<FX_SIZE
>(extents[k]);
435 array_c descr = array_c_null;
436 array_c_from_ptr(&descr,
"t",
static_cast<void*
>(raw),
437 getCFITypeFlag<double>(),
438 static_cast<FX_SIZE
>(
sizeof(
double)),
439 static_cast<FX_RANK
>(2), bounds);
440 return fxArray<double>(descr);
453 double* raw =
nullptr;
454 int64_t extents[2] = {0, 0};
456 const int64_t total = extents[0] * extents[1];
457 if (raw ==
nullptr || total <= 0)
return {};
458 const double* first =
reinterpret_cast<const double*
>(raw);
459 return std::vector<double>(first, first + total);
467 double* raw =
nullptr;
468 int64_t extents[2] = {0, 0};
470 return { extents[0], extents[1] };
486 fxArray<double>
xg()
const {
487 double* raw =
nullptr;
488 int64_t extents[2] = {0, 0};
491 for (
int k = 0; k < 2; ++k) {
493 bounds[2 * k + 1] =
static_cast<FX_SIZE
>(extents[k]);
495 array_c descr = array_c_null;
496 array_c_from_ptr(&descr,
"xg",
static_cast<void*
>(raw),
497 getCFITypeFlag<double>(),
498 static_cast<FX_SIZE
>(
sizeof(
double)),
499 static_cast<FX_RANK
>(2), bounds);
500 return fxArray<double>(descr);
509 double* raw =
nullptr;
510 int64_t extents[1] = {0};
512 const int64_t total = extents[0];
513 if (raw ==
nullptr || total <= 0)
return {};
514 const double* first =
reinterpret_cast<const double*
>(raw);
515 return std::vector<double>(first, first + total);
531 fxArray<double>
z()
const {
532 double* raw =
nullptr;
533 int64_t extents[1] = {0};
536 for (
int k = 0; k < 1; ++k) {
538 bounds[2 * k + 1] =
static_cast<FX_SIZE
>(extents[k]);
540 array_c descr = array_c_null;
541 array_c_from_ptr(&descr,
"z",
static_cast<void*
>(raw),
542 getCFITypeFlag<double>(),
543 static_cast<FX_SIZE
>(
sizeof(
double)),
544 static_cast<FX_RANK
>(1), bounds);
545 return fxArray<double>(descr);
570 template <FP_SIZE dim>
573 if (
dims() != dim) {
if (ierr) *ierr = FITPACK_INPUT_ERROR;
return FP_REAL(0); }
574 std::vector<FP_REAL> xw(x.
begin(), x.
end());
575 return eval(xw, ierr);
579 template <FP_SIZE dim>
582 if (
dims() != dim) {
if (ierr) *ierr = FITPACK_INPUT_ERROR;
return {}; }
584 return eval(dim,
static_cast<FP_SIZE
>(xp.size()), xw, ierr);
589 template <FP_SIZE dim>
590 FP_REAL
dfdx(
const fpPoint<dim>& x,
const std::vector<FP_SIZE>& nu, FP_FLAG* ierr =
nullptr)
592 if (
dims() != dim) {
if (ierr) *ierr = FITPACK_INPUT_ERROR;
return FP_REAL(0); }
593 std::vector<FP_REAL> xw(x.
begin(), x.
end());
594 std::vector<FP_SIZE> nuw(nu);
595 return dfdx(xw, nuw, ierr);
599 template <FP_SIZE dim>
600 std::vector<FP_REAL>
dfdx(
const std::vector<
fpPoint<dim>>& xp,
const std::vector<FP_SIZE>& nu,
601 FP_FLAG* ierr =
nullptr)
603 if (
dims() != dim) {
if (ierr) *ierr = FITPACK_INPUT_ERROR;
return {}; }
605 std::vector<FP_SIZE> nuw(nu);
606 return dfdx(dim,
static_cast<FP_SIZE
>(xp.size()), xw, nuw, ierr);
610 template <FP_SIZE dim>
613 if (
dims() != dim)
return FP_REAL(0);
614 std::vector<FP_REAL> lw(lower.
begin(), lower.
end()), uw(upper.
begin(), upper.
end());
double & smoothing()
Definition fpFitter.hpp:317
fpFitter(NoAlloc)
Protected constructor for derived classes (does not allocate)
Definition fpFitter.hpp:354
T * as()
Definition fpFitter.hpp:360
fitpack_gridded_spline_c & c_handle()
Get underlying C wrapper (for interop)
Definition fpGridSpline.hpp:172
fpGridSpline(fpGridSpline &&other) noexcept
Move constructor - transfer ownership.
Definition fpGridSpline.hpp:98
virtual double integral(std::vector< double > &lower, std::vector< double > &upper) const
integral
Definition fpGridSpline.hpp:305
fpGridSpline(fitpack_gridded_spline_c &c_wrapper, ViewTag)
Non-owning view ctor: bit-copy the C handle, mark is_pointer=true. Used by parent classes' polymorphi...
Definition fpGridSpline.hpp:131
std::vector< double > xg_vector() const
Deep copy of component 'xg' as a std::vector.
Definition fpGridSpline.hpp:452
bool is_pointer() const override
Check if this is a non-owning pointer.
Definition fpGridSpline.hpp:150
const fitpack_gridded_spline_c & c_handle() const
Get underlying C wrapper (const, for interop)
Definition fpGridSpline.hpp:179
const fpFitter & as_parent() const
Definition fpGridSpline.hpp:201
int32_t & dims()
Definition fpGridSpline.hpp:553
virtual void new_points(int32_t n, int32_t xg_n1, int32_t xg_n2, std::vector< double > &xg, std::vector< double > &z, int32_t *m=nullptr, bool *row_major=nullptr, int32_t *order=nullptr)
new_points
Definition fpGridSpline.hpp:212
fxArray< double > t() const
Zero-copy fxArray view of component 't'.
Definition fpGridSpline.hpp:426
static fpGridSpline make_view()
Construct an empty (non-owning) wrapper, for use as a view target.
Definition fpGridSpline.hpp:191
std::array< int64_t, 2 > t_shape() const
Extents of component 't', leading dimension first.
Definition fpGridSpline.hpp:406
int32_t comm_size() const override
Definition fpGridSpline.hpp:328
FP_REAL dfdx(const fpPoint< dim > &x, const std::vector< FP_SIZE > &nu, FP_FLAG *ierr=nullptr)
Partial derivative of orders nu(1..dim) at one scattered point.
Definition fpGridSpline.hpp:590
virtual fpGridSpline derivative_spline(std::vector< int32_t > &nu, int32_t *ierr=nullptr)
derivative_spline
Definition fpGridSpline.hpp:320
virtual int32_t fit(double *smoothing=nullptr, int32_t *order=nullptr)
Definition fpGridSpline.hpp:225
fpGridSpline(fitpack_gridded_spline_c &c_wrapper, bool move=false)
Construct from existing C wrapper (takes ownership if move=true)
Definition fpGridSpline.hpp:117
virtual int32_t least_squares(double *smoothing=nullptr, bool *reset_knots=nullptr)
Definition fpGridSpline.hpp:229
int32_t core_comm_size() const override
Definition fpGridSpline.hpp:354
fpGridSpline & operator=(fpGridSpline &&other) noexcept
Move assignment - transfer ownership.
Definition fpGridSpline.hpp:106
fpGridSpline & operator=(const fpGridSpline &other)
Copy assignment - deep copy.
Definition fpGridSpline.hpp:87
std::vector< double > t_vector() const
Deep copy of component 't' as a std::vector.
Definition fpGridSpline.hpp:392
void comm_pack(std::vector< double > &buffer) const override
comm_pack
Definition fpGridSpline.hpp:335
fpGridSpline(const fpGridSpline &other)
Copy constructor - deep copy.
Definition fpGridSpline.hpp:79
fxArray< double > z() const
Zero-copy fxArray view of component 'z'.
Definition fpGridSpline.hpp:531
fpGridSpline()
Default constructor - allocates new fitpack_gridded_spline.
Definition fpGridSpline.hpp:65
virtual std::vector< double > eval_ongrid(int32_t xg_n1, int32_t xg_n2, std::vector< double > &xg, std::vector< int32_t > &m, int32_t n_result, int32_t *ierr=nullptr)
eval_ongrid
Definition fpGridSpline.hpp:240
bool is_allocated() const override
Check if object is allocated.
Definition fpGridSpline.hpp:143
FP_REAL integral(const fpPoint< dim > &lower, const fpPoint< dim > &upper) const
Integral of the spline over the box [lower, upper].
Definition fpGridSpline.hpp:611
fpGridSpline(NoAlloc tag)
Definition fpGridSpline.hpp:619
void core_comm_pack(std::vector< double > &buffer) const override
core_comm_pack
Definition fpGridSpline.hpp:361
void destroy_base() override
Definition fpGridSpline.hpp:376
void core_comm_expand(std::vector< double > &buffer) override
core_comm_expand
Definition fpGridSpline.hpp:370
virtual double eval(std::vector< double > &x, int32_t *ierr=nullptr) const
eval
Definition fpGridSpline.hpp:251
const char * cpp_type_name() const override
Return the fully-qualified C++ class name for this class. Owned by the C++ layer — does not round-tri...
Definition fpGridSpline.hpp:165
virtual double dfdx(std::vector< double > &x, std::vector< int32_t > &nu, int32_t *ierr=nullptr)
dfdx
Definition fpGridSpline.hpp:283
virtual std::vector< double > dfdx_ongrid(int32_t xg_n1, int32_t xg_n2, std::vector< double > &xg, std::vector< int32_t > &m, std::vector< int32_t > &nu, int32_t n_result, int32_t *ierr=nullptr)
dfdx_ongrid
Definition fpGridSpline.hpp:272
fpFitter & as_parent()
Upcast to parent type (reference, no copy)
Definition fpGridSpline.hpp:198
void comm_expand(std::vector< double > &buffer) override
comm_expand
Definition fpGridSpline.hpp:344
virtual std::vector< double > dfdx(int32_t xp_n1, int32_t xp_n2, std::vector< double > &xp, std::vector< int32_t > &nu, int32_t *ierr=nullptr)
dfdx
Definition fpGridSpline.hpp:292
virtual fpGridSpline cross_section(int32_t ax, double u, int32_t *ierr=nullptr)
Definition fpGridSpline.hpp:311
fxArray< double > xg() const
Zero-copy fxArray view of component 'xg'.
Definition fpGridSpline.hpp:486
std::vector< double > z_vector() const
Deep copy of component 'z' as a std::vector.
Definition fpGridSpline.hpp:508
virtual int32_t interpolate()
Definition fpGridSpline.hpp:233
const char * c_type_name() const override
Return the C wrapper struct name for this class (e.g. "fitpack_gridded_spline_c").
Definition fpGridSpline.hpp:157
std::array< int64_t, 2 > xg_shape() const
Extents of component 'xg', leading dimension first.
Definition fpGridSpline.hpp:466
virtual std::vector< double > eval(int32_t xp_n1, int32_t xp_n2, std::vector< double > &xp, int32_t *ierr=nullptr)
eval
Definition fpGridSpline.hpp:260
virtual int32_t new_fit(int32_t n, int32_t xg_n1, int32_t xg_n2, std::vector< double > &xg, std::vector< double > &z, int32_t *m=nullptr, bool *row_major=nullptr, int32_t *order=nullptr, double *smoothing=nullptr)
new_fit
Definition fpGridSpline.hpp:220
std::vector< FP_REAL > dfdx(const std::vector< fpPoint< dim > > &xp, const std::vector< FP_SIZE > &nu, FP_FLAG *ierr=nullptr)
Partial derivative of orders nu(1..dim) at every scattered point.
Definition fpGridSpline.hpp:600
~fpGridSpline() override
Destructor - deallocates if owned.
Definition fpGridSpline.hpp:72
double mse() const override
Definition fpGridSpline.hpp:350
const int32_t & dims() const
Definition fpGridSpline.hpp:556
std::vector< FP_REAL > eval(const std::vector< fpPoint< dim > > &xp, FP_FLAG *ierr=nullptr)
Evaluate the spline at every scattered point.
Definition fpGridSpline.hpp:580
FP_REAL eval(const fpPoint< dim > &x, FP_FLAG *ierr=nullptr) const
Evaluate the spline at one scattered point.
Definition fpGridSpline.hpp:571
std::vector< FP_REAL > fpPointFlatten(const std::vector< fpPoint< dims > > &points)
Flatten a vector of points into Fortran x(dims,m) order. One contiguous copy.
Definition fpPoint.hpp:117
Tag type for derived class constructors (no allocation)
Definition fpFitter.hpp:349
Tag type for non-owning view constructors of polymorphic concrete subtypes. Declared on roots and abs...
Definition fpFitter.hpp:103
A point in dims-dimensional space.
Definition fpPoint.hpp:45
constexpr FP_REAL * begin()
Definition fpPoint.hpp:62
constexpr FP_REAL * end()
Definition fpPoint.hpp:63