From 312046e85019ed06d31fffafd17f269787958803 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 11 Jan 2025 14:29:53 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 1 + ext/base/grev/README.md | 25 +++--- ext/base/grev/benchmark/benchmark.js | 12 +-- ext/base/grev/benchmark/benchmark.ndarray.js | 14 ++-- ext/base/grev/docs/repl.txt | 33 ++++---- ext/base/grev/docs/types/index.d.ts | 12 +-- ext/base/grev/lib/accessors.js | 14 ++-- ext/base/grev/lib/main.js | 86 ++------------------ ext/base/grev/lib/ndarray.js | 22 ++--- 9 files changed, 64 insertions(+), 155 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8aa196087..2e2fda89b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1357,6 +1357,7 @@ A total of 8 people contributed to this release. Thank you to the following cont
+- [`ea9e425`](https://github.com/stdlib-js/stdlib/commit/ea9e42538dd6342bf5c02c2d7c68aa1eae3b15d6) - **refactor:** update `blas/ext/base/grev` to follow current project conventions [(#4659)](https://github.com/stdlib-js/stdlib/pull/4659) _(by Muhammad Haris)_ - [`53c0427`](https://github.com/stdlib-js/stdlib/commit/53c0427db5f49ab6209c55941ff15270a3569d44) - **refactor:** update `blas/ext/base/gcusumors` to follow current project conventions [(#4453)](https://github.com/stdlib-js/stdlib/pull/4453) _(by Muhammad Haris, Athan Reines)_ - [`dbb338e`](https://github.com/stdlib-js/stdlib/commit/dbb338e80336515e9f7ef602188985bbbe9b9010) - **docs:** update related packages sections [(#4690)](https://github.com/stdlib-js/stdlib/pull/4690) _(by stdlib-bot)_ - [`09b5945`](https://github.com/stdlib-js/stdlib/commit/09b5945ae5fedad2de3f3154c599868b8414967d) - **docs:** update namespace TypeScript declaration comments [(#4691)](https://github.com/stdlib-js/stdlib/pull/4691) _(by stdlib-bot, Philipp Burckhardt)_ diff --git a/ext/base/grev/README.md b/ext/base/grev/README.md index 66feb86ae..f8b8ef0d7 100644 --- a/ext/base/grev/README.md +++ b/ext/base/grev/README.md @@ -30,9 +30,9 @@ limitations under the License. var grev = require( '@stdlib/blas/ext/base/grev' ); ``` -#### grev( N, x, stride ) +#### grev( N, x, strideX ) -Reverses a strided array `x` in-place. +Reverses a strided array in-place. ```javascript var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; @@ -45,17 +45,14 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input array. -- **stride**: index increment. +- **strideX**: stride length. -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to reverse every other element +The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to reverse every other element: ```javascript -var floor = require( '@stdlib/math/base/special/floor' ); - var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; -var N = floor( x.length / 2 ); -grev( N, x, 2 ); +grev( 4, x, 2 ); // x => [ -1.0, 1.0, 4.0, -5.0, 3.0, 0.0, -2.0, -3.0 ] ``` @@ -63,23 +60,21 @@ 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' ); // Initial array... var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] ); // Create an offset view... var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length/2 ); // Reverse every other element... -grev( N, x1, 2 ); +grev( 3, x1, 2 ); // x0 => [ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] ``` -#### grev.ndarray( N, x, stride, offset ) +#### grev.ndarray( N, x, strideX, offsetX ) -Reverses a strided array `x` in-place using alternative indexing semantics. +Reverses a strided array in-place using alternative indexing semantics. ```javascript var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; @@ -90,9 +85,9 @@ grev.ndarray( x.length, x, 1, 0 ); The function has the following additional parameters: -- **offset**: starting index. +- **offsetX**: starting index. -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 access only the last three elements of `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 access only the last three elements: ```javascript var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; diff --git a/ext/base/grev/benchmark/benchmark.js b/ext/base/grev/benchmark/benchmark.js index c33cfd3f8..fb052d3c8 100644 --- a/ext/base/grev/benchmark/benchmark.js +++ b/ext/base/grev/benchmark/benchmark.js @@ -21,7 +21,9 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ).factory; +var zeros = require( '@stdlib/array/base/zeros' ); +var gfillBy = require( './../../../../ext/base/gfill-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; @@ -38,13 +40,7 @@ var grev = require( './../lib/main.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 = gfillBy( len, zeros( len ), 1, uniform( -100, 100 ) ); return benchmark; function benchmark( b ) { diff --git a/ext/base/grev/benchmark/benchmark.ndarray.js b/ext/base/grev/benchmark/benchmark.ndarray.js index 58cebefa6..6d58e35a9 100644 --- a/ext/base/grev/benchmark/benchmark.ndarray.js +++ b/ext/base/grev/benchmark/benchmark.ndarray.js @@ -21,11 +21,13 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +var uniform = require( '@stdlib/random/base/uniform' ).factory; +var zeros = require( '@stdlib/array/base/zeros' ); +var gfillBy = require( './../../../../ext/base/gfill-by' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var pkg = require( './../package.json' ).name; -var grev = require( './../lib/main.js' ).ndarray; +var grev = require( './../lib/ndarray.js' ); // FUNCTIONS // @@ -38,13 +40,7 @@ var grev = require( './../lib/main.js' ).ndarray; * @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 = gfillBy( len, zeros( len ), 1, uniform( -100, 100 ) ); return benchmark; function benchmark( b ) { diff --git a/ext/base/grev/docs/repl.txt b/ext/base/grev/docs/repl.txt index 0d0044d38..f636ca039 100644 --- a/ext/base/grev/docs/repl.txt +++ b/ext/base/grev/docs/repl.txt @@ -1,9 +1,9 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Reverses a strided array in-place. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use typed array views. @@ -18,8 +18,8 @@ x: ArrayLikeObject Input array. - stride: integer - Index increment for `x`. + strideX: integer + Stride length. Returns ------- @@ -33,27 +33,25 @@ > {{alias}}( x.length, x, 1 ) [ -3.0, -1.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}( N, x, 2 ) + > {{alias}}( 3, x, 2 ) [ 4.0, 1.0, 3.0, -5.0, -2.0, -1.0, -3.0 ] // Using view offsets: > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.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 ); - > {{alias}}( N, x1, 2 ) + > {{alias}}( 3, x1, 2 ) [ -6.0, 3.0, -4.0, 5.0, -2.0 ] > x0 [ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] -{{alias}}.ndarray( N, x, stride, offset ) +{{alias}}.ndarray( N, x, strideX, offsetX ) Reverses a strided array in-place 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 + buffer, the offset parameter supports indexing semantics based on a starting index. Parameters @@ -64,11 +62,11 @@ x: ArrayLikeObject Input array. - stride: integer - Index increment for `x`. + strideX: integer + Stride length. - offset: integer - Starting index of `x`. + offsetX: integer + Starting index. Returns ------- @@ -84,8 +82,7 @@ // Using an index offset: > x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ]; - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1 ) + > {{alias}}.ndarray( 3, x, 2, 1 ) [ 1.0, -6.0, 3.0, -4.0, 5.0, -2.0 ] See Also diff --git a/ext/base/grev/docs/types/index.d.ts b/ext/base/grev/docs/types/index.d.ts index 762ecf267..ffa60cddb 100644 --- a/ext/base/grev/docs/types/index.d.ts +++ b/ext/base/grev/docs/types/index.d.ts @@ -31,7 +31,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns `x` * * @example @@ -40,15 +40,15 @@ interface Routine { * grev( x.length, x, 1 ); * // x => [ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] */ - ( N: number, x: Collection, stride: number ): Collection; + ( N: number, x: Collection, strideX: number ): Collection; /** * Reverses a strided array in-place. using alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns `x` * * @example @@ -57,7 +57,7 @@ interface Routine { * grev.ndarray( x.length, x, 1, 0 ); * // x => [ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] */ - ndarray( N: number, x: Collection, stride: number, offset: number ): Collection; + ndarray( N: number, x: Collection, strideX: number, offsetX: number ): Collection; } /** @@ -65,7 +65,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns `x` * * @example diff --git a/ext/base/grev/lib/accessors.js b/ext/base/grev/lib/accessors.js index 386a2858e..88d17d206 100644 --- a/ext/base/grev/lib/accessors.js +++ b/ext/base/grev/lib/accessors.js @@ -34,8 +34,8 @@ var floor = require( '@stdlib/math/base/special/floor' ); * @param {Object} x - input array object * @param {Collection} x.data - input array data * @param {Array} x.accessors - array element accessors -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {Object} input array object * * @example @@ -63,7 +63,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); * var view = reinterpret64( x.data, 0 ); * // view => [ -1.0, -3.0, 4.0, 0.0, 3.0, -5.0, -2.0, 1.0 ] */ -function grev( N, x, stride, offset ) { +function grev( N, x, strideX, offsetX ) { var xbuf; var set; var get; @@ -81,14 +81,14 @@ function grev( N, x, stride, offset ) { set = x.accessors[ 1 ]; n = floor( N/2 ); - ix = offset; - iy = ix + ((N-1)*stride); + ix = offsetX; + iy = ix + ( ( N - 1 ) * strideX ); for ( i = 0; i < n; i++ ) { tmp = get( xbuf, ix ); set( xbuf, ix, get( xbuf, iy ) ); set( xbuf, iy, tmp ); - ix += stride; - iy -= stride; + ix += strideX; + iy -= strideX; } return x; } diff --git a/ext/base/grev/lib/main.js b/ext/base/grev/lib/main.js index 1f5fa21f8..4b7815880 100644 --- a/ext/base/grev/lib/main.js +++ b/ext/base/grev/lib/main.js @@ -20,14 +20,8 @@ // MODULES // -var floor = require( '@stdlib/math/base/special/floor' ); -var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); -var accessors = require( './accessors.js' ); - - -// VARIABLES // - -var M = 3; +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -37,7 +31,7 @@ var M = 3; * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - index increment +* @param {integer} strideX - stride length * @returns {NumericArray} input array * * @example @@ -46,78 +40,8 @@ var M = 3; * grev( x.length, x, 1 ); * // x => [ -3.0, -1.0, 0.0, 4.0, -5.0, 3.0, 1.0, -2.0 ] */ -function grev( N, x, stride ) { - var tmp; - var ix; - var iy; - var o; - var m; - var n; - var i; - - if ( N <= 0 ) { - return x; - } - o = arraylike2object( x ); - if ( o.accessorProtocol ) { - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - accessors( N, o, stride, ix ); - return o.data; - } - n = floor( N/2 ); - - // Use loop unrolling if the stride is equal to `1`... - if ( stride === 1 ) { - m = n % M; - iy = N - 1; - - // If we have a remainder, run a clean-up loop... - if ( m > 0 ) { - for ( ix = 0; ix < m; ix++ ) { - tmp = x[ ix ]; - x[ ix ] = x[ iy ]; - x[ iy ] = tmp; - iy -= 1; - } - } - if ( n < M ) { - return x; - } - for ( ix = m; ix < n; ix += M ) { - tmp = x[ ix ]; - x[ ix ] = x[ iy ]; - x[ iy ] = tmp; - - tmp = x[ ix+1 ]; - x[ ix+1 ] = x[ iy-1 ]; - x[ iy-1 ] = tmp; - - tmp = x[ ix+2 ]; - x[ ix+2 ] = x[ iy-2 ]; - x[ iy-2 ] = tmp; - - iy -= M; - } - return x; - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - iy = ix + ((N-1)*stride); - for ( i = 0; i < n; i++ ) { - tmp = x[ ix ]; - x[ ix ] = x[ iy ]; - x[ iy ] = tmp; - ix += stride; - iy -= stride; - } - return x; +function grev( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/ext/base/grev/lib/ndarray.js b/ext/base/grev/lib/ndarray.js index f798489b3..e1330fa89 100644 --- a/ext/base/grev/lib/ndarray.js +++ b/ext/base/grev/lib/ndarray.js @@ -37,8 +37,8 @@ var M = 3; * * @param {PositiveInteger} N - number of indexed elements * @param {NumericArray} x - input array -* @param {integer} stride - index increment -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {NumericArray} input array * * @example @@ -47,7 +47,7 @@ var M = 3; * grev( 3, x, 1, x.length-3 ); * // x => [ 1.0, -2.0, 3.0, -6.0, 5.0, -4.0 ] */ -function grev( N, x, stride, offset ) { +function grev( N, x, strideX, offsetX ) { var tmp; var ix; var iy; @@ -61,14 +61,14 @@ function grev( N, x, stride, offset ) { } o = arraylike2object( x ); if ( o.accessorProtocol ) { - accessors( N, o, stride, offset ); + accessors( N, o, strideX, offsetX ); return o.data; } n = floor( N/2 ); - ix = offset; + ix = offsetX; // Use loop unrolling if the stride is equal to `1`... - if ( stride === 1 ) { + if ( strideX === 1 ) { m = n % M; iy = ix + N - 1; @@ -78,8 +78,8 @@ function grev( N, x, stride, offset ) { tmp = x[ ix ]; x[ ix ] = x[ iy ]; x[ iy ] = tmp; - ix += stride; - iy -= stride; + ix += strideX; + iy -= strideX; } } if ( n < M ) { @@ -103,13 +103,13 @@ function grev( N, x, stride, offset ) { } return x; } - iy = ix + ((N-1)*stride); + iy = ix + ( ( N - 1 ) * strideX ); for ( i = 0; i < n; i++ ) { tmp = x[ ix ]; x[ ix ] = x[ iy ]; x[ iy ] = tmp; - ix += stride; - iy -= stride; + ix += strideX; + iy -= strideX; } return x; }