Skip to content

Commit 7a6cc2b

Browse files
committed
feat: add ndarray/base/shape
1 parent 3a44e7e commit 7a6cc2b

File tree

10 files changed

+745
-0
lines changed

10 files changed

+745
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# shape
22+
23+
> Return the shape of a provided [ndarray][@stdlib/ndarray/base/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 shape = require( '@stdlib/ndarray/base/shape' );
41+
```
42+
43+
#### shape( x, copy )
44+
45+
Returns the shape of a provided [ndarray][@stdlib/ndarray/base/ctor].
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/zeros' );
49+
50+
var x = zeros( [ 3, 2, 3 ] );
51+
// returns <ndarray>
52+
53+
var sh = shape( x, false );
54+
// returns [ 3, 2, 3 ]
55+
```
56+
57+
When `copy` is `false`, changes to the returned shape array may mutate the input [ndarray][@stdlib/ndarray/base/ctor] shape. If there is a chance that the returned shape will be mutated (either directly or by downstream consumers), set `copy` to `true` to prevent unintended side effects.
58+
59+
```javascript
60+
var zeros = require( '@stdlib/ndarray/zeros' );
61+
62+
var x = zeros( [ 3, 2, 3 ] );
63+
// returns <ndarray>
64+
65+
var sh = shape( x, true );
66+
// returns [ 3, 2, 3 ]
67+
68+
var bool = ( x.shape === sh );
69+
// returns false
70+
```
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
77+
78+
<section class="notes">
79+
80+
</section>
81+
82+
<!-- /.notes -->
83+
84+
<!-- Package usage examples. -->
85+
86+
<section class="examples">
87+
88+
## Examples
89+
90+
<!-- eslint no-undef: "error" -->
91+
92+
<!-- eslint-disable new-cap -->
93+
94+
```javascript
95+
var zeros = require( '@stdlib/ndarray/zeros' );
96+
var slice = require( '@stdlib/ndarray/slice' );
97+
var E = require( '@stdlib/slice/multi' );
98+
var S = require( '@stdlib/slice/ctor' );
99+
var shape = require( '@stdlib/ndarray/base/shape' );
100+
101+
// Create an array:
102+
var x = zeros( [ 10, 10, 10, 10 ] );
103+
// returns <ndarray>
104+
105+
// Define some slices:
106+
var slices = [
107+
// :,:,:,:
108+
E( null, null, null, null ),
109+
110+
// 5:10,4,2:4,::-1
111+
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),
112+
113+
// :,:,2,:
114+
E( null, null, 2, null ),
115+
116+
// 1,2,3,:
117+
E( 1, 2, 3, null ),
118+
119+
// 1,3,::2,4::2
120+
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
121+
];
122+
123+
// Determine the shape of each slice...
124+
var s;
125+
var i;
126+
for ( i = 0; i < slices.length; i++ ) {
127+
s = slice( x, slices[ i ] );
128+
console.log( 'slice(%s) => %s', x.shape.join( 'x' ), shape( s, false ).join( 'x' ) );
129+
}
130+
```
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
<!-- 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. -->
137+
138+
<section class="references">
139+
140+
</section>
141+
142+
<!-- /.references -->
143+
144+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145+
146+
<section class="related">
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib
157+
158+
</section>
159+
160+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 zeros = require( '@stdlib/ndarray/zeros' );
25+
var isCollection = require( '@stdlib/assert/is-collection' );
26+
var pkg = require( './../package.json' ).name;
27+
var shape = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':copy=false', function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
zeros( [ 10, 10, 10, 1 ] ),
39+
zeros( [ 5, 5, 5, 1, 1 ] ),
40+
zeros( [ 3, 4, 5 ] )
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = shape( values[ i%values.length ], false );
46+
if ( typeof out !== 'object' ) {
47+
b.fail( 'should return an array' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isCollection( out ) ) {
52+
b.fail( 'should return an array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
57+
58+
bench( pkg+':copy=true', function benchmark( b ) {
59+
var values;
60+
var out;
61+
var i;
62+
63+
values = [
64+
zeros( [ 10, 10, 10, 1 ] ),
65+
zeros( [ 5, 5, 5, 1, 1 ] ),
66+
zeros( [ 3, 4, 5 ] )
67+
];
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
out = shape( values[ i%values.length ], true );
72+
if ( typeof out !== 'object' ) {
73+
b.fail( 'should return an array' );
74+
}
75+
}
76+
b.toc();
77+
if ( !isCollection( out ) ) {
78+
b.fail( 'should return an array' );
79+
}
80+
b.pass( 'benchmark finished' );
81+
b.end();
82+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, copy )
3+
Returns the shape of a provided ndarray.
4+
5+
Parameters
6+
----------
7+
x: ndarray
8+
Input ndarray.
9+
10+
copy: boolean
11+
Boolean indicating whether to explicitly copy the value assigned to the
12+
input ndarray's `shape` property. When `copy` is `false`, changes to the
13+
returned shape array may mutate the input ndarray shape. If there is a
14+
chance that the returned shape will be mutated (either directly or by
15+
downstream consumers), set `copy` to `true` to prevent unintended side
16+
effects.
17+
18+
Returns
19+
-------
20+
out: Array<integer>
21+
Shape.
22+
23+
Examples
24+
--------
25+
> var out = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 3, 3, 3 ] ), false )
26+
[ 3, 3, 3 ]
27+
28+
See Also
29+
--------
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 { ndarray, Shape } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns the shape of a provided ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - When `copy` is `false`, changes to the returned shape array may mutate the input ndarray shape. If there is a chance that the returned shape will be mutated (either directly or by downstream consumers), set `copy` to `true` to prevent unintended side effects.
31+
*
32+
* @param x - input ndarray
33+
* @param copy - boolean indicating whether to explicitly copy the value assigned to the input ndarray's `shape` property
34+
* @returns shape
35+
*
36+
* @example
37+
* var zeros = require( `@stdlib/ndarray/zeros` );
38+
*
39+
* var sh = shape( zeros( [ 3, 3, 3 ] ), false );
40+
* // returns [ 3, 3, 3 ]
41+
*/
42+
declare function shape( x: ndarray, copy: boolean ): Shape;
43+
44+
45+
// EXPORTS //
46+
47+
export = shape;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 zeros = require( '@stdlib/ndarray/zeros' );
20+
import shape = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns an ndarray shape...
26+
{
27+
shape( zeros( [ 3, 2, 1 ] ), false ); // $ExpectType Shape
28+
shape( zeros( [ 3, 2, 1 ] ), true ); // $ExpectType Shape
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
32+
{
33+
shape( '5', false ); // $ExpectError
34+
shape( 5, false ); // $ExpectError
35+
shape( true, false ); // $ExpectError
36+
shape( false, false ); // $ExpectError
37+
shape( null, false ); // $ExpectError
38+
shape( undefined, false ); // $ExpectError
39+
shape( [ '1', '2' ], false ); // $ExpectError
40+
shape( {}, false ); // $ExpectError
41+
shape( ( x: number ): number => x, false ); // $ExpectError
42+
}
43+
44+
// The compiler throws an error if the function is provided a second argument which is not a boolean...
45+
{
46+
shape( zeros( [ 3, 2, 1 ] ), '5' ); // $ExpectError
47+
shape( zeros( [ 3, 2, 1 ] ), 5 ); // $ExpectError
48+
shape( zeros( [ 3, 2, 1 ] ), null ); // $ExpectError
49+
shape( zeros( [ 3, 2, 1 ] ), undefined ); // $ExpectError
50+
shape( zeros( [ 3, 2, 1 ] ), [ '1', '2' ] ); // $ExpectError
51+
shape( zeros( [ 3, 2, 1 ] ), {} ); // $ExpectError
52+
shape( zeros( [ 3, 2, 1 ] ), ( x: number ): number => x ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided an unsupported number of arguments...
56+
{
57+
shape(); // $ExpectError
58+
shape( zeros( [ 2, 2 ] ) ); // $ExpectError
59+
shape( zeros( [ 2, 2 ] ), false, {} ); // $ExpectError
60+
}

0 commit comments

Comments
 (0)