Skip to content

Commit 857a8c7

Browse files
committed
feat: add ndarray/base/order
1 parent 4227c05 commit 857a8c7

File tree

10 files changed

+809
-0
lines changed

10 files changed

+809
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# order
22+
23+
> Return the [layout order][@stdlib/ndarray/orders] of a provided [ndarray][@stdlib/ndarray/base/ctor].
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 order = require( '@stdlib/ndarray/base/order' );
41+
```
42+
43+
#### order( x )
44+
45+
Returns the [layout order][@stdlib/ndarray/orders] of a provided [ndarray][@stdlib/ndarray/base/ctor].
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/zeros' );
49+
50+
var x = zeros( [ 3, 2, 3 ], {
51+
'order': 'row-major'
52+
});
53+
// returns <ndarray>
54+
55+
var out = order( x );
56+
// returns 'row-major'
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<!-- Package usage examples. -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
<!-- eslint-disable new-cap -->
80+
81+
```javascript
82+
var zeros = require( '@stdlib/ndarray/zeros' );
83+
var slice = require( '@stdlib/ndarray/slice' );
84+
var E = require( '@stdlib/slice/multi' );
85+
var S = require( '@stdlib/slice/ctor' );
86+
var order = require( '@stdlib/ndarray/base/order' );
87+
88+
// Create an array:
89+
var x = zeros( [ 10, 10, 10, 10 ] );
90+
// returns <ndarray>
91+
92+
// Define some slices:
93+
var slices = [
94+
// :,:,:,:
95+
E( null, null, null, null ),
96+
97+
// 5:10,4,2:4,::-1
98+
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),
99+
100+
// :,:,2,:
101+
E( null, null, 2, null ),
102+
103+
// 1,2,3,:
104+
E( 1, 2, 3, null ),
105+
106+
// 1,3,::2,4::2
107+
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
108+
];
109+
110+
// Determine the layout order for each slice...
111+
var s;
112+
var i;
113+
for ( i = 0; i < slices.length; i++ ) {
114+
s = slice( x, slices[ i ] );
115+
console.log( '%s => %s', s.order, order( s ) );
116+
}
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- 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. -->
124+
125+
<section class="references">
126+
127+
</section>
128+
129+
<!-- /.references -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
</section>
136+
137+
<!-- /.related -->
138+
139+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
140+
141+
<section class="links">
142+
143+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib
144+
145+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib
146+
147+
</section>
148+
149+
<!-- /.links -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 typedarray = require( '@stdlib/array/typed' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var pkg = require( './../package.json' ).name;
28+
var order = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var out;
36+
var i;
37+
38+
values = [
39+
ndarray( 'float64', typedarray( 20000, 'float64' ), [ 10, 10, 10, 1 ], [ 1000, 100, 10, 1 ], 100, 'row-major' ),
40+
ndarray( 'int32', typedarray( 20000, 'int32' ), [ 5, 5, 5, 1, 1 ], [ 125, 25, 5, 1, 1 ], 50, 'column-major' ),
41+
ndarray( 'uint8', typedarray( 20000, 'uint8' ), [ 3, 4, 5 ], [ 20, 5, 1 ], 72, 'row-major' )
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = order( values[ i%values.length ] );
47+
if ( typeof out !== 'string' ) {
48+
b.fail( 'should return a string' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isString( out ) ) {
53+
b.fail( 'should return a string' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( x )
3+
Returns the layout order of a provided ndarray.
4+
5+
If unable to resolve a layout order, the function returns `null`.
6+
7+
Parameters
8+
----------
9+
x: ndarray
10+
Input ndarray.
11+
12+
Returns
13+
-------
14+
out: string|null
15+
Layout order.
16+
17+
Examples
18+
--------
19+
> var opts = { 'order': 'row-major' };
20+
> var dt = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 3, 3, 3 ], opts ) )
21+
'row-major'
22+
23+
See Also
24+
--------
25+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
* Returns the layout order of a provided ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - If unable to resolve a layout order, the function returns `null`.
31+
*
32+
* @param x - input ndarray
33+
* @returns layout order (or null)
34+
*
35+
* @example
36+
* var zeros = require( `@stdlib/ndarray/zeros` );
37+
*
38+
* var x = zeros( [ 3, 3, 3 ], {
39+
* 'order': 'row-major'
40+
* });
41+
*
42+
* var o = order( x );
43+
* // returns 'row-major'
44+
*/
45+
declare function order( x: ndarray ): Order | null;
46+
47+
48+
// EXPORTS //
49+
50+
export = order;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 order = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a layout order...
26+
{
27+
order( zeros( [ 3, 2, 1 ] ) ); // $ExpectType Order | null
28+
}
29+
30+
// The compiler throws an error if the function is provided a value other than an ndarray...
31+
{
32+
order( '5' ); // $ExpectError
33+
order( 5 ); // $ExpectError
34+
order( true ); // $ExpectError
35+
order( false ); // $ExpectError
36+
order( null ); // $ExpectError
37+
order( undefined ); // $ExpectError
38+
order( [ '1', '2' ] ); // $ExpectError
39+
order( {} ); // $ExpectError
40+
order( ( x: number ): number => x ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided an unsupported number of arguments...
44+
{
45+
order(); // $ExpectError
46+
order( zeros( [ 2, 2 ] ), {} ); // $ExpectError
47+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
/* eslint-disable new-cap */
20+
21+
'use strict';
22+
23+
var zeros = require( '@stdlib/ndarray/zeros' );
24+
var slice = require( '@stdlib/ndarray/slice' );
25+
var E = require( '@stdlib/slice/multi' );
26+
var S = require( '@stdlib/slice/ctor' );
27+
var order = require( './../lib' );
28+
29+
// Create an array:
30+
var x = zeros( [ 10, 10, 10, 10 ] );
31+
// returns <ndarray>
32+
33+
// Define some slices:
34+
var slices = [
35+
// :,:,:,:
36+
E( null, null, null, null ),
37+
38+
// 5:10,4,2:4,::-1
39+
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),
40+
41+
// :,:,2,:
42+
E( null, null, 2, null ),
43+
44+
// 1,2,3,:
45+
E( 1, 2, 3, null ),
46+
47+
// 1,3,::2,4::2
48+
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
49+
];
50+
51+
// Determine the layout order of each slice...
52+
var s;
53+
var i;
54+
for ( i = 0; i < slices.length; i++ ) {
55+
s = slice( x, slices[ i ] );
56+
console.log( '%s => %s', s.order, order( s ) );
57+
}

0 commit comments

Comments
 (0)