Skip to content

Commit 9e9557a

Browse files
committed
feat: add ndarray/unshift
--- 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 134e322 commit 9e9557a

File tree

14 files changed

+1468
-0
lines changed

14 files changed

+1468
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# unshift
22+
23+
> Return a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by prepending the provided scalar values to the input ndarray.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var unshift = require( '@stdlib/ndarray/unshift' );
41+
```
42+
43+
#### unshift( x, ...values )
44+
45+
Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by prepending the provided scalar values to the input ndarray.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
50+
51+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
52+
53+
var out = unshift( x, -2.0, -1.0, 0.0 );
54+
// returns <ndarray>
55+
56+
var arr = ndarray2array( out );
57+
// returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ]
58+
```
59+
60+
The function accepts the following arguments:
61+
62+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
63+
- **...values**: scalar values to prepend.
64+
65+
#### unshift.assign( x, ...values, out )
66+
67+
Prepends the provided scalar values to the input [ndarray][@stdlib/ndarray/ctor] and assigns the result to a provided one-dimensional output [ndarray][@stdlib/ndarray/ctor].
68+
69+
```javascript
70+
var array = require( '@stdlib/ndarray/array' );
71+
var zeros = require( '@stdlib/ndarray/zeros' );
72+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
73+
74+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
75+
var y = zeros( [ 7 ] );
76+
77+
var out = unshift.assign( x, -2.0, -1.0, 0.0, y );
78+
// returns <ndarray>
79+
80+
var bool = ( out === y );
81+
// returns true
82+
83+
var arr = ndarray2array( y );
84+
// returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ]
85+
```
86+
87+
The function accepts the following arguments:
88+
89+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
90+
- **...values**: scalar values to prepend.
91+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
98+
99+
<section class="notes">
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<!-- Package usage examples. -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
```javascript
112+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
113+
var array = require( '@stdlib/ndarray/array' );
114+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
115+
var unshift = require( '@stdlib/ndarray/unshift' );
116+
117+
var opts = {
118+
'dtype': 'generic'
119+
};
120+
var x = array( discreteUniform( 6, 0, 10, opts ), opts );
121+
console.log( ndarray2array( x ) );
122+
123+
var out = unshift( x, -2.0, -1.0, 0.0 );
124+
console.log( ndarray2array( out ) );
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="references">
134+
135+
</section>
136+
137+
<!-- /.references -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
152+
153+
</section>
154+
155+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var zeros = require( '@stdlib/ndarray/zeros' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var unshift = require( './../lib/assign.js' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::two scalar values', pkg ), function benchmark( b ) {
34+
var opts;
35+
var out;
36+
var v;
37+
var x;
38+
var i;
39+
40+
opts = {
41+
'dtype': 'float64'
42+
};
43+
x = zeros( [ 3 ], opts );
44+
out = zeros( [ 5 ], opts );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
v = unshift( x, 1.0, 2.0, out );
49+
if ( typeof v !== 'object' ) {
50+
b.fail( 'should return an ndarray' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isndarrayLike( v ) ) {
55+
b.fail( 'should return an ndarray' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( format( '%s::four scalar values', pkg ), function benchmark( b ) {
62+
var opts;
63+
var out;
64+
var v;
65+
var x;
66+
var i;
67+
68+
opts = {
69+
'dtype': 'float64'
70+
};
71+
x = zeros( [ 3 ], opts );
72+
out = zeros( [ 7 ], opts );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
v = unshift( x, 1.0, 2.0, 3.0, 4.0, out );
77+
if ( typeof v !== 'object' ) {
78+
b.fail( 'should return an ndarray' );
79+
}
80+
}
81+
b.toc();
82+
if ( !isndarrayLike( v ) ) {
83+
b.fail( 'should return an ndarray' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
});
88+
89+
bench( format( '%s::six scalar values', pkg ), function benchmark( b ) {
90+
var opts;
91+
var out;
92+
var v;
93+
var x;
94+
var i;
95+
96+
opts = {
97+
'dtype': 'float64'
98+
};
99+
x = zeros( [ 3 ], opts );
100+
out = zeros( [ 9 ], opts );
101+
102+
b.tic();
103+
for ( i = 0; i < b.iterations; i++ ) {
104+
v = unshift( x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, out );
105+
if ( typeof v !== 'object' ) {
106+
b.fail( 'should return an ndarray' );
107+
}
108+
}
109+
b.toc();
110+
if ( !isndarrayLike( v ) ) {
111+
b.fail( 'should return an ndarray' );
112+
}
113+
b.pass( 'benchmark finished' );
114+
b.end();
115+
});
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var zeros = require( '@stdlib/ndarray/zeros' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var unshift = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::two scalar values', pkg ), function benchmark( b ) {
34+
var opts;
35+
var v;
36+
var x;
37+
var i;
38+
39+
opts = {
40+
'dtype': 'float64'
41+
};
42+
x = zeros( [ 3 ], opts );
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
v = unshift( x, 1.0, 2.0 );
47+
if ( typeof v !== 'object' ) {
48+
b.fail( 'should return an ndarray' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isndarrayLike( v ) ) {
53+
b.fail( 'should return an ndarray' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( format( '%s::four scalar values', pkg ), function benchmark( b ) {
60+
var opts;
61+
var v;
62+
var x;
63+
var i;
64+
65+
opts = {
66+
'dtype': 'float64'
67+
};
68+
x = zeros( [ 3 ], opts );
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
v = unshift( x, 1.0, 2.0, 3.0, 4.0 );
73+
if ( typeof v !== 'object' ) {
74+
b.fail( 'should return an ndarray' );
75+
}
76+
}
77+
b.toc();
78+
if ( !isndarrayLike( v ) ) {
79+
b.fail( 'should return an ndarray' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
});
84+
85+
bench( format( '%s::six scalar values', pkg ), function benchmark( b ) {
86+
var opts;
87+
var v;
88+
var x;
89+
var i;
90+
91+
opts = {
92+
'dtype': 'float64'
93+
};
94+
x = zeros( [ 3 ], opts );
95+
96+
b.tic();
97+
for ( i = 0; i < b.iterations; i++ ) {
98+
v = unshift( x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 );
99+
if ( typeof v !== 'object' ) {
100+
b.fail( 'should return an ndarray' );
101+
}
102+
}
103+
b.toc();
104+
if ( !isndarrayLike( v ) ) {
105+
b.fail( 'should return an ndarray' );
106+
}
107+
b.pass( 'benchmark finished' );
108+
b.end();
109+
});

0 commit comments

Comments
 (0)