Skip to content

Commit 4fe4366

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/sediff
PR-URL: #10905 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#192
1 parent 0cea577 commit 4fe4366

33 files changed

Lines changed: 4648 additions & 0 deletions
Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# sediff
22+
23+
> Calculate the differences between consecutive elements of a single-precision floating-point strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sediff = require( '@stdlib/blas/ext/base/sediff' );
31+
```
32+
33+
<!-- lint disable maximum-heading-length -->
34+
35+
#### sediff( N, x, strideX, N1, prepend, strideP, N2, append, strideA, out, strideOut )
36+
37+
<!-- lint enable maximum-heading-length -->
38+
39+
Calculates the differences between consecutive elements of a single-precision floating-point strided array.
40+
41+
```javascript
42+
var Float32Array = require( '@stdlib/array/float32' );
43+
44+
var x = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
45+
var p = new Float32Array( [ 1.0 ] );
46+
var a = new Float32Array( [ 11.0 ] );
47+
var out = new Float32Array( 6 );
48+
49+
sediff( x.length, x, 1, 1, p, 1, 1, a, 1, out, 1 );
50+
// out => <Float32Array>[ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ]
51+
```
52+
53+
The function has the following parameters:
54+
55+
- **N**: number of indexed elements.
56+
- **x**: input [`Float32Array`][@stdlib/array/float32].
57+
- **strideX**: stride length for `x`.
58+
- **N1**: number of indexed elements to prepend.
59+
- **prepend**: a [`Float32Array`][@stdlib/array/float32] containing values to prepend after computing differences.
60+
- **strideP**: stride length for `prepend`.
61+
- **N2**: number of indexed elements to append.
62+
- **append**: a [`Float32Array`][@stdlib/array/float32] containing values to append after computing differences.
63+
- **strideA**: stride length for `append`.
64+
- **out**: output [`Float32Array`][@stdlib/array/float32]. Must have `N + N1 + N2 - 1` indexed elements.
65+
- **strideOut**: stride length for `out`.
66+
67+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute differences of every other element:
68+
69+
```javascript
70+
var Float32Array = require( '@stdlib/array/float32' );
71+
72+
var x = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
73+
var p = new Float32Array( [ 1.0 ] );
74+
var a = new Float32Array( [ 11.0 ] );
75+
var out = new Float32Array( 4 );
76+
77+
sediff( 3, x, 2, 1, p, 1, 1, a, 1, out, 1 );
78+
// out => <Float32Array>[ 1.0, 4.0, 4.0, 11.0 ]
79+
```
80+
81+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
82+
83+
```javascript
84+
var Float32Array = require( '@stdlib/array/float32' );
85+
86+
// Initial array...
87+
var x0 = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
88+
89+
// Create an offset view...
90+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
91+
92+
var p = new Float32Array( [ 1.0 ] );
93+
var a = new Float32Array( [ 11.0 ] );
94+
var out = new Float32Array( 5 );
95+
96+
sediff( x1.length, x1, 1, 1, p, 1, 1, a, 1, out, 1 );
97+
// out => <Float32Array>[ 1.0, 2.0, 2.0, 2.0, 11.0 ]
98+
```
99+
100+
<!-- lint disable maximum-heading-length -->
101+
102+
#### sediff.ndarray( N, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, out, strideOut, offsetOut )
103+
104+
<!-- lint enable maximum-heading-length -->
105+
106+
Calculates the differences between consecutive elements of a single-precision floating-point strided array using alternative indexing semantics.
107+
108+
```javascript
109+
var Float32Array = require( '@stdlib/array/float32' );
110+
111+
var x = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
112+
var p = new Float32Array( [ 1.0 ] );
113+
var a = new Float32Array( [ 11.0 ] );
114+
var out = new Float32Array( 6 );
115+
116+
sediff.ndarray( x.length, x, 1, 0, 1, p, 1, 0, 1, a, 1, 0, out, 1, 0 );
117+
// out => <Float32Array>[ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ]
118+
```
119+
120+
The function has the following additional parameters:
121+
122+
- **offsetX**: starting index for `x`.
123+
- **offsetP**: starting index for `prepend`.
124+
- **offsetA**: starting index for `append`.
125+
- **offsetOut**: starting index for `out`.
126+
127+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to access only the last three elements:
128+
129+
```javascript
130+
var Float32Array = require( '@stdlib/array/float32' );
131+
132+
var x = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
133+
var p = new Float32Array( [ 1.0 ] );
134+
var a = new Float32Array( [ 11.0 ] );
135+
var out = new Float32Array( 4 );
136+
137+
sediff.ndarray( 3, x, 1, x.length-3, 1, p, 1, 0, 1, a, 1, 0, out, 1, 0 );
138+
// out => <Float32Array>[ 1.0, 2.0, 2.0, 11.0 ]
139+
```
140+
141+
</section>
142+
143+
<!-- /.usage -->
144+
145+
<section class="notes">
146+
147+
## Notes
148+
149+
- If `N + N1 + N2 <= 1`, both functions return the output array unchanged.
150+
151+
</section>
152+
153+
<!-- /.notes -->
154+
155+
<section class="examples">
156+
157+
## Examples
158+
159+
<!-- eslint no-undef: "error" -->
160+
161+
```javascript
162+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
163+
var Float32Array = require( '@stdlib/array/float32' );
164+
var sediff = require( '@stdlib/blas/ext/base/sediff' );
165+
166+
var x = discreteUniform( 10, -100, 100, {
167+
'dtype': 'float32'
168+
});
169+
console.log( 'Input array:', x );
170+
171+
var p = discreteUniform( 2, -100, 100, {
172+
'dtype': 'float32'
173+
});
174+
console.log( 'Prepend array:', p );
175+
176+
var a = discreteUniform( 2, -100, 100, {
177+
'dtype': 'float32'
178+
});
179+
console.log( 'Append array:', a );
180+
181+
var out = new Float32Array( 13 );
182+
183+
sediff( x.length, x, 1, 2, p, 1, 2, a, 1, out, 1 );
184+
console.log( 'Output:', out );
185+
```
186+
187+
</section>
188+
189+
<!-- /.examples -->
190+
191+
<!-- C interface documentation. -->
192+
193+
* * *
194+
195+
<section class="c">
196+
197+
## C APIs
198+
199+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
200+
201+
<section class="intro">
202+
203+
</section>
204+
205+
<!-- /.intro -->
206+
207+
<!-- C usage documentation. -->
208+
209+
<section class="usage">
210+
211+
### Usage
212+
213+
```c
214+
#include "stdlib/blas/ext/base/sediff.h"
215+
```
216+
217+
<!-- lint disable maximum-heading-length -->
218+
219+
#### stdlib_strided_sediff( N, \*X, strideX, N1, \*Prepend, strideP, N2, \*Append, strideA, \*Out, strideOut )
220+
221+
<!-- lint enable maximum-heading-length -->
222+
223+
Calculates the differences between consecutive elements of a single-precision floating-point strided array.
224+
225+
```c
226+
const float x[] = { 2.0f, 4.0f, 6.0f, 8.0f, 10.0f };
227+
const float p[] = { 1.0f };
228+
const float a[] = { 11.0f };
229+
float out[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
230+
231+
stdlib_strided_sediff( 5, x, 1, 1, p, 1, 1, a, 1, out, 1 );
232+
```
233+
234+
The function accepts the following arguments:
235+
236+
- **N**: `[in] CBLAS_INT` number of indexed elements.
237+
- **X**: `[in] float` input [`Float32Array`][@stdlib/array/float32].
238+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
239+
- **N1**: `[in] CBLAS_INT` number of indexed elements to prepend.
240+
- **Prepend**: `[in] float` a [`Float32Array`][@stdlib/array/float32] containing values to prepend after computing differences.
241+
- **strideP**: `[in] CBLAS_INT` stride length for `Prepend`.
242+
- **N2**: `[in] CBLAS_INT` number of indexed elements to append.
243+
- **Append**: `[in] float` a [`Float32Array`][@stdlib/array/float32] containing values to append after computing differences.
244+
- **strideA**: `[in] CBLAS_INT` stride length for `Append`.
245+
- **Out**: `[out] float` output [`Float32Array`][@stdlib/array/float32]. Must have `N + N1 + N2 - 1` indexed elements.
246+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
247+
248+
```c
249+
void stdlib_strided_sediff( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT N1, const float *Prepend, const CBLAS_INT strideP, const CBLAS_INT N2, const float *Append, const CBLAS_INT strideA, float *Out, const CBLAS_INT strideOut );
250+
```
251+
252+
<!-- lint disable maximum-heading-length -->
253+
254+
#### stdlib_strided_sediff_ndarray( N, \*X, strideX, offsetX, N1, \*Prepend, strideP, offsetP, N2, \*Append, strideA, offsetA, \*Out, strideOut, offsetOut )
255+
256+
<!-- lint enable maximum-heading-length -->
257+
258+
Calculates the differences between consecutive elements of a single-precision floating-point strided array using alternative indexing semantics.
259+
260+
```c
261+
const float x[] = { 2.0f, 4.0f, 6.0f, 8.0f, 10.0f };
262+
const float p[] = { 1.0f };
263+
const float a[] = { 11.0f };
264+
float out[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
265+
266+
stdlib_strided_sediff_ndarray( 5, x, 1, 0, 1, p, 1, 0, 1, a, 1, 0, out, 1, 0 );
267+
```
268+
269+
The function accepts the following arguments:
270+
271+
- **N**: `[in] CBLAS_INT` number of indexed elements.
272+
- **X**: `[in] float` input [`Float32Array`][@stdlib/array/float32].
273+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
274+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
275+
- **N1**: `[in] CBLAS_INT` number of indexed elements to prepend.
276+
- **Prepend**: `[in] float` a [`Float32Array`][@stdlib/array/float32] containing values to prepend after computing differences.
277+
- **strideP**: `[in] CBLAS_INT` stride length for `Prepend`.
278+
- **offsetP**: `[in] CBLAS_INT` starting index for `Prepend`.
279+
- **N2**: `[in] CBLAS_INT` number of indexed elements to append.
280+
- **Append**: `[in] float` a [`Float32Array`][@stdlib/array/float32] containing values to append after computing differences.
281+
- **strideA**: `[in] CBLAS_INT` stride length for `Append`.
282+
- **offsetA**: `[in] CBLAS_INT` starting index for `Append`.
283+
- **Out**: `[out] float` output [`Float32Array`][@stdlib/array/float32]. Must have `N + N1 + N2 - 1` indexed elements.
284+
- **strideOut**: `[in] CBLAS_INT` stride length for `Out`.
285+
- **offsetOut**: `[in] CBLAS_INT` starting index for `Out`.
286+
287+
```c
288+
void stdlib_strided_sediff_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const CBLAS_INT N1, const float *Prepend, const CBLAS_INT strideP, const CBLAS_INT offsetP, const CBLAS_INT N2, const float *Append, const CBLAS_INT strideA, const CBLAS_INT offsetA, float *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );
289+
```
290+
291+
</section>
292+
293+
<!-- /.usage -->
294+
295+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
296+
297+
<section class="notes">
298+
299+
</section>
300+
301+
<!-- /.notes -->
302+
303+
<!-- C API usage examples. -->
304+
305+
<section class="examples">
306+
307+
### Examples
308+
309+
```c
310+
#include "stdlib/blas/ext/base/sediff.h"
311+
#include <stdio.h>
312+
313+
int main( void ) {
314+
// Create a strided array:
315+
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
316+
317+
// Define a list of values to prepend:
318+
const float p[] = { -1.0f };
319+
320+
// Define a list of values to append:
321+
const float a[] = { 10.0f };
322+
323+
// Define an output array:
324+
float out[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
325+
326+
// Compute forward differences:
327+
stdlib_strided_sediff( 8, x, 1, 1, p, 1, 1, a, 1, out, 1 );
328+
329+
// Print the result:
330+
for ( int i = 0; i < 9; i++ ) {
331+
printf( "out[ %i ] = %f\n", i, out[ i ] );
332+
}
333+
}
334+
```
335+
336+
</section>
337+
338+
<!-- /.examples -->
339+
340+
</section>
341+
342+
<!-- /.c -->
343+
344+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
345+
346+
<section class="related">
347+
348+
</section>
349+
350+
<!-- /.related -->
351+
352+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
353+
354+
<section class="links">
355+
356+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
357+
358+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
359+
360+
<!-- <related-links> -->
361+
362+
<!-- </related-links> -->
363+
364+
</section>
365+
366+
<!-- /.links -->

0 commit comments

Comments
 (0)