Skip to content
Draft
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
264 changes: 264 additions & 0 deletions lib/node_modules/@stdlib/lapack/base/dlapmr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
<!--

@license Apache-2.0

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

-->

# dlapmr

> Rearrange the rows of a matrix as specified by a permutation vector.

<section class="usage">

## Usage

```javascript
var dlapmr = require( '@stdlib/lapack/base/dlapmr' );
```

#### dlapmr( order, forwrd, M, N, X, LDX, K, strideK )

Rearranges the rows of an `M` by `N` matrix `X` as specified by a permutation vector `K`.

```javascript
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); // [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
var K = new Int32Array( [ 2, 0, 1 ] );

dlapmr( 'row-major', true, 3, 2, X, 2, K, 1 );
// X => <Float64Array>[ 5.0, 6.0, 1.0, 2.0, 3.0, 4.0 ]
```

The function has the following parameters:

- **order**: storage layout.
- **forwrd**: boolean indicating whether to apply a forward or backward permutation. If `true`, forward permutation: `X(K(I),*)` is moved to `X(I,*)`. If `false`, backward permutation: `X(I,*)` is moved to `X(K(I),*)`.
- **M**: number of rows of `X`.
- **N**: number of columns of `X`.
- **X**: input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
- **LDX**: stride of the first dimension of `X` (a.k.a., leading dimension of the matrix `X`).
- **K**: permutation vector as an [`Int32Array`][mdn-int32array].
- **strideK**: stride length of `K`.

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

<!-- eslint-disable stdlib/capitalized-comments -->

```javascript
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

// Initial arrays...
var X0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var K0 = new Int32Array( [ 0, 2, 0, 1 ] );

// Create offset views...
var X1 = new Float64Array( X0.buffer, X0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var K1 = new Int32Array( K0.buffer, K0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

dlapmr( 'row-major', true, 3, 2, X1, 2, K1, 1 );
// X0 => <Float64Array>[ 0.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0 ]
```

#### dlapmr.ndarray( forwrd, M, N, X, sx1, sx2, ox, K, sk, ok )

Rearranges the rows of an `M` by `N` matrix `X` as specified by a permutation vector `K` using alternative indexing semantics.

```javascript
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); // [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
var K = new Int32Array( [ 2, 0, 1 ] );

dlapmr.ndarray( true, 3, 2, X, 2, 1, 0, K, 1, 0 );
// X => <Float64Array>[ 5.0, 6.0, 1.0, 2.0, 3.0, 4.0 ]
```

The function has the following parameters:

- **forwrd**: boolean indicating whether to apply a forward or backward permutation.
- **M**: number of rows of `X`.
- **N**: number of columns of `X`.
- **X**: input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
- **sx1**: stride of the first dimension of `X`.
- **sx2**: stride of the second dimension of `X`.
- **ox**: starting index for `X`.
- **K**: permutation vector as an [`Int32Array`][mdn-int32array].
- **sk**: stride length of `K`.
- **ok**: starting index for `K`.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example,

<!-- eslint-disable max-len -->

```javascript
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

var X = new Float64Array( [ 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var K = new Int32Array( [ 0, 0, 2, 0, 1 ] );

dlapmr.ndarray( true, 3, 2, X, 2, 1, 2, K, 1, 2 );
// X => <Float64Array>[ 0.0, 0.0, 5.0, 6.0, 1.0, 2.0, 3.0, 4.0 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- `dlapmr()` corresponds to the [LAPACK][LAPACK] function [`dlapmr`][lapack-dlapmr].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Int32Array = require( '@stdlib/array/int32' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var dlapmr = require( '@stdlib/lapack/base/dlapmr' );

// Specify matrix meta data:
var shape = [ 4, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;

// Create a matrix stored in linear memory:
var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
console.log( ndarray2array( X, shape, strides, offset, order ) );

// Define a permutation vector:
var K = new Int32Array( [ 1, 3, 0, 2 ] );

// Rearrange rows:
dlapmr( order, true, shape[ 0 ], shape[ 1 ], X, strides[ 0 ], K, 1 );
console.log( ndarray2array( X, shape, strides, offset, order ) );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

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

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
TODO
```

#### TODO

TODO.

```c
TODO
```

TODO

```c
TODO
```

</section>

<!-- /.usage -->

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

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
TODO
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

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

[lapack]: https://www.netlib.org/lapack/explore-html/

[lapack-dlapmr]: https://www.netlib.org/lapack/explore-html/d3/d10/group__lapmr.html

[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array

[mdn-int32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

</section>

<!-- /.links -->
123 changes: 123 additions & 0 deletions lib/node_modules/@stdlib/lapack/base/dlapmr/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 Int32Array = require( '@stdlib/array/int32' );
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var floor = require( '@stdlib/math/base/special/floor' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var dlapmr = require( './../lib/dlapmr.js' );


// VARIABLES //

var LAYOUTS = [
'row-major',
'column-major'
];


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {string} order - storage layout
* @param {PositiveInteger} N - number of elements along each dimension
* @returns {Function} benchmark function
*/
function createBenchmark( order, N ) {
var K;
var X;
var i;

// Create an identity permutation to avoid corrupting data across iterations:
K = new Int32Array( N );
for ( i = 0; i < N; i++ ) {
K[ i ] = i;
}
X = uniform( N*N, -10.0, 10.0, {
'dtype': 'float64'
});
return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = dlapmr( order, true, N, N, X, N, K, 1 );
if ( isnan( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( z[ i%z.length ] ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

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

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

for ( k = 0; k < LAYOUTS.length; k++ ) {
ord = LAYOUTS[ k ];
for ( i = min; i <= max; i++ ) {
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
f = createBenchmark( ord, N );
bench( format( '%s::square_matrix:order=%s,size=%d', pkg, ord, N*N ), f );
}
}
}

main();
Loading