Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/to-flippedud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# toFlippedud

> Return a new ndarray where the order of elements along the second-to-last dimension of an input ndarray is reversed.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var toFlippedud = require( '@stdlib/ndarray/base/to-flippedud' );
```

#### toFlippedud( x )

Returns a new ndarray where the order of elements along the second-to-last dimension of an input ndarray is reversed.

```javascript
var ndarray = require( '@stdlib/ndarray/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
var shape = [ 3, 2 ];
var strides = [ 2, 1 ];
var offset = 0;

var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
// returns <ndarray>

var sh = x.shape;
// returns [ 3, 2 ]

var arr = ndarray2array( x );
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]

var y = toFlippedud( x );
// returns <ndarray>

sh = y.shape;
// returns [ 3, 2 ]

arr = ndarray2array( y );
// returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ]
```

The function accepts the following arguments:

- **x**: input ndarray.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If provided a zero-dimensional ndarray, as the ndarray has no dimensions to reverse, the function simply returns a copy the input ndarray. Similarly, if provided a one-dimensional ndarray, as the ndarray has only one dimension, the function simply returns a copy of the input ndarray.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

```javascript
var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var toFlippedud = require( '@stdlib/ndarray/base/to-flippedud' );

// Create a linear ndarray buffer:
var buf = zeroTo( 16 );

// Create a three-dimensional ndarray:
var x = array( buf, {
'shape': [ 2, 4, 2 ]
});

// Reverse the order of second-to-last dimension:
var y = toFlippedud( x );
// returns <ndarray>

var a = ndarray2array( y );
console.log( a );
// => [ [ [ 6, 7 ], [ 4, 5 ], [ 2, 3 ], [ 0, 1 ] ], [ [ 14, 15 ], [ 12, 13 ], [ 10, 11 ], [ 8, 9 ] ] ]
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- 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">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var baseCtor = require( '@stdlib/ndarray/base/ctor' );
var pkg = require( './../package.json' ).name;
var toFlippedud = require( './../lib' );


// MAIN //

bench( pkg+':ndims=1', function benchmark( b ) {
var values;
var out;
var i;

values = [
baseCtor( 'float64', zeroTo( 10 ), [ 10 ], [ 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 5 ), [ 5 ], [ 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 20 ), [ 20 ], [ 1 ], 0, 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = toFlippedud( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( out ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':ndims=2,len=100', function benchmark( b ) {
var values;
var out;
var i;

values = [
baseCtor( 'float64', zeroTo( 100 ), [ 10, 10 ], [ 10, 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 100 ), [ 5, 20 ], [ 20, 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 100 ), [ 20, 5 ], [ 5, 1 ], 0, 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = toFlippedud( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( out ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':ndims=3,len=1000', function benchmark( b ) {
var values;
var out;
var i;

values = [
baseCtor( 'float64', zeroTo( 1000 ), [ 10, 10, 10 ], [ 100, 10, 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 1000 ), [ 5, 20, 10 ], [ 200, 10, 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 1000 ), [ 20, 5, 10 ], [ 50, 10, 1 ], 0, 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = toFlippedud( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( out ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':ndims=2,len=10000', function benchmark( b ) {
var values;
var out;
var i;

values = [
baseCtor( 'float64', zeroTo( 10000 ), [ 100, 100 ], [ 100, 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 10000 ), [ 50, 200 ], [ 200, 1 ], 0, 'row-major' ),
baseCtor( 'float64', zeroTo( 10000 ), [ 200, 50 ], [ 50, 1 ], 0, 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = toFlippedud( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( out ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});
31 changes: 31 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/to-flippedud/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{{alias}}( x )
Returns a new ndarray where the order of elements along the second-to-last
dimension of an input ndarray is reversed.

Parameters
----------
x: ndarray
Input array.

Returns
-------
out: ndarray
Output array.

Examples
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] )
<ndarray>
> x.shape
[ 3, 2 ]
> var y = {{alias}}( x )
<ndarray>
> y.shape
[ 3, 2 ]
> {{alias:@stdlib/ndarray/to-array}}( y )
[ [ 5, 6 ], [ 3, 4 ], [ 1, 2 ] ]

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 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.
*/

// TypeScript Version: 4.1

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

import { ndarray } from '@stdlib/types/ndarray';

/**
* Returns a new ndarray where the order of elements along the second-to-last dimension of an input ndarray is reversed.
*
* @param x - input array
* @returns output array
*
* @example
* var typedarray = require( '@stdlib/array/typed' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* var buffer = typedarray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], 'float64' );
* var shape = [ 3, 2 ];
* var strides = [ 2, 1 ];
* var offset = 0;
*
* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
* // returns <ndarray>
*
* var sh = x.shape;
* // returns [ 3, 2 ]
*
* var arr = ndarray2array( x );
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
*
* var y = toFlippedud( x );
* // returns <ndarray>
*
* sh = y.shape;
* // returns [ 3, 2 ]
*
* arr = ndarray2array( y );
* // returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ]
*/
declare function toFlippedud<T extends ndarray>( x: T ): T;


// EXPORTS //

export = toFlippedud;
Loading