Skip to content

Commit b566bb2

Browse files
committed
feat: add blas/base/grot
1 parent c584077 commit b566bb2

File tree

15 files changed

+3040
-0
lines changed

15 files changed

+3040
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
# grot
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:grot" 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 grot = require( '@stdlib/blas/base/grot' );
45+
```
46+
47+
#### grot( N, x, strideX, y, strideY, c, s )
48+
49+
Applies a plane rotation.
50+
51+
```javascript
52+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
53+
var y = [ 6.0, 7.0, 8.0, 9.0, 10.0 ];
54+
55+
grot( x.length, x, 1, y, 1, 0.8, 0.6 );
56+
// x => [ ~4.4, ~5.8, 7.2, 8.6, 10.0 ]
57+
// y => [ ~4.2, 4.4, 4.6, 4.8, 5.0 ]
58+
```
59+
60+
The function has the following parameters:
61+
62+
- **N**: number of indexed elements.
63+
- **x**: first input array.
64+
- **strideX**: index increment for `x`.
65+
- **y**: second input array.
66+
- **strideY**: index increment for `y`.
67+
- **c**: cosine of the angle of rotation.
68+
- **s**: sine of the angle of rotation.
69+
70+
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,
71+
72+
```javascript
73+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
74+
var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ];
75+
76+
grot( 3, x, 2, y, 2, 0.8, 0.6 );
77+
// x => [ 5.0, 2.0, 7.8, 4.0, 10.6, 6.0 ]
78+
// y => [ ~5.0, 8.0, 5.4, 10.0, ~5.8, 12.0 ]
79+
```
80+
81+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
82+
83+
<!-- eslint-disable stdlib/capitalized-comments -->
84+
85+
```javascript
86+
var Float64Array = require( '@stdlib/array/float64' );
87+
88+
// Initial arrays...
89+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
90+
var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
91+
92+
// Create offset views...
93+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
94+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
95+
96+
grot( 3, x1, -2, y1, 1, 0.8, 0.6 );
97+
// x0 => <Float64Array>[ 1.0, ~8.8, 3.0, 9.8, 5.0, 10.8 ]
98+
// y0 => <Float64Array>[ 7.0, 8.0, 9.0, 4.4, 6.4, ~8.4 ]
99+
```
100+
101+
#### grot.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, c, s )
102+
103+
Applies a plane rotation using alternative indexing semantics.
104+
105+
```javascript
106+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
107+
var y = [ 6.0, 7.0, 8.0, 9.0, 10.0 ];
108+
109+
grot.ndarray( 4, x, 1, 1, y, 1, 1, 0.8, 0.6 );
110+
// x => [ 1.0, ~5.8, 7.2, 8.6, 10.0 ]
111+
// y => [ 6.0, 4.4, ~4.6, ~4.8, 5.0 ]
112+
```
113+
114+
The function has the following additional parameters:
115+
116+
- **offsetX**: starting index for `x`.
117+
- **offsetY**: starting index for `y`.
118+
119+
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,
120+
121+
```javascript
122+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
123+
var y = [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ];
124+
125+
grot.ndarray( 3, x, 2, 1, y, 2, 1, 0.8, 0.6 );
126+
// x => [ 1.0, 6.4, 3.0, 9.2, 5.0, 12.0 ]
127+
// y => [ 7.0, 5.2, 9.0, 5.6, 11.0, ~6.0 ]
128+
```
129+
130+
</section>
131+
132+
<!-- /.usage -->
133+
134+
<section class="notes">
135+
136+
## Notes
137+
138+
- If `N <= 0`, both functions leave `x` and `y` unchanged.
139+
- `grot()` corresponds to the [BLAS][blas] level 1 function [`grot`][grot].
140+
141+
</section>
142+
143+
<!-- /.notes -->
144+
145+
<section class="examples">
146+
147+
## Examples
148+
149+
<!-- eslint no-undef: "error" -->
150+
151+
```javascript
152+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
153+
var grot = require( '@stdlib/blas/base/grot' );
154+
155+
var opts = {
156+
'dtype': 'generic'
157+
};
158+
var x = discreteUniform( 10, 0, 500, opts );
159+
console.log( x );
160+
161+
var y = discreteUniform( x.length, 0, 255, opts );
162+
console.log( y );
163+
164+
// Apply a plane rotation:
165+
grot( x.length, x, 1, y, 1, 0.8, 0.6 );
166+
console.log( x );
167+
console.log( y );
168+
```
169+
170+
</section>
171+
172+
<!-- /.examples -->
173+
174+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
175+
176+
<section class="related">
177+
178+
</section>
179+
180+
<!-- /.related -->
181+
182+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
183+
184+
<section class="links">
185+
186+
[blas]: http://www.netlib.org/blas
187+
188+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
189+
190+
</section>
191+
192+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
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 grot = require( './../lib' );
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 = grot( 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)