Skip to content

Commit fbb31fb

Browse files
aayush0325kgryte
andauthored
feat: add support for accessor arrays and refactor stats/base/min
PR-URL: #5042 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent b74a0f9 commit fbb31fb

File tree

13 files changed

+336
-147
lines changed

13 files changed

+336
-147
lines changed

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

+18-32
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,29 @@ limitations under the License.
3636
var min = require( '@stdlib/stats/base/min' );
3737
```
3838

39-
#### min( N, x, stride )
39+
#### min( N, x, strideX )
4040

4141
Computes the minimum value of a strided array `x`.
4242

4343
```javascript
4444
var x = [ 1.0, -2.0, 2.0 ];
45-
var N = x.length;
4645

47-
var v = min( N, x, 1 );
46+
var v = min( x.length, x, 1 );
4847
// returns -2.0
4948
```
5049

5150
The function has the following parameters:
5251

5352
- **N**: number of indexed elements.
5453
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55-
- **stride**: index increment for `x`.
54+
- **strideX**: stride length for `x`.
5655

57-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the minimum value of every other element in `x`,
56+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the minimum value of every other element in `x`,
5857

5958
```javascript
60-
var floor = require( '@stdlib/math/base/special/floor' );
61-
6259
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
63-
var N = floor( x.length / 2 );
6460

65-
var v = min( N, x, 2 );
61+
var v = min( 4, x, 2 );
6662
// returns -2.0
6763
```
6864

@@ -72,42 +68,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [
7268

7369
```javascript
7470
var Float64Array = require( '@stdlib/array/float64' );
75-
var floor = require( '@stdlib/math/base/special/floor' );
7671

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

80-
var N = floor( x0.length / 2 );
81-
82-
var v = min( N, x1, 2 );
75+
var v = min( 4, x1, 2 );
8376
// returns -2.0
8477
```
8578

86-
#### min.ndarray( N, x, stride, offset )
79+
#### min.ndarray( N, x, strideX, offsetX )
8780

8881
Computes the minimum value of a strided array using alternative indexing semantics.
8982

9083
```javascript
9184
var x = [ 1.0, -2.0, 2.0 ];
92-
var N = x.length;
9385

94-
var v = min.ndarray( N, x, 1, 0 );
86+
var v = min.ndarray( x.length, x, 1, 0 );
9587
// returns -2.0
9688
```
9789

9890
The function has the following additional parameters:
9991

100-
- **offset**: starting index for `x`.
92+
- **offsetX**: starting index for `x`.
10193

102-
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 minimum value for every other value in `x` starting from the second value
94+
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 minimum value for every other element in the strided array starting from the second element
10395

10496
```javascript
105-
var floor = require( '@stdlib/math/base/special/floor' );
106-
10797
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
108-
var N = floor( x.length / 2 );
10998

110-
var v = min.ndarray( N, x, 2, 1 );
99+
var v = min.ndarray( 4, x, 2, 1 );
111100
// returns -2.0
112101
```
113102

@@ -120,6 +109,7 @@ var v = min.ndarray( N, x, 2, 1 );
120109
## Notes
121110

122111
- If `N <= 0`, both functions return `NaN`.
112+
- 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]).
123113
- Depending on the environment, the typed versions ([`dmin`][@stdlib/stats/base/dmin], [`smin`][@stdlib/stats/base/smin], etc.) are likely to be significantly more performant.
124114

125115
</section>
@@ -133,18 +123,12 @@ var v = min.ndarray( N, x, 2, 1 );
133123
<!-- eslint no-undef: "error" -->
134124

135125
```javascript
136-
var randu = require( '@stdlib/random/base/randu' );
137-
var round = require( '@stdlib/math/base/special/round' );
138-
var Float64Array = require( '@stdlib/array/float64' );
126+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
139127
var min = require( '@stdlib/stats/base/min' );
140128

141-
var x;
142-
var i;
143-
144-
x = new Float64Array( 10 );
145-
for ( i = 0; i < x.length; i++ ) {
146-
x[ i ] = round( (randu()*100.0) - 50.0 );
147-
}
129+
var x = discreteUniform( 10, -50, 50, {
130+
'dtype': 'float64'
131+
});
148132
console.log( x );
149133

150134
var v = min( x.length, x, 1 );
@@ -180,6 +164,8 @@ console.log( v );
180164

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

167+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
168+
183169
[@stdlib/stats/base/dmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/dmin
184170

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

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

+10-9
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 min = require( './../lib/min.js' );
28+
var min = 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 min = require( './../lib/min.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/min/benchmark/benchmark.ndarray.js

+9-8
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 min = 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 min = 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/min/docs/repl.txt

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
{{alias}}( N, x, stride )
2+
{{alias}}( N, x, strideX )
33
Computes the minimum value 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 array
6+
are accessed at runtime.
77

88
Indexing is relative to the first index. To introduce an offset, use a typed
99
array view.
@@ -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
-2.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
-2.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
-2.0
5046

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

@@ -64,10 +61,10 @@
6461
x: Array<number>|TypedArray
6562
Input array.
6663

67-
stride: integer
68-
Index increment.
64+
strideX: integer
65+
Stride length.
6966

70-
offset: integer
67+
offsetX: integer
7168
Starting index.
7269

7370
Returns
@@ -84,8 +81,7 @@
8481

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

9187
See Also

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
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>;
2429

2530
/**
2631
* Interface describing `min`.
@@ -31,7 +36,7 @@ interface Routine {
3136
*
3237
* @param N - number of indexed elements
3338
* @param x - input array
34-
* @param stride - stride length
39+
* @param strideX - stride length
3540
* @returns minimum value
3641
*
3742
* @example
@@ -40,15 +45,15 @@ interface Routine {
4045
* var v = min( x.length, x, 1 );
4146
* // returns -2.0
4247
*/
43-
( N: number, x: NumericArray, stride: number ): number;
48+
( N: number, x: InputArray, strideX: number ): number;
4449

4550
/**
4651
* Computes the minimum value of a strided array using alternative indexing semantics.
4752
*
4853
* @param N - number of indexed elements
4954
* @param x - input array
50-
* @param stride - stride length
51-
* @param offset - starting index
55+
* @param strideX - stride length
56+
* @param offsetX - starting index
5257
* @returns minimum value
5358
*
5459
* @example
@@ -57,15 +62,15 @@ interface Routine {
5762
* var v = min.ndarray( x.length, x, 1, 0 );
5863
* // returns -2.0
5964
*/
60-
ndarray( N: number, x: NumericArray, stride: number, offset: number ): number;
65+
ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number;
6166
}
6267

6368
/**
6469
* Computes the minimum value of a strided array.
6570
*
6671
* @param N - number of indexed elements
6772
* @param x - input array
68-
* @param stride - stride length
73+
* @param strideX - stride length
6974
* @returns minimum value
7075
*
7176
* @example

lib/node_modules/@stdlib/stats/base/min/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 min = require( './index' );
2021

2122

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

2829
min( x.length, x, 1 ); // $ExpectType number
30+
min( 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 min = require( './index' );
8587
const x = new Float64Array( 10 );
8688

8789
min.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
min.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/min/examples/index.js

+4-10
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 min = 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 = min( x.length, x, 1 );

0 commit comments

Comments
 (0)