Skip to content

Commit 1e7f5ba

Browse files
committed
feat: add support for accessor arrays blas/base/gaxpy
1 parent 71f1df4 commit 1e7f5ba

File tree

8 files changed

+531
-60
lines changed

8 files changed

+531
-60
lines changed

lib/node_modules/@stdlib/blas/base/gaxpy/docs/repl.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818
alpha: number
1919
Constant.
2020

21-
x: Array<number>|TypedArray
22-
Input array.
21+
x: ArrayLikeObject
22+
First input array.
2323

2424
strideX: integer
2525
Index increment for `x`.
2626

27-
y: Array<number>|TypedArray
28-
Output array.
27+
y: ArrayLikeObject
28+
Second input array.
2929

3030
strideY: integer
3131
Index increment for `y`.
3232

3333
Returns
3434
-------
35-
y: Array<number>|TypedArray
36-
Output array.
35+
y: ArrayLikeObject
36+
Input array `y`.
3737

3838
Examples
3939
--------
@@ -76,17 +76,17 @@
7676
alpha: number
7777
Constant.
7878

79-
x: Array<number>|TypedArray
80-
Input array.
79+
x: ArrayLikeObject
80+
First input array.
8181

8282
strideX: integer
8383
Index increment for `x`.
8484

8585
offsetX: integer
8686
Starting index for `x`.
8787

88-
y: Array<number>|TypedArray
89-
Output array.
88+
y: ArrayLikeObject
89+
Second input array.
9090

9191
strideY: integer
9292
Index increment for `y`.
@@ -96,8 +96,8 @@
9696

9797
Returns
9898
-------
99-
y: Array<number>|TypedArray
100-
Output array.
99+
y: ArrayLikeObject
100+
Input array `y`.
101101

102102
Examples
103103
--------

lib/node_modules/@stdlib/blas/base/gaxpy/docs/types/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
import { NumericArray } from '@stdlib/types/array';
23+
import { Collection } from '@stdlib/types/array';
2424

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

4949
/**
5050
* Multiplies `x` by a constant `alpha` and adds the result to `y` using alternative indexing semantics.
@@ -66,7 +66,7 @@ interface Routine {
6666
* gaxpy.ndarray( x.length, 5.0, x, 1, 0, y, 1, 0 );
6767
* // y => [ 6.0, 11.0, 16.0, 21.0, 26.0 ]
6868
*/
69-
ndarray( N: number, alpha: number, x: NumericArray, strideX: number, offsetX: number, y: NumericArray, strideY: number, offsetY: number ): NumericArray;
69+
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>;
7070
}
7171

7272
/**

lib/node_modules/@stdlib/blas/base/gaxpy/examples/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2222
var gaxpy = require( './../lib' );
2323

2424
var opts = {
25-
'dtype': 'generic'
25+
'dtype': 'float64'
2626
};
2727
var x = discreteUniform( 10, 0, 100, opts );
2828
console.log( x );
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
22+
// MAIN //
23+
24+
/**
25+
* Multiplies a vector `x` by a constant and adds the result to `y`.
26+
*
27+
* @param {PositiveInteger} N - number of indexed elements
28+
* @param {number} alpha - scalar
29+
* @param {Object} x - input array object
30+
* @param {Collection} x.data - input array data
31+
* @param {Array<Function>} x.accessors - array element accessors
32+
* @param {integer} strideX - `x` stride length
33+
* @param {NonNegativeInteger} offsetX - starting `x` index
34+
* @param {Object} y - output array object
35+
* @param {Collection} y.data - output array data
36+
* @param {Array<Function>} y.accessors - array element accessors
37+
* @param {integer} strideY - `y` stride length
38+
* @param {NonNegativeInteger} offsetY - starting `y` index
39+
* @returns {Object} output array object
40+
*
41+
* @example
42+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
43+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
44+
* var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
45+
*
46+
* function setter( data, idx, value ) {
47+
* data.set( value, idx );
48+
* }
49+
*
50+
* function getter( data, idx ) {
51+
* return data.get( idx );
52+
* }
53+
*
54+
* var x = {
55+
* 'data': new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ),
56+
* 'accessors': [ getter, setter ]
57+
* };
58+
*
59+
* var y = {
60+
* 'data': new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] ),
61+
* 'accessors': [ getter, setter ]
62+
* };
63+
*
64+
* gcopy( x.data.length, 5.0, x, 1, 0, y, 1, 0 );
65+
*
66+
* var view = reinterpret64( y.data, 0 );
67+
* // view => <Float32Array>[ 6.0, 11.0, 16.0, 21.0, 26.0 ]
68+
*/
69+
function gaxpy( N, alpha, x, strideX, offsetX, y, strideY, offsetY ) {
70+
var xbuf;
71+
var ybuf;
72+
var set;
73+
var get;
74+
var tmp;
75+
var ix;
76+
var iy;
77+
var y0;
78+
var i;
79+
80+
// Cache references to array data:
81+
xbuf = x.data;
82+
ybuf = y.data;
83+
84+
// Cache a reference to the element accessors:
85+
get = x.accessors[ 0 ];
86+
set = y.accessors[ 1 ];
87+
88+
ix = offsetX;
89+
iy = offsetY;
90+
for ( i = 0; i < N; i++ ) {
91+
y0 = ybuf[ iy ];
92+
tmp = alpha * get( xbuf, ix ) + y0;
93+
set( ybuf, iy, tmp );
94+
ix += strideX;
95+
iy += strideY;
96+
}
97+
return y;
98+
}
99+
100+
101+
// EXPORTS //
102+
103+
module.exports = gaxpy;

