Skip to content

Commit fa7e71f

Browse files
committed
feat: add blas/ext/base/ndarray/dxsa
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 46f97c0 commit fa7e71f

10 files changed

Lines changed: 776 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
# dxsa
22+
23+
> Subtract a scalar constant from each element in a one-dimensional double-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dxsa = require( '@stdlib/blas/ext/base/ndarray/dxsa' );
37+
```
38+
39+
#### dxsa( arrays )
40+
41+
Subtracts a scalar constant from each element in a one-dimensional double-precision floating-point ndarray.
42+
43+
```javascript
44+
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
47+
var x = new Float64Vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
48+
49+
var alpha = scalar2ndarray( 5.0, {
50+
'dtype': 'float64'
51+
});
52+
53+
dxsa( [ x, alpha ] );
54+
// x => <ndarray>[ -7.0, -4.0, -2.0, -10.0, -1.0, -5.0, -6.0, -8.0 ]
55+
```
56+
57+
The function has the following parameters:
58+
59+
- **arrays**: array-like object containing the following ndarrays:
60+
61+
- a one-dimensional input ndarray.
62+
- a zero-dimensional ndarray containing the scalar constant to subtract.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**).
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
86+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
87+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
88+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
89+
var dxsa = require( '@stdlib/blas/ext/base/ndarray/dxsa' );
90+
91+
var opts = {
92+
'dtype': 'float64'
93+
};
94+
95+
var x = discreteUniform( [ 10 ], -100, 100, opts );
96+
console.log( ndarray2array( x ) );
97+
98+
var alpha = scalar2ndarray( 5.0, opts );
99+
console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) );
100+
101+
dxsa( [ x, alpha ] );
102+
console.log( ndarray2array( x ) );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
</section>
122+
123+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/uniform' );
25+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var dxsa = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var alpha;
50+
var x;
51+
52+
x = uniform( [ len ], -100.0, 100.0, options );
53+
alpha = scalar2ndarray( 5.0, options );
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var out;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
out = dxsa( [ x, alpha ] );
69+
if ( typeof out !== 'object' ) {
70+
b.fail( 'should return an ndarray' );
71+
}
72+
}
73+
b.toc();
74+
if ( typeof out !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( format( '%s:len=%d', pkg, len ), f );
104+
}
105+
}
106+
107+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( arrays )
3+
Subtracts a scalar constant from each element in a one-dimensional
4+
double-precision floating-point ndarray.
5+
6+
The input ndarray is modified *in-place* (i.e., the input ndarray is
7+
*mutated*).
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing the following ndarrays:
13+
14+
- a one-dimensional input ndarray.
15+
- a zero-dimensional ndarray containing the scalar constant to subtract.
16+
17+
Returns
18+
-------
19+
out: ndarray
20+
Input ndarray.
21+
22+
Examples
23+
--------
24+
> var buf = [ -2.0, 1.0, 3.0, -5.0 ];
25+
> var x = new {{alias:@stdlib/ndarray/vector/float64}}( buf );
26+
> var opts = { 'dtype': 'float64' };
27+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, opts );
28+
> {{alias}}( [ x, alpha ] )
29+
<ndarray>[ -7.0, -4.0, -2.0, -10.0 ]
30+
31+
See Also
32+
--------
33+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float64ndarray, typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Subtracts a scalar constant from each element in a one-dimensional double-precision floating-point ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a zero-dimensional ndarray containing the scalar constant to subtract.
34+
*
35+
* @param arrays - array-like object containing ndarrays
36+
* @returns input ndarray
37+
*
38+
* @example
39+
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
40+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
41+
*
42+
* var x = new Float64Vector( [ -2.0, 1.0, 3.0, -5.0 ] );
43+
*
44+
* var alpha = scalar2ndarray( 5.0, {
45+
* 'dtype': 'float64'
46+
* });
47+
*
48+
* var out = dxsa( [ x, alpha ] );
49+
* // returns <ndarray>[ -7.0, -4.0, -2.0, -10.0 ]
50+
*/
51+
declare function dxsa( arrays: [ float64ndarray, typedndarray<number> ] ): float64ndarray;
52+
53+
54+
// EXPORTS //
55+
56+
export = dxsa;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
23+
import dxsa = require( './index' );
24+
25+
26+
// TESTS //
27+
28+
// The function returns an ndarray...
29+
{
30+
const x = zeros( [ 10 ], {
31+
'dtype': 'float64'
32+
});
33+
const alpha = scalar2ndarray( 5.0, {
34+
'dtype': 'float64'
35+
});
36+
37+
dxsa( [ x, alpha ] ); // $ExpectType float64ndarray
38+
}
39+
40+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
41+
{
42+
dxsa( '10' ); // $ExpectError
43+
dxsa( 5 ); // $ExpectError
44+
dxsa( true ); // $ExpectError
45+
dxsa( false ); // $ExpectError
46+
dxsa( null ); // $ExpectError
47+
dxsa( undefined ); // $ExpectError
48+
dxsa( [] ); // $ExpectError
49+
dxsa( {} ); // $ExpectError
50+
dxsa( ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is provided an unsupported number of arguments...
54+
{
55+
const x = zeros( [ 10 ], {
56+
'dtype': 'float64'
57+
});
58+
const alpha = scalar2ndarray( 5.0, {
59+
'dtype': 'float64'
60+
});
61+
62+
dxsa(); // $ExpectError
63+
dxsa( [ x, alpha ], {} ); // $ExpectError
64+
}

0 commit comments

Comments
 (0)