Skip to content

Commit bd4ba4e

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gdiff
PR-URL: #10903 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#191
1 parent a023392 commit bd4ba4e

16 files changed

Lines changed: 3406 additions & 0 deletions

File tree

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
# gdiff
22+
23+
> Calculate the k-th discrete forward difference of a strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gdiff = require( '@stdlib/blas/ext/base/gdiff' );
31+
```
32+
33+
<!-- lint disable maximum-heading-length -->
34+
35+
#### gdiff( 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 strided array.
38+
39+
```javascript
40+
var x = [ 2.0, 4.0, 6.0, 8.0, 10.0 ];
41+
var p = [ 1.0 ];
42+
var a = [ 11.0 ];
43+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
44+
var w = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
45+
46+
gdiff( x.length, 1, x, 1, 1, p, 1, 1, a, 1, out, 1, w, 1 );
47+
48+
console.log( out );
49+
// => [ 1.0, 2.0, 2.0, 2.0, 2.0, 1.0 ]
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **k**: number of times to recursively compute differences.
56+
- **x**: input array.
57+
- **strideX**: stride length for `x`.
58+
- **N1**: number of indexed elements to `prepend`.
59+
- **prepend**: array containing values to prepend prior to computing differences.
60+
- **strideP**: stride length for `prepend`.
61+
- **N2**: number of indexed elements to `append`.
62+
- **append**: array containing values to append prior to computing differences.
63+
- **strideA**: stride length for `append`.
64+
- **out**: output array. Must have `N + N1 + N2 - k` elements.
65+
- **strideOut**: stride length for `out`.
66+
- **workspace**: workspace array. Must have `N + N1 + N2 - 1` elements.
67+
- **strideW**: stride length for `workspace`.
68+
69+
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:
70+
71+
```javascript
72+
var x = [ 2.0, 4.0, 6.0, 8.0, 10.0 ];
73+
var p = [ 1.0 ];
74+
var a = [ 11.0 ];
75+
var out = [ 0.0, 0.0, 0.0, 0.0 ];
76+
var w = [ 0.0, 0.0, 0.0, 0.0 ];
77+
78+
gdiff( 3, 1, x, 2, 1, p, 1, 1, a, 1, out, 1, w, 1 );
79+
80+
console.log( out );
81+
// => [ 1.0, 4.0, 4.0, 1.0 ]
82+
```
83+
84+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
85+
86+
```javascript
87+
var Float64Array = require( '@stdlib/array/float64' );
88+
89+
// Initial array...
90+
var x0 = new Float64Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
91+
92+
// Create an offset view...
93+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
94+
95+
var p = [ 1.0 ];
96+
var a = [ 11.0 ];
97+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
98+
var w = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
99+
100+
gdiff( x1.length, 1, x1, 1, 1, p, 1, 1, a, 1, out, 1, w, 1 );
101+
102+
console.log( out );
103+
// => [ 3.0, 2.0, 2.0, 2.0, 1.0 ]
104+
```
105+
106+
<!-- lint disable maximum-heading-length -->
107+
108+
#### gdiff.ndarray( N, k, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, out, strideOut, offsetOut, workspace, strideW, offsetW )
109+
110+
Calculates the k-th discrete forward difference of a strided array using alternative indexing semantics.
111+
112+
```javascript
113+
var x = [ 2.0, 4.0, 6.0, 8.0, 10.0 ];
114+
var p = [ 1.0 ];
115+
var a = [ 11.0 ];
116+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
117+
var w = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
118+
119+
gdiff.ndarray( x.length, 1, x, 1, 0, 1, p, 1, 0, 1, a, 1, 0, out, 1, 0, w, 1, 0 );
120+
121+
console.log( out );
122+
// => [ 1.0, 2.0, 2.0, 2.0, 2.0, 1.0 ]
123+
```
124+
125+
The function has the following additional parameters:
126+
127+
- **offsetX**: starting index for `x`.
128+
- **offsetP**: starting index for `prepend`.
129+
- **offsetA**: starting index for `append`.
130+
- **offsetOut**: starting index for `out`.
131+
- **offsetW**: starting index for `workspace`.
132+
133+
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:
134+
135+
```javascript
136+
var x = [ 2.0, 4.0, 6.0, 8.0, 10.0 ];
137+
var p = [ 1.0 ];
138+
var a = [ 11.0 ];
139+
var out = [ 0.0, 0.0, 0.0, 0.0 ];
140+
var w = [ 0.0, 0.0, 0.0, 0.0 ];
141+
142+
gdiff.ndarray( 3, 1, x, 1, x.length-3, 1, p, 1, 0, 1, a, 1, 0, out, 1, 0, w, 1, 0 );
143+
144+
console.log( out );
145+
// => [ 5.0, 2.0, 2.0, 1.0 ]
146+
```
147+
148+
</section>
149+
150+
<!-- /.usage -->
151+
152+
<section class="notes">
153+
154+
## Notes
155+
156+
- When `k <= 1`, the workspace array is unused and thus ignored.
157+
- If `N + N1 + N2 <= 1` or `k >= N + N1 + N2`, both functions return the output array unchanged.
158+
159+
</section>
160+
161+
<!-- /.notes -->
162+
163+
<section class="examples">
164+
165+
## Examples
166+
167+
<!-- eslint no-undef: "error" -->
168+
169+
```javascript
170+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
171+
var zeros = require( '@stdlib/array/zeros' );
172+
var gdiff = require( '@stdlib/blas/ext/base/gdiff' );
173+
174+
var x = discreteUniform( 10, -100, 100, {
175+
'dtype': 'generic'
176+
});
177+
console.log( 'Input array: ', x );
178+
179+
var p = discreteUniform( 2, -100, 100, {
180+
'dtype': 'generic'
181+
});
182+
console.log( 'Prepend array: ', p );
183+
184+
var a = discreteUniform( 2, -100, 100, {
185+
'dtype': 'generic'
186+
});
187+
console.log( 'Append array: ', a );
188+
189+
var out = zeros( 10, 'generic' );
190+
var w = zeros( 13, 'generic' );
191+
192+
gdiff( x.length, 4, x, 1, 2, p, 1, 2, a, 1, out, 1, w, 1 );
193+
console.log( 'Output: ', out );
194+
```
195+
196+
</section>
197+
198+
<!-- /.examples -->
199+
200+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
201+
202+
<section class="related">
203+
204+
</section>
205+
206+
<!-- /.related -->
207+
208+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
209+
210+
<section class="links">
211+
212+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
213+
214+
<!-- <related-links> -->
215+
216+
<!-- </related-links> -->
217+
218+
</section>
219+
220+
<!-- /.links -->
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gdiff = require( './../lib/gdiff.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var ol;
51+
var N1;
52+
var N2;
53+
var x;
54+
var p;
55+
var a;
56+
var w;
57+
var o;
58+
var k;
59+
var N;
60+
61+
N = len;
62+
N1 = 1;
63+
N2 = 1;
64+
k = 1;
65+
ol = N + N1 + N2 - k;
66+
67+
x = uniform( N, -100, 100, options );
68+
p = uniform( N1, -100, 100, options );
69+
a = uniform( N2, -100, 100, options );
70+
w = zeros( N+N1+N2-1, options.dtype );
71+
o = zeros( ol, options.dtype );
72+
return benchmark;
73+
74+
/**
75+
* Benchmark function.
76+
*
77+
* @private
78+
* @param {Benchmark} b - benchmark instance
79+
*/
80+
function benchmark( b ) {
81+
var i;
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
gdiff( N, k, x, 1, N1, p, 1, N2, a, 1, o, 1, w, 1 );
86+
if ( isnan( o[ i%ol ] ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
}
90+
b.toc();
91+
if ( isnan( o[ i%ol ] ) ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
}
97+
}
98+
99+
100+
// MAIN //
101+
102+
/**
103+
* Main execution sequence.
104+
*
105+
* @private
106+
*/
107+
function main() {
108+
var len;
109+
var min;
110+
var max;
111+
var f;
112+
var i;
113+
114+
min = 1; // 10^min
115+
max = 6; // 10^max
116+
117+
for ( i = min; i <= max; i++ ) {
118+
len = pow( 10, i );
119+
f = createBenchmark( len );
120+
bench( format( '%s:len=%d', pkg, len ), f );
121+
}
122+
}
123+
124+
main();

0 commit comments

Comments
 (0)