Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/gapx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

</section>
Expand Down Expand Up @@ -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

<!-- <related-links> -->

[@stdlib/blas/ext/base/dapx]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dapx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

/// <reference types="@stdlib/types"/>

import { NumericArray } from '@stdlib/types/array';
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';

/**
* Input array.
*/
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;

/**
* Interface describing `gapx`.
Expand All @@ -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: InputArray, strideX: number ): InputArray;

/**
* Adds a scalar constant to each element in a strided array using alternative indexing semantics.
Expand All @@ -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: InputArray, strideX: number, offsetX: number ): InputArray;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import AccessorArray = require( '@stdlib/array/base/accessor' );
import gapx = require( './index' );


Expand All @@ -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...
Expand Down Expand Up @@ -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...
Expand Down
70 changes: 70 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/gapx/lib/accessors.js
Original file line number Diff line number Diff line change
@@ -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<Function>} 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;
11 changes: 11 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/gapx/lib/ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

'use strict';

// MODULES //

var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var accessors = require( './accessors.js' );


// VARIABLES //

var M = 5;
Expand All @@ -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`...
Expand Down
86 changes: 86 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/gapx/test/test.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading