Skip to content

Commit c7a7bd4

Browse files
feat: add blas/ext/base/sxsy
PR-URL: #13030 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#802
1 parent 9b655da commit c7a7bd4

33 files changed

Lines changed: 4146 additions & 0 deletions
Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
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+
# sxsy
22+
23+
> Subtract elements of a single-precision floating-point strided array `y` from the corresponding elements of a single-precision floating-point strided array `x` and assign the results to `y`.
24+
25+
<section class="intro">
26+
27+
This BLAS extension implements the operation
28+
29+
<!-- <equation class="equation" label="eq:xsy" align="center" raw="\mathbf{y} = \mathbf{x} - \mathbf{y}" alt="Equation for xsy operation."> -->
30+
31+
```math
32+
\mathbf{y} = \mathbf{x} - \mathbf{y}
33+
```
34+
35+
<!-- </equation> -->
36+
37+
This API is a specialized version of the package [`@stdlib/blas/ext/base/saxpby`][@stdlib/blas/ext/base/saxpby] with `α = 1` and `β = -1` and performs element-wise subtraction between two vectors.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var sxsy = require( '@stdlib/blas/ext/base/sxsy' );
49+
```
50+
51+
#### sxsy( N, x, strideX, y, strideY )
52+
53+
Subtracts elements of a single-precision floating-point strided array `y` from the corresponding elements of a single-precision floating-point strided array `x` and assigns the results to `y`.
54+
55+
```javascript
56+
var Float32Array = require( '@stdlib/array/float32' );
57+
58+
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
59+
var y = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
60+
61+
sxsy( x.length, x, 1, y, 1 );
62+
// y => <Float32Array>[ -1.0, -2.0, -3.0, -4.0, -5.0 ]
63+
```
64+
65+
The function has the following parameters:
66+
67+
- **N**: number of indexed elements.
68+
- **x**: input [`Float32Array`][@stdlib/array/float32].
69+
- **strideX**: stride length for `x`.
70+
- **y**: output [`Float32Array`][@stdlib/array/float32].
71+
- **strideY**: stride length for `y`.
72+
73+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to subtract every other element of `y` from every other element of `x`:
74+
75+
```javascript
76+
var Float32Array = require( '@stdlib/array/float32' );
77+
78+
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
79+
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
80+
81+
sxsy( 3, x, 2, y, 2 );
82+
// y => <Float32Array>[ -6.0, 8.0, -6.0, 10.0, -6.0, 12.0 ]
83+
```
84+
85+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
86+
87+
```javascript
88+
var Float32Array = require( '@stdlib/array/float32' );
89+
90+
// Initial arrays...
91+
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
92+
var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
93+
94+
// Create offset views...
95+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
96+
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
97+
98+
sxsy( 3, x1, 1, y1, 1 );
99+
// y0 => <Float32Array>[ 7.0, 8.0, -7.0, -7.0, -7.0, 12.0 ]
100+
```
101+
102+
#### sxsy.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
103+
104+
Subtracts elements of a single-precision floating-point strided array `y` from the corresponding elements of a single-precision floating-point strided array `x` and assigns the results to `y` using alternative indexing semantics.
105+
106+
```javascript
107+
var Float32Array = require( '@stdlib/array/float32' );
108+
109+
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
110+
var y = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] );
111+
112+
sxsy.ndarray( x.length, x, 1, 0, y, 1, 0 );
113+
// y => <Float32Array>[ -1.0, -2.0, -3.0, -4.0, -5.0 ]
114+
```
115+
116+
The function has the following additional parameters:
117+
118+
- **offsetX**: starting index for `x`.
119+
- **offsetY**: starting index for `y`.
120+
121+
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 subtract the last three elements of `y` from the last three elements of `x`:
122+
123+
```javascript
124+
var Float32Array = require( '@stdlib/array/float32' );
125+
126+
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
127+
var y = new Float32Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
128+
129+
sxsy.ndarray( 3, x, 1, x.length-3, y, 1, y.length-3 );
130+
// y => <Float32Array>[ 6.0, 7.0, -5.0, -5.0, -5.0 ]
131+
```
132+
133+
</section>
134+
135+
<!-- /.usage -->
136+
137+
<section class="notes">
138+
139+
## Notes
140+
141+
- If `N <= 0`, both functions return `y` unchanged.
142+
143+
</section>
144+
145+
<!-- /.notes -->
146+
147+
<section class="examples">
148+
149+
## Examples
150+
151+
<!-- eslint no-undef: "error" -->
152+
153+
```javascript
154+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
155+
var sxsy = require( '@stdlib/blas/ext/base/sxsy' );
156+
157+
var x = discreteUniform( 10, -100, 100, {
158+
'dtype': 'float32'
159+
});
160+
console.log( x );
161+
162+
var y = discreteUniform( 10, -100, 100, {
163+
'dtype': 'float32'
164+
});
165+
console.log( y );
166+
167+
sxsy( x.length, x, 1, y, 1 );
168+
console.log( y );
169+
```
170+
171+
</section>
172+
173+
<!-- /.examples -->
174+
175+
<!-- C interface documentation. -->
176+
177+
* * *
178+
179+
<section class="c">
180+
181+
## C APIs
182+
183+
<section class="intro">
184+
185+
</section>
186+
187+
<!-- /.intro -->
188+
189+
<section class="usage">
190+
191+
### Usage
192+
193+
```c
194+
#include "stdlib/blas/ext/base/sxsy.h"
195+
```
196+
197+
#### stdlib_strided_sxsy( N, \*X, strideX, \*Y, strideY )
198+
199+
Subtracts elements of a single-precision floating-point strided array `y` from the corresponding elements of a single-precision floating-point strided array `x` and assigns the results to `y`.
200+
201+
```c
202+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
203+
float y[] = { 2.0f, 3.0f, 4.0f, 5.0f };
204+
205+
stdlib_strided_sxsy( 4, x, 1, y, 1 );
206+
```
207+
208+
The function accepts the following arguments:
209+
210+
- **N**: `[in] CBLAS_INT` number of indexed elements.
211+
- **X**: `[in] float*` input array.
212+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
213+
- **Y**: `[inout] float*` output array.
214+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
215+
216+
```c
217+
void stdlib_strided_sxsy( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, float *Y, const CBLAS_INT strideY );
218+
```
219+
220+
<!--lint disable maximum-heading-length-->
221+
222+
#### stdlib_strided_sxsy_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
223+
224+
<!--lint enable maximum-heading-length-->
225+
226+
Subtracts elements of a single-precision floating-point strided array `y` from the corresponding elements of a single-precision floating-point strided array `x` and assigns the results to `y` using alternative indexing semantics.
227+
228+
```c
229+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f };
230+
float y[] = { 2.0f, 3.0f, 4.0f, 5.0f };
231+
232+
stdlib_strided_sxsy_ndarray( 4, x, 1, 0, y, 1, 0 );
233+
```
234+
235+
The function accepts the following arguments:
236+
237+
- **N**: `[in] CBLAS_INT` number of indexed elements.
238+
- **X**: `[in] float*` input array.
239+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
240+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
241+
- **Y**: `[inout] float*` output array.
242+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
243+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
244+
245+
```c
246+
void stdlib_strided_sxsy_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
247+
```
248+
249+
</section>
250+
251+
<!-- /.usage -->
252+
253+
<section class="notes">
254+
255+
</section>
256+
257+
<!-- /.notes -->
258+
259+
<section class="examples">
260+
261+
### Examples
262+
263+
```c
264+
#include "stdlib/blas/ext/base/sxsy.h"
265+
#include <stdio.h>
266+
267+
int main( void ) {
268+
// Create strided arrays:
269+
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
270+
float y[] = { 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f };
271+
272+
// Specify the number of indexed elements:
273+
const int N = 8;
274+
275+
// Specify strides:
276+
const int strideX = 1;
277+
const int strideY = 1;
278+
279+
// Subtract elements of `y` from the corresponding elements of `x`:
280+
stdlib_strided_sxsy( N, x, strideX, y, strideY );
281+
282+
// Print the result:
283+
for ( int i = 0; i < 8; i++ ) {
284+
printf( "y[ %i ] = %f\n", i, y[ i ] );
285+
}
286+
}
287+
```
288+
289+
</section>
290+
291+
<!-- /.examples -->
292+
293+
</section>
294+
295+
<!-- /.c -->
296+
297+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
298+
299+
<section class="related">
300+
301+
</section>
302+
303+
<!-- /.related -->
304+
305+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
306+
307+
<section class="links">
308+
309+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
310+
311+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
312+
313+
[@stdlib/blas/ext/base/saxpby]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/saxpby
314+
315+
<!-- <related-links> -->
316+
317+
<!-- </related-links> -->
318+
319+
</section>
320+
321+
<!-- /.links -->

0 commit comments

Comments
 (0)