Skip to content

Commit ffb21b5

Browse files
committed
feat: add blas/ext/base/dediff
1 parent 8336cd9 commit ffb21b5

33 files changed

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

0 commit comments

Comments
 (0)