lib/node_modules/@stdlib/blas/base/gaxpy/lib/main.js

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
25+
26+
2127
// VARIABLES //
2228

2329
var M = 4;
@@ -45,50 +51,7 @@ var M = 4;
4551
* // y => [ 6.0, 11.0, 16.0, 21.0, 26.0 ]
4652
*/
4753
function gaxpy( N, alpha, x, strideX, y, strideY ) {
48-
var ix;
49-
var iy;
50-
var m;
51-
var i;
52-
if ( N <= 0 || alpha === 0.0 ) {
53-
return y;
54-
}
55-
// Use unrolled loops if both strides are equal to `1`...
56-
if ( strideX === 1 && strideY === 1 ) {
57-
m = N % M;
58-
59-
// If we have a remainder, run a clean-up loop...
60-
if ( m > 0 ) {
61-
for ( i = 0; i < m; i++ ) {
62-
y[ i ] += alpha * x[ i ];
63-
}
64-
}
65-
if ( N < M ) {
66-
return y;
67-
}
68-
for ( i = m; i < N; i += M ) {
69-
y[ i ] += alpha * x[ i ];
70-
y[ i+1 ] += alpha * x[ i+1 ];
71-
y[ i+2 ] += alpha * x[ i+2 ];
72-
y[ i+3 ] += alpha * x[ i+3 ];
73-
}
74-
return y;
75-
}
76-
if ( strideX < 0 ) {
77-
ix = (1-N) * strideX;
78-
} else {
79-
ix = 0;
80-
}
81-
if ( strideY < 0 ) {
82-
iy = (1-N) * strideY;
83-
} else {
84-
iy = 0;
85-
}
86-
for ( i = 0; i < N; i++ ) {
87-
y[ iy ] += alpha * x[ ix ];
88-
ix += strideX;
89-
iy += strideY;
90-
}
91-
return y;
54+
return ndarray( N, alpha, x, strideX, stride2offset( N, strideX ), y, strideY, stride2offset( N, strideY ) );
9255
}
9356

9457

lib/node_modules/@stdlib/blas/base/gaxpy/lib/ndarray.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
24+
var accessors = require( './accessors.js' );
25+
26+
2127
// VARIABLES //
2228

2329
var M = 4;
@@ -49,11 +55,21 @@ var M = 4;
4955
function gaxpy( N, alpha, x, strideX, offsetX, y, strideY, offsetY ) {
5056
var ix;
5157
var iy;
58+
var ox;
59+
var oy;
5260
var m;
5361
var i;
5462
if ( N <= 0 || alpha === 0.0 ) {
5563
return y;
5664
}
65+
ox = arraylike2object( x );
66+
oy = arraylike2object( y );
67+
if ( ox.accessorProtocol || oy.accessorProtocol ) {
68+
accessors( N, ox, strideX, offsetX, oy, strideY, offsetY );
69+
return oy.data;
70+
}
71+
ix = offsetX;
72+
iy = offsetY;
5773
ix = offsetX;
5874
iy = offsetY;
5975

0 commit comments

Comments
 (0)