Skip to content

Commit a081627

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ddiff
PR-URL: #10376 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#163
1 parent f6af97e commit a081627

34 files changed

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

0 commit comments

Comments
 (0)