Skip to content

Commit e4ed317

Browse files
committed
feat: add blas/ext/base/ndarray/gunitspace
--- 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 status: passed - 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 78898df commit e4ed317

10 files changed

Lines changed: 899 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
# gunitspace
22+
23+
> Fill a one-dimensional ndarray with linearly spaced numeric elements which increment by `1` starting from a specified value.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gunitspace = require( '@stdlib/blas/ext/base/ndarray/gunitspace' );
37+
```
38+
39+
#### gunitspace( arrays )
40+
41+
Fills a one-dimensional ndarray with linearly spaced numeric elements which increment by `1` starting from a specified value.
42+
43+
```javascript
44+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = [ 0.0, 0.0, 0.0, 0.0 ];
48+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
49+
// returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
50+
51+
var start = scalar2ndarray( 3.0, 'generic', 'row-major' );
52+
53+
var out = gunitspace( [ x, start ] );
54+
// returns <ndarray>[ 3.0, 4.0, 5.0, 6.0 ]
55+
```
56+
57+
The function has the following parameters:
58+
59+
- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a starting value.
60+
61+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<section class="notes">
66+
67+
## Notes
68+
69+
- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**).
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var zeros = require( '@stdlib/array/zeros' );
83+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
84+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
85+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
86+
var gunitspace = require( '@stdlib/blas/ext/base/ndarray/gunitspace' );
87+
88+
var xbuf = zeros( 10, 'generic' );
89+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
90+
console.log( ndarray2array( x ) );
91+
92+
var start = scalar2ndarray( 3.0, {
93+
'dtype': 'generic'
94+
});
95+
96+
gunitspace( [ x, start ] );
97+
console.log( ndarray2array( x ) );
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
105+
106+
<section class="related">
107+
108+
</section>
109+
110+
<!-- /.related -->
111+
112+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
113+
114+
<section class="links">
115+
116+
</section>
117+
118+
<!-- /.links -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 zeros = require( '@stdlib/array/zeros' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var format = require( '@stdlib/string/format' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
29+
var pkg = require( './../package.json' ).name;
30+
var gunitspace = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var start;
51+
var xbuf;
52+
var x;
53+
54+
xbuf = zeros( len, options.dtype );
55+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
56+
start = scalar2ndarray( 3.0, options.dtype, 'row-major' );
57+
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var out;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
out = gunitspace( [ x, start ] );
73+
if ( typeof out !== 'object' ) {
74+
b.fail( 'should return an ndarray' );
75+
}
76+
}
77+
b.toc();
78+
if ( typeof out !== 'object' ) {
79+
b.fail( 'should return an ndarray' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( format( '%s:len=%d', pkg, len ), f );
108+
}
109+
}
110+
111+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( arrays )
3+
Fills a one-dimensional ndarray with linearly spaced numeric elements which
4+
increment by `1` starting from a specified value.
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 a one-dimensional input ndarray and a
13+
zero-dimensional ndarray containing a starting value.
14+
15+
Returns
16+
-------
17+
out: ndarray
18+
Input ndarray.
19+
20+
Examples
21+
--------
22+
> var xbuf = [ 0.0, 0.0, 0.0, 0.0 ];
23+
> var dt = 'generic';
24+
> var sh = [ xbuf.length ];
25+
> var sx = [ 1 ];
26+
> var ox = 0;
27+
> var ord = 'row-major';
28+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
29+
> var start = {{alias:@stdlib/ndarray/base/from-scalar}}( 3.0, dt, ord );
30+
> {{alias}}( [ x, start ] )
31+
<ndarray>[ 3.0, 4.0, 5.0, 6.0 ]
32+
33+
See Also
34+
--------
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Fills a one-dimensional ndarray with linearly spaced numeric elements which increment by `1` starting from a specified value.
27+
*
28+
* @param arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a starting value
29+
* @returns input ndarray
30+
*
31+
* @example
32+
* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = [ 0.0, 0.0, 0.0, 0.0 ];
36+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
37+
* // returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
38+
*
39+
* var start = scalar2ndarray( 3.0, 'generic', 'row-major' );
40+
*
41+
* var out = gunitspace( [ x, start ] );
42+
* // returns <ndarray>[ 3.0, 4.0, 5.0, 6.0 ]
43+
*/
44+
declare function gunitspace<T extends typedndarray<unknown> = typedndarray<unknown>>( arrays: [ T, typedndarray<unknown> ] ): T;
45+
46+
47+
// EXPORTS //
48+
49+
export = gunitspace;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 gunitspace = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
const start = zeros( [], {
33+
'dtype': 'generic'
34+
});
35+
36+
gunitspace( [ x, start ] ); // $ExpectType genericndarray<number>
37+
}
38+
39+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
40+
{
41+
gunitspace( '10' ); // $ExpectError
42+
gunitspace( 10 ); // $ExpectError
43+
gunitspace( true ); // $ExpectError
44+
gunitspace( false ); // $ExpectError
45+
gunitspace( null ); // $ExpectError
46+
gunitspace( undefined ); // $ExpectError
47+
gunitspace( [] ); // $ExpectError
48+
gunitspace( {} ); // $ExpectError
49+
gunitspace( ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
const x = zeros( [ 10 ], {
55+
'dtype': 'generic'
56+
});
57+
const start = zeros( [], {
58+
'dtype': 'generic'
59+
});
60+
61+
gunitspace(); // $ExpectError
62+
gunitspace( [ x, start ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)