Skip to content

Commit 5b42d5f

Browse files
committed
feat: add ndarray/base/flatten-shape-from
--- 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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 5c9fb38 commit 5b42d5f

File tree

18 files changed

+1532
-0
lines changed

18 files changed

+1532
-0
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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+
# flattenShapeFrom
22+
23+
> Flatten a shape starting from a specified dimension.
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 flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' );
41+
```
42+
43+
#### flattenShapeFrom( shape, dim )
44+
45+
Flattens a shape starting from a specified dimension.
46+
47+
```javascript
48+
var sh = flattenShapeFrom( [ 3, 3, 2 ], 1 );
49+
// returns [ 3, 6 ]
50+
```
51+
52+
The function accepts the following parameters:
53+
54+
- **shape**: array shape.
55+
- **dim**: dimension to start flattening from.
56+
57+
#### flattenShapeFrom.assign( shape, dim, out )
58+
59+
Flattens a shape starting from a specified dimension and assigns results to a provided output array.
60+
61+
```javascript
62+
var sh = [ 0, 0 ];
63+
64+
var out = flattenShapeFrom.assign( [ 3, 3, 2 ], 1, sh );
65+
// returns [ 3, 6 ]
66+
67+
var bool = ( sh === out );
68+
// returns true
69+
```
70+
71+
The function accepts the following parameters:
72+
73+
- **shape**: array shape.
74+
- **dim**: dimension to start flattening from.
75+
- **out**: output array.
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
82+
83+
<section class="notes">
84+
85+
</section>
86+
87+
<!-- /.notes -->
88+
89+
<!-- Package usage examples. -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
99+
var zip = require( '@stdlib/array/base/zip' );
100+
var logEachMap = require( '@stdlib/console/log-each-map' );
101+
var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' );
102+
103+
var opts = {
104+
'dtype': 'int32'
105+
};
106+
var d1 = discreteUniform( 100, 1, 10, opts );
107+
var d2 = discreteUniform( d1.length, 1, 10, opts );
108+
var d3 = discreteUniform( d1.length, 1, 10, opts );
109+
var d4 = discreteUniform( d1.length, 1, 10, opts );
110+
111+
var dims = discreteUniform( d1.length, 0, 3, opts );
112+
var shapes = zip( [ d1, d2, d3, d4 ] );
113+
114+
logEachMap( 'shape: (%s). dim: %d. flattened: (%s).', shapes, dims, flattenShapeFrom );
115+
```
116+
117+
</section>
118+
119+
<!-- /.examples -->
120+
121+
<!-- C interface documentation. -->
122+
123+
* * *
124+
125+
<section class="c">
126+
127+
## C APIs
128+
129+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
130+
131+
<section class="intro">
132+
133+
</section>
134+
135+
<!-- /.intro -->
136+
137+
<!-- C usage documentation. -->
138+
139+
<section class="usage">
140+
141+
### Usage
142+
143+
```c
144+
#include "stdlib/ndarray/base/flatten_shape_from.h"
145+
```
146+
147+
#### stdlib_ndarray_flatten_shape_from( ndims, \*shape, dim, \*out )
148+
149+
Flattens a shape starting from a specified dimension.
150+
151+
```c
152+
const int64_t ndims = 3;
153+
const int64_t shape[] = { 2, 3, 10 };
154+
int64_t out[ 2 ];
155+
156+
stdlib_ndarray_flatten_shape( ndims, shape, 1, out );
157+
```
158+
159+
The function accepts the following arguments:
160+
161+
- **ndims**: `[in] int64_t` number of dimensions.
162+
- **shape**: `[in] int64_t*` array shape (dimensions).
163+
- **dim**: `[in] int64_t` dimension to start flattening from.
164+
- **out**: `[out] int64_t*` output array.
165+
166+
```c
167+
int8_t stdlib_ndarray_flatten_shape_from( const int64_t ndims, const int64_t *shape, const int64_t dim, int64_t *out );
168+
```
169+
170+
</section>
171+
172+
<!-- /.usage -->
173+
174+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
175+
176+
<section class="notes">
177+
178+
</section>
179+
180+
<!-- /.notes -->
181+
182+
<!-- C API usage examples. -->
183+
184+
<section class="examples">
185+
186+
### Examples
187+
188+
```c
189+
#include "stdlib/ndarray/base/flatten_shape_from.h"
190+
#include <stdio.h>
191+
#include <inttypes.h>
192+
193+
int main( void ) {
194+
const int64_t shape[] = { 2, 3, 4, 10 };
195+
const int64_t ndims = 4;
196+
const int64_t dim = 2;
197+
int64_t out[ 3 ];
198+
199+
stdlib_ndarray_flatten_shape_from( ndims, shape, dim, out );
200+
201+
int i;
202+
printf( "shape = ( " );
203+
for ( i = 0; i < ndims-dim+1; i++ ) {
204+
printf( "%"PRId64"", out[ i ] );
205+
if ( i < ndims-dim ) {
206+
printf( ", " );
207+
}
208+
}
209+
printf( " )\n" );
210+
}
211+
```
212+
213+
</section>
214+
215+
<!-- /.examples -->
216+
217+
</section>
218+
219+
<!-- /.c -->
220+
221+
<!-- 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. -->
222+
223+
<section class="references">
224+
225+
</section>
226+
227+
<!-- /.references -->
228+
229+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
230+
231+
<section class="related">
232+
233+
</section>
234+
235+
<!-- /.related -->
236+
237+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
238+
239+
<section class="links">
240+
241+
</section>
242+
243+
<!-- /.links -->
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var flattenShapeFrom = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var shape;
34+
var out;
35+
var i;
36+
37+
shape = discreteUniform( 5, 1, 10, {
38+
'dtype': 'generic'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
shape[ 0 ] += 1;
44+
out = flattenShapeFrom( shape, 0 );
45+
if ( out.length !== 1 ) {
46+
b.fail( 'should have expected length' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isArray( out ) ) {
51+
b.fail( 'should return an array' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
56+
57+
bench( pkg+':assign', function benchmark( b ) {
58+
var shape;
59+
var out;
60+
var i;
61+
62+
shape = discreteUniform( 5, 1, 10, {
63+
'dtype': 'generic'
64+
});
65+
66+
out = [ 0 ];
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
shape[ 0 ] += 1;
71+
out = flattenShapeFrom.assign( shape, 0, out );
72+
if ( out.length !== 1 ) {
73+
b.fail( 'should have expected length' );
74+
}
75+
}
76+
b.toc();
77+
if ( !isArray( out ) ) {
78+
b.fail( 'should return an array' );
79+
}
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
});

0 commit comments

Comments
 (0)