Skip to content

Commit aec0634

Browse files
authored
feat: add stats/array/rangeabs
PR-URL: stdlib-js#10227 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 71f75c9 commit aec0634

File tree

10 files changed

+737
-0
lines changed

10 files changed

+737
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
# rangeabs
22+
23+
> Calculate the [range][range] of absolute values of an array.
24+
25+
<section class="intro">
26+
27+
The [**range**][range] is defined as the difference between the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var rangeabs = require( '@stdlib/stats/array/rangeabs' );
39+
```
40+
41+
#### rangeabs( x )
42+
43+
Computes the [range][range] of absolute values of an array.
44+
45+
```javascript
46+
var x = [ 1.0, -2.0, 2.0 ];
47+
48+
var v = rangeabs( x );
49+
// returns 1.0
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **x**: input array.
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<section class="notes">
61+
62+
## Notes
63+
64+
- If provided an empty array, the function returns `NaN`.
65+
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
79+
var rangeabs = require( '@stdlib/stats/array/rangeabs' );
80+
81+
var x = discreteUniform( 10, -50, 50, {
82+
'dtype': 'float64'
83+
});
84+
console.log( x );
85+
86+
var v = rangeabs( x );
87+
console.log( v );
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
95+
96+
<section class="related">
97+
98+
</section>
99+
100+
<!-- /.related -->
101+
102+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="links">
105+
106+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
107+
108+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
109+
110+
</section>
111+
112+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var rangeabs = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = uniform( len, -10, 10, options );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var v;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
v = rangeabs( x );
65+
if ( isnan( v ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( v ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( format( '%s:len=%d', pkg, len ), f );
100+
}
101+
}
102+
103+
main();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( x )
3+
Computes the range of absolute values of an array.
4+
5+
If provided an empty array, the function returns `NaN`.
6+
7+
Parameters
8+
----------
9+
x: Array<number>|TypedArray
10+
Input array.
11+
12+
Returns
13+
-------
14+
out: number
15+
Range.
16+
17+
Examples
18+
--------
19+
> var x = [ 1.0, -2.0, 2.0 ];
20+
> {{alias}}( x )
21+
1.0
22+
23+
See Also
24+
--------
25+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
30+
/**
31+
* Computes the range of absolute values of an array.
32+
*
33+
* @param x - input array
34+
* @returns range
35+
*
36+
* @example
37+
* var x = [ 1.0, -2.0, 2.0 ];
38+
*
39+
* var v = rangeabs( x );
40+
* // returns 1.0
41+
*/
42+
declare function rangeabs( x: InputArray ): number;
43+
44+
45+
// EXPORTS //
46+
47+
export = rangeabs;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import AccessorArray = require( '@stdlib/array/base/accessor' );
20+
import rangeabs = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a number...
26+
{
27+
const x = new Float64Array( 10 );
28+
29+
rangeabs( x ); // $ExpectType number
30+
rangeabs( new AccessorArray( x ) ); // $ExpectType number
31+
}
32+
33+
// The compiler throws an error if the function is provided a first argument which is not a numeric array...
34+
{
35+
rangeabs( 10 ); // $ExpectError
36+
rangeabs( '10' ); // $ExpectError
37+
rangeabs( true ); // $ExpectError
38+
rangeabs( false ); // $ExpectError
39+
rangeabs( null ); // $ExpectError
40+
rangeabs( undefined ); // $ExpectError
41+
rangeabs( {} ); // $ExpectError
42+
rangeabs( ( x: number ): number => x ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided an unsupported number of arguments...
46+
{
47+
const x = new Float64Array( 10 );
48+
49+
rangeabs(); // $ExpectError
50+
rangeabs( x, {} ); // $ExpectError
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var rangeabs = require( './../lib' );
23+
24+
var x = discreteUniform( 10, -50, 50, {
25+
'dtype': 'float64'
26+
});
27+
console.log( x );
28+
29+
var v = rangeabs( x );
30+
console.log( v );

0 commit comments

Comments
 (0)