Skip to content

Commit 325f217

Browse files
committed
feat: add array/cartesian-square
1 parent f34fcb6 commit 325f217

File tree

11 files changed

+683
-0
lines changed

11 files changed

+683
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
# cartesianSquare
22+
23+
> Return the [Cartesian square][cartesian-product].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var cartesianSquare = require( '@stdlib/array/cartesian-square' );
31+
```
32+
33+
#### cartesianSquare( x )
34+
35+
Returns the [Cartesian square][cartesian-product].
36+
37+
```javascript
38+
var x = [ 1, 2 ];
39+
40+
var out = cartesianSquare( x );
41+
// returns [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
42+
```
43+
44+
If provided an empty array, the function returns an empty array.
45+
46+
```javascript
47+
var out = cartesianSquare( [] );
48+
// returns []
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
</section>
58+
59+
<!-- /.notes -->
60+
61+
<section class="examples">
62+
63+
## Examples
64+
65+
<!-- eslint no-undef: "error" -->
66+
67+
```javascript
68+
var linspace = require( '@stdlib/array/linspace' );
69+
var cartesianSquare = require( '@stdlib/array/cartesian-square' );
70+
71+
var x = linspace( 0, 5, 6 );
72+
73+
var out = cartesianSquare( x );
74+
// returns [ [ 0, 0 ], [ 0, 1 ], ..., [ 5, 4 ], [ 5, 5 ] ]
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
82+
83+
<section class="related">
84+
85+
</section>
86+
87+
<!-- /.related -->
88+
89+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="links">
92+
93+
[cartesian-product]: https://en.wikipedia.org/wiki/Cartesian_product
94+
95+
</section>
96+
97+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 isArrayArray = require( '@stdlib/assert/is-array-array' );
25+
var zeroTo = require( '@stdlib/array/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var cartesianSquare = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':len=100', function benchmark( b ) {
33+
var x;
34+
var i;
35+
var v;
36+
37+
x = zeroTo( 10 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
v = cartesianSquare( x );
42+
if ( typeof v !== 'object' ) {
43+
b.fail( 'should return an array' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isArrayArray( v ) ) {
48+
b.fail( 'should return an array of arrays' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var cartesianSquare = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = discreteUniform( len, 0, 3 );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = cartesianSquare( x );
57+
if ( typeof v !== 'object' ) {
58+
b.fail( 'should return an array' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isArrayArray( v ) ) {
63+
b.fail( 'should return an array of arrays' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 2^min
86+
max = 10; // 2^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 2, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':len='+(len*len), f );
92+
}
93+
}
94+
95+
main();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
{{alias}}( x )
3+
Returns the Cartesian square.
4+
5+
If provided an empty array, the function returns an empty array.
6+
7+
Parameters
8+
----------
9+
x: ArrayLikeObject
10+
Input array.
11+
12+
Returns
13+
-------
14+
out: Array<Array>
15+
Cartesian product.
16+
17+
Examples
18+
--------
19+
> var out = {{alias}}( [ 1, 2 ] )
20+
[ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
21+
22+
See Also
23+
--------
24+
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 { Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns the Cartesian square.
27+
*
28+
* ## Notes
29+
*
30+
* - If provided an empty array, the function returns an empty array.
31+
*
32+
* @param x - input array
33+
* @returns Cartesian product
34+
*
35+
* @example
36+
* var x = [ 1, 2 ];
37+
*
38+
* var out = cartesianSquare( x );
39+
* // returns [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
40+
*/
41+
declare function cartesianSquare<T = unknown>( x: Collection<T> | AccessorArrayLike<T> ): Array<Array<T>>;
42+
43+
44+
// EXPORTS //
45+
46+
export = cartesianSquare;
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+
import cartesianSquare = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of arrays...
25+
{
26+
cartesianSquare( [ 1, 2, 3, 4 ] ); // $ExpectType number[][]
27+
cartesianSquare<number>( [ 1, 2, 3, 4 ] ); // $ExpectType number[][]
28+
cartesianSquare<string>( [ '1', '2', '3', '4' ] ); // $ExpectType string[][]
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not an array-like object...
32+
{
33+
cartesianSquare( 1 ); // $ExpectError
34+
cartesianSquare( true ); // $ExpectError
35+
cartesianSquare( false ); // $ExpectError
36+
cartesianSquare( null ); // $ExpectError
37+
cartesianSquare( void 0 ); // $ExpectError
38+
cartesianSquare( {} ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an unsupported number of arguments...
42+
{
43+
cartesianSquare(); // $ExpectError
44+
cartesianSquare( [], [] ); // $ExpectError
45+
cartesianSquare( [], [], [] ); // $ExpectError
46+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 linspace = require( '@stdlib/array/linspace' );
22+
var cartesianSquare = require( './../lib' );
23+
24+
var x = linspace( 0, 5, 6 );
25+
26+
var out = cartesianSquare( x );
27+
console.log( out );
28+
// => [ [ 0, 0 ], [ 0, 1 ], ..., [ 5, 4 ], [ 5, 5 ] ]

0 commit comments

Comments
 (0)