Skip to content

Commit a4212a2

Browse files
committed
Auto-generated commit
1 parent 05ea580 commit a4212a2

File tree

11 files changed

+745
-0
lines changed

11 files changed

+745
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`fd99657`](https://github.com/stdlib-js/stdlib/commit/fd99657f0da9706d3c1d7b03c5f9caf97069d3df) - add `ndarray/prepend-singleton-dimensions` [(#9478)](https://github.com/stdlib-js/stdlib/pull/9478)
1314
- [`f475c84`](https://github.com/stdlib-js/stdlib/commit/f475c843a4b1579eef6533e464e4c16766d7ecdd) - add writable parameter to `ndarray/base/expand-dimensions` [(#9476)](https://github.com/stdlib-js/stdlib/pull/9476)
1415
- [`f40ccb7`](https://github.com/stdlib-js/stdlib/commit/f40ccb75929e92538b8c366145589addccdaafbe) - add `ndarray/base/ternary-loop-interchange-order` [(#9499)](https://github.com/stdlib-js/stdlib/pull/9499)
1516
- [`1f79854`](https://github.com/stdlib-js/stdlib/commit/1f798549409c47de0261c5396dccf64012e54a9c) - add `ndarray/base/ternary-tiling-block-size` [(#9495)](https://github.com/stdlib-js/stdlib/pull/9495)
@@ -694,6 +695,7 @@ A total of 40 issues were closed in this release:
694695

695696
<details>
696697

698+
- [`fd99657`](https://github.com/stdlib-js/stdlib/commit/fd99657f0da9706d3c1d7b03c5f9caf97069d3df) - **feat:** add `ndarray/prepend-singleton-dimensions` [(#9478)](https://github.com/stdlib-js/stdlib/pull/9478) _(by Muhammad Haris, Athan Reines)_
697699
- [`f475c84`](https://github.com/stdlib-js/stdlib/commit/f475c843a4b1579eef6533e464e4c16766d7ecdd) - **feat:** add writable parameter to `ndarray/base/expand-dimensions` [(#9476)](https://github.com/stdlib-js/stdlib/pull/9476) _(by Muhammad Haris, Athan Reines)_
698700
- [`da6ecc9`](https://github.com/stdlib-js/stdlib/commit/da6ecc96d7f503e03007eef616703fc7a71587b8) - **chore:** fix JavaScript lint errors [(#9572)](https://github.com/stdlib-js/stdlib/pull/9572) _(by Shreelaxmi Hegde, Athan Reines)_
699701
- [`ed3afe7`](https://github.com/stdlib-js/stdlib/commit/ed3afe7252708109f1a8350c15bcdabcc96f6e0a) - **chore:** fix C lint errors [(#9571)](https://github.com/stdlib-js/stdlib/pull/9571) _(by Geo Daoyu)_
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
# prependSingletonDimensions
22+
23+
> Return a read-only view of an input [ndarray][@stdlib/ndarray/ctor] with a specified number of prepended singleton dimensions.
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+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var prependSingletonDimensions = require( '@stdlib/ndarray/prepend-singleton-dimensions' );
43+
```
44+
45+
#### prependSingletonDimensions( x, n )
46+
47+
Returns a read-only view of an input [ndarray][@stdlib/ndarray/ctor] with a specified number of prepended singleton dimensions.
48+
49+
<!-- eslint-disable id-length -->
50+
51+
```javascript
52+
var array = require( '@stdlib/ndarray/array' );
53+
54+
// Create a 2x2 ndarray:
55+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
56+
// returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
57+
58+
// Prepend singleton dimensions:
59+
var y = prependSingletonDimensions( x, 3 );
60+
// returns <ndarray>[ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ]
61+
```
62+
63+
The function accepts the following arguments:
64+
65+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
66+
- **n**: number of singleton dimensions to prepend.
67+
68+
</section>
69+
70+
<!-- /.usage -->
71+
72+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
73+
74+
<section class="notes">
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<!-- Package usage examples. -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint-disable id-length -->
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var uniform = require( '@stdlib/random/uniform' );
92+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
93+
var prependSingletonDimensions = require( '@stdlib/ndarray/prepend-singleton-dimensions' );
94+
95+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
96+
console.log( ndarray2array( x ) );
97+
98+
var y = prependSingletonDimensions( x, 3 );
99+
console.log( ndarray2array( y ) );
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- 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. -->
107+
108+
<section class="references">
109+
110+
</section>
111+
112+
<!-- /.references -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor
127+
128+
<!-- <related-links> -->
129+
130+
<!-- </related-links> -->
131+
132+
</section>
133+
134+
<!-- /.links -->
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var empty = require( './../../empty' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var prependSingletonDimensions = require( './../lib' ); // eslint-disable-line id-length
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::1d', pkg ), function benchmark( b ) {
34+
var values;
35+
var v;
36+
var i;
37+
38+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
39+
40+
values = [
41+
empty( [ 2 ], { 'dtype': 'float64' } ),
42+
empty( [ 2 ], { 'dtype': 'float32' } ),
43+
empty( [ 2 ], { 'dtype': 'int32' } ),
44+
empty( [ 2 ], { 'dtype': 'complex128' } ),
45+
empty( [ 2 ], { 'dtype': 'generic' } )
46+
];
47+
48+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
v = prependSingletonDimensions( values[ i%values.length ], 1 );
53+
if ( typeof v !== 'object' ) {
54+
b.fail( 'should return an object' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isndarrayLike( v ) ) {
59+
b.fail( 'should return an ndarray' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
64+
65+
bench( format( '%s::2d', pkg ), function benchmark( b ) {
66+
var values;
67+
var v;
68+
var i;
69+
70+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
71+
72+
values = [
73+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
74+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
75+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
76+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
77+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
78+
];
79+
80+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
v = prependSingletonDimensions( values[ i%values.length ], 1 );
85+
if ( typeof v !== 'object' ) {
86+
b.fail( 'should return an object' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isndarrayLike( v ) ) {
91+
b.fail( 'should return an ndarray' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
96+
97+
bench( format( '%s::3d', pkg ), function benchmark( b ) {
98+
var values;
99+
var v;
100+
var i;
101+
102+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
103+
104+
values = [
105+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
106+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
107+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
108+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
109+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
110+
];
111+
112+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
113+
114+
b.tic();
115+
for ( i = 0; i < b.iterations; i++ ) {
116+
v = prependSingletonDimensions( values[ i%values.length ], 1 );
117+
if ( typeof v !== 'object' ) {
118+
b.fail( 'should return an object' );
119+
}
120+
}
121+
b.toc();
122+
if ( !isndarrayLike( v ) ) {
123+
b.fail( 'should return an ndarray' );
124+
}
125+
b.pass( 'benchmark finished' );
126+
b.end();
127+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x, n )
3+
Returns a read-only view of an input ndarray with a specified number of
4+
prepended singleton dimensions.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input array.
10+
11+
n: integer
12+
Number of singleton dimensions to prepend.
13+
14+
Returns
15+
-------
16+
out: ndarray
17+
Output array.
18+
19+
Examples
20+
--------
21+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
22+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
23+
> var y = {{alias}}( x, 3 )
24+
<ndarray>[ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ]
25+
26+
See Also
27+
--------
28+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
* Returns a read-only view of an input ndarray with a specified number of prepended singleton dimensions.
27+
*
28+
* @param x - input array
29+
* @param n - number of singleton dimensions to prepend
30+
* @returns output array
31+
*
32+
* @example
33+
* var array = require( '@stdlib/ndarray/array' );
34+
*
35+
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
36+
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
37+
*
38+
* var y = prependSingletonDimensions( x, 3 );
39+
* // returns <ndarray>[ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ]
40+
*/
41+
declare function prependSingletonDimensions<T = unknown, U extends typedndarray<T> = typedndarray<T>>( x: U, n: number ): U;
42+
43+
44+
// EXPORTS //
45+
46+
export = prependSingletonDimensions;

0 commit comments

Comments
 (0)