Skip to content

Commit 528efd8

Browse files
feat: add support for accessor arrays and refactor stats/base/cumin
PR-URL: #5335 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 21f8e14 commit 528efd8

File tree

12 files changed

+742
-139
lines changed

12 files changed

+742
-139
lines changed

Diff for: lib/node_modules/@stdlib/stats/base/cumin/README.md

+12-15
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 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 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 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 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 @@ cumin.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 ([`dcumin`][@stdlib/stats/base/dcumin], [`scumin`][@stdlib/stats/base/scumin], 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,20 +135,14 @@ cumin.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 cumin = require( '@stdlib/stats/base/cumin' );
141141

142-
var y;
143-
var x;
144-
var i;
145-
146-
x = new Float64Array( 10 );
147-
y = new Float64Array( x.length );
148-
for ( i = 0; i < x.length; i++ ) {
149-
x[ i ] = round( randu()*100.0 );
150-
}
142+
var x = discreteUniform( 10, 0, 100, {
143+
'dtype': 'float64'
144+
});
145+
var y = new Float64Array( x.length );
151146
console.log( x );
152147
console.log( y );
153148

@@ -187,6 +182,8 @@ console.log( y );
187182

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

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

192189
<!-- <related-links> -->

Diff for: lib/node_modules/@stdlib/stats/base/cumin/benchmark/benchmark.js

+16-16
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' );
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 cumin = require( './../lib/cumin.js' );
2931

3032

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

3342
/**
@@ -38,35 +47,26 @@ var cumin = require( './../lib/cumin.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 );
59+
6060
b.tic();
6161
for ( i = 0; i < b.iterations; i++ ) {
6262
x[ 0 ] += 1.0;
6363
v = cumin( x.length, x, 1, y, 1 );
64-
if ( isnan( v[ i%len ] ) ) {
64+
if ( isnan( v[ i % len ] ) ) {
6565
b.fail( 'should not return NaN' );
6666
}
6767
}
6868
b.toc();
69-
if ( isnan( v[ i%len ] ) ) {
69+
if ( isnan( v[ i % len ] ) ) {
7070
b.fail( 'should not return NaN' );
7171
}
7272
b.pass( 'benchmark finished' );

Diff for: lib/node_modules/@stdlib/stats/base/cumin/benchmark/benchmark.ndarray.js

+16-16
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' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2625
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var zeros = require( '@stdlib/array/zeros' );
28+
var gfill = require( '@stdlib/blas/ext/base/gfill' );
2729
var pkg = require( './../package.json' ).name;
2830
var cumin = require( './../lib/ndarray.js' );
2931

3032

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

3342
/**
@@ -38,35 +47,26 @@ var cumin = require( './../lib/ndarray.js' );
3847
* @returns {Function} benchmark function
3948
*/
4049
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-
}
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 );
59+
6060
b.tic();
6161
for ( i = 0; i < b.iterations; i++ ) {
6262
x[ 0 ] += 1.0;
6363
v = cumin( x.length, x, 1, 0, y, 1, 0 );
64-
if ( isnan( v[ i%len ] ) ) {
64+
if ( isnan( v[ i % len ] ) ) {
6565
b.fail( 'should not return NaN' );
6666
}
6767
}
6868
b.toc();
69-
if ( isnan( v[ i%len ] ) ) {
69+
if ( isnan( v[ i % len ] ) ) {
7070
b.fail( 'should not return NaN' );
7171
}
7272
b.pass( 'benchmark finished' );

Diff for: lib/node_modules/@stdlib/stats/base/cumin/docs/repl.txt

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{{alias}}( N, x, strideX, y, strideY )
33
Computes the cumulative minimum of a strided array.
44

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

88
Indexing is relative to the first index. To introduce an offset, use a typed
99
array view.
@@ -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,24 +40,23 @@
4040
> {{alias}}( x.length, x, 1, y, 1 )
4141
[ 1.0, -2.0, -2.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, -2.0, 0.0, -2.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, -2.0 ]
5856
> y0
5957
<Float64Array>[ 0.0, 0.0, 0.0, -2.0, -2.0, -2.0 ]
6058

59+
6160
{{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
6261
Computes the cumulative minimum of a strided array using alternative
6362
indexing semantics.
@@ -75,7 +74,7 @@
7574
Input array.
7675

7776
strideX: integer
78-
Index increment for `x`.
77+
Stride length for `x`.
7978

8079
offsetX: integer
8180
Starting index for `x`.
@@ -84,7 +83,7 @@
8483
Output array.
8584

8685
strideY: integer
87-
Index increment for `y`.
86+
Stride length for `y`.
8887

8988
offsetY: integer
9089
Starting index for `y`.
@@ -105,8 +104,7 @@
105104
// Advanced indexing:
106105
> x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
107106
> 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 )
107+
> {{alias}}.ndarray( 3, x, 2, 1, y, -1, y.length-1 )
110108
[ 0.0, 0.0, 0.0, -2.0, -2.0, -2.0 ]
111109

112110
See Also

Diff for: lib/node_modules/@stdlib/stats/base/cumin/docs/types/index.d.ts

+19-9
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 `cumin`.
@@ -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
* cumin( x.length, x, 1, y, 1 );
4454
* // y => [ 1.0, 1.0, 2.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 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
* cumin.ndarray( x.length, x, 1, 0, y, 1, 0 );
6575
* // y => [ 1.0, 1.0, 2.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 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

Diff for: lib/node_modules/@stdlib/stats/base/cumin/docs/types/test.ts

+3
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 cumin = require( './index' );
2021

2122

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

2930
cumin( x.length, x, 1, y, 1 ); // $ExpectType NumericArray
31+
cumin( 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,7 @@ import cumin = require( './index' );
124126
const y = new Float64Array( 10 );
125127

126128
cumin.ndarray( x.length, x, 1, 0, y, 1, 0 ); // $ExpectType NumericArray
129+
cumin.ndarray( x.length, new AccessorArray( x ), 1, 0, new AccessorArray( y ), 1, 0 ); // $ExpectType AccessorArray<number>
127130
}
128131

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

0 commit comments

Comments
 (0)