Skip to content

Commit 2727513

Browse files
feat: add blas/ext/base/dwapx
PR-URL: #12868 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#735
1 parent e663e10 commit 2727513

33 files changed

Lines changed: 4228 additions & 0 deletions
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dwapx
22+
23+
> Add a scalar constant to each element in a double-precision floating-point strided array `x` and assign the results to elements in a double-precision floating-point strided array `w`.
24+
25+
<section class="intro">
26+
27+
This BLAS extension implements the operation
28+
29+
<!-- <equation class="equation" label="eq:wapx" align="center" raw="\mathbf{w} = \mathbf{x} + \alpha" alt="Equation for wapx operation."> -->
30+
31+
```math
32+
\mathbf{w} = \mathbf{x} + \alpha
33+
```
34+
35+
<!-- </equation> -->
36+
37+
This API is complementary to the package [`@stdlib/blas/ext/base/dapx`][@stdlib/blas/ext/base/dapx], which performs an in-place update.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var dwapx = require( '@stdlib/blas/ext/base/dwapx' );
49+
```
50+
51+
#### dwapx( N, alpha, x, strideX, w, strideW )
52+
53+
Adds a scalar constant to each element in a double-precision floating-point strided array `x` and assigns the results to elements in a double-precision floating-point strided array `w`.
54+
55+
```javascript
56+
var Float64Array = require( '@stdlib/array/float64' );
57+
58+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
59+
var w = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
60+
61+
dwapx( x.length, 5.0, x, 1, w, 1 );
62+
// w => <Float64Array>[ 6.0, 7.0, 8.0, 9.0, 10.0 ]
63+
```
64+
65+
The function has the following parameters:
66+
67+
- **N**: number of indexed elements.
68+
- **alpha**: scalar constant.
69+
- **x**: input [`Float64Array`][@stdlib/array/float64].
70+
- **strideX**: stride length for `x`.
71+
- **w**: output [`Float64Array`][@stdlib/array/float64].
72+
- **strideW**: stride length for `w`.
73+
74+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to add `alpha` to every other element in `x` and assign the results to every other element in `w`:
75+
76+
```javascript
77+
var Float64Array = require( '@stdlib/array/float64' );
78+
79+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
80+
var w = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
81+
82+
dwapx( 3, 5.0, x, 2, w, 2 );
83+
// w => <Float64Array>[ 6.0, 0.0, 8.0, 0.0, 10.0, 0.0 ]
84+
```
85+
86+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
87+
88+
```javascript
89+
var Float64Array = require( '@stdlib/array/float64' );
90+
91+
// Initial arrays...
92+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
93+
var w0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
94+
95+
// Create offset views...
96+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
97+
var w1 = new Float64Array( w0.buffer, w0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
98+
99+
dwapx( 3, 5.0, x1, 1, w1, 1 );
100+
// w0 => <Float64Array>[ 0.0, 0.0, 7.0, 8.0, 9.0, 0.0 ]
101+
```
102+
103+
#### dwapx.ndarray( N, alpha, x, strideX, offsetX, w, strideW, offsetW )
104+
105+
Adds a scalar constant to each element in a double-precision floating-point strided array `x` and assigns the results to elements in a double-precision floating-point strided array `w` using alternative indexing semantics.
106+
107+
```javascript
108+
var Float64Array = require( '@stdlib/array/float64' );
109+
110+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
111+
var w = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
112+
113+
dwapx.ndarray( x.length, 5.0, x, 1, 0, w, 1, 0 );
114+
// w => <Float64Array>[ 6.0, 7.0, 8.0, 9.0, 10.0 ]
115+
```
116+
117+
The function has the following additional parameters:
118+
119+
- **offsetX**: starting index for `x`.
120+
- **offsetW**: starting index for `w`.
121+
122+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to add `alpha` to the last three elements of `x` and assign the results to the last three elements of `w`:
123+
124+
```javascript
125+
var Float64Array = require( '@stdlib/array/float64' );
126+
127+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
128+
var w = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
129+
130+
dwapx.ndarray( 3, 5.0, x, 1, x.length-3, w, 1, w.length-3 );
131+
// w => <Float64Array>[ 0.0, 0.0, 8.0, 9.0, 10.0 ]
132+
```
133+
134+
</section>
135+
136+
<!-- /.usage -->
137+
138+
<section class="notes">
139+
140+
## Notes
141+
142+
- If `N <= 0`, both functions return `w` unchanged.
143+
144+
</section>
145+
146+
<!-- /.notes -->
147+
148+
<section class="examples">
149+
150+
## Examples
151+
152+
<!-- eslint no-undef: "error" -->
153+
154+
```javascript
155+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
156+
var dwapx = require( '@stdlib/blas/ext/base/dwapx' );
157+
158+
var x = discreteUniform( 10, -100, 100, {
159+
'dtype': 'float64'
160+
});
161+
console.log( x );
162+
163+
var w = discreteUniform( 10, -100, 100, {
164+
'dtype': 'float64'
165+
});
166+
console.log( w );
167+
168+
dwapx( x.length, 5.0, x, 1, w, 1 );
169+
console.log( w );
170+
```
171+
172+
</section>
173+
174+
<!-- /.examples -->
175+
176+
<!-- C interface documentation. -->
177+
178+
* * *
179+
180+
<section class="c">
181+
182+
## C APIs
183+
184+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
185+
186+
<section class="intro">
187+
188+
</section>
189+
190+
<!-- /.intro -->
191+
192+
<!-- C usage documentation. -->
193+
194+
<section class="usage">
195+
196+
### Usage
197+
198+
```c
199+
#include "stdlib/blas/ext/base/dwapx.h"
200+
```
201+
202+
#### stdlib_strided_dwapx( N, alpha, \*X, strideX, \*W, strideW )
203+
204+
Adds a scalar constant to each element in a double-precision floating-point strided array `X` and assigns the results to elements in a double-precision floating-point strided array `W`.
205+
206+
```c
207+
const double x[] = { 1.0, 2.0, 3.0, 4.0 };
208+
double w[] = { 0.0, 0.0, 0.0, 0.0 };
209+
210+
stdlib_strided_dwapx( 4, 5.0, x, 1, w, 1 );
211+
```
212+
213+
The function accepts the following arguments:
214+
215+
- **N**: `[in] CBLAS_INT` number of indexed elements.
216+
- **alpha**: `[in] double` scalar constant.
217+
- **X**: `[in] double*` input array.
218+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
219+
- **W**: `[out] double*` output array.
220+
- **strideW**: `[in] CBLAS_INT` stride length for `W`.
221+
222+
```c
223+
void stdlib_strided_dwapx( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, double *W, const CBLAS_INT strideW );
224+
```
225+
226+
<!-- lint disable maximum-heading-length -->
227+
228+
#### stdlib_strided_dwapx_ndarray( N, alpha, \*X, strideX, offsetX, \*W, strideW, offsetW )
229+
230+
<!-- lint enable maximum-heading-length -->
231+
232+
Adds a scalar constant to each element in a double-precision floating-point strided array `X` and assigns the results to elements in a double-precision floating-point strided array `W` using alternative indexing semantics.
233+
234+
```c
235+
const double x[] = { 1.0, 2.0, 3.0, 4.0 };
236+
double w[] = { 0.0, 0.0, 0.0, 0.0 };
237+
238+
stdlib_strided_dwapx_ndarray( 4, 5.0, x, 1, 0, w, 1, 0 );
239+
```
240+
241+
The function accepts the following arguments:
242+
243+
- **N**: `[in] CBLAS_INT` number of indexed elements.
244+
- **alpha**: `[in] double` scalar constant.
245+
- **X**: `[in] double*` input array.
246+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
247+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
248+
- **W**: `[out] double*` output array.
249+
- **strideW**: `[in] CBLAS_INT` stride length for `W`.
250+
- **offsetW**: `[in] CBLAS_INT` starting index for `W`.
251+
252+
```c
253+
void stdlib_strided_dwapx_ndarray( const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *W, const CBLAS_INT strideW, const CBLAS_INT offsetW );
254+
```
255+
256+
</section>
257+
258+
<!-- /.usage -->
259+
260+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
261+
262+
<section class="notes">
263+
264+
</section>
265+
266+
<!-- /.notes -->
267+
268+
<!-- C API usage examples. -->
269+
270+
<section class="examples">
271+
272+
### Examples
273+
274+
```c
275+
#include "stdlib/blas/ext/base/dwapx.h"
276+
#include <stdio.h>
277+
278+
int main( void ) {
279+
// Create strided arrays:
280+
const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
281+
double w[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
282+
283+
// Specify the number of indexed elements:
284+
const int N = 8;
285+
286+
// Specify strides:
287+
const int strideX = 1;
288+
const int strideW = 1;
289+
290+
// Add a constant to each element in `x` and assign to `w`:
291+
stdlib_strided_dwapx( N, 5.0, x, strideX, w, strideW );
292+
293+
// Print the result:
294+
for ( int i = 0; i < 8; i++ ) {
295+
printf( "w[ %i ] = %lf\n", i, w[ i ] );
296+
}
297+
}
298+
```
299+
300+
</section>
301+
302+
<!-- /.examples -->
303+
304+
</section>
305+
306+
<!-- /.c -->
307+
308+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
309+
310+
<section class="related">
311+
312+
</section>
313+
314+
<!-- /.related -->
315+
316+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
317+
318+
<section class="links">
319+
320+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
321+
322+
[@stdlib/blas/ext/base/dapx]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dapx
323+
324+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
325+
326+
<!-- <related-links> -->
327+
328+
<!-- </related-links> -->
329+
330+
</section>
331+
332+
<!-- /.links -->

0 commit comments

Comments
 (0)