Skip to content

feat: add lapack/base/dorg2r #7784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
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
254 changes: 254 additions & 0 deletions lib/node_modules/@stdlib/lapack/base/dorg2r/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
<!--

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

-->

# dorg2r

> LAPACK routine to generate an M-by-N real matrix Q with orthonormal columns.

<section class="usage">

## Usage

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

#### dorg2r( order, M, N, K, A, LDA, tau, work )

Generates an M-by-N real matrix Q with orthonormal columns. The matrix Q is defined as the first N columns of a product of K elementary reflectors of order M. `Q = H(1) H(2) . . . H(K)` as returned by `dgeqrf`.

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

var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var tau = new Float64Array( [ 0.0, 0.0 ] );
var work = new Float64Array( 10 );

dorg2r( 'column-major', 3, 2, 2, A, 3, tau, work );
// A => <Float64Array>[ 1.0, 0.0, 0.0, 0.0, 1.0, 0.0 ]
```

The function has the following parameters:

- **order**: storage layout.
- **M**: number of rows in matrix `A`.
- **N**: number of columns in matrix `A`.
- **K**: number of elementary reflectors whose product defines the matrix Q.
- **A**: input matrix (overwritten by Householder vectors from `dgeqrf`) stored in linear memory as a [`Float64Array`][mdn-float64array].
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
- **tau**: vector of K scalar factors of the elementary reflectors as a [`Float64Array`][mdn-float64array].
- **work**: workspace array as a [`Float64Array`][mdn-float64array].

The function returns the input matrix `A` overwritten with the orthogonal matrix Q.

#### dorg2r.ndarray( M, N, K, A, sa1, sa2, oa, tau, st, ot, work, sw, ow )

Generates an M-by-N real matrix Q with orthonormal columns using alternative indexing semantics. The matrix Q is defined as the first N columns of a product of K elementary reflectors of order M. `Q = H(1) H(2) . . . H(K)` as returned by `dgeqrf`.

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

var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var tau = new Float64Array( [ 0.0, 0.0 ] );
var work = new Float64Array( 10 );

dorg2r.ndarray( 3, 2, 2, A, 1, 3, 0, tau, 1, 0, work, 1, 0 );
// A => <Float64Array>[ 1.0, 0.0, 0.0, 0.0, 1.0, 0.0 ]
```

The function has the following additional parameters:

- **M**: number of rows in matrix `A`.
- **N**: number of columns in matrix `A`.
- **K**: number of elementary reflectors whose product defines the matrix Q.
- **A**: input matrix (overwritten by Householder vectors from `dgeqrf`) stored in linear memory as a [`Float64Array`][mdn-float64array].
- **sa1**: stride of the first dimension of `A`.
- **sa2**: stride of the second dimension of `A`.
- **oa**: index offset for `A`.
- **tau**: vector of K scalar factors of the elementary reflectors as a [`Float64Array`][mdn-float64array].
- **st**: stride length for `tau`.
- **ot**: index offset for `tau`.
- **work**: workspace array as a [`Float64Array`][mdn-float64array].
- **sw**: stride length for `work`.
- **ow**: index offset for `work`.

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 Float64Array = require( '@stdlib/array/float64' );

var A = new Float64Array( [ 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var tau = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] );
var work = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );

dorg2r.ndarray( 3, 2, 2, A, 1, 3, 3, tau, 1, 2, work, 1, 0 );
// A => <Float64Array>[ 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notess

- The function overwrites the input matrix `A` with the orthogonal matrix `Q`.
- The matrix Q is generated from the Householder vectors and scalar factors returned by `dgeqrf`.
- `dorg2r()` corresponds to the [LAPACK][LAPACK] function [`dorg2r`][lapack-dorg2r].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

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

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

// Specify matrix meta data:
var M = 4;
var N = 3;
var K = 2;

// Create a matrix stored in linear memory:
var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
console.log( 'Original matrix A containing Householder vectors:' );
console.log( ndarray2array( A, [ M, N ], [ 1, M ], 0, 'column-major' ) );

// Define scalar factors of the elementary reflectors:
var tau = new Float64Array( [ 1.2, 0.8 ] );

// Create workspace array:
var work = new Float64Array( N );

// Generate the orthogonal matrix Q:
dorg2r( 'column-major', M, N, K, A, M, tau, work );
console.log( 'Resulting orthogonal matrix Q:' );
console.log( ndarray2array( A, [ M, N ], [ 1, M ], 0, 'column-major' ) );
```

</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-dorg2r]: https://netlib.org/lapack/explore-html/da/da2/group__ung2r_ga8877e79ab6e262ae7497f11d8534635c.html#ga8877e79ab6e262ae7497f11d8534635c

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

[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/dorg2r/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* @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 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 pkg = require( './../package.json' ).name;
var dorg2r = require( './../lib/dorg2r.js' );


// VARIABLES //

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


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {string} order - storage layout
* @param {PositiveInteger} N - matrix order (N-by-N)
* @returns {Function} benchmark function
*/
function createBenchmark( order, N ) {
var work;
var opts;
var tau;
var A;

opts = {
'dtype': 'float64'
};

// Random input matrix and auxiliary arrays:
A = uniform( N*N, -1.0, 1.0, opts );
tau = uniform( N, 0.1, 1.0, opts );
work = uniform( N, 0.0, 0.0, opts );

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 = dorg2r( order, N, N, 0, A, N, tau, work );
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( pkg+':order='+ord+',size='+(N*N), f );
}
}
}

main();
Loading