Skip to content

Commit

Permalink
more n67 function 3 (#447)
Browse files Browse the repository at this point in the history
* added lgresp, lgrint, qderiv
* added 3.10 to build matrix
  • Loading branch information
AndrewAnnex committed May 8, 2022
1 parent ba8d027 commit 96e8e39
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [ 3.6, 3.7, 3.8, 3.9 ]
python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10' ]
steps:
- name: Setup 🔬🍦🏗️
if: ${{ matrix.os == 'windows-latest' }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-to-test-and-live-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
steps:
- name: Checkout 🌶️ 🥧
uses: actions/checkout@v2
- name: Set up Python 🐍 3.8
- name: Set up Python 🐍 3.9
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.9
- name: Display Python 🐍
run: python -c "import sys; print(sys.version)"
- name: Install dependencies
Expand Down
1 change: 1 addition & 0 deletions cspice.def
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ EXPORTS appndc_c
ckw03_c
ckw05_c
clearc_c
cleard_c
cleari_c
clight_c
clpool_c
Expand Down
121 changes: 94 additions & 27 deletions src/spiceypy/spiceypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8288,6 +8288,76 @@ def ldpool(filename: str) -> None:
libspice.ldpool_c(filename)


@spice_error_check
def lgresp(first: float, step: float, yvals: ndarray, x: float) -> float:
"""
Evaluate a Lagrange interpolating polynomial for a specified
set of coordinate pairs whose first components are equally
spaced, at a specified abscissa value.

https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/lgresp_c.html

:param first: First abscissa value.
:param step: Step Size.
:param yvals: Ordinate Values.
:param x: Point at which to interpolate the polynomial.
:return: The function returns the value at `x' of the unique polynomial of degree n-1 that fits the points in the plane defined by `first', `step', and `yvals'.
"""
n = ctypes.c_int(len(yvals))
_first = ctypes.c_double(first)
_step = ctypes.c_double(step)
_yvals = stypes.to_double_vector(yvals)
_x = ctypes.c_double(x)
return libspice.lgresp_c(n, _first, _step, _yvals, _x)


@spice_error_check
def lgrind(
xvals: Sequence[float], yvals: Sequence[float], x: float
) -> Tuple[float, float]:
"""
Evaluate a Lagrange interpolating polynomial for a specified
set of coordinate pairs, at a specified abscissa value.
Return the value of both polynomial and derivative.

https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/lgrind_c.html

:param xvals: Abscissa values.
:param yvals: Ordinate values.
:param x: Point at which to interpolate the polynomial.
:return: Polynomial value at x, Polynomial derivative at x.
"""
n = ctypes.c_int(len(xvals))
xvals = stypes.to_double_vector(xvals)
yvals = stypes.to_double_vector(yvals)
work = stypes.empty_double_vector(n.value * 2)
x = ctypes.c_double(x)
p = ctypes.c_double(0)
dp = ctypes.c_double(0)
libspice.lgrind_c(n, xvals, yvals, work, x, p, dp)
return p.value, dp.value


@spice_error_check
def lgrint(xvals: ndarray, yvals: ndarray, x: float) -> float:
"""
Evaluate a Lagrange interpolating polynomial for a specified
set of coordinate pairs, at a specified abscissa value.

https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/lgrint_c.html

:param xvals: Abscissa values.
:param yvals: Ordinate values.
:param x: Point at which to interpolate the polynomial.
:return: The function returns the value at `x' of the unique polynomial of degree n-1 that fits the points in the plane defined by `xvals' and `yvals'.
"""
n = ctypes.c_int(len(xvals))
_xvals = stypes.to_double_vector(xvals)
_yvals = stypes.to_double_vector(yvals)
_x = ctypes.c_double(x)
return libspice.lgrint_c(n, _xvals, _yvals, _x)


@spice_error_check
def limbpt(
method: str,
Expand Down Expand Up @@ -8377,33 +8447,6 @@ def limbpt(
)


@spice_error_check
def lgrind(
xvals: Sequence[float], yvals: Sequence[float], x: float
) -> Tuple[float, float]:
"""
Evaluate a Lagrange interpolating polynomial for a specified
set of coordinate pairs, at a specified abscissa value.
Return the value of both polynomial and derivative.

https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/lgrind_c.html

:param xvals: Abscissa values.
:param yvals: Ordinate values.
:param x: Point at which to interpolate the polynomial.
:return: Polynomial value at x, Polynomial derivative at x.
"""
n = ctypes.c_int(len(xvals))
xvals = stypes.to_double_vector(xvals)
yvals = stypes.to_double_vector(yvals)
work = stypes.empty_double_vector(n.value * 2)
x = ctypes.c_double(x)
p = ctypes.c_double(0)
dp = ctypes.c_double(0)
libspice.lgrind_c(n, xvals, yvals, work, x, p, dp)
return p.value, dp.value


@spice_error_check
def lmpool(cvals: Union[ndarray, Iterable[str]]) -> None:
"""
Expand Down Expand Up @@ -10259,6 +10302,30 @@ def qcktrc(tracelen: int = _default_len_out) -> str:
return stypes.to_python_string(tracestr)


@spice_error_check
def qderiv(f0: ndarray, f2: ndarray, delta: float) -> ndarray:
"""
Estimate the derivative of a function by finding the derivative
of a quadratic approximating function. This derivative estimate
is equivalent to that found by computing the average of forward
and backward differences.

https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/qderiv_c.html

:param f0: Function values at left endpoint.
:param f2: Function values at right endpoint.
:param delta: Separation of abscissa points.
:return: Derivative vector.
"""
_ndim = ctypes.c_int(len(f0))
_f0 = stypes.to_double_vector(f0)
_f2 = stypes.to_double_vector(f2)
_delta = ctypes.c_double(delta)
_dfdt = stypes.empty_double_vector(_ndim)
libspice.qderiv_c(_ndim, _f0, _f2, _delta, _dfdt)
return stypes.c_vector_to_python(_dfdt)


@spice_error_check
def qdq2av(q: ndarray, dq: Union[ndarray, Iterable[float]]) -> ndarray:
"""
Expand Down
21 changes: 21 additions & 0 deletions src/spiceypy/tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5257,12 +5257,25 @@ def test_ldpool():
cleanup_kernel(kernel)


def test_lgresp():
yvals = [-2.0, -8.0, 26.0, 148.0]
a = spice.lgresp(-1.0, 2.0, yvals, 2.0)
assert a == pytest.approx(1.0)


def test_lgrind():
p, dp = spice.lgrind([-1.0, 0.0, 1.0, 3.0], [-2.0, -7.0, -8.0, 26.0], 2.0)
assert p == pytest.approx(1.0)
assert dp == pytest.approx(16.0)


def test_lgrint():
xvals = [-1.0, 0.0, 1.0, 3.0]
yvals = [-2.0, -7.0, -8.0, 26.0]
a = spice.lgrint(xvals, yvals, 2.0)
assert a == pytest.approx(1.0)


def test_limbpt():
spice.furnsh(CoreKernels.spk)
spice.furnsh(ExtraKernels.marsSpk)
Expand Down Expand Up @@ -6358,6 +6371,14 @@ def test_qcktrc():
spice.reset()


def test_qderiv():
delta = 1.0e-3
f0 = [(2.0 - delta) ** 2.0]
f2 = [(2.0 + delta) ** 2.0]
dfdt = spice.qderiv(f0, f2, delta)
assert 4 - dfdt[0] < 1e-12


def test_qdq2av():
angle = [-20.0 * spice.rpd(), 50.0 * spice.rpd(), -60.0 * spice.rpd()]
m = spice.eul2m(angle[2], angle[1], angle[0], 3, 1, 3)
Expand Down
5 changes: 5 additions & 0 deletions src/spiceypy/utils/libspicehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,8 @@
]
libspice.lcase_c.argtypes = [c_char_p, c_int, c_char_p]
libspice.ldpool_c.argtypes = [c_char_p]
libspice.lgresp_c.argtypes = [c_int, c_double, c_double, c_double_p, c_double]
libspice.lgresp_c.restype = c_double
libspice.lgrind_c.argtypes = [
c_int,
c_double_p,
Expand All @@ -1265,6 +1267,8 @@
c_double_p,
c_double_p,
]
libspice.lgrint_c.argtypes = [c_int, c_double_p, c_double_p, c_double]
libspice.lgrint_c.restype = c_double
libspice.limbpt_c.argtypes = [
c_char_p,
c_char_p,
Expand Down Expand Up @@ -1489,6 +1493,7 @@

libspice.q2m_c.argtypes = [c_double * 4, (c_double * 3) * 3]
libspice.qcktrc_c.argtypes = [c_int, c_char_p]
libspice.qderiv_c.argtypes = [c_int, c_double_p, c_double_p, c_double, c_double_p]
libspice.qdq2av_c.argtypes = [c_double * 4, c_double * 4, c_double * 3]
libspice.qxq_c.argtypes = [c_double * 4, c_double * 4, c_double * 4]

Expand Down

0 comments on commit 96e8e39

Please sign in to comment.