Skip to content

Commit 6e74647

Browse files
committed
feat: add ndarray/base/assert/has-equal-shape
1 parent 9a87aca commit 6e74647

File tree

10 files changed

+631
-0
lines changed

10 files changed

+631
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# hasEqualShape
22+
23+
> Test if two ndarrays have the same shape.
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 hasEqualShape = require( '@stdlib/ndarray/base/assert/has-equal-shape' );
41+
```
42+
43+
#### hasEqualShape( x, y )
44+
45+
Tests if two ndarrays have the same shape.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ 1, 2, 3, 4 ] );
51+
var y = array( [ 5, 6, 7, 8 ] );
52+
53+
var bool = hasEqualShape( x, y );
54+
// returns true
55+
```
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 array = require( '@stdlib/ndarray/array' );
79+
var hasEqualShape = require( '@stdlib/ndarray/base/assert/has-equal-shape' );
80+
81+
var x1 = array( [ [ 1, 2 ], [ 3, 4 ] ] );
82+
var y1 = array( [ [ 5, 6 ], [ 7, 8 ] ] );
83+
84+
var bool = hasEqualShape( x1, y1 );
85+
// returns true
86+
87+
var x2 = array( [ [ 1, 2 ], [ 3, 4 ] ] );
88+
var y2 = array( [ [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ] );
89+
90+
bool = hasEqualShape( x2, y2 );
91+
// returns false
92+
```
93+
94+
</section>
95+
96+
<!-- /.examples -->
97+
98+
<!-- 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. -->
99+
100+
<section class="references">
101+
102+
</section>
103+
104+
<!-- /.references -->
105+
106+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
107+
108+
<section class="related">
109+
110+
</section>
111+
112+
<!-- /.related -->
113+
114+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
115+
116+
<section class="links">
117+
118+
</section>
119+
120+
<!-- /.links -->
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var array = require( '@stdlib/ndarray/array' );
26+
var pkg = require( './../package.json' ).name;
27+
var hasEqualShape = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::true', function benchmark( b ) {
33+
var values;
34+
var out;
35+
var v;
36+
var i;
37+
38+
values = [
39+
array( [ [ 1, 2 ], [ 3, 4 ] ] ),
40+
array( [ [ 1, 2 ], [ 3, 4 ] ] )
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
v = values[ i%values.length ];
46+
out = hasEqualShape( v, v );
47+
if ( typeof out !== 'boolean' ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isBoolean( out ) ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+'::false', function benchmark( b ) {
60+
var values;
61+
var out;
62+
var v1;
63+
var v2;
64+
var i;
65+
66+
values = [
67+
array( [ [ 1, 2 ], [ 3, 4 ] ] ),
68+
array( [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] )
69+
];
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
v1 = values[ i%values.length ];
74+
v2 = values[ (i+1)%values.length ];
75+
out = hasEqualShape( v1, v2 );
76+
if ( typeof out !== 'boolean' ) {
77+
b.fail( 'should return a boolean' );
78+
}
79+
}
80+
b.toc();
81+
if ( !isBoolean( out ) ) {
82+
b.fail( 'should return a boolean' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( x, y )
3+
Tests if two ndarrays have the same shape.
4+
5+
Parameters
6+
----------
7+
x: ndarray
8+
First input ndarray.
9+
10+
y: ndarray
11+
Second input ndarray.
12+
13+
Returns
14+
-------
15+
bool: boolean
16+
Boolean indicating if both ndarrays have the same shape.
17+
18+
Examples
19+
--------
20+
> var x = {{alias:@stdlib/ndarray/array}}( [ 1, 2, 3, 4 ] );
21+
> var y = {{alias:@stdlib/ndarray/array}}( [ 5, 6, 7, 8 ] );
22+
> var bool = {{alias}}( x, y )
23+
true
24+
25+
See Also
26+
--------
27+
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) 2024 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 } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Tests whether two ndarrays have the same shape.
27+
*
28+
* @param x - first input ndarray
29+
* @param y - second input ndarray
30+
* @returns boolean indicating whether two ndarrays have the same shape
31+
*
32+
* @example
33+
* var array = require( '@stdlib/ndarray/array' );
34+
*
35+
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
36+
* var y = array( [ [ 5, 6 ], [ 7, 8 ] ] );
37+
*
38+
* var bool = hasEqualShape( x, y );
39+
* // returns true
40+
*/
41+
declare function hasEqualShape( x: ndarray, y: ndarray ): boolean;
42+
43+
44+
// EXPORTS //
45+
46+
export = hasEqualShape;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 array = require( '@stdlib/ndarray/array' );
20+
import hasEqualShape = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a boolean...
26+
{
27+
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
28+
29+
hasEqualShape( x, x ); // $ExpectType boolean
30+
}
31+
32+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
33+
{
34+
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
35+
36+
hasEqualShape( '5', x ); // $ExpectError
37+
hasEqualShape( 5, x ); // $ExpectError
38+
hasEqualShape( true, x ); // $ExpectError
39+
hasEqualShape( false, x ); // $ExpectError
40+
hasEqualShape( null, x ); // $ExpectError
41+
hasEqualShape( [], x ); // $ExpectError
42+
hasEqualShape( {}, x ); // $ExpectError
43+
hasEqualShape( ( x: number ): number => x, x ); // $ExpectError
44+
}
45+
46+
// The compiler throws an error if the function is provided a second argument which is not an ndarray...
47+
{
48+
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
49+
50+
hasEqualShape( x, '5' ); // $ExpectError
51+
hasEqualShape( x, 5 ); // $ExpectError
52+
hasEqualShape( x, true ); // $ExpectError
53+
hasEqualShape( x, false ); // $ExpectError
54+
hasEqualShape( x, null ); // $ExpectError
55+
hasEqualShape( x, [] ); // $ExpectError
56+
hasEqualShape( x, {} ); // $ExpectError
57+
hasEqualShape( x, ( x: number ): number => x ); // $ExpectError
58+
}
59+
60+
// The compiler throws an error if the function is provided an unsupported number of arguments...
61+
{
62+
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
63+
64+
hasEqualShape(); // $ExpectError
65+
hasEqualShape( x ); // $ExpectError
66+
hasEqualShape( x, x, {} ); // $ExpectError
67+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 array = require( '@stdlib/ndarray/array' );
22+
var hasEqualShape = require( './../lib' );
23+
24+
var x1 = array( [ [ 1, 2 ], [ 3, 4 ] ] );
25+
var y1 = array( [ [ 5, 6 ], [ 7, 8 ] ] );
26+
27+
var bool = hasEqualShape( x1, y1 );
28+
console.log( bool );
29+
// => true
30+
31+
var x2 = array( [ [ 1, 2 ], [ 3, 4 ] ] );
32+
var y2 = array( [ [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ] );
33+
34+
bool = hasEqualShape( x2, y2 );
35+
console.log( bool );
36+
// => false

0 commit comments

Comments
 (0)