-
-
Notifications
You must be signed in to change notification settings - Fork 805
feat: add protocol support to stats/base/range
#5779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 4 commits
c2ce045
ccc1cd0
d74bbfd
5d543ed
165db04
ac356d1
34f7be0
888de28
7bbc2db
dca37eb
a9cc5fd
5bad41c
81e2b19
73a5b99
df60874
3fe3a45
ef9646e
a4b6e5a
74aaf56
9d2bcd9
0bc61fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -38,7 +38,7 @@ The [**range**][range] is defined as the difference between the maximum and mini | |||||||||||
var range = require( '@stdlib/stats/base/range' ); | ||||||||||||
``` | ||||||||||||
|
||||||||||||
#### range( N, x, stride ) | ||||||||||||
#### range( N, x, strideX ) | ||||||||||||
|
||||||||||||
Computes the [range][range] of a strided array `x`. | ||||||||||||
|
||||||||||||
|
@@ -54,17 +54,14 @@ The function has the following parameters: | |||||||||||
|
||||||||||||
- **N**: number of indexed elements. | ||||||||||||
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. | ||||||||||||
- **stride**: index increment for `x`. | ||||||||||||
- **strideX**: stride length for `x`. | ||||||||||||
|
||||||||||||
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`, | ||||||||||||
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`, | ||||||||||||
|
||||||||||||
```javascript | ||||||||||||
var floor = require( '@stdlib/math/base/special/floor' ); | ||||||||||||
|
||||||||||||
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; | ||||||||||||
var N = floor( x.length / 2 ); | ||||||||||||
|
||||||||||||
var v = range( N, x, 2 ); | ||||||||||||
var v = range( 4, x, 2 ); | ||||||||||||
// returns 6.0 | ||||||||||||
``` | ||||||||||||
|
||||||||||||
|
@@ -74,18 +71,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [ | |||||||||||
|
||||||||||||
```javascript | ||||||||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||||||||
var floor = require( '@stdlib/math/base/special/floor' ); | ||||||||||||
|
||||||||||||
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); | ||||||||||||
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||||||||
|
||||||||||||
var N = floor( x0.length / 2 ); | ||||||||||||
|
||||||||||||
var v = range( N, x1, 2 ); | ||||||||||||
var v = range( 4, x1, 2 ); | ||||||||||||
// returns 6.0 | ||||||||||||
``` | ||||||||||||
|
||||||||||||
#### range.ndarray( N, x, stride, offset ) | ||||||||||||
#### range.ndarray( N, x, strideX, offsetX ) | ||||||||||||
|
||||||||||||
Computes the [range][range] of a strided array using alternative indexing semantics. | ||||||||||||
|
||||||||||||
|
@@ -99,17 +93,14 @@ var v = range.ndarray( N, x, 1, 0 ); | |||||||||||
|
||||||||||||
The function has the following additional parameters: | ||||||||||||
|
||||||||||||
- **offset**: starting index for `x`. | ||||||||||||
- **offsetX**: starting index for `x`. | ||||||||||||
|
||||||||||||
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 | ||||||||||||
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 | ||||||||||||
|
||||||||||||
```javascript | ||||||||||||
var floor = require( '@stdlib/math/base/special/floor' ); | ||||||||||||
|
||||||||||||
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; | ||||||||||||
var N = floor( x.length / 2 ); | ||||||||||||
|
||||||||||||
var v = range.ndarray( N, x, 2, 1 ); | ||||||||||||
var v = range.ndarray( 4, x, 2, 1 ); | ||||||||||||
// returns 6.0 | ||||||||||||
``` | ||||||||||||
|
||||||||||||
|
@@ -123,6 +114,7 @@ var v = range.ndarray( N, x, 2, 1 ); | |||||||||||
|
||||||||||||
- If `N <= 0`, both functions return `NaN`. | ||||||||||||
- Depending on the environment, the typed versions ([`drange`][@stdlib/stats/base/drange], [`srange`][@stdlib/stats/base/srange], etc.) are likely to be significantly more performant. | ||||||||||||
- 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]). | ||||||||||||
|
||||||||||||
</section> | ||||||||||||
|
||||||||||||
|
@@ -137,16 +129,14 @@ var v = range.ndarray( N, x, 2, 1 ); | |||||||||||
```javascript | ||||||||||||
var randu = require( '@stdlib/random/base/randu' ); | ||||||||||||
var round = require( '@stdlib/math/base/special/round' ); | ||||||||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||||||||
var linspace = require( '@stdlib/array/base/linspace' ); | ||||||||||||
var range = require( '@stdlib/stats/base/range' ); | ||||||||||||
|
||||||||||||
var x; | ||||||||||||
var i; | ||||||||||||
|
||||||||||||
x = new Float64Array( 10 ); | ||||||||||||
for ( i = 0; i < x.length; i++ ) { | ||||||||||||
x[ i ] = round( (randu()*100.0) - 50.0 ); | ||||||||||||
} | ||||||||||||
x = linspace(-50, 50, 10); | ||||||||||||
x = x.map(round); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will then become
Suggested change
|
||||||||||||
console.log( x ); | ||||||||||||
|
||||||||||||
var v = range( x.length, x, 1 ); | ||||||||||||
|
@@ -185,6 +175,8 @@ console.log( v ); | |||||||||||
|
||||||||||||
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray | ||||||||||||
|
||||||||||||
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor | ||||||||||||
|
||||||||||||
<!-- <related-links> --> | ||||||||||||
|
||||||||||||
[@stdlib/stats/base/drange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/drange | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,8 @@ | |
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var linspace = require( '@stdlib/array/base/linspace' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var pkg = require( './../package.json' ).name; | ||
var range = require( './../lib/range.js' ); | ||
|
@@ -38,13 +38,7 @@ var range = require( './../lib/range.js' ); | |
* @returns {Function} benchmark function | ||
*/ | ||
function createBenchmark( len ) { | ||
var x; | ||
var i; | ||
|
||
x = []; | ||
for ( i = 0; i < len; i++ ) { | ||
x.push( ( randu()*20.0 ) - 10.0 ); | ||
} | ||
var x = linspace( 0.0, len, len ); | ||
return benchmark; | ||
|
||
function benchmark( b ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
|
||
var bench = require( '@stdlib/bench' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var linspace = require( '@stdlib/array/linspace' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment. |
||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pow = require( '@stdlib/math/base/special/pow' ); | ||
var pkg = require( './../package.json' ).name; | ||
|
@@ -39,12 +40,7 @@ var range = require( './../lib/ndarray.js' ); | |
*/ | ||
function createBenchmark( len ) { | ||
var x; | ||
var i; | ||
|
||
x = []; | ||
for ( i = 0; i < len; i++ ) { | ||
x.push( ( randu()*20.0 ) - 10.0 ); | ||
} | ||
var x = linspace( 0.0, len, len ); | ||
return benchmark; | ||
|
||
function benchmark( b ) { | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,92 +1,124 @@ | ||||||||
{{alias}}( N, x, strideX ) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
|
||||||||
{{alias}}( N, x, stride ) | ||||||||
Computes the range of a strided array. | ||||||||
|
||||||||
The `N` and `stride` parameters determine which elements in `x` are accessed | ||||||||
at runtime. | ||||||||
|
||||||||
Indexing is relative to the first index. To introduce an offset, use a typed | ||||||||
array view. | ||||||||
The `N` and stride parameters determine which elements in the strided | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look properly wrapped at 80 chars. |
||||||||
array are accessed at runtime. | ||||||||
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
Indexing is relative to the first index. To introduce an offset, use a | ||||||||
typed array view. | ||||||||
|
||||||||
|
||||||||
If `N <= 0`, the function returns `NaN`. | ||||||||
|
||||||||
|
||||||||
Parameters | ||||||||
---------- | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you adding all these blank lines? Please remove and follow our REPL text style guide. |
||||||||
|
||||||||
N: integer | ||||||||
Number of indexed elements. | ||||||||
|
||||||||
|
||||||||
x: Array<number>|TypedArray | ||||||||
Input array. | ||||||||
|
||||||||
stride: integer | ||||||||
Index increment. | ||||||||
|
||||||||
strideX: integer | ||||||||
Stride length. | ||||||||
|
||||||||
|
||||||||
Returns | ||||||||
------- | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||||||||
|
||||||||
out: number | ||||||||
Range. | ||||||||
|
||||||||
|
||||||||
Examples | ||||||||
-------- | ||||||||
|
||||||||
|
||||||||
// Standard Usage: | ||||||||
> var x = [ 1.0, -2.0, 2.0 ]; | ||||||||
> {{alias}}( x.length, x, 1 ) | ||||||||
4.0 | ||||||||
|
||||||||
// Using `N` and `stride` parameters: | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to go through and remove all these stray lines. Please study other repl.txt files. |
||||||||
// Using `N` and stride parameters: | ||||||||
> x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; | ||||||||
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); | ||||||||
> var stride = 2; | ||||||||
> {{alias}}( N, x, stride ) | ||||||||
> {{alias}}( 3, x, stride ) | ||||||||
4.0 | ||||||||
|
||||||||
|
||||||||
// Using view offsets: | ||||||||
> var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); | ||||||||
> var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); | ||||||||
> N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); | ||||||||
> stride = 2; | ||||||||
> {{alias}}( N, x1, stride ) | ||||||||
> {{alias}}( 3, x1, stride ) | ||||||||
4.0 | ||||||||
|
||||||||
{{alias}}.ndarray( N, x, stride, offset ) | ||||||||
Computes the range of a strided array using alternative indexing semantics. | ||||||||
|
||||||||
{{alias}}.ndarray( N, x, strideX, offsetX ) | ||||||||
|
||||||||
|
||||||||
Computes the range of a strided array using alternative indexing | ||||||||
semantics. | ||||||||
|
||||||||
|
||||||||
While typed array views mandate a view offset based on the underlying | ||||||||
buffer, the `offset` parameter supports indexing semantics based on a | ||||||||
starting index. | ||||||||
|
||||||||
|
||||||||
Parameters | ||||||||
---------- | ||||||||
|
||||||||
|
||||||||
N: integer | ||||||||
Number of indexed elements. | ||||||||
|
||||||||
|
||||||||
x: Array<number>|TypedArray | ||||||||
Input array. | ||||||||
|
||||||||
stride: integer | ||||||||
Index increment. | ||||||||
|
||||||||
offset: integer | ||||||||
strideX: integer | ||||||||
Stride length. | ||||||||
|
||||||||
|
||||||||
offsetX: integer | ||||||||
Starting index. | ||||||||
|
||||||||
|
||||||||
Returns | ||||||||
------- | ||||||||
|
||||||||
|
||||||||
out: number | ||||||||
Range. | ||||||||
|
||||||||
|
||||||||
Examples | ||||||||
-------- | ||||||||
|
||||||||
|
||||||||
// Standard Usage: | ||||||||
> var x = [ 1.0, -2.0, 2.0 ]; | ||||||||
> {{alias}}.ndarray( x.length, x, 1, 0 ) | ||||||||
4.0 | ||||||||
|
||||||||
|
||||||||
// Using offset parameter: | ||||||||
> var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ]; | ||||||||
> var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); | ||||||||
> {{alias}}.ndarray( N, x, 2, 1 ) | ||||||||
> {{alias}}.ndarray( 3, x, 2, 1 ) | ||||||||
4.0 | ||||||||
|
||||||||
See Also | ||||||||
-------- | ||||||||
|
||||||||
See Also | ||||||||
-------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
@stdlib/random/array/discrete-uniform
.