Skip to content

Commit 1bbed39

Browse files
jalajk3004kgrytestdlib-botgururaj1512
authored
feat: add protocol support to stats/base/range
PR-URL: #5779 Closes: #5679 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Gururaj Gurram <[email protected]>
1 parent adf9888 commit 1bbed39

File tree

13 files changed

+431
-149
lines changed

13 files changed

+431
-149
lines changed

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

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,29 @@ The [**range**][range] is defined as the difference between the maximum and mini
3838
var range = require( '@stdlib/stats/base/range' );
3939
```
4040

41-
#### range( N, x, stride )
41+
#### range( N, x, strideX )
4242

43-
Computes the [range][range] of a strided array `x`.
43+
Computes the [range][range] of a strided array.
4444

4545
```javascript
4646
var x = [ 1.0, -2.0, 2.0 ];
47-
var N = x.length;
4847

49-
var v = range( N, x, 1 );
48+
var v = range( x.length, x, 1 );
5049
// returns 4.0
5150
```
5251

5352
The function has the following parameters:
5453

5554
- **N**: number of indexed elements.
5655
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
57-
- **stride**: index increment for `x`.
56+
- **strideX**: stride length for `x`.
5857

59-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [range][range] of every other element in `x`,
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [range][range] of every other element in `x`,
6059

6160
```javascript
62-
var floor = require( '@stdlib/math/base/special/floor' );
63-
6461
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
65-
var N = floor( x.length / 2 );
6662

67-
var v = range( N, x, 2 );
63+
var v = range( 4, x, 2 );
6864
// returns 6.0
6965
```
7066

@@ -74,42 +70,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [
7470

7571
```javascript
7672
var Float64Array = require( '@stdlib/array/float64' );
77-
var floor = require( '@stdlib/math/base/special/floor' );
7873

7974
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
8075
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8176

82-
var N = floor( x0.length / 2 );
83-
84-
var v = range( N, x1, 2 );
77+
var v = range( 4, x1, 2 );
8578
// returns 6.0
8679
```
8780

88-
#### range.ndarray( N, x, stride, offset )
81+
#### range.ndarray( N, x, strideX, offsetX )
8982

9083
Computes the [range][range] of a strided array using alternative indexing semantics.
9184

9285
```javascript
9386
var x = [ 1.0, -2.0, 2.0 ];
94-
var N = x.length;
9587

96-
var v = range.ndarray( N, x, 1, 0 );
88+
var v = range.ndarray( x.length, x, 1, 0 );
9789
// returns 4.0
9890
```
9991

10092
The function has the following additional parameters:
10193

102-
- **offset**: starting index for `x`.
94+
- **offsetX**: starting index for `x`.
10395

104-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [range][range] for every other value in `x` starting from the second value
96+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [range][range] for every other element in `x` starting from the second element
10597

10698
```javascript
107-
var floor = require( '@stdlib/math/base/special/floor' );
108-
10999
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
110-
var N = floor( x.length / 2 );
111100

112-
var v = range.ndarray( N, x, 2, 1 );
101+
var v = range.ndarray( 4, x, 2, 1 );
113102
// returns 6.0
114103
```
115104

@@ -123,6 +112,7 @@ var v = range.ndarray( N, x, 2, 1 );
123112

124113
- If `N <= 0`, both functions return `NaN`.
125114
- Depending on the environment, the typed versions ([`drange`][@stdlib/stats/strided/drange], [`srange`][@stdlib/stats/strided/srange], etc.) are likely to be significantly more performant.
115+
- 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]).
126116

127117
</section>
128118

@@ -135,18 +125,12 @@ var v = range.ndarray( N, x, 2, 1 );
135125
<!-- eslint no-undef: "error" -->
136126

137127
```javascript
138-
var randu = require( '@stdlib/random/base/randu' );
139-
var round = require( '@stdlib/math/base/special/round' );
140-
var Float64Array = require( '@stdlib/array/float64' );
128+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
141129
var range = require( '@stdlib/stats/base/range' );
142130

143-
var x;
144-
var i;
145-
146-
x = new Float64Array( 10 );
147-
for ( i = 0; i < x.length; i++ ) {
148-
x[ i ] = round( (randu()*100.0) - 50.0 );
149-
}
131+
var x = discreteUniform(10, -50, 50, {
132+
'dtype': 'float64'
133+
});
150134
console.log( x );
151135

152136
var v = range( x.length, x, 1 );
@@ -185,6 +169,8 @@ console.log( v );
185169

186170
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
187171

172+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
173+
188174
<!-- <related-links> -->
189175

