Skip to content

Commit 3f1caa3

Browse files
aman-095kgrytePranavchiku
authored
feat: add blas/base/drot
PR-URL: #1823 Closes: #276 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Pranav Goswami <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Pranav Goswami <[email protected]> Signed-off-by: Athan Reines <[email protected]> Signed-off-by: Pranav Goswami <[email protected]>
1 parent a249a30 commit 3f1caa3

40 files changed

+5670
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# drot
22+
23+
> Apply a plane rotation.
24+
25+
<section class="intro">
26+
27+
This BLAS level 1 routine applies a real plane rotation to real double-precision floating-point vectors. The plane rotation is applied to `N` points, where the points to be rotated are contained in vectors `x` and `y` and where the cosine and sine of the angle of rotation are `c` and `s`, respectively. The operation is as follows:
28+
29+
<!-- <equation class="equation" label="eq:drot" align="center" raw="\begin{bmatrix}x_i \\ y_i\end{bmatrix} = \begin{bmatrix} c & s \\ -s & c\end{bmatrix}\begin{bmatrix} x_i \\ y_i \end{bmatrix}" alt="Plane rotation"> -->
30+
31+
<!-- </equation> -->
32+
33+
where `x_i` and `y_i` are the individual elements on which the rotation is applied.
34+
35+
</section>
36+
37+
<!-- /.intro -->
38+
39+
<section class="usage">
40+
41+
## Usage
42+
43+
```javascript
44+
var drot = require( '@stdlib/blas/base/drot' );
45+
```
46+
47+
#### drot( N, x, strideX, y, strideY, c, s )
48+
49+
Applies a plane rotation.
50+
51+
```javascript
52+
var Float64Array = require( '@stdlib/array/float64' );
53+
54+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
55+
var y = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
56+
57+
drot( x.length, x, 1, y, 1, 0.8, 0.6 );
58+
// x => <Float64Array>[ ~4.4, ~5.8, 7.2, 8.6, 10.0 ]
59+
// y => <Float64Array>[ ~4.2, 4.4, 4.6, 4.8, 5.0 ]
60+
```
61+
62+
The function has the following parameters:
63+
64+
- **N**: number of indexed elements.
65+
- **x**: first input [`Float64Array`][mdn-float64array].
66+
- **strideX**: index increment for `x`.
67+
- **y**: second input [`Float64Array`][mdn-float64array].
68+
- **strideY**: index increment for `y`.
69+
- **c**: cosine of the angle of rotation.
70+
- **s**: sine of the angle of rotation.
71+
72+
The `N` and stride parameters determine how values in the strided arrays are accessed at runtime. For example, to apply a plane rotation to every other element,
73+
74+
```javascript
75+
var Float64Array = require( '@stdlib/array/float64' );
76+
77+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
78+
var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
79+
80+
drot( 3, x, 2, y, 2, 0.8, 0.6 );
81+
// x => <Float64Array>[ 5.0, 2.0, 7.8, 4.0, 10.6, 6.0 ]
82+
// y => <Float64Array>[ ~5.0, 8.0, 5.4, 10.0, ~5.8, 12.0 ]
83+
```
84+
85+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
86+
87+
<!-- eslint-disable stdlib/capitalized-comments -->
88+
89+
```javascript
90+
var Float64Array = require( '@stdlib/array/float64' );
91+
92+
// Initial arrays...
93+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
94+
var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
95+
96+
// Create offset views...
97+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
98+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
99+
100+
drot( 3, x1, -2, y1, 1, 0.8, 0.6 );
101+
// x0 => <Float64Array>[ 1.0, ~8.8, 3.0, 9.8, 5.0, 10.8 ]
102+
// y0 => <Float64Array>[ 7.0, 8.0, 9.0, 4.4, 6.4, ~8.4 ]
103+
```
104+
105+
#### drot.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, c, s )
106+
107+
Applies a plane rotation using alternative indexing semantics.
108+
109+
```javascript
110+
var Float64Array = require( '@stdlib/array/float64' );
111+
112+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
113+
var y = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
114+
115+
drot.ndarray( 4, x, 1, 1, y, 1, 1, 0.8, 0.6 );
116+
// x => <Float64Array>[ 1.0, ~5.8, 7.2, 8.6, 10.0 ]
117+
// y => <Float64Array>[ 6.0, 4.4, ~4.6, ~4.8, 5.0 ]
118+
```
119+
120+
The function has the following additional parameters:
121+
122+
- **offsetX**: starting index for `x`.
123+
- **offsetY**: starting index for `y`.
124+
125+
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, to apply a plane rotation to every other element starting from the second element,
126+
127+
```javascript
128+
var Float64Array = require( '@stdlib/array/float64' );
129+
130+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
131+
var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
132+
133+
drot.ndarray( 3, x, 2, 1, y, 2, 1, 0.8, 0.6 );
134+
// x => <Float64Array>[ 1.0, 6.4, 3.0, 9.2, 5.0, 12.0 ]
135+
// y => <Float64Array>[ 7.0, 5.2, 9.0, 5.6, 11.0, ~6.0 ]
136+
```
137+
138+
</section>
139+
140+
<!-- /.usage -->
141+
142+
<section class="notes">
143+
144+
## Notes
145+
146+
- If `N <= 0`, both functions leave `x` and `y` unchanged.
147+
- `drot()` corresponds to the [BLAS][blas] level 1 function [`drot`][drot].
148+
149+
</section>
150+
151+
<!-- /.notes -->
152+
153+
<section class="examples">
154+
155+
## Examples
156+
157+
<!-- eslint no-undef: "error" -->
158+
159+
```javascript
160+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
161+
var drot = require( '@stdlib/blas/base/drot' );
162+
163+
var opts = {
164+
'dtype': 'float64'
165+
};
166+
var x = discreteUniform( 10, 0, 500, opts );
167+
console.log( x );
168+
169+
var y = discreteUniform( x.length, 0, 255, opts );
170+
console.log( y );
171+
172+
// Apply a plane rotation:
173+
drot( x.length, x, 1, y, 1, 0.8, 0.6 );
174+
console.log( x );
175+
console.log( y );
176+
```
177+
178+
</section>
179+
180+
<!-- /.examples -->
181+
182+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
183+
184+
<section class="related">
185+
186+
</section>
187+
188+
<!-- /.related -->
189+
190+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="links">
193+
194+
[blas]: http://www.netlib.org/blas
195+
196+
[drot]: https://www.netlib.org/lapack/explore-html/d1/d45/group__rot_gae48ef017306866ac2d5a8c5a52617858.html#gae48ef017306866ac2d5a8c5a5261785
197+
198+
[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
199+
200+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
201+
202+
</section>
203+
204+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var drot = require( './../lib/drot.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var x = uniform( len, -100.0, 100.0, options );
49+
var y = uniform( len, -100.0, 100.0, options );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var z;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
z = drot( x.length, x, 1, y, 1, 0.707, 0.707 );
65+
if ( isnan( z[ i%x.length ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( z[ i%x.length ] ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( pkg+':len='+len, f );
100+
}
101+
}
102+
103+
main();

0 commit comments

Comments
 (0)