Skip to content

feat: add support for accessor arrays blas/base/gaxpy #7244

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

Merged
merged 12 commits into from
Jun 7, 2025
24 changes: 12 additions & 12 deletions lib/node_modules/@stdlib/blas/base/gaxpy/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
alpha: number
Constant.

x: Array<number>|TypedArray
Input array.
x: ArrayLikeObject
First input array.

strideX: integer
Index increment for `x`.

y: Array<number>|TypedArray
Output array.
y: ArrayLikeObject
Second input array.

strideY: integer
Index increment for `y`.

Returns
-------
y: Array<number>|TypedArray
Output array.
y: ArrayLikeObject
Input array `y`.

Examples
--------
Expand Down Expand Up @@ -76,17 +76,17 @@
alpha: number
Constant.

x: Array<number>|TypedArray
Input array.
x: ArrayLikeObject
First input array.

strideX: integer
Index increment for `x`.

offsetX: integer
Starting index for `x`.

y: Array<number>|TypedArray
Output array.
y: ArrayLikeObject
Second input array.

strideY: integer
Index increment for `y`.
Expand All @@ -96,8 +96,8 @@

Returns
-------
y: Array<number>|TypedArray
Output array.
y: ArrayLikeObject
Input array `y`.

Examples
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

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

/**
* Interface describing `gaxpy`.
Expand All @@ -44,7 +44,7 @@ interface Routine {
* gaxpy( x.length, 5.0, x, 1, y, 1 );
* // y => [ 6.0, 11.0, 16.0, 21.0, 26.0 ]
*/
( N: number, alpha: number, x: NumericArray, strideX: number, y: NumericArray, strideY: number ): NumericArray;
<T = unknown, U = unknown>( N: number, alpha: number, x: Collection<T>, strideX: number, y: Collection<U>, strideY: number ): Collection<T | U>;

/**
* Multiplies `x` by a constant `alpha` and adds the result to `y` using alternative indexing semantics.
Expand All @@ -66,7 +66,7 @@ interface Routine {
* gaxpy.ndarray( x.length, 5.0, x, 1, 0, y, 1, 0 );
* // y => [ 6.0, 11.0, 16.0, 21.0, 26.0 ]
*/
ndarray( N: number, alpha: number, x: NumericArray, strideX: number, offsetX: number, y: NumericArray, strideY: number, offsetY: number ): NumericArray;
ndarray<T = unknown, U = unknown>( N: number, alpha: number, x: Collection<T>, strideX: number, offsetX: number, y: Collection<T>, strideY: number, offsetY: number ): Collection<T | U>;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/blas/base/gaxpy/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var gaxpy = require( './../lib' );

var opts = {
'dtype': 'generic'
'dtype': 'float64'
};
var x = discreteUniform( 10, 0, 100, opts );
console.log( x );
Expand Down
88 changes: 88 additions & 0 deletions lib/node_modules/@stdlib/blas/base/gaxpy/lib/accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @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 //

/**
* Multiplies a vector `x` by a constant and adds the result to `y`.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {number} alpha - scalar
* @param {Object} x - input array object
* @param {Collection} x.data - input array data
* @param {Array<Function>} x.accessors - array element accessors
* @param {integer} strideX - `x` stride length
* @param {NonNegativeInteger} offsetX - starting `x` index
* @param {Object} y - output array object
* @param {Collection} y.data - output array data
* @param {Array<Function>} y.accessors - array element accessors
* @param {integer} strideY - `y` stride length
* @param {NonNegativeInteger} offsetY - starting `y` index
* @returns {Object} output array object
*
* @example
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
* var Float32Array = require( '@stdlib/array/float32' );
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
* var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
*
* var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
* var y = new Float32Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
*
* var z = gaxpy( x.length, 5.0, arraylike2object( toAccessorArray( x ) ), 1, 0, arraylike2object( toAccessorArray( y ) ), 1, 0 );
*
* // y => <Float32Array>[ 6.0, 11.0, 16.0, 21.0, 26.0. 31.0 ]
*/
function gaxpy( N, alpha, x, strideX, offsetX, y, strideY, offsetY ) {
var xbuf;
var ybuf;
var set;
var get;
var tmp;
var ix;
var iy;
var y0;
var i;

// Cache references to array data:
xbuf = x.data;
ybuf = y.data;

// Cache a reference to the element accessors:
get = x.accessors[ 0 ];
set = y.accessors[ 1 ];

ix = offsetX;
iy = offsetY;
for ( i = 0; i < N; i++ ) {
y0 = ybuf[ iy ];
tmp = alpha * ( get( xbuf, ix ) + y0 );
set( ybuf, iy, tmp );
ix += strideX;
iy += strideY;
}
return y;
}


// EXPORTS //

module.exports = gaxpy;
50 changes: 4 additions & 46 deletions lib/node_modules/@stdlib/blas/base/gaxpy/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

'use strict';

// VARIABLES //
// MODULES //

var M = 4;
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
var ndarray = require( './ndarray.js' );


// MAIN //
Expand All @@ -45,50 +46,7 @@
* // y => [ 6.0, 11.0, 16.0, 21.0, 26.0 ]
*/
function gaxpy( N, alpha, x, strideX, y, strideY ) {
var ix;
var iy;
var m;
var i;
if ( N <= 0 || alpha === 0.0 ) {
return y;
}
// Use unrolled loops if both strides are equal to `1`...
if ( strideX === 1 && strideY === 1 ) {
m = N % M;

// If we have a remainder, run a clean-up loop...
if ( m > 0 ) {
for ( i = 0; i < m; i++ ) {
y[ i ] += alpha * x[ i ];
}
}
if ( N < M ) {
return y;
}
for ( i = m; i < N; i += M ) {
y[ i ] += alpha * x[ i ];
y[ i+1 ] += alpha * x[ i+1 ];
y[ i+2 ] += alpha * x[ i+2 ];
y[ i+3 ] += alpha * x[ i+3 ];
}
return y;
}
if ( strideX < 0 ) {
ix = (1-N) * strideX;
} else {
ix = 0;
}
if ( strideY < 0 ) {
iy = (1-N) * strideY;
} else {
iy = 0;
}
for ( i = 0; i < N; i++ ) {
y[ iy ] += alpha * x[ ix ];
ix += strideX;
iy += strideY;
}
return y;
return ndarray( N, alpha, x, strideX, stride2offset( N, strideX ), y, strideY, stride2offset( N, strideY ) );

Check warning on line 49 in lib/node_modules/@stdlib/blas/base/gaxpy/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 113. Maximum allowed is 80
}


Expand Down
16 changes: 16 additions & 0 deletions lib/node_modules/@stdlib/blas/base/gaxpy/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 = 4;
Expand Down Expand Up @@ -49,11 +55,21 @@ var M = 4;
function gaxpy( N, alpha, x, strideX, offsetX, y, strideY, offsetY ) {
var ix;
var iy;
var ox;
var oy;
var m;
var i;
if ( N <= 0 || alpha === 0.0 ) {
return y;
}
ox = arraylike2object( x );
oy = arraylike2object( y );
if ( ox.accessorProtocol || oy.accessorProtocol ) {
accessors( N, ox, strideX, offsetX, oy, strideY, offsetY );
return oy.data;
}
ix = offsetX;
iy = offsetY;
ix = offsetX;
iy = offsetY;

Expand Down
Loading
Loading