190176
[@stdlib/stats/strided/drange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/drange

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
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' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
28-
var range = require( './../lib/range.js' );
28+
var range = require( './../lib/main.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
2936

3037

3138
// FUNCTIONS //
@@ -38,13 +45,7 @@ var range = require( './../lib/range.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*20.0 ) - 10.0 );
47-
}
48+
var x = uniform( len, -10, 10, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@
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' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
2727
var pkg = require( './../package.json' ).name;
2828
var range = require( './../lib/ndarray.js' );
2929

3030

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
3138
// FUNCTIONS //
3239

3340
/**
@@ -38,13 +45,7 @@ var range = require( './../lib/ndarray.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = [];
45-
for ( i = 0; i < len; i++ ) {
46-
x.push( ( randu()*20.0 ) - 10.0 );
47-
}
48+
var x = uniform( len, -10, 10, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
{{alias}}( N, x, stride )
2+
{{alias}}( N, x, strideX )
33
Computes the range of a strided array.
44

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

8-
Indexing is relative to the first index. To introduce an offset, use a typed
9-
array view.
8+
Indexing is relative to the first index. To introduce an offset, use a
9+
typed array view.
1010

1111
If `N <= 0`, the function returns `NaN`.
1212

@@ -18,8 +18,8 @@
1818
x: Array<number>|TypedArray
1919
Input array.
2020

21-
stride: integer
22-
Index increment.
21+
strideX: integer
22+
Stride length.
2323

2424
Returns
2525
-------
@@ -33,22 +33,19 @@
3333
> {{alias}}( x.length, x, 1 )
3434
4.0
3535

36-
// Using `N` and `stride` parameters:
36+
// Using `N` and stride parameters:
3737
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ];
38-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
39-
> var stride = 2;
40-
> {{alias}}( N, x, stride )
38+
> {{alias}}( 3, x, 2 )
4139
4.0
4240

4341
// Using view offsets:
4442
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] );
4543
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
46-
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 );
47-
> stride = 2;
48-
> {{alias}}( N, x1, stride )
44+
> {{alias}}( 3, x1, 2 )
4945
4.0
5046

51-
{{alias}}.ndarray( N, x, stride, offset )
47+
48+
{{alias}}.ndarray( N, x, strideX, offsetX )
5249
Computes the range of a strided array using alternative indexing semantics.
5350

5451
While typed array views mandate a view offset based on the underlying
@@ -63,10 +60,10 @@
6360
x: Array<number>|TypedArray
6461
Input array.
6562

66-
stride: integer
67-
Index increment.
63+
strideX: integer
64+
Stride length.
6865

69-
offset: integer
66+
offsetX: integer
7067
Starting index.
7168

7269
Returns
@@ -83,10 +80,8 @@
8380

8481
// Using offset parameter:
8582
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ];
86-
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 );
87-
> {{alias}}.ndarray( N, x, 2, 1 )
83+
> {{alias}}.ndarray( 3, x, 2, 1 )
8884
4.0
8985

9086
See Also
9187
--------
92-

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

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

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

23-
import { NumericArray } from '@stdlib/types/array';
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
2424

2525
/**
2626
* Interface describing `range`.
2727
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
2830
interface Routine {
2931
/**
3032
* Computes the range of a strided array.
3133
*
3234
* @param N - number of indexed elements
3335
* @param x - input array
34-
* @param stride - stride length
36+
* @param strideX - stride length
3537
* @returns range
3638
*
3739
* @example
@@ -40,15 +42,15 @@ interface Routine {
4042
* var v = range( x.length, x, 1 );
4143
* // returns 4.0
4244
*/
43-
( N: number, x: NumericArray, stride: number ): number;
45+
( N: number, x: InputArray, strideX: number ): number;
4446

4547
/**
4648
* Computes the range of a strided array using alternative indexing semantics.
4749
*
4850
* @param N - number of indexed elements
4951
* @param x - input array
50-
* @param stride - stride length
51-
* @param offset - starting index
52+
* @param strideX - stride length
53+
* @param offsetX - starting index
5254
* @returns range
5355
*
5456
* @example
@@ -57,15 +59,15 @@ interface Routine {
5759
* var v = range.ndarray( x.length, x, 1, 0 );
5860
* // returns 4.0
5961
*/
60-
ndarray( N: number, x: NumericArray, stride: number, offset: number ): number;
62+
ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number;
6163
}
6264

6365
/**
6466
* Computes the range of a strided array.
6567
*
6668
* @param N - number of indexed elements
6769
* @param x - input array
68-
* @param stride - stride length
70+
* @param strideX - stride length
6971
* @returns range
7072
*
7173
* @example

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

Lines changed: 3 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 range = require( './index' );
2021

2122

@@ -26,6 +27,7 @@ import range = require( './index' );
2627
const x = new Float64Array( 10 );
2728

2829
range( x.length, x, 1 ); // $ExpectType number
30+
range( x.length, new AccessorArray( x ), 1 ); // $ExpectType number
2931
}
3032

3133
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -85,6 +87,7 @@ import range = require( './index' );
8587
const x = new Float64Array( 10 );
8688

8789
range.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
range.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number
8891
}
8992

9093
// 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/range/examples/index.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
1818

1919
'use strict';
2020

21-
var randu = require( '@stdlib/random/base/randu' );
22-
var round = require( '@stdlib/math/base/special/round' );
23-
var Float64Array = require( '@stdlib/array/float64' );
21+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2422
var range = require( './../lib' );
2523

26-
var x;
27-
var i;
28-
29-
x = new Float64Array( 10 );
30-
for ( i = 0; i < x.length; i++ ) {
31-
x[ i ] = round( (randu()*100.0) - 50.0 );
32-
}
24+
var x = discreteUniform(10, -50, 50, {
25+
'dtype': 'float64'
26+
});
3327
console.log( x );
3428

3529
var v = range( x.length, x, 1 );

0 commit comments

Comments
 (0)