diff --git a/lib/node_modules/@stdlib/blas/ext/base/gapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/gapx/README.md index 86643fe3af53..f23c48777fc3 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gapx/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/gapx/README.md @@ -106,6 +106,7 @@ gapx.ndarray( 3, 5.0, x, 1, x.length-3 ); ## Notes - If `N <= 0`, both functions return `x` unchanged. +- 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]) - Depending on the environment, the typed versions ([`dapx`][@stdlib/blas/ext/base/dapx], [`sapx`][@stdlib/blas/ext/base/sapx], etc.) are likely to be significantly more performant. @@ -158,6 +159,8 @@ console.log( x ); [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 + [@stdlib/blas/ext/base/dapx]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dapx diff --git a/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/index.d.ts index 1220a61887b9..9481bf4ce6d2 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/index.d.ts @@ -20,7 +20,12 @@ /// -import { NumericArray } from '@stdlib/types/array'; +import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; /** * Interface describing `gapx`. @@ -41,7 +46,7 @@ interface Routine { * gapx( x.length, 5.0, x, 1 ); * // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] */ - ( N: number, alpha: number, x: NumericArray, strideX: number ): NumericArray; + ( N: number, alpha: number, x: T, strideX: number ): T; /** * Adds a scalar constant to each element in a strided array using alternative indexing semantics. @@ -59,7 +64,7 @@ interface Routine { * gapx.ndarray( x.length, 5.0, x, 1, 0 ); * // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] */ - ndarray( N: number, alpha: number, x: NumericArray, strideX: number, offsetX: number ): NumericArray; + ndarray( N: number, alpha: number, x: T, strideX: number, offsetX: number ): T; } /** diff --git a/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/test.ts index 4358b591b9dd..f50aa98372be 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/gapx/docs/types/test.ts @@ -16,6 +16,7 @@ * limitations under the License. */ +import AccessorArray = require( '@stdlib/array/base/accessor' ); import gapx = require( './index' ); @@ -25,7 +26,8 @@ import gapx = require( './index' ); { const x = new Float64Array( 10 ); - gapx( x.length, 5.0, x, 1 ); // $ExpectType NumericArray + gapx( x.length, 5.0, x, 1 ); // $ExpectType Float64Array + gapx( x.length, 5.0, new AccessorArray( x ), 1 ); // $ExpectType AccessorArray } // The compiler throws an error if the function is provided a first argument which is not a number... @@ -100,7 +102,8 @@ import gapx = require( './index' ); { const x = new Float64Array( 10 ); - gapx.ndarray( x.length, 5.0, x, 1, 0 ); // $ExpectType NumericArray + gapx.ndarray( x.length, 5.0, x, 1, 0 ); // $ExpectType Float64Array + gapx.ndarray( x.length, 5.0, new AccessorArray( x ), 1, 0 ); // $ExpectType AccessorArray } // The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... diff --git a/lib/node_modules/@stdlib/blas/ext/base/gapx/lib/accessors.js b/lib/node_modules/@stdlib/blas/ext/base/gapx/lib/accessors.js new file mode 100644 index 000000000000..7240fc540052 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/gapx/lib/accessors.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Adds a scalar constant to each element in a strided array. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {number} alpha - scalar constant +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index +* @returns {Object} input array object +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = gapx( 4, 5.0, arraylike2object( x ), 2, 1 ); +* // returns {...} +*/ +function gapx( N, alpha, x, strideX, offsetX ) { + var xbuf; + var get; + var set; + var ix; + var i; + + // Cache reference to array data: + xbuf = x.data; + + // Cache reference to the element accessors: + get = x.accessors[ 0 ]; + set = x.accessors[ 1 ]; + + ix = offsetX; + for ( i = 0; i < N; i++ ) { + set( xbuf, ix, alpha + get( xbuf, ix ) ); + ix += strideX; + } + return x; +} + + +// EXPORTS // + +module.exports = gapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gapx/lib/ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gapx/lib/ndarray.js index f63d2cc6b5c8..24259ad5de6e 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gapx/lib/ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gapx/lib/ndarray.js @@ -18,6 +18,12 @@ 'use strict'; +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + // VARIABLES // var M = 5; @@ -44,11 +50,16 @@ var M = 5; function gapx( N, alpha, x, strideX, offsetX ) { var ix; var m; + var o; var i; if ( N <= 0 || alpha === 0.0 ) { return x; } + o = arraylike2object( x ); + if ( o.accessorProtocol ) { + return accessors( N, alpha, o, strideX, offsetX ); + } ix = offsetX; // Use loop unrolling if the stride is equal to `1`... diff --git a/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.main.js index 7154b0cd1a77..4fe212cf1862 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.main.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var Float64Array = require( '@stdlib/array/float64' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var gapx = require( './../lib' ); @@ -75,6 +76,43 @@ tape( 'the function adds a constant to each element of a strided array', functio t.end(); }); +tape( 'the function adds a constant to each element of a strided array (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 4.0, + 2.0, + -3.0, + 5.0, + -1.0, + 2.0, + -5.0, + 6.0 + ]; + expected = [ + 9.0, + 7.0, + 2.0, + 10.0, + 4.0, + 7.0, + 0.0, + 11.0 + ]; + + gapx( x.length, 5.0, toAccessorArray( x ), 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 1.0, 2.0 ]; + expected = [ 6.0, 7.0 ]; + + gapx( x.length, 5.0, toAccessorArray( x ), 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a reference to the input array', function test( t ) { var out; var x; @@ -139,6 +177,30 @@ tape( 'the function supports specifying a stride', function test( t ) { t.end(); }); +tape( 'the function supports specifying a stride (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 7.0, // 0 + -3.0, + 0.0, // 1 + 7.0, + 11.0 // 2 + ]; + + gapx( 3, 5.0, toAccessorArray( x ), 2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports specifying a negative stride', function test( t ) { var expected; var x; @@ -163,6 +225,30 @@ tape( 'the function supports specifying a negative stride', function test( t ) { t.end(); }); +tape( 'the function supports specifying a negative stride (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + 7.0, // 2 + -3.0, + 0.0, // 1 + 7.0, + 11.0 // 0 + ]; + + gapx( 3, 5.0, toAccessorArray( x ), -2 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports view offsets', function test( t ) { var expected; var x0; diff --git a/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.ndarray.js b/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.ndarray.js index b1cf6dc9774e..52f3541e5f62 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.ndarray.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var Float64Array = require( '@stdlib/array/float64' ); +var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); var gapx = require( './../lib/ndarray.js' ); @@ -75,6 +76,43 @@ tape( 'the function adds a constant to each strided array element', function tes t.end(); }); +tape( 'the function adds a constant to each strided array element (accessors)', function test( t ) { + var expected; + var x; + + x = [ + 4.0, + 2.0, + -3.0, + 5.0, + -1.0, + 2.0, + -5.0, + 6.0 + ]; + expected = [ + 9.0, + 7.0, + 2.0, + 10.0, + 4.0, + 7.0, + 0.0, + 11.0 + ]; + + gapx( x.length, 5.0, toAccessorArray( x ), 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + x = [ 1.0, 2.0 ]; + expected = [ 6.0, 7.0 ]; + + gapx( x.length, 5.0, toAccessorArray( x ), 1, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a reference to the input array', function test( t ) { var out; var x; @@ -139,6 +177,30 @@ tape( 'the function supports specifying a stride', function test( t ) { t.end(); }); +tape( 'the function supports specifying a stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]; + expected = [ + 7.0, // 0 + -3.0, + 0.0, // 1 + 7.0, + 11.0 // 2 + ]; + + gapx( 3, 5.0, toAccessorArray( x ), 2, 0 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports specifying a negative stride', function test( t ) { var expected; var x; @@ -163,6 +225,30 @@ tape( 'the function supports specifying a negative stride', function test( t ) { t.end(); }); +tape( 'the function supports specifying a negative stride', function test( t ) { + var expected; + var x; + + x = [ + 2.0, // 2 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 0 + ]; + expected = [ + 7.0, // 2 + -3.0, + 0.0, // 1 + 7.0, + 11.0 // 0 + ]; + + gapx( 3, 5.0, toAccessorArray( x ), -2, x.length-1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + tape( 'the function supports an offset parameter', function test( t ) { var expected; var x; @@ -189,6 +275,32 @@ tape( 'the function supports an offset parameter', function test( t ) { t.end(); }); +tape( 'the function supports an offset parameter', function test( t ) { + var expected; + var x; + + x = [ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]; + expected = [ + 1.0, + 7.0, // 0 + 3.0, + 9.0, // 1 + 5.0, + 11.0 // 2 + ]; + + gapx( 3, 5.0, toAccessorArray( x ), 2, 1 ); + t.deepEqual( x, expected, 'returns expected value' ); + t.end(); +}); + tape( 'if `stride` is equal to `1`, the function efficiently adds a constant to each element in a strided array', function test( t ) { var expected; var alpha;