nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
polyfun.hpp
Go to the documentation of this file.
1
3// src/builtin/include/nk/builtin/polyfun.hpp
4//
5// Pure C++ Polynomials, interpolation, integration, and piecewise polynomials.
6#pragma once
7
8#include <functional>
9#include <memory_resource>
10#include <string>
11#include <nk/value/value.hpp>
12#include <nk/value/span.hpp>
13#include <nk/value/fn_handle.hpp>
14
15namespace nk::builtin {
16
19
20
21using ::nk::FnHandle;
22
28Value roots(const Value &p, std::pmr::memory_resource *mr = nullptr);
29
35Value poly(const Value &r, std::pmr::memory_resource *mr = nullptr);
36
43Value polyval(const Value &p, const Value &x, std::pmr::memory_resource *mr = nullptr);
44
50Value polyder(const Value &p, std::pmr::memory_resource *mr = nullptr);
51
57std::tuple<Value, Value> polyder(const Value &b, const Value &a, std::pmr::memory_resource *mr = nullptr);
58
65Value polyint(const Value &p, double k = 0.0, std::pmr::memory_resource *mr = nullptr);
66
73Value polyfit(const Value &x, const Value &y, int n, std::pmr::memory_resource *mr = nullptr);
74
75#include <tuple>
76
78struct PolyDiv {
81};
82
88
99Value polyvalm(const Value &p, const Value &A, std::pmr::memory_resource *mr = nullptr);
100
109Value poly_of_matrix(const Value &A, std::pmr::memory_resource *mr = nullptr);
110
119PadeCoef padecoef(double T, int N, std::pmr::memory_resource *mr = nullptr);
120
128PolyDiv polydiv(const Value &b, const Value &a, std::pmr::memory_resource *mr = nullptr);
129
137std::tuple<Value, Value, Value> tf2zp(const Value &b, const Value &a, std::pmr::memory_resource *mr = nullptr);
138
147std::tuple<Value, Value> zp2tf(const Value &z, const Value &p, double k, std::pmr::memory_resource *mr = nullptr);
148
155
166ResidueResult residue(const Value &b, const Value &a, std::pmr::memory_resource *mr = nullptr);
167
178ResidueResult residuez(const Value &b, const Value &a, std::pmr::memory_resource *mr = nullptr);
179
180// ── Interpolation & Piecewise Polynomials ───────────────────────────────────
181
190Value interp1(const Value &x, const Value &v, const Value &xq, const std::string &method = "linear", std::pmr::memory_resource *mr = nullptr);
191
209Value interp2(const Value &V, const Value &Xq, const Value &Yq, const std::string &method = "linear", std::pmr::memory_resource *mr = nullptr);
210
221Value interp2(const Value &x, const Value &y, const Value &v, const Value &xq, const Value &yq, const std::string &method = "linear", std::pmr::memory_resource *mr = nullptr);
222
231Value interp3(const Value &V, const Value &Xq, const Value &Yq, const Value &Zq, const std::string &method = "linear", std::pmr::memory_resource *mr = nullptr);
232
244Value interp3(const Value &X, const Value &Y, const Value &Z, const Value &V, const Value &Xq, const Value &Yq, const Value &Zq, const std::string &method = "linear", std::pmr::memory_resource *mr = nullptr);
245
253Value spline(const Value &x, const Value &y, const Value &xq = Value(), std::pmr::memory_resource *mr = nullptr);
254
262Value pchip(const Value &x, const Value &y, const Value &xq = Value(), std::pmr::memory_resource *mr = nullptr);
263
271Value makima(const Value &x, const Value &y, const Value &xq, std::pmr::memory_resource *mr = nullptr);
272
279Value mkpp(const Value &breaks, const Value &coefs, std::pmr::memory_resource *mr = nullptr);
280
286Value unmkpp(const Value &pp, std::pmr::memory_resource *mr = nullptr);
287
294Value ppval(const Value &pp, const Value &xq, std::pmr::memory_resource *mr = nullptr);
295
302Value gradient(const Value &f, double h = 1.0, std::pmr::memory_resource *mr = nullptr);
303
310std::tuple<Value, Value> gradient2(const Value &f, double hx = 1.0, double hy = 1.0, std::pmr::memory_resource *mr = nullptr);
311
318Value del2(const Value &u, double h = 1.0, std::pmr::memory_resource *mr = nullptr);
319
325Value trapz(const Value &y, std::pmr::memory_resource *mr = nullptr);
326
333Value trapz(const Value &x, const Value &y, std::pmr::memory_resource *mr = nullptr);
334
341Value trapz(const Value &x, const Value &y, int dim, std::pmr::memory_resource *mr = nullptr);
342
348Value cumtrapz(const Value &y, std::pmr::memory_resource *mr = nullptr);
349
355Value cumtrapz(const Value &x, const Value &y, std::pmr::memory_resource *mr = nullptr);
356
363Value cumtrapz(const Value &x, const Value &y, int dim, std::pmr::memory_resource *mr = nullptr);
364
370Value cumtrapzDim(const Value &y, int dim, std::pmr::memory_resource *mr = nullptr);
371
380Value integral(FnHandle fn, double a, double b, double absTol = 1e-10, std::pmr::memory_resource *mr = nullptr);
381
392Value integral2(FnHandle fn, double a, double b, double c, double d, double absTol = 1e-10, std::pmr::memory_resource *mr = nullptr);
393
406Value integral3(FnHandle fn, double a, double b, double c, double d, double e, double f, double absTol = 1e-10, std::pmr::memory_resource *mr = nullptr);
407
408// ── Computational Geometry ──────────────────────────────────────────────────
409
416
422
429
435Value polyarea(const Value &x, const Value &y, std::pmr::memory_resource *mr = nullptr);
436
445Value inpolygon(const Value &xq, const Value &yq, const Value &xv, const Value &yv, std::pmr::memory_resource *mr = nullptr);
446
454InpolygonResult inpolygon2(const Value &xq, const Value &yq, const Value &xv, const Value &yv, std::pmr::memory_resource *mr = nullptr);
455
462Value convhull(const Value &x, const Value &y, std::pmr::memory_resource *mr = nullptr);
463
471PolyxpolyResult polyxpoly(const Value &x1, const Value &y1, const Value &x2, const Value &y2, std::pmr::memory_resource *mr = nullptr);
472
479MatchpairsResult matchpairs(const Value &C, double cU, const std::string &mode = "min", std::pmr::memory_resource *mr = nullptr);
480
488Value boundary(const Value &x, const Value &y, double shrink = 0.5, std::pmr::memory_resource *mr = nullptr);
489
496Value delaunay(const Value &x, const Value &y, std::pmr::memory_resource *mr = nullptr);
497
507Value griddata(const Value &x, const Value &y, const Value &v, const Value &xq, const Value &yq, std::pmr::memory_resource *mr = nullptr);
508
518Value griddata(const Value &x, const Value &y, const Value &v, const Value &xq, const Value &yq, const std::string &method, std::pmr::memory_resource *mr = nullptr);
519
527Value griddatan(const Value &Xv, const Value &vv, const Value &xi, std::pmr::memory_resource *mr = nullptr);
528
536Value griddatan(const Value &Xv, const Value &vv, const Value &xi, const std::string &method, std::pmr::memory_resource *mr = nullptr);
537
545Value histcounts2(const Value &x, const Value &y, const Value &xedgesV, const Value &yedgesV, std::pmr::memory_resource *mr = nullptr);
546
547
549} // namespace nk::builtin
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61
Value polyint(const Value &p, double k=0.0, std::pmr::memory_resource *mr=nullptr)
Computes polynomial integral (polyint(p, k)).
Value spline(const Value &x, const Value &y, const Value &xq=Value(), std::pmr::memory_resource *mr=nullptr)
Cubic spline data interpolation (spline(x, y, xq)).
Value griddata(const Value &x, const Value &y, const Value &v, const Value &xq, const Value &yq, std::pmr::memory_resource *mr=nullptr)
Interpolates scattered 2-D data onto query points (griddata(x, y, v, xq, yq, method)).
Value pchip(const Value &x, const Value &y, const Value &xq=Value(), std::pmr::memory_resource *mr=nullptr)
Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) (pchip(x, y, xq)).
ResidueResult residue(const Value &b, const Value &a, std::pmr::memory_resource *mr=nullptr)
Partial fraction expansion (residues) in Laplace s-domain ([r, p, k] = residue(b, a)).
PadeCoef padecoef(double T, int N, std::pmr::memory_resource *mr=nullptr)
Padé approximation of time delay ([num, den] = padecoef(T, N)).
Value cumtrapz(const Value &y, std::pmr::memory_resource *mr=nullptr)
Cumulative trapezoidal numerical integration with unit spacing (cumtrapz(y)).
PolyxpolyResult polyxpoly(const Value &x1, const Value &y1, const Value &x2, const Value &y2, std::pmr::memory_resource *mr=nullptr)
Intersection points of polygonal lines or boundaries ([xi, yi] = polyxpoly(x1, y1,...
Value boundary(const Value &x, const Value &y, double shrink=0.5, std::pmr::memory_resource *mr=nullptr)
Computes boundary / alpha-shape around 2-D point cloud (boundary(x, y, shrink)).
Value convhull(const Value &x, const Value &y, std::pmr::memory_resource *mr=nullptr)
Computes 2-D convex hull of planar point set (convhull(x, y)).
Value mkpp(const Value &breaks, const Value &coefs, std::pmr::memory_resource *mr=nullptr)
Constructs a piecewise polynomial structure (ppform).
Value ppval(const Value &pp, const Value &xq, std::pmr::memory_resource *mr=nullptr)
Evaluates a piecewise polynomial structure at query points xq.
std::tuple< Value, Value > gradient2(const Value &f, double hx=1.0, double hy=1.0, std::pmr::memory_resource *mr=nullptr)
Numerical 2-D gradient along rows and columns ([fx, fy] = gradient(f, hx, hy)).
Value interp1(const Value &x, const Value &v, const Value &xq, const std::string &method="linear", std::pmr::memory_resource *mr=nullptr)
1-D table lookup and data interpolation (interp1(x, v, xq, method)).
Value integral3(FnHandle fn, double a, double b, double c, double d, double e, double f, double absTol=1e-10, std::pmr::memory_resource *mr=nullptr)
Numerical 3-D triple integration over cuboid region (integral3(fn, a, b, c, d, e, f)).
Value polyarea(const Value &x, const Value &y, std::pmr::memory_resource *mr=nullptr)
Area of 2-D polygonal contour (polyarea(x, y)).
Value histcounts2(const Value &x, const Value &y, const Value &xedgesV, const Value &yedgesV, std::pmr::memory_resource *mr=nullptr)
Computes 2-D bivariate histogram bin counts (histcounts2(x, y, xedges, yedges)).
Value inpolygon(const Value &xq, const Value &yq, const Value &xv, const Value &yv, std::pmr::memory_resource *mr=nullptr)
Tests if 2-D query points are inside polygonal region (inpolygon(xq, yq, xv, yv)).
Value interp2(const Value &V, const Value &Xq, const Value &Yq, const std::string &method="linear", std::pmr::memory_resource *mr=nullptr)
2-D data interpolation on a grid (interp2(x, y, v, xq, yq, method)).
Value integral2(FnHandle fn, double a, double b, double c, double d, double absTol=1e-10, std::pmr::memory_resource *mr=nullptr)
Numerical 2-D double integration over rectangular region (integral2(fn, a, b, c, d)).
Value integral(FnHandle fn, double a, double b, double absTol=1e-10, std::pmr::memory_resource *mr=nullptr)
Numerical 1-D adaptive quadrature integration (integral(fn, a, b)).
Value polyder(const Value &p, std::pmr::memory_resource *mr=nullptr)
Computes polynomial derivative (polyder(p)).
InpolygonResult inpolygon2(const Value &xq, const Value &yq, const Value &xv, const Value &yv, std::pmr::memory_resource *mr=nullptr)
Tests points inside and on edge of 2-D polygonal region ([in, on] = inpolygon(xq, yq,...
Value griddatan(const Value &Xv, const Value &vv, const Value &xi, std::pmr::memory_resource *mr=nullptr)
N-D scattered data interpolation (griddatan(Xv, vv, xi, method)).
Value del2(const Value &u, double h=1.0, std::pmr::memory_resource *mr=nullptr)
Five-point discrete Laplacian / Laplace operator (del2(u, h)).
Value trapz(const Value &y, std::pmr::memory_resource *mr=nullptr)
Trapezoidal numerical integration with unit spacing (trapz(y)).
Value interp3(const Value &V, const Value &Xq, const Value &Yq, const Value &Zq, const std::string &method="linear", std::pmr::memory_resource *mr=nullptr)
3-D data interpolation on a uniform grid (interp3(V, Xq, Yq, Zq, method)).
Value polyfit(const Value &x, const Value &y, int n, std::pmr::memory_resource *mr=nullptr)
Polynomial least-squares curve fitting (polyfit(x, y, n)).
Value polyvalm(const Value &p, const Value &A, std::pmr::memory_resource *mr=nullptr)
Matrix polynomial evaluation (polyvalm(p, A)).
Value unmkpp(const Value &pp, std::pmr::memory_resource *mr=nullptr)
Extracts piecewise polynomial structure fields ([breaks, coefs, l, k, d] = unmkpp(pp)).
Value gradient(const Value &f, double h=1.0, std::pmr::memory_resource *mr=nullptr)
Numerical gradient of 1-D array or uniform multi-dimensional array (gradient(f, h)).
Value makima(const Value &x, const Value &y, const Value &xq, std::pmr::memory_resource *mr=nullptr)
Modified Akima cubic Hermite interpolation (makima(x, y, xq)).
PolyDiv polydiv(const Value &b, const Value &a, std::pmr::memory_resource *mr=nullptr)
Polynomial deconvolution and division ([q, r] = polydiv(b, a)).
Value poly_of_matrix(const Value &A, std::pmr::memory_resource *mr=nullptr)
Characteristic polynomial of square matrix (poly_of_matrix(A)).
MatchpairsResult matchpairs(const Value &C, double cU, const std::string &mode="min", std::pmr::memory_resource *mr=nullptr)
Solves linear sum assignment problem with cost threshold (matchpairs(C, cU, mode)).
Value cumtrapzDim(const Value &y, int dim, std::pmr::memory_resource *mr=nullptr)
Cumulative trapezoidal numerical integration with unit spacing along dimension (cumtrapz(y,...
std::tuple< Value, Value > zp2tf(const Value &z, const Value &p, double k, std::pmr::memory_resource *mr=nullptr)
Zero-pole-gain to transfer function conversion ([b, a] = zp2tf(z, p, k)).
std::tuple< Value, Value, Value > tf2zp(const Value &b, const Value &a, std::pmr::memory_resource *mr=nullptr)
Transfer function to zero-pole-gain conversion ([z, p, k] = tf2zp(b, a)).
Value poly(const Value &r, std::pmr::memory_resource *mr=nullptr)
Polynomial with specified roots or characteristic polynomial of a matrix.
ResidueResult residuez(const Value &b, const Value &a, std::pmr::memory_resource *mr=nullptr)
Partial fraction expansion in Z-domain ([r, p, k] = residuez(b, a)).
Value roots(const Value &p, std::pmr::memory_resource *mr=nullptr)
Computes polynomial roots (roots(p)).
Value delaunay(const Value &x, const Value &y, std::pmr::memory_resource *mr=nullptr)
2-D Delaunay triangulation of planar point set (delaunay(x, y)).
Value polyval(const Value &p, const Value &x, std::pmr::memory_resource *mr=nullptr)
Evaluates polynomial p at points x via Horner's scheme.
Result of 2-D polygon inclusion test (inpolygon).
Definition polyfun.hpp:418
Value in
Logical array of points inside polygon.
Definition polyfun.hpp:419
Value on
Logical array of points on polygon boundary.
Definition polyfun.hpp:420
Result of linear assignment problem (matchpairs).
Definition polyfun.hpp:411
Value uR
Unmatched row indices.
Definition polyfun.hpp:413
Value uC
Unmatched column indices.
Definition polyfun.hpp:414
Value M
Matched index pairs [row, col].
Definition polyfun.hpp:412
Padé approximation numerator and denominator polynomials.
Definition polyfun.hpp:84
Value num
Numerator polynomial coefficients.
Definition polyfun.hpp:85
Value den
Denominator polynomial coefficients.
Definition polyfun.hpp:86
Result of polynomial division and deconvolution ([q, r] = polydiv(b, a)).
Definition polyfun.hpp:78
Value q
Quotient polynomial coefficients.
Definition polyfun.hpp:79
Value r
Remainder polynomial coefficients.
Definition polyfun.hpp:80
Result of polygon intersection (polyxpoly).
Definition polyfun.hpp:424
Value xi
Intersection x-coordinates.
Definition polyfun.hpp:425
Value segments
Intersection line segments.
Definition polyfun.hpp:427
Value yi
Intersection y-coordinates.
Definition polyfun.hpp:426
Result of partial fraction expansion (residues, poles, direct term).
Definition polyfun.hpp:150
Value p
Vector of complex poles.
Definition polyfun.hpp:152
Value r
Vector of complex residues.
Definition polyfun.hpp:151
Value k
Vector of direct term polynomial coefficients.
Definition polyfun.hpp:153