Skip to content

Commit 903f141

Browse files
committed
feat: add ndarray/base/complement-shape
--- 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 7c208b6 commit 903f141

File tree

10 files changed

+626
-0
lines changed

10 files changed

+626
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
# complementShape
22+
23+
> Return the shape defined by the dimensions which are not included in a list of 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+
```javascript
40+
var complementShape = require( '@stdlib/ndarray/base/complement-shape' );
41+
```
42+
43+
#### complementShape( shape, dims )
44+
45+
Returns the shape defined by the dimensions which are not included in a list of dimensions.
46+
47+
```javascript
48+
var sh = complementShape( [ 3, 2 ], [ -1 ] );
49+
// returns [ 3 ]
50+
```
51+
52+
The function accepts the following parameters:
53+
54+
- **shape**: array shape.
55+
- **dims**: list of dimensions.
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62+
63+
<section class="notes">
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<!-- Package usage examples. -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
79+
var zip = require( '@stdlib/array/base/zip' );
80+
var filled = require( '@stdlib/array/base/filled' );
81+
var logEachMap = require( '@stdlib/console/log-each-map' );
82+
var complementShape = require( '@stdlib/ndarray/base/complement-shape' );
83+
84+
var opts = {
85+
'dtype': 'int32'
86+
};
87+
var d1 = discreteUniform( 100, 1, 10, opts );
88+
var d2 = discreteUniform( d1.length, 1, 10, opts );
89+
var d3 = discreteUniform( d1.length, 1, 10, opts );
90+
var d4 = discreteUniform( d1.length, 1, 10, opts );
91+
92+
var dims = discreteUniform( 2, -4, 3, opts );
93+
var shapes = zip( [ d1, d2, d3, d4 ] );
94+
95+
logEachMap( 'shape: (%s). dims: (%s). complement: (%s).', shapes, filled( dims, d1.length ), complementShape );
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- 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. -->
103+
104+
<section class="references">
105+
106+
</section>
107+
108+
<!-- /.references -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
</section>
123+
124+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 complementShape = 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 = complementShape( shape, [ -1, -2, -4 ] );
45+
if ( out.length !== 2 ) {
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+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( shape, dims )
3+
Returns the shape defined by the dimensions which are not included in a list
4+
of dimensions.
5+
6+
Parameters
7+
----------
8+
shape: ArrayLike<number>
9+
Array shape.
10+
11+
dims: ArrayLike<number>
12+
List of dimensions.
13+
14+
Returns
15+
-------
16+
out: Array<number>
17+
Output shape.
18+
19+
Examples
20+
--------
21+
> var sh = [ 3, 2 ];
22+
> var out = {{alias}}( sh, [ 1 ] )
23+
[ 3 ]
24+
25+
See Also
26+
--------
27+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns the shape defined by the dimensions which are not included in a list of dimensions.
27+
*
28+
* @param shape - array shape
29+
* @param dims - list of dimensions
30+
* @returns output shape
31+
*
32+
* @example
33+
* var sh = complementShape( [ 3, 2 ], [ -1 ] );
34+
* // returns [ 3 ]
35+
*
36+
* sh = complementShape( [ 3, 2, 1 ], [ -2 ] );
37+
* // returns [ 3, 1 ]
38+
*
39+
* sh = complementShape( [ 3 ], [ 0 ] );
40+
* // returns []
41+
*
42+
* sh = complementShape( [ 3, 2 ], [ 0 ] );
43+
* // returns [ 2 ]
44+
*
45+
* sh = complementShape( [ 3 ], [ 0 ] );
46+
* // returns []
47+
*
48+
* sh = complementShape( [], [] );
49+
* // returns []
50+
*/
51+
declare function complementShape( shape: Collection<number>, dims: Collection<number> ): Array<number>;
52+
53+
54+
// EXPORTS //
55+
56+
export = complementShape;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import complementShape = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of numbers...
25+
{
26+
complementShape( [ 3, 2, 1 ], [ -1 ] ); // $ExpectType number[]
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument that is not an array-like object containing numbers...
30+
{
31+
complementShape( true, [ -1 ] ); // $ExpectError
32+
complementShape( false, [ -1 ] ); // $ExpectError
33+
complementShape( null, [ -1 ] ); // $ExpectError
34+
complementShape( undefined, [ -1 ] ); // $ExpectError
35+
complementShape( '5', [ -1 ] ); // $ExpectError
36+
complementShape( [ '1', '2' ], [ -1 ] ); // $ExpectError
37+
complementShape( {}, [ -1 ] ); // $ExpectError
38+
complementShape( ( x: number ): number => x, [ -1 ] ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument that is not an array-like object containing numbers...
42+
{
43+
complementShape( [ 2, 3 ], 5 ); // $ExpectError
44+
complementShape( [ 2, 3 ], true ); // $ExpectError
45+
complementShape( [ 2, 3 ], false ); // $ExpectError
46+
complementShape( [ 2, 3 ], null ); // $ExpectError
47+
complementShape( [ 2, 3 ], undefined ); // $ExpectError
48+
complementShape( [ 2, 3 ], '5' ); // $ExpectError
49+
complementShape( [ 2, 3 ], [ '1', '2' ] ); // $ExpectError
50+
complementShape( [ 2, 3 ], {} ); // $ExpectError
51+
complementShape( [ 2, 3 ], ( x: number ): number => x ); // $ExpectError
52+
}
53+
54+
// The compiler throws an error if the function is provided an unsupported number of arguments...
55+
{
56+
complementShape(); // $ExpectError
57+
complementShape( [ 3, 2 ] ); // $ExpectError
58+
complementShape( [ 3, 2 ], [ -1 ], 0 ); // $ExpectError
59+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var zip = require( '@stdlib/array/base/zip' );
23+
var filled = require( '@stdlib/array/base/filled' );
24+
var logEachMap = require( '@stdlib/console/log-each-map' );
25+
var complementShape = require( './../lib' );
26+
27+
var opts = {
28+
'dtype': 'int32'
29+
};
30+
var d1 = discreteUniform( 100, 1, 10, opts );
31+
var d2 = discreteUniform( d1.length, 1, 10, opts );
32+
var d3 = discreteUniform( d1.length, 1, 10, opts );
33+
var d4 = discreteUniform( d1.length, 1, 10, opts );
34+
35+
var dims = discreteUniform( 2, -4, 3, opts );
36+
var shapes = zip( [ d1, d2, d3, d4 ] );
37+
38+
logEachMap( 'shape: (%s). dims: (%s). complement: (%s).', shapes, filled( dims, d1.length ), complementShape );

0 commit comments

Comments
 (0)