-
-
Notifications
You must be signed in to change notification settings - Fork 869
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
Open
aayush0325
wants to merge
10
commits into
stdlib-js:develop
Choose a base branch
from
aayush0325:lapack-dlartg
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,272
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
249cf71
feat: add lapack/base/dlartg
aayush0325 104d60e
feat: add main exports
aayush0325 bd80199
docs: add ts files
aayush0325 594abb7
docs: add examples
aayush0325 426f2b4
bench: add benchmarks
aayush0325 efbae24
test: add tests
aayush0325 600710a
docs: update docs
aayush0325 3aa564d
docs: add repl.txt
aayush0325 98232f7
fix: use hypot and nested max in base implementation
aayush0325 3ab8825
docs: update description
aayush0325 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
|
||
This routine constructs a Givens rotation for a two-dimensional vector such that the following equation is satisfied: | ||
|
||
|
||
<!-- <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**: [`Float64Array`][mdn-float64array] containing a single element representing the first component of the vector to be rotated. | ||
- **G**: [`Float64Array`][mdn-float64array] containing a single element representing the second component of the vector to be rotated | ||
- **C**: [`Float64Array`][mdn-float64array] containing a single element which is overwritten by the cosine of the rotation. | ||
- **S**: [`Float64Array`][mdn-float64array] containing a single element which is overwritten by the sine of the rotation. | ||
- **R**: [`Float64Array`][mdn-float64array] containing a single element which is overwritten by the length of the rotated vector. | ||
|
||
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**: [`Float64Array`][mdn-float64array] containing a single element representing the first component of the vector to be rotated. | ||
- **offsetF**: index of the element in `F`. | ||
- **G**: [`Float64Array`][mdn-float64array] containing a single element representing the second component of the vector to be rotated. | ||
- **offsetG**: index of the element in `G`. | ||
- **C**: [`Float64Array`][mdn-float64array] containing a single element which is overwritten by the cosine of the rotation. | ||
- **offsetC**: index of the element in `C`. | ||
- **S**: [`Float64Array`][mdn-float64array] containing a single element which is overwritten by the sine of the rotation. | ||
- **offsetS**: index of the element in `S`. | ||
- **R**: [`Float64Array`][mdn-float64array] containing a single element which is overwritten by the length of the rotated vector. | ||
- **offsetR**: index of the element in `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 --> |
67 changes: 67 additions & 0 deletions
67
lib/node_modules/@stdlib/lapack/base/dlartg/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?
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.
i mean a column matrix here

[ F ]
[ G ]
this is what im referring to as a 2 dimensional vector, i was a little confused here as well so i decided to go with this.
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.
let me know if i should use something else here please i don't really know what else to use