Skip to content

Commit

Permalink
MAINT: Convert backlash to C++.
Browse files Browse the repository at this point in the history
  • Loading branch information
WarrenWeckesser committed Jun 29, 2024
1 parent 29d7ded commit 5cb7329
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 163 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires = [

[project]
name = 'ufunclab'
version = '0.0.8.dev10'
version = '0.0.8.dev11'
description = 'NumPy ufuncs and utilities.'
readme = 'README.md'
requires-python = '>=3.9'
Expand Down
156 changes: 0 additions & 156 deletions src/backlash/backlash_gufunc.c.src

This file was deleted.

45 changes: 45 additions & 0 deletions src/backlash/backlash_gufunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef BACKLASH_GUFUNC_H
#define BACKLASH_GUFUNC_H

#define PY_SSIZE_T_CLEAN
#include "Python.h"

#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include "numpy/ndarraytypes.h"

#include "../src/util/strided.hpp"


template<typename T>
static void
backlash_core(
npy_intp n, // core dimension n
T *p_x, // pointer to first element of x, a strided 1-d array with shape (n,)
const npy_intp x_stride, // stride (in bytes) of x
T *p_deadband, // pointer to deadband
T *p_initial, // pointer to initial
T *p_out, // pointer to out, a strided 1-d array with shape (n,)
const npy_intp out_stride // stride (in bytes) of out
)
{
T deadband = *p_deadband;
T initial = *p_initial;
T halfband = deadband/2;
T current_y = initial;
for (npy_intp k = 0; k < n; ++k) {
T current_x = get(p_x, x_stride, k);
T xminus = current_x - halfband;
if (xminus > current_y) {
current_y = xminus;
}
else {
T xplus = current_x + halfband;
if (xplus < current_y) {
current_y = xplus;
}
}
set(p_out, out_stride, k, current_y);
}
}

#endif
55 changes: 55 additions & 0 deletions src/backlash/define_cxx_gufunc_extmod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


from ufunc_config_types import UFuncExtMod, UFunc, UFuncSource


BACKLASH_DOCSTRING = """\
backlash(x, deadband, initial, /, ...)
Compute the backlash signal of the input signal x.
Parameters
----------
x : array_like
Input signal
deadband : scalar
Width of the deadband of the backlash process.
initial : scalar
Initial state of the output.
Returns
-------
out : ndarray
Output of the backlash process.
Examples
--------
>>> x = np.array([0, 1, 1.1, 1.0, 1.5, 1.4, 1.2, 0.5])
>>> backlash(x, 0.4, 0.0)
array([0. , 0.8, 0.9, 0.9, 1.3, 1.3, 1.3, 0.7])
"""


backlash_core_source = UFuncSource(
funcname='backlash_core',
typesignatures=['fff->f', 'ddd->d', 'ggg->g'],
)

backlash_gufunc = UFunc(
name='backlash',
docstring=BACKLASH_DOCSTRING,
header='backlash_gufunc.h',
signature='(n),(),() -> (n)',
sources=[backlash_core_source],
)


MODULE_DOCSTRING = """\
This module defines the backlash function.
"""

extmod = UFuncExtMod(
module='_backlash',
docstring=MODULE_DOCSTRING,
ufuncs=[backlash_gufunc],
)
12 changes: 6 additions & 6 deletions ufunclab/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ npymath_lib = cc.find_library('npymath', dirs: npymath_path)

gufunc_cxx_src_dirs = [
'all_same',
'backlash',
'corr',
'mad',
'meanvar',
Expand Down Expand Up @@ -188,7 +189,6 @@ py3.extension_module(
#----------------------------------------------------------------------

numpy_templated_c_gufunc_dirs = [
'backlash',
'cross',
'fillnan1d',
'first',
Expand All @@ -201,11 +201,11 @@ numpy_templated_c_gufunc_dirs = [

foreach src_dir : numpy_templated_c_gufunc_dirs

module_name = '_' + src_dir # e.g. '_backlash'
output = src_dir + '_gufunc.c' # e.g. 'backlash_gufunc.c'
src_filename = output + '.src' # e.g. 'backlash_gufunc.c.src'
src_pth = join_paths('../src', src_dir) # e.g. '../src/backlash'
src_fullpath = join_paths(src_pth, src_filename) # e.g. '../src/blacklash/backlash_gufunc.c.src'
module_name = '_' + src_dir # e.g. '_fillnan1d'
output = src_dir + '_gufunc.c' # e.g. 'fillnan1d_gufunc.c'
src_filename = output + '.src' # e.g. 'fillnan1d_gufunc.c.src'
src_pth = join_paths('../src', src_dir) # e.g. '../src/fillnan1d'
src_fullpath = join_paths(src_pth, src_filename) # e.g. '../src/fillnan1d/fillnan1d_gufunc.c.src'

mod_src = custom_target(
src_dir,
Expand Down

0 comments on commit 5cb7329

Please sign in to comment.