Skip to content

Commit 52bc84e

Browse files
committed
feat: add ndarray/base/output-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 31c91e5 commit 52bc84e

10 files changed

Lines changed: 643 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
# outputOrder
22+
23+
> Resolves the [order][@stdlib/ndarray/order] (i.e. memory layout) of an output ndarray according to a list of input ndarrays.
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 outputOrder = require( '@stdlib/ndarray/base/output-order' );
41+
```
42+
43+
#### outputOrder( arrays )
44+
45+
Resolves the [order][@stdlib/ndarray/order] (i.e. memory layout) of an output ndarray according to a list of input ndarrays.
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/zeros' );
49+
50+
var x = zeros( [ 2, 2 ] );
51+
var y = zeros( [ 2, 2 ] );
52+
53+
var o = outputOrder( [ x, y ] );
54+
// returns <string>
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **arrays**: list of input ndarrays.
60+
61+
</section>
62+
63+
<!-- /.usage -->
64+
65+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- When the layout differs among input ndarrays, the function returns the [default layout][@stdlib/ndarray/defaults].
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<!-- Package usage examples. -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint-disable max-len -->
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var array = require( '@stdlib/ndarray/array' );
89+
var outputOrder = require( '@stdlib/ndarray/base/output-order' );
90+
91+
// Create ndarrays:
92+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
93+
var y = array( [ [ 5, 6 ], [ 7, 8 ] ] );
94+
var z = array( [ [ 0, 0 ], [ 0, 0 ] ] );
95+
96+
// Resolve a storage layout:
97+
var o = outputOrder( [ x, y, z ] );
98+
// returns <string>
99+
100+
console.log( o );
101+
```
102+
103+
</section>
104+
105+
<!-- /.examples -->
106+
107+
<!-- 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. -->
108+
109+
<section class="references">
110+
111+
</section>
112+
113+
<!-- /.references -->
114+
115+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
116+
117+
<section class="related">
118+
119+
</section>
120+
121+
<!-- /.related -->
122+
123+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124+
125+
<section class="links">
126+
127+
[@stdlib/ndarray/order]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/order
128+
129+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
130+
131+
</section>
132+
133+
<!-- /.links -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 zeros = require( '@stdlib/ndarray/zeros' );
26+
var pkg = require( './../package.json' ).name;
27+
var outputOrder = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Returns an ndarray having a specified layout.
34+
*
35+
* @private
36+
* @param {string} order - storage layout
37+
* @returns {ndarray} ndarray
38+
*/
39+
function array( order ) {
40+
return zeros( [], {
41+
'order': order
42+
});
43+
}
44+
45+
46+
// MAIN //
47+
48+
bench( pkg, function benchmark( b ) {
49+
var arr;
50+
var out;
51+
var i;
52+
53+
arr = [
54+
[ array( 'row-major' ), array( 'row-major') ],
55+
[ array( 'row-major' ), array( 'column-major') ],
56+
[ array( 'column-major' ), array( 'column-major') ]
57+
];
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
out = outputOrder( [ arr[ i%arr.length ] ] );
62+
if ( typeof out !== 'string' ) {
63+
b.fail( 'should return a string' );
64+
}
65+
}
66+
b.toc();
67+
if ( !isString( out ) ) {
68+
b.fail( 'should return a string' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( arrays )
3+
Resolves the order (i.e. memory layout) of an output ndarray according to a
4+
list of input ndarrays.
5+
6+
When the layout differs among input ndarrays, the function returns the
7+
default layout.
8+
9+
Parameters
10+
----------
11+
arrays: Array<ndarray>
12+
List of input ndarrays.
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 o = {{alias}}( [ x, y, z ] )
25+
<string>
26+
27+
See Also
28+
--------
29+
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray, Order } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Resolves the order (i.e. memory layout) of an output ndarray according to a list of input ndarrays.
27+
*
28+
* ## Notes
29+
*
30+
* - When the layout differs among provided ndarrays, the function returns the default layout.
31+
*
32+
* @param arrays - list of input ndarrays
33+
* @returns storage layout
34+
*
35+
* @example
36+
* var zeros = require( '@stdlib/ndarray/zeros' );
37+
*
38+
* var x = zeros( [ 2, 2 ] );
39+
* var y = zeros( [ 2, 2 ] );
40+
*
41+
* var order = outputOrder( [ x, y ] );
42+
* // returns <string>
43+
*/
44+
declare function outputOrder( arrays: ArrayLike<ndarray> ): Order;
45+
46+
47+
// EXPORTS //
48+
49+
export = outputOrder;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 zeros = require( '@stdlib/ndarray/zeros' );
20+
import outputOrder = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a storage layout...
26+
{
27+
const x = zeros( [ 2, 2 ] );
28+
const y = zeros( [ 2, 2 ] );
29+
outputOrder( [ x, y ] ); // $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 ndarrays...
33+
{
34+
outputOrder( true ); // $ExpectError
35+
outputOrder( false ); // $ExpectError
36+
outputOrder( '5' ); // $ExpectError
37+
outputOrder( 123 ); // $ExpectError
38+
outputOrder( {} ); // $ExpectError
39+
outputOrder( ( 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 x = zeros( [ 2, 2 ] );
45+
const y = zeros( [ 2, 2 ] );
46+
outputOrder(); // $ExpectError
47+
outputOrder( [ x, y ], [] ); // $ExpectError
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
var array = require( '@stdlib/ndarray/array' );
22+
var outputOrder = require( './../lib' );
23+
24+
// Create ndarrays:
25+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
26+
var y = array( [ [ 5, 6 ], [ 7, 8 ] ] );
27+
var z = array( [ [ 0, 0 ], [ 0, 0 ] ] );
28+
29+
// Resolve a storage layout:
30+
var o = outputOrder( [ x, y, z ] );
31+
// returns <string>
32+
33+
console.log( o );

0 commit comments

Comments
 (0)