Skip to content

Commit 5cb7329

Browse files
MAINT: Convert backlash to C++.
1 parent 29d7ded commit 5cb7329

File tree

5 files changed

+107
-163
lines changed

5 files changed

+107
-163
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires = [
88

99
[project]
1010
name = 'ufunclab'
11-
version = '0.0.8.dev10'
11+
version = '0.0.8.dev11'
1212
description = 'NumPy ufuncs and utilities.'
1313
readme = 'README.md'
1414
requires-python = '>=3.9'

src/backlash/backlash_gufunc.c.src

-156
This file was deleted.

src/backlash/backlash_gufunc.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef BACKLASH_GUFUNC_H
2+
#define BACKLASH_GUFUNC_H
3+
4+
#define PY_SSIZE_T_CLEAN
5+
#include "Python.h"
6+
7+
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
8+
#include "numpy/ndarraytypes.h"
9+
10+
#include "../src/util/strided.hpp"
11+
12+
13+
template<typename T>
14+
static void
15+
backlash_core(
16+
npy_intp n, // core dimension n
17+
T *p_x, // pointer to first element of x, a strided 1-d array with shape (n,)
18+
const npy_intp x_stride, // stride (in bytes) of x
19+
T *p_deadband, // pointer to deadband
20+
T *p_initial, // pointer to initial
21+
T *p_out, // pointer to out, a strided 1-d array with shape (n,)
22+
const npy_intp out_stride // stride (in bytes) of out
23+
)
24+
{
25+
T deadband = *p_deadband;
26+
T initial = *p_initial;
27+
T halfband = deadband/2;
28+
T current_y = initial;
29+
for (npy_intp k = 0; k < n; ++k) {
30+
T current_x = get(p_x, x_stride, k);
31+
T xminus = current_x - halfband;
32+
if (xminus > current_y) {
33+
current_y = xminus;
34+
}
35+
else {
36+
T xplus = current_x + halfband;
37+
if (xplus < current_y) {
38+
current_y = xplus;
39+
}
40+
}
41+
set(p_out, out_stride, k, current_y);
42+
}
43+
}
44+
45+
#endif
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
3+
from ufunc_config_types import UFuncExtMod, UFunc, UFuncSource
4+
5+
6+
BACKLASH_DOCSTRING = """\
7+
backlash(x, deadband, initial, /, ...)
8+
9+
Compute the backlash signal of the input signal x.
10+
11+
Parameters
12+
----------
13+
x : array_like
14+
Input signal
15+
deadband : scalar
16+
Width of the deadband of the backlash process.
17+
initial : scalar
18+
Initial state of the output.
19+
20+
Returns
21+
-------
22+
out : ndarray
23+
Output of the backlash process.
24+
25+
Examples
26+
--------
27+
>>> x = np.array([0, 1, 1.1, 1.0, 1.5, 1.4, 1.2, 0.5])
28+
>>> backlash(x, 0.4, 0.0)
29+
array([0. , 0.8, 0.9, 0.9, 1.3, 1.3, 1.3, 0.7])
30+
"""
31+
32+
33+
backlash_core_source = UFuncSource(
34+
funcname='backlash_core',
35+
typesignatures=['fff->f', 'ddd->d', 'ggg->g'],
36+
)
37+
38+
backlash_gufunc = UFunc(
39+
name='backlash',
40+
docstring=BACKLASH_DOCSTRING,
41+
header='backlash_gufunc.h',
42+
signature='(n),(),() -> (n)',
43+
sources=[backlash_core_source],
44+
)
45+
46+
47+
MODULE_DOCSTRING = """\
48+
This module defines the backlash function.
49+
"""
50+
51+
extmod = UFuncExtMod(
52+
module='_backlash',
53+
docstring=MODULE_DOCSTRING,
54+
ufuncs=[backlash_gufunc],
55+
)

ufunclab/meson.build

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ npymath_lib = cc.find_library('npymath', dirs: npymath_path)
4747

4848
gufunc_cxx_src_dirs = [
4949
'all_same',
50+
'backlash',
5051
'corr',
5152
'mad',
5253
'meanvar',
@@ -188,7 +189,6 @@ py3.extension_module(
188189
#----------------------------------------------------------------------
189190

190191
numpy_templated_c_gufunc_dirs = [
191-
'backlash',
192192
'cross',
193193
'fillnan1d',
194194
'first',
@@ -201,11 +201,11 @@ numpy_templated_c_gufunc_dirs = [
201201

202202
foreach src_dir : numpy_templated_c_gufunc_dirs
203203

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

210210
mod_src = custom_target(
211211
src_dir,

0 commit comments

Comments
 (0)