Skip to content

Commit 8498df4

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/ddiff
PR-URL: #12870 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#667
1 parent 6e65472 commit 8498df4

10 files changed

Lines changed: 1240 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 one-dimensional double-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var ddiff = require( '@stdlib/blas/ext/base/ndarray/ddiff' );
37+
```
38+
39+
#### ddiff( arrays )
40+
41+
Calculates the k-th discrete forward difference of a one-dimensional double-precision floating-point ndarray.
42+
43+
```javascript
44+
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
47+
var x = new Float64Vector( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
48+
var prepend = new Float64Vector( [ 1.0 ] );
49+
var append = new Float64Vector( [ 11.0 ] );
50+
var out = new Float64Vector( 6 );
51+
var workspace = new Float64Vector( 6 );
52+
var k = scalar2ndarray( 1, {
53+
'dtype': 'generic'
54+
});
55+
56+
var y = ddiff( [ x, prepend, append, out, workspace, k ] );
57+
// returns <ndarray>[ 1.0, 2.0, 2.0, 2.0, 2.0, 1.0 ]
58+
```
59+
60+
The function has the following parameters:
61+
62+
- **arrays**: array-like object containing the following ndarrays:
63+
64+
- a one-dimensional input ndarray.
65+
- a one-dimensional ndarray containing values to prepend prior to computing differences.
66+
- a one-dimensional ndarray containing values to append prior to computing differences.
67+
- a one-dimensional output ndarray. Must have `N + N1 + N2 - k` elements, where `N` is the number of elements in the input ndarray, `N1` is the number of elements to prepend, `N2` is the number of elements to append, and `k` is the number of times to recursively compute differences.
68+
- a one-dimensional workspace ndarray. Must have `N + N1 + N2 - 1` elements.
69+
- a zero-dimensional ndarray specifying the number of times to recursively compute differences.
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- When `k <= 1`, the workspace ndarray is unused.
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
93+
var zeros = require( '@stdlib/ndarray/zeros' );
94+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
95+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
96+
var ddiff = require( '@stdlib/blas/ext/base/ndarray/ddiff' );
97+
98+
var N = 10;
99+
var N1 = 2;
100+
var N2 = 2;
101+
var k = 4;
102+
var opts = {
103+
'dtype': 'float64'
104+
};
105+
106+
var x = discreteUniform( [ N ], -100, 100, opts );
107+
var p = discreteUniform( [ N1 ], -100, 100, opts );
108+
var a = discreteUniform( [ N2 ], -100, 100, opts );
109+
var out = zeros( [ N + N1 + N2 - k ], opts );
110+
var w = zeros( [ N + N1 + N2 - 1 ], opts );
111+
var knd = scalar2ndarray( k, {
112+
'dtype': 'generic'
113+
});
114+
115+
console.log( 'x: ', ndarray2array( x ) );
116+
console.log( 'prepend: ', ndarray2array( p ) );
117+
console.log( 'append: ', ndarray2array( a ) );
118+
119+
ddiff( [ x, p, a, out, w, knd ] );
120+
console.log( 'out: ', ndarray2array( out ) );
121+
```
122+
123+
</section>
124+
125+
<!-- /.examples -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
</section>
132+
133+
<!-- /.related -->
134+
135+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="links">
138+
139+
</section>
140+
141+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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/uniform' );
25+
var zeros = require( '@stdlib/ndarray/zeros' );
26+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
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 ddiff = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var workspace;
44+
var prepend;
45+
var append;
46+
var opts;
47+
var out;
48+
var k;
49+
var x;
50+
51+
opts = {
52+
'dtype': 'float64'
53+
};
54+
x = uniform( [ len ], -100.0, 100.0, opts );
55+
prepend = uniform( [ 1 ], -100.0, 100.0, opts );
56+
append = uniform( [ 1 ], -100.0, 100.0, opts );
57+
out = zeros( [ len + 1 ], opts );
58+
workspace = zeros( [ len + 1 ], opts );
59+
k = scalar2ndarray( 1, {
60+
'dtype': 'generic'
61+
});
62+
return benchmark;
63+
64+
/**
65+
* Benchmark function.
66+
*
67+
* @private
68+
* @param {Benchmark} b - benchmark instance
69+
*/
70+
function benchmark( b ) {
71+
var v;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
v = ddiff( [ x, prepend, append, out, workspace, k ] );
77+
if ( typeof v !== 'object' ) {
78+
b.fail( 'should return an ndarray' );
79+
}
80+
}
81+
b.toc();
82+
if ( typeof v !== 'object' ) {
83+
b.fail( 'should return an ndarray' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var f;
103+
var i;
104+
105+
min = 1; // 10^min
106+
max = 6; // 10^max
107+
108+
for ( i = min; i <= max; i++ ) {
109+
len = pow( 10, i );
110+
f = createBenchmark( len );
111+
bench( format( '%s:len=%d', pkg, len ), f );
112+
}
113+
}
114+
115+
main();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( arrays )
3+
Calculates the k-th discrete forward difference of a one-dimensional double-
4+
precision floating-point ndarray.
5+
6+
The output ndarray must have `N + N1 + N2 - k` elements, where `N` is the
7+
number of elements in the input ndarray, `N1` is the number of elements to
8+
prepend, `N2` is the number of elements to append, and `k` is the number
9+
of times to recursively compute differences.
10+
11+
The workspace ndarray must have `N + N1 + N2 - 1` elements.
12+
13+
When `k <= 1`, the workspace ndarray is unused.
14+
15+
Parameters
16+
----------
17+
arrays: ArrayLikeObject<ndarray>
18+
Array-like object containing the following ndarrays:
19+
20+
- a one-dimensional input ndarray.
21+
- a one-dimensional ndarray containing values to prepend.
22+
- a one-dimensional ndarray containing values to append.
23+
- a one-dimensional output ndarray.
24+
- a one-dimensional workspace ndarray.
25+
- a zero-dimensional ndarray specifying the number of times to
26+
recursively compute differences.
27+
28+
Returns
29+
-------
30+
out: ndarray
31+
Output ndarray.
32+
33+
Examples
34+
--------
35+
> var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
36+
> var p = new {{alias:@stdlib/ndarray/vector/float64}}( [ 1.0 ] );
37+
> var a = new {{alias:@stdlib/ndarray/vector/float64}}( [ 11.0 ] );
38+
> var out = new {{alias:@stdlib/ndarray/vector/float64}}( 6 );
39+
> var w = new {{alias:@stdlib/ndarray/vector/float64}}( 6 );
40+
> var k = {{alias:@stdlib/ndarray/from-scalar}}( 1, { 'dtype': 'generic' } );
41+
> {{alias}}( [ x, p, a, out, w, k ] )
42+
<ndarray>[ 1.0, 2.0, 2.0, 2.0, 2.0, 1.0 ]
43+
44+
See Also
45+
--------
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray, float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Calculates the k-th discrete forward difference of a one-dimensional double-precision floating-point ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a one-dimensional ndarray containing values to prepend.
34+
* - a one-dimensional ndarray containing values to append.
35+
* - a one-dimensional output ndarray.
36+
* - a one-dimensional workspace ndarray.
37+
* - a zero-dimensional ndarray specifying the number of times to recursively compute differences.
38+
*
39+
* @param arrays - array-like object containing ndarrays
40+
* @returns output ndarray
41+
*
42+
* @example
43+
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
44+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
45+
*
46+
* var x = new Float64Vector( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
47+
* var prepend = new Float64Vector( [ 1.0 ] );
48+
* var append = new Float64Vector( [ 11.0 ] );
49+
* var out = new Float64Vector( 6 );
50+
* var workspace = new Float64Vector( 6 );
51+
* var k = scalar2ndarray( 1, {
52+
* 'dtype': 'generic'
53+
* });
54+
*
55+
* var y = ddiff( [ x, prepend, append, out, workspace, k ] );
56+
* // returns <ndarray>[ 1.0, 2.0, 2.0, 2.0, 2.0, 1.0 ]
57+
*/
58+
declare function ddiff( arrays: [ float64ndarray, float64ndarray, float64ndarray, float64ndarray, float64ndarray, typedndarray<number> ] ): float64ndarray;
59+
60+
61+
// EXPORTS //
62+
63+
export = ddiff;

0 commit comments

Comments
 (0)