Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Jan 27, 2024
1 parent 74ab3ff commit 1079580
Show file tree
Hide file tree
Showing 18 changed files with 752 additions and 14 deletions.
4 changes: 2 additions & 2 deletions base/cartesian-product/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var resolveGetter = require( './../../../base/resolve-getter' );
/**
* Returns the Cartesian product.
*
* @param {ArrayLikeObject} x1 - first input array
* @param {ArrayLikeObject} x2 - second input array
* @param {Collection} x1 - first input array
* @param {Collection} x2 - second input array
* @returns {Array<Array>} list of ordered tuples comprising the Cartesian product
*
* @example
Expand Down
4 changes: 2 additions & 2 deletions base/cartesian-square/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/array';
import { Collection, AccessorArrayLike } from '@stdlib/types/array';

/**
* Returns the Cartesian square.
Expand All @@ -38,7 +38,7 @@ import { Collection } from '@stdlib/types/array';
* var out = cartesianSquare( x );
* // returns [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
*/
declare function cartesianSquare<T = unknown>( x: Collection<T> ): Array<Array<T>>;
declare function cartesianSquare<T = unknown>( x: Collection<T> | AccessorArrayLike<T> ): Array<Array<T>>;


// EXPORTS //
Expand Down
13 changes: 10 additions & 3 deletions base/cartesian-square/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

'use strict';

// MODULES //

var resolveGetter = require( './../../../base/resolve-getter' );


// MAIN //

/**
* Returns the Cartesian square.
*
* @param {ArrayLikeObject} x - input array
* @param {Collection} x - input array
* @returns {Array<Array>} list of ordered tuples comprising the Cartesian product
*
* @example
Expand All @@ -33,18 +38,20 @@
* // returns [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
*/
function cartesianSquare( x ) {
var get;
var out;
var N;
var v;
var i;
var j;

get = resolveGetter( x );
N = x.length;
out = [];
for ( i = 0; i < N; i++ ) {
v = x[ i ];
v = get( x, i );
for ( j = 0; j < N; j++ ) {
out.push( [ v, x[ j ] ] );
out.push( [ v, get( x, j ) ] );
}
}
return out;
Expand Down
43 changes: 41 additions & 2 deletions base/cartesian-square/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

var tape = require( 'tape' );
var toAccessorArray = require( './../../../base/to-accessor-array' );
var cartesianSquare = require( './../lib' );


Expand All @@ -32,7 +33,7 @@ tape( 'main export is a function', function test( t ) {
t.end();
});

tape( 'the function returns the Cartesian square', function test( t ) {
tape( 'the function returns the Cartesian square (indexed)', function test( t ) {
var expected;
var actual;

Expand Down Expand Up @@ -61,11 +62,49 @@ tape( 'the function returns the Cartesian square', function test( t ) {
t.end();
});

tape( 'the function returns an empty array if provided an empty array', function test( t ) {
tape( 'the function returns the Cartesian square (accessors)', function test( t ) {
var expected;
var actual;

actual = cartesianSquare( toAccessorArray( [ 1, 2 ] ) );
expected = [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ];
t.deepEqual( actual, expected, 'returns expected value' );

actual = cartesianSquare( toAccessorArray( [ 1 ] ) );
expected = [ [ 1, 1 ] ];
t.deepEqual( actual, expected, 'returns expected value' );

actual = cartesianSquare( toAccessorArray( [ 1, 2, 3 ] ) );
expected = [
[ 1, 1 ],
[ 1, 2 ],
[ 1, 3 ],
[ 2, 1 ],
[ 2, 2 ],
[ 2, 3 ],
[ 3, 1 ],
[ 3, 2 ],
[ 3, 3 ]
];
t.deepEqual( actual, expected, 'returns expected value' );

t.end();
});

tape( 'the function returns an empty array if provided an empty array (indexed)', function test( t ) {
var actual;

actual = cartesianSquare( [] );
t.deepEqual( actual, [], 'returns expected value' );

t.end();
});

tape( 'the function returns an empty array if provided an empty array (accessors)', function test( t ) {
var actual;

actual = cartesianSquare( toAccessorArray( [] ) );
t.deepEqual( actual, [], 'returns expected value' );

t.end();
});
97 changes: 97 additions & 0 deletions cartesian-square/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!--
@license Apache-2.0
Copyright (c) 2024 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# cartesianSquare

> Return the [Cartesian square][cartesian-product].
<section class="usage">

## Usage

```javascript
var cartesianSquare = require( '@stdlib/array/cartesian-square' );
```

#### cartesianSquare( x )

Returns the [Cartesian square][cartesian-product].

```javascript
var x = [ 1, 2 ];

var out = cartesianSquare( x );
// returns [ [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 2 ] ]
```

If provided an empty array, the function returns an empty array.

```javascript
var out = cartesianSquare( [] );
// returns []
```

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var linspace = require( '@stdlib/array/linspace' );
var cartesianSquare = require( '@stdlib/array/cartesian-square' );

var x = linspace( 0, 5, 6 );

var out = cartesianSquare( x );
// returns [ [ 0, 0 ], [ 0, 1 ], ..., [ 5, 4 ], [ 5, 5 ] ]
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[cartesian-product]: https://en.wikipedia.org/wiki/Cartesian_product

</section>

<!-- /.links -->
52 changes: 52 additions & 0 deletions cartesian-square/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isArrayArray = require( '@stdlib/assert/is-array-array' );
var zeroTo = require( './../../zero-to' );
var pkg = require( './../package.json' ).name;
var cartesianSquare = require( './../lib' );


// MAIN //

bench( pkg+':len=100', function benchmark( b ) {
var x;
var i;
var v;

x = zeroTo( 10 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = cartesianSquare( x );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArrayArray( v ) ) {
b.fail( 'should return an array of arrays' );
}
b.pass( 'benchmark finished' );
b.end();
});
95 changes: 95 additions & 0 deletions cartesian-square/benchmark/benchmark.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isArrayArray = require( '@stdlib/assert/is-array-array' );
var pkg = require( './../package.json' ).name;
var cartesianSquare = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = discreteUniform( len, 0, 3 );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var v;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = cartesianSquare( x );
if ( typeof v !== 'object' ) {
b.fail( 'should return an array' );
}
}
b.toc();
if ( !isArrayArray( v ) ) {
b.fail( 'should return an array of arrays' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 2^min
max = 10; // 2^max

for ( i = min; i <= max; i++ ) {
len = pow( 2, i );
f = createBenchmark( len );
bench( pkg+':len='+(len*len), f );
}
}

main();
Loading

0 comments on commit 1079580

Please sign in to comment.