-
-
Notifications
You must be signed in to change notification settings - Fork 866
feat: add lapack/base/dlartg
#7596
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
base: develop
Are you sure you want to change the base?
Changes from all commits
249cf71
104d60e
bd80199
594abb7
426f2b4
efbae24
600710a
3aa564d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,292 @@ | ||||||||||||||||||||||
<!-- | ||||||||||||||||||||||
|
||||||||||||||||||||||
@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. | ||||||||||||||||||||||
|
||||||||||||||||||||||
--> | ||||||||||||||||||||||
|
||||||||||||||||||||||
# dlartg | ||||||||||||||||||||||
|
||||||||||||||||||||||
> LAPACK routine to construct a Givens rotation of a two-dimensional vector that zeros out the second component. | ||||||||||||||||||||||
|
||||||||||||||||||||||
<section class="intro"> | ||||||||||||||||||||||
|
||||||||||||||||||||||
`dlartg` constructs a Givens rotation for a two-dimensional vector such that the following equation is satisfied: | ||||||||||||||||||||||
|
||||||||||||||||||||||
Comment on lines
+27
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
<!-- <equation class="equation" label="eq:givens_rotation" align="center" raw="\begin{bmatrix} C & S \\ -S & C \end{bmatrix} \begin{bmatrix} F \\ G \end{bmatrix} = \begin{bmatrix} R \\ 0 \end{bmatrix} | ||||||||||||||||||||||
" alt="Equation for Givens rotation."> --> | ||||||||||||||||||||||
|
||||||||||||||||||||||
```math | ||||||||||||||||||||||
\begin{bmatrix} C & S \\ -S & C \end{bmatrix} \begin{bmatrix} F \\ G \end{bmatrix} = \begin{bmatrix} R \\ 0 \end{bmatrix} | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
<!-- </equation> --> | ||||||||||||||||||||||
|
||||||||||||||||||||||
where `C` represents the cosine of the rotation `S` represents the sine of the rotation and `R` represents the length of the vector after rotation. | ||||||||||||||||||||||
|
||||||||||||||||||||||
</section> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<!-- /.intro --> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<section class="usage"> | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Usage | ||||||||||||||||||||||
|
||||||||||||||||||||||
```javascript | ||||||||||||||||||||||
var dlartg = require( '@stdlib/lapack/base/dlartg' ); | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
#### dlartg( F, G, C, S, R ) | ||||||||||||||||||||||
|
||||||||||||||||||||||
Constructs a Givens rotation of a two-dimensional vector that zeros out the second component. For a two-dimensional vector `( F, G )` it computes the sine `S`, cosine `C` and the resultant `R`. | ||||||||||||||||||||||
|
||||||||||||||||||||||
```javascript | ||||||||||||||||||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
var F = new Float64Array( [ 3.0 ] ); | ||||||||||||||||||||||
var G = new Float64Array( [ 4.0 ] ); | ||||||||||||||||||||||
var C = new Float64Array( 1 ); | ||||||||||||||||||||||
var S = new Float64Array( 1 ); | ||||||||||||||||||||||
var R = new Float64Array( 1 ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
dlartg( F, G, C, S, R ); | ||||||||||||||||||||||
// R => <Float64Array>[ 5.0 ] | ||||||||||||||||||||||
// C => <Float64Array>[ 0.6 ] | ||||||||||||||||||||||
// S => <Float64Array>[ 0.8 ] | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
The function has the following parameters: | ||||||||||||||||||||||
|
||||||||||||||||||||||
- **F**: single element array representing the first component of the vector to be rotated as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **G**: single element array representing the second component of the vector to be rotated as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **C**: single element array overwritten by the cosine of the rotation as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **S**: single element array overwritten by the sine of the rotation as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **R**: single element array overwritten by the length of the rotated vector as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
Comment on lines
+74
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
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 Float64Array = require( '@stdlib/array/float64' ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Initial arrays... | ||||||||||||||||||||||
var F0 = new Float64Array( [ 0.0, 3.0 ] ); | ||||||||||||||||||||||
var G0 = new Float64Array( [ 0.0, 4.0 ] ); | ||||||||||||||||||||||
var C0 = new Float64Array( [ 0.0, 0.0 ] ); | ||||||||||||||||||||||
var S0 = new Float64Array( [ 0.0, 0.0 ] ); | ||||||||||||||||||||||
var R0 = new Float64Array( [ 0.0, 0.0 ] ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Create offset views... | ||||||||||||||||||||||
var F1 = new Float64Array( F0.buffer, F0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||||||||||||||||||
var G1 = new Float64Array( G0.buffer, G0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||||||||||||||||||
var C1 = new Float64Array( C0.buffer, C0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||||||||||||||||||
var S1 = new Float64Array( S0.buffer, S0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||||||||||||||||||
var R1 = new Float64Array( R0.buffer, R0.BYTES_PER_ELEMENT*1 ); // start at 2nd element | ||||||||||||||||||||||
|
||||||||||||||||||||||
dlartg( F1, G1, C1, S1, R1 ); | ||||||||||||||||||||||
// R0 => <Float64Array>[ 0.0, 5.0 ] | ||||||||||||||||||||||
// C0 => <Float64Array>[ 0.0, 0.6 ] | ||||||||||||||||||||||
// S0 => <Float64Array>[ 0.0, 0.8 ] | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
#### dlartg.ndarray( F, offsetF, G, offsetG, C, offsetC, S, offsetS, R, offsetR ) | ||||||||||||||||||||||
|
||||||||||||||||||||||
Constructs a Givens rotation of a two-dimensional vector that zeros out the second component, using alternative indexing semantics. For a two-dimensional vector `( F, G )` it computes the sine `S`, cosine `C` and the resultant `R`. | ||||||||||||||||||||||
|
||||||||||||||||||||||
```javascript | ||||||||||||||||||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
var F = new Float64Array( [ 3.0 ] ); | ||||||||||||||||||||||
var G = new Float64Array( [ 4.0 ] ); | ||||||||||||||||||||||
var C = new Float64Array( 1 ); | ||||||||||||||||||||||
var S = new Float64Array( 1 ); | ||||||||||||||||||||||
var R = new Float64Array( 1 ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
dlartg.ndarray( F, 0, G, 0, C, 0, S, 0, R, 0 ); | ||||||||||||||||||||||
// R => <Float64Array>[ 5.0 ] | ||||||||||||||||||||||
// C => <Float64Array>[ 0.6 ] | ||||||||||||||||||||||
// S => <Float64Array>[ 0.8 ] | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
The function has the following parameters: | ||||||||||||||||||||||
|
||||||||||||||||||||||
- **F**: single element array representing the first component of the vector to be rotated as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as above. |
||||||||||||||||||||||
- **offsetF**: starting index for `F`. | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It is somewhat awkward to say "starting index" when we are talking about only a single element. We can be more precise, IMO. Applies here and below and likely elsewhere throughout this PR. |
||||||||||||||||||||||
- **G**: single element array representing the second component of the vector to be rotated as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **offsetG**: starting index for `G`. | ||||||||||||||||||||||
- **C**: single element array overwritten by the cosine of the rotation as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **offsetC**: starting index for `C`. | ||||||||||||||||||||||
- **S**: single element array overwritten by the sine of the rotation as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **offsetS**: starting index for `S`. | ||||||||||||||||||||||
- **R**: single element array overwritten by the length of the rotated vector as a [`Float64Array`][mdn-float64array]. | ||||||||||||||||||||||
- **offsetR**: starting index for `R`. | ||||||||||||||||||||||
|
||||||||||||||||||||||
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 F = new Float64Array( [ 0.0, 3.0 ] ); | ||||||||||||||||||||||
var G = new Float64Array( [ 0.0, 4.0 ] ); | ||||||||||||||||||||||
var C = new Float64Array( 2 ); | ||||||||||||||||||||||
var S = new Float64Array( 2 ); | ||||||||||||||||||||||
var R = new Float64Array( 2 ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
dlartg.ndarray( F, 1, G, 1, C, 1, S, 1, R, 1 ); | ||||||||||||||||||||||
// R => <Float64Array>[ 0.0, 5.0 ] | ||||||||||||||||||||||
// C => <Float64Array>[ 0.0, 0.6 ] | ||||||||||||||||||||||
// S => <Float64Array>[ 0.0, 0.8 ] | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
</section> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<!-- /.usage --> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<section class="notes"> | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Notes | ||||||||||||||||||||||
|
||||||||||||||||||||||
- `dlartg()` corresponds to the [LAPACK][LAPACK] function [`dlartg`][lapack-dlartg]. | ||||||||||||||||||||||
|
||||||||||||||||||||||
</section> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<!-- /.notes --> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<section class="examples"> | ||||||||||||||||||||||
|
||||||||||||||||||||||
## Examples | ||||||||||||||||||||||
|
||||||||||||||||||||||
<!-- eslint no-undef: "error" --> | ||||||||||||||||||||||
|
||||||||||||||||||||||
```javascript | ||||||||||||||||||||||
var Float64Array = require( '@stdlib/array/float64' ); | ||||||||||||||||||||||
var uniform = require( '@stdlib/random/array/uniform' ); | ||||||||||||||||||||||
var dlartg = require( '@stdlib/lapack/base/dlartg' ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
var F = uniform( 1, -10.0, 10.0 ); | ||||||||||||||||||||||
var G = uniform( 1, -10.0, 10.0 ); | ||||||||||||||||||||||
var C = new Float64Array( 1 ); | ||||||||||||||||||||||
var S = new Float64Array( 1 ); | ||||||||||||||||||||||
var R = new Float64Array( 1 ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
dlartg( F, G, C, S, R ); | ||||||||||||||||||||||
|
||||||||||||||||||||||
console.log( C ); | ||||||||||||||||||||||
console.log( S ); | ||||||||||||||||||||||
console.log( R ); | ||||||||||||||||||||||
``` | ||||||||||||||||||||||
|
||||||||||||||||||||||
</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-dlartg]: https://netlib.org/lapack/explore-html/da/dd3/group__lartg_ga86f8f877eaea0386cdc2c3c175d9ea88.html#ga86f8f877eaea0386cdc2c3c175d9ea88 | ||||||||||||||||||||||
|
||||||||||||||||||||||
[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 --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
var Float64Array = require( '@stdlib/array/float64' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pkg = require( './../package.json' ).name; | ||
var dlartg = require( './../lib/dlartg.js' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var opts = { | ||
'dtype': 'float64' | ||
}; | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var C; | ||
var S; | ||
var R; | ||
var F; | ||
var G; | ||
var i; | ||
|
||
F = discreteUniform( 1, -50, 50, opts ); | ||
G = discreteUniform( 1, -50, 50, opts ); | ||
C = new Float64Array( 1 ); | ||
S = new Float64Array( 1 ); | ||
R = new Float64Array( 1 ); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
dlartg( F, G, C, S, R ); | ||
if ( isnan( C[ 0 ] ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( S[ 0 ] ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aayush0325 I'm a little confused. What is meant by a "two-dimensional" vector? Vectors are, by definition, one dimensional. Are you meaning a column matrix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe you are intending a two-element vector?