Skip to content

Commit 2e0f8e6

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/push
PR-URL: #9054 Closes: stdlib-js/metr-issue-tracker#133 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent a74bbd2 commit 2e0f8e6

File tree

14 files changed

+1581
-0
lines changed

14 files changed

+1581
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
# push
22+
23+
> Return a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by appending provided scalar values to an input [ndarray][@stdlib/ndarray/ctor].
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 push = require( '@stdlib/ndarray/push' );
41+
```
42+
43+
#### push( x, ...values )
44+
45+
Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by appending provided scalar values to a one-dimensional input [ndarray][@stdlib/ndarray/ctor].
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
51+
52+
var out = push( x, 5.0, 6.0, 7.0 );
53+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
54+
```
55+
56+
The function accepts the following arguments:
57+
58+
- **arr**: input [ndarray][@stdlib/ndarray/ctor]. Must be one-dimensional.
59+
- **...values**: scalar values to append.
60+
61+
#### push.assign( x, ...values, out )
62+
63+
Appends scalar values to a one-dimensional input [ndarray][@stdlib/ndarray/ctor] and assigns the result to a one-dimensional output [ndarray][@stdlib/ndarray/ctor].
64+
65+
```javascript
66+
var array = require( '@stdlib/ndarray/array' );
67+
var zeros = require( '@stdlib/ndarray/zeros' );
68+
69+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
70+
var y = zeros( [ 7 ] );
71+
72+
var out = push.assign( x, 5.0, 6.0, 7.0, y );
73+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
74+
75+
var bool = ( out === y );
76+
// returns true
77+
```
78+
79+
The function accepts the following arguments:
80+
81+
- **arr**: input [ndarray][@stdlib/ndarray/ctor]. Must be one-dimensional.
82+
- **...values**: scalar values to append.
83+
- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must be one-dimensional.
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="notes">
92+
93+
## Notes
94+
95+
- Scalar values are cast to the [data type][@stdlib/ndarray/dtypes] of the input [ndarray][@stdlib/ndarray/ctor].
96+
97+
</section>
98+
99+
<!-- /.notes -->
100+
101+
<!-- Package usage examples. -->
102+
103+
<section class="examples">
104+
105+
## Examples
106+
107+
```javascript
108+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
109+
var array = require( '@stdlib/ndarray/array' );
110+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
111+
var push = require( '@stdlib/ndarray/push' );
112+
113+
var opts = {
114+
'dtype': 'generic'
115+
};
116+
var x = array( discreteUniform( 6, 0, 10, opts ), opts );
117+
console.log( ndarray2array( x ) );
118+
119+
var out = push( x, 12, 14 );
120+
console.log( ndarray2array( out ) );
121+
```
122+
123+
</section>
124+
125+
<!-- /.examples -->
126+
127+
<!-- 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. -->
128+
129+
<section class="references">
130+
131+
</section>
132+
133+
<!-- /.references -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
</section>
140+
141+
<!-- /.related -->
142+
143+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
144+
145+
<section class="links">
146+
147+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
148+
149+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
150+
151+
</section>
152+
153+
<!-- /.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 push = require( './../lib/assign.js' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s:assign:num_scalars=2', 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 = push( 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:assign:num_scalars=4', 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 = push( 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:assign:num_scalars=6', 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 = push( 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 push = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s:num_scalars=2', 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 = push( 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:num_scalars=4', 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 = push( 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:num_scalars=6', 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 = push( 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)