Skip to content

Commit b3ec3d3

Browse files
committed
feat: add ndarray/base/consensus-order
--- 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 8ac6900 commit b3ec3d3

10 files changed

Lines changed: 817 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# consensusOrder
22+
23+
> Resolve the most common underlying storage layout.
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 consensusOrder = require( '@stdlib/ndarray/base/consensus-order' );
41+
```
42+
43+
#### consensusOrder( strides )
44+
45+
Resolves the most common underlying storage [layout][@stdlib/ndarray/order].
46+
47+
```javascript
48+
// Define the strides for input arrays:
49+
var stridesX = [ 2, 1 ]; // row-major
50+
var stridesY = [ 4, 2 ]; // row-major
51+
52+
// Define the strides for an output array:
53+
var stridesZ = [ 1, 2 ]; // column-major
54+
55+
// Resolve the most common layout:
56+
var o = consensusOrder( [ stridesX, stridesY, stridesZ ] );
57+
// returns 'row-major'
58+
```
59+
60+
The function accepts the following arguments:
61+
62+
- **strides**: list of stride arrays.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
## Notes
73+
74+
- In the event of a tie or when all input array strides are "disorganized" (i.e., neither row- nor column-major), the function returns the [default layout][@stdlib/ndarray/defaults].
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<!-- Package usage examples. -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint-disable max-len -->
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var array = require( '@stdlib/ndarray/array' );
92+
var getStrides = require( '@stdlib/ndarray/strides' );
93+
var consensusOrder = require( '@stdlib/ndarray/base/consensus-order' );
94+
95+
// Create ndarrays:
96+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
97+
var y = array( [ [ 5, 6 ], [ 7, 8 ] ] );
98+
var z = array( [ [ 0, 0 ], [ 0, 0 ] ] );
99+
100+
// Resolve the most common layout:
101+
var o = consensusOrder( [ getStrides( x ), getStrides( y ), getStrides( z ) ] );
102+
// returns <string>
103+
104+
console.log( o );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- 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. -->
112+
113+
<section class="references">
114+
115+
</section>
116+
117+
<!-- /.references -->
118+
119+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
120+
121+
<section class="related">
122+
123+
</section>
124+
125+
<!-- /.related -->
126+
127+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
128+
129+
<section class="links">
130+
131+
[@stdlib/ndarray/order]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/order
132+
133+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
134+
135+
</section>
136+
137+
<!-- /.links -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var consensusOrder = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::row-major', pkg ), function benchmark( b ) {
34+
var strides;
35+
var factors;
36+
var shape;
37+
var out;
38+
var i;
39+
40+
shape = [ 10, 10, 10 ];
41+
strides = shape2strides( shape, 'row-major' );
42+
factors = [
43+
-1,
44+
1
45+
];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
strides[ i%shape.length ] *= factors[ i%factors.length ];
50+
out = consensusOrder( [ strides, strides, strides ] );
51+
if ( typeof out !== 'string' ) {
52+
b.fail( 'should return a string' );
53+
}
54+
}
55+
b.toc();
56+
if ( !isString( out ) ) {
57+
b.fail( 'should return a string' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});
62+
63+
bench( format( '%s::column-major', pkg ), function benchmark( b ) {
64+
var strides;
65+
var factors;
66+
var shape;
67+
var out;
68+
var i;
69+
70+
shape = [ 10, 10, 10 ];
71+
strides = shape2strides( shape, 'column-major' );
72+
factors = [
73+
-1,
74+
1
75+
];
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
strides[ i%shape.length ] *= factors[ i%factors.length ];
80+
out = consensusOrder( [ strides, strides, strides ] );
81+
if ( typeof out !== 'string' ) {
82+
b.fail( 'should return a string' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isString( out ) ) {
87+
b.fail( 'should return a string' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( strides )
3+
Resolves the most common underlying storage layout.
4+
5+
In the event of a tie or when all input array strides are "disorganized"
6+
(i.e., neither row- nor column-major), the function returns the default
7+
layout.
8+
9+
Parameters
10+
----------
11+
strides: Array<Array>
12+
List of stride arrays.
13+
14+
Returns
15+
-------
16+
out: string
17+
Storage layout.
18+
19+
Examples
20+
--------
21+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
22+
> var y = {{alias:@stdlib/ndarray/array}}( [ [ 5, 6 ], [ 7, 8 ] ] );
23+
> var z = {{alias:@stdlib/ndarray/array}}( [ [ 0, 0 ], [ 0, 0 ] ] );
24+
> var sx = {{alias:@stdlib/ndarray/strides}}( x );
25+
> var sy = {{alias:@stdlib/ndarray/strides}}( y );
26+
> var sz = {{alias:@stdlib/ndarray/strides}}( z );
27+
> var o = {{alias}}( [ sx, sy, sz ] )
28+
<string>
29+
30+
See Also
31+
--------
32+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 { ArrayLike, Collection } from '@stdlib/types/array';
24+
import { Order } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Reorders ndarray dimensions and associated strides for loop interchange.
28+
*
29+
* ## Notes
30+
*
31+
* - In the event of a tie or when all input array strides are "disorganized" (i.e., neither row- nor column-major), the function returns the default layout.
32+
*
33+
* @param strides - list of array strides
34+
* @returns storage layout
35+
*
36+
* @example
37+
* var strides = [ [ 2, 1 ], [ 4, 1 ] ];
38+
*
39+
* var order = consensusOrder( strides );
40+
* // returns 'row-major'
41+
*
42+
* @example
43+
* var strides = [ [ 1, 2 ], [ 1, 4 ] ];
44+
*
45+
* var order = consensusOrder( strides );
46+
* // returns 'column-major'
47+
*
48+
* @example
49+
* var strides = [ [ 2, 1 ], [ 1, 4 ] ];
50+
*
51+
* var order = consensusOrder( strides );
52+
* // returns <string>
53+
*
54+
* @example
55+
* var strides = [ [ 1, 1 ], [ 1, 1 ] ];
56+
*
57+
* var order = consensusOrder( strides );
58+
* // returns <string>
59+
*/
60+
declare function consensusOrder( strides: ArrayLike<Collection<number>> ): Order;
61+
62+
63+
// EXPORTS //
64+
65+
export = consensusOrder;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 consensusLayout = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a storage layout...
25+
{
26+
const sx = [ 2, 1 ];
27+
const sy = [ 2, 1 ];
28+
const sz = [ 4, 2 ];
29+
consensusLayout( [ sx, sy, sz ] ); // $ExpectType Layout
30+
}
31+
32+
// The compiler throws an error if the function is provided a first argument which is not an array-like object of numbers...
33+
{
34+
consensusLayout( true ); // $ExpectError
35+
consensusLayout( false ); // $ExpectError
36+
consensusLayout( '5' ); // $ExpectError
37+
consensusLayout( 123 ); // $ExpectError
38+
consensusLayout( {} ); // $ExpectError
39+
consensusLayout( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided an unsupported number of arguments...
43+
{
44+
const sx = [ 2, 1 ];
45+
const sy = [ 2, 1 ];
46+
const sz = [ 4, 2 ];
47+
consensusLayout(); // $ExpectError
48+
consensusLayout( [ sx, sy, sz ], [] ); // $ExpectError
49+
}

0 commit comments

Comments
 (0)