Skip to content

Commit 3c9b304

Browse files
committed
refactor(stats/base/cuminabs): add accessor protocol support
- Refactored cuminabs implementation to support accessor protocol. - Standardized descriptions and parameter documentation. - Updated examples and benchmarks to align with stdlib conventions. - Reduced code duplication by delegating main entry point to ndarray API. - Added explicit tests for accessor arrays. - Ensured compatibility with strided array dependencies.
1 parent 5883c68 commit 3c9b304

File tree

12 files changed

+739
-130
lines changed

12 files changed

+739
-130
lines changed

lib/node_modules/@stdlib/stats/base/cuminabs/README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ The function has the following parameters:
5252

5353
- **N**: number of indexed elements.
5454
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55-
- **strideX**: index increment for `x`.
55+
- **strideX**: stride length for `x`.
5656
- **y**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
57-
- **strideY**: index increment for `y`.
57+
- **strideY**: stride length for `y`.
5858

59-
The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to compute the cumulative minimum absolute value of every other element in `x`,
59+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative minimum absolute value of every other element in `x`,
6060

6161
```javascript
6262
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
@@ -102,7 +102,7 @@ The function has the following additional parameters:
102102
- **offsetX**: starting index for `x`.
103103
- **offsetY**: starting index for `y`.
104104

105-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, `offsetX` and `offsetY` parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative minimum absolute value of every other value in `x` starting from the second value and to store in the last `N` elements of `y` starting from the last element
105+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on a starting indices. For example, to calculate the cumulative minimum absolute value of every other value in `x` starting from the second value and to store in the last `N` elements of `y` starting from the last element
106106

107107
```javascript
108108
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
@@ -122,6 +122,7 @@ cuminabs.ndarray( 4, x, 2, 1, y, -1, y.length-1 );
122122

123123
- If `N <= 0`, both functions return `y` unchanged.
124124
- Depending on the environment, the typed versions ([`dcuminabs`][@stdlib/stats/strided/dcuminabs], [`scuminabs`][@stdlib/stats/base/scuminabs], etc.) are likely to be significantly more performant.
125+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
125126

126127
</section>
127128

@@ -134,11 +135,15 @@ cuminabs.ndarray( 4, x, 2, 1, y, -1, y.length-1 );
134135
<!-- eslint no-undef: "error" -->
135136

136137
```javascript
137-
var randu = require( '@stdlib/random/base/randu' );
138-
var round = require( '@stdlib/math/base/special/round' );
139138
var Float64Array = require( '@stdlib/array/float64' );
139+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
140140
var cuminabs = require( '@stdlib/stats/base/cuminabs' );
141141

142+
var x = discreteUniform( 10, 0, 100, {
143+
'dtype': 'float64'
144+
});
145+
var y = new Float64Array( x.length );
146+
142147
var y;
143148
var x;
144149
var i;
@@ -188,8 +193,11 @@ console.log( y );
188193

189194
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
190195

196+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
197+
191198
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
192199

200+
193201
<!-- <related-links> -->
194202

195203
[@stdlib/stats/base/cumaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/cumaxabs

lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var gfill = require( '@stdlib/blas/ext/base/gfill' );
2527
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
2830
var cuminabs = require( './../lib/cuminabs.js' );
2931

3032

33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
3140
// FUNCTIONS //
3241

3342
/**
@@ -38,35 +47,25 @@ var cuminabs = require( './../lib/cuminabs.js' );
3847
* @returns {Function} benchmark function
3948
*/
4049
function createBenchmark( len ) {
41-
var y;
42-
var x;
43-
var i;
44-
45-
x = [];
46-
y = [];
47-
for ( i = 0; i < len; i++ ) {
48-
x.push( ( randu()*20.0 ) - 10.0 );
49-
y.push( 0.0 );
50-
}
50+
var x = uniform( len, -10, 10, options );
51+
var y = zeros( len, options.dtype );
5152
return benchmark;
5253

5354
function benchmark( b ) {
5455
var v;
5556
var i;
5657

57-
for ( i = 0; i < len; i++ ) {
58-
y[ i ] = 0.0;
59-
}
58+
gfill( len, 0.0, y, 1 );
6059
b.tic();
6160
for ( i = 0; i < b.iterations; i++ ) {
6261
x[ 0 ] += 1.0;
6362
v = cuminabs( x.length, x, 1, y, 1 );
64-
if ( isnan( v[ i%len ] ) ) {
63+
if ( isnan( v[ i % len ] ) ) {
6564
b.fail( 'should not return NaN' );
6665
}
6766
}
6867
b.toc();
69-
if ( isnan( v[ i%len ] ) ) {
68+
if ( isnan( v[ i % len ] ) ) {
7069
b.fail( 'should not return NaN' );
7170
}
7271
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/stats/base/cuminabs/benchmark/benchmark.ndarray.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var zeros = require( '@stdlib/array/zeros' );
27+
var gfill = require( '@stdlib/blas/ext/base/gfill' );
2628
var pow = require( '@stdlib/math/base/special/pow' );
2729
var pkg = require( './../package.json' ).name;
2830
var cuminabs = require( './../lib/ndarray.js' );
2931

3032

33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
3139
// FUNCTIONS //
3240

3341
/**
@@ -38,35 +46,25 @@ var cuminabs = require( './../lib/ndarray.js' );
3846
* @returns {Function} benchmark function
3947
*/
4048
function createBenchmark( len ) {
41-
var x;
42-
var y;
43-
var i;
44-
45-
x = [];
46-
y = [];
47-
for ( i = 0; i < len; i++ ) {
48-
x.push( ( randu()*20.0 ) - 10.0 );
49-
y.push( 0.0 );
50-
}
49+
var x = uniform( len, -10, 10, options );
50+
var y = zeros( len, options.dtype );
5151
return benchmark;
5252

5353
function benchmark( b ) {
5454
var v;
5555
var i;
5656

57-
for ( i = 0; i < len; i++ ) {
58-
y[ i ] = 0.0;
59-
}
57+
gfill( len, 0.0, y, 1 );
6058
b.tic();
6159
for ( i = 0; i < b.iterations; i++ ) {
6260
x[ 0 ] += 1.0;
6361
v = cuminabs( x.length, x, 1, 0, y, 1, 0 );
64-
if ( isnan( v[ i%len ] ) ) {
62+
if ( isnan( v[ i % len ] ) ) {
6563
b.fail( 'should not return NaN' );
6664
}
6765
}
6866
b.toc();
69-
if ( isnan( v[ i%len ] ) ) {
67+
if ( isnan( v[ i % len ] ) ) {
7068
b.fail( 'should not return NaN' );
7169
}
7270
b.pass( 'benchmark finished' );

lib/node_modules/@stdlib/stats/base/cuminabs/docs/repl.txt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{alias}}( N, x, strideX, y, strideY )
33
Computes the cumulative minimum absolute value of a strided array.
44

5-
The `N` and `stride` parameters determine which elements in `x` and `y` are
5+
The `N` and stride parameters determine which elements in the strided arrays are
66
accessed at runtime.
77

88
Indexing is relative to the first index. To introduce an offset, use a typed
@@ -19,13 +19,13 @@
1919
Input array.
2020

2121
strideX: integer
22-
Index increment for `x`.
22+
Stride length for `x`.
2323

2424
y: Array<number>|TypedArray
2525
Output array.
2626

2727
strideY: integer
28-
Index increment for `y`.
28+
Stride length for `y`.
2929

3030
Returns
3131
-------
@@ -40,20 +40,18 @@
4040
> {{alias}}( x.length, x, 1, y, 1 )
4141
[ 1.0, 1.0, 1.0 ]
4242

43-
// Using `N` and `stride` parameters:
43+
// Using `N` and stride parameters:
4444
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ];
4545
> y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
46-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
47-
> {{alias}}( N, x, 2, y, 2 )
46+
> {{alias}}( 3, x, 2, y, 2 )
4847
[ 2.0, 0.0, 1.0, 0.0, 1.0, 0.0 ]
4948

5049
// Using view offsets:
5150
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
5251
> var y0 = new {{alias:@stdlib/array/float64}}( x0.length );
5352
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
5453
> var y1 = new {{alias:@stdlib/array/float64}}( y0.buffer, y0.BYTES_PER_ELEMENT*3 );
55-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
56-
> {{alias}}( N, x1, 2, y1, 1 )
54+
> {{alias}}( 3, x1, 2, y1, 1 )
5755
<Float64Array>[ 2.0, 2.0, 1.0 ]
5856
> y0
5957
<Float64Array>[ 0.0, 0.0, 0.0, 2.0, 2.0, 1.0 ]
@@ -75,7 +73,7 @@
7573
Input array.
7674

7775
strideX: integer
78-
Index increment for `x`.
76+
Stride length for `x`.
7977

8078
offsetX: integer
8179
Starting index for `x`.
@@ -84,7 +82,7 @@
8482
Output array.
8583

8684
strideY: integer
87-
Index increment for `y`.
85+
Stride length for `y`.
8886

8987
offsetY: integer
9088
Starting index for `y`.
@@ -105,8 +103,7 @@
105103
// Advanced indexing:
106104
> x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
107105
> y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
108-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
109-
> {{alias}}.ndarray( N, x, 2, 1, y, -1, y.length-1 )
106+
> {{alias}}.ndarray( 3, x, 2, 1, y, -1, y.length-1 )
110107
[ 0.0, 0.0, 0.0, 1.0, 2.0, 2.0 ]
111108

112109
See Also

lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/index.d.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { NumericArray } from '@stdlib/types/array';
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
30+
/**
31+
* Output array.
32+
*/
33+
type OutputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2434

2535
/**
2636
* Interface describing `cuminabs`.
@@ -31,9 +41,9 @@ interface Routine {
3141
*
3242
* @param N - number of indexed elements
3343
* @param x - input array
34-
* @param strideX - `x` stride length
44+
* @param strideX - stride length for `x`
3545
* @param y - output array
36-
* @param strideY - `y` stride length
46+
* @param strideY - stride length for `y`
3747
* @returns output array
3848
*
3949
* @example
@@ -43,17 +53,17 @@ interface Routine {
4353
* cuminabs( x.length, x, 1, y, 1 );
4454
* // y => [ 1.0, 1.0, 1.0 ]
4555
*/
46-
( N: number, x: NumericArray, strideX: number, y: NumericArray, strideY: number ): NumericArray;
56+
<T extends OutputArray>( N: number, x: InputArray, strideX: number, y: T, strideY: number ): T;
4757

4858
/**
4959
* Computes the cumulative minimum absolute value of a strided array using alternative indexing semantics.
5060
*
5161
* @param N - number of indexed elements
5262
* @param x - input array
53-
* @param strideX - `x` stride length
63+
* @param strideX - stride length for `x`
5464
* @param offsetX - starting index for `x`
5565
* @param y - output array
56-
* @param strideY - `y` stride length
66+
* @param strideY - stride length for `y`
5767
* @param offsetY - starting index for `y`
5868
* @returns output array
5969
*
@@ -64,17 +74,17 @@ interface Routine {
6474
* cuminabs.ndarray( x.length, x, 1, 0, y, 1, 0 );
6575
* // y => [ 1.0, 1.0, 1.0 ]
6676
*/
67-
ndarray( N: number, x: NumericArray, strideX: number, offsetX: number, y: NumericArray, strideY: number, offsetY: number ): NumericArray;
77+
ndarray<T extends OutputArray>( N: number, x: InputArray, strideX: number, offsetX: number, y: T, strideY: number, offsetY: number ): T;
6878
}
6979

7080
/**
7181
* Computes the cumulative minimum absolute value of a strided array.
7282
*
7383
* @param N - number of indexed elements
7484
* @param x - input array
75-
* @param strideX - `x` stride length
85+
* @param strideX - stride length for `x`
7686
* @param y - output array
77-
* @param strideY - `y` stride length
87+
* @param strideY - stride length for `y`
7888
* @returns output array
7989
*
8090
* @example

lib/node_modules/@stdlib/stats/base/cuminabs/docs/types/test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
1920
import cuminabs = require( './index' );
2021

2122

@@ -27,6 +28,7 @@ import cuminabs = require( './index' );
2728
const y = new Float64Array( 10 );
2829

2930
cuminabs( x.length, x, 1, y, 1 ); // $ExpectType NumericArray
31+
cuminabs( x.length, new AccessorArray( x ), 1, new AccessorArray( y ), 1 ); // $ExpectType AccessorArray<number>
3032
}
3133

3234
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -124,6 +126,8 @@ import cuminabs = require( './index' );
124126
const y = new Float64Array( 10 );
125127

126128
cuminabs.ndarray( x.length, x, 1, 0, y, 1, 0 ); // $ExpectType NumericArray
129+
cuminabs.ndarray( x.length, new AccessorArray( x ), 1, 0, new AccessorArray( y ), 1, 0 ); // $ExpectType AccessorArray<number>
130+
127131
}
128132

129133
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...

lib/node_modules/@stdlib/stats/base/cuminabs/examples/index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
22-
var round = require( '@stdlib/math/base/special/round' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2322
var Float64Array = require( '@stdlib/array/float64' );
2423
var cuminabs = require( './../lib' );
2524

26-
var y;
27-
var x;
28-
var i;
25+
var x = discreteUniform(10, -50, 50, {
26+
'dtype': 'float64'
27+
});
28+
var y = new Float64Array( x.length );
2929

30-
x = new Float64Array( 10 );
31-
y = new Float64Array( x.length );
32-
for ( i = 0; i < x.length; i++ ) {
33-
x[ i ] = round( (randu()*100.0) - 50.0 );
34-
}
3530
console.log( x );
3631
console.log( y );
3732

0 commit comments

Comments
 (0)