Skip to content

Commit bdaa7e0

Browse files
aman-095kgryte
andauthored
feat: add blas/base/csrot
PR-URL: #2256 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 562f806 commit bdaa7e0

40 files changed

+6408
-0
lines changed
Lines changed: 390 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,390 @@
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+
# csrot
22+
23+
> Applies a plane rotation.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var csrot = require( '@stdlib/blas/base/csrot' );
31+
```
32+
33+
#### csrot( N, cx, strideX, cy, strideY, c, s )
34+
35+
Applies a plane rotation.
36+
37+
```javascript
38+
var Complex64Array = require( '@stdlib/array/complex64' );
39+
var realf = require( '@stdlib/complex/realf' );
40+
var imagf = require( '@stdlib/complex/imagf' );
41+
42+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
43+
var cy = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
44+
45+
csrot( cx.length, cx, 1, cy, 1, 0.8, 0.6 );
46+
47+
var z = cy.get( 0 );
48+
// returns <Complex64>
49+
50+
var re = realf( z );
51+
// returns ~-0.6
52+
53+
var im = imagf( z );
54+
// returns ~-1.2
55+
56+
z = cx.get( 0 );
57+
// returns <Complex64>
58+
59+
re = realf( z );
60+
// returns ~0.8
61+
62+
im = imagf( z );
63+
// returns ~1.6
64+
```
65+
66+
The function has the following parameters:
67+
68+
- **N**: number of indexed elements.
69+
- **cx**: first input [`Complex64Array`][@stdlib/array/complex64].
70+
- **strideX**: index increment for `cx`.
71+
- **cy**: second input [`Complex64Array`][@stdlib/array/complex64].
72+
- **strideY**: index increment for `cy`.
73+
74+
The `N` and stride parameters determine how values from `cx` and `cy` are accessed at runtime. For example, to apply a plane rotation to every other element,
75+
76+
```javascript
77+
var Complex64Array = require( '@stdlib/array/complex64' );
78+
var realf = require( '@stdlib/complex/realf' );
79+
var imagf = require( '@stdlib/complex/imagf' );
80+
81+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
82+
var cy = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
83+
84+
csrot( 2, cx, 2, cy, 2, 0.8, 0.6 );
85+
86+
var z = cy.get( 0 );
87+
// returns <Complex64>
88+
89+
var re = realf( z );
90+
// returns ~-0.6
91+
92+
var im = imagf( z );
93+
// returns ~-1.2
94+
95+
z = cx.get( 0 );
96+
// returns <Complex64>
97+
98+
re = realf( z );
99+
// returns ~0.8
100+
101+
im = imagf( z );
102+
// returns ~1.6
103+
```
104+
105+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
106+
107+
<!-- eslint-disable stdlib/capitalized-comments -->
108+
109+
```javascript
110+
var Complex64Array = require( '@stdlib/array/complex64' );
111+
var realf = require( '@stdlib/complex/realf' );
112+
var imagf = require( '@stdlib/complex/imagf' );
113+
114+
// Initial arrays...
115+
var cx0 = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
116+
var cy0 = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
117+
118+
// Create offset views...
119+
var cx1 = new Complex64Array( cx0.buffer, cx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
120+
var cy1 = new Complex64Array( cy0.buffer, cy0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
121+
122+
csrot( 2, cx1, -2, cy1, 1, 0.8, 0.6 );
123+
124+
var z = cy0.get( 2 );
125+
// returns <Complex64>
126+
127+
var re = realf( z );
128+
// returns ~-4.2
129+
130+
var im = imagf( z );
131+
// returns ~-4.8
132+
133+
z = cx0.get( 3 );
134+
// returns <Complex64>
135+
136+
re = realf( z );
137+
// returns ~5.6
138+
139+
im = imagf( z );
140+
// returns ~6.4
141+
```
142+
143+
#### csrot.ndarray( N, cx, strideX, offsetX, cy, strideY, offsetY, c, s )
144+
145+
Applies a plane rotation using alternative indexing semantics.
146+
147+
```javascript
148+
var Complex64Array = require( '@stdlib/array/complex64' );
149+
var realf = require( '@stdlib/complex/realf' );
150+
var imagf = require( '@stdlib/complex/imagf' );
151+
152+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
153+
var cy = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
154+
155+
csrot.ndarray( cx.length, cx, 1, 0, cy, 1, 0, 0.8, 0.6 );
156+
157+
var z = cy.get( 0 );
158+
// returns <Complex64>
159+
160+
var re = realf( z );
161+
// returns ~-0.6
162+
163+
var im = imagf( z );
164+
// returns ~-1.2
165+
166+
z = cx.get( 0 );
167+
// returns <Complex64>
168+
169+
re = realf( z );
170+
// returns ~0.8
171+
172+
im = imagf( z );
173+
// returns ~1.6
174+
```
175+
176+
The function has the following additional parameters:
177+
178+
- **offsetX**: starting index for `cx`.
179+
- **offsetY**: starting index for `cy`.
180+
181+
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,
182+
183+
```javascript
184+
var Complex64Array = require( '@stdlib/array/complex64' );
185+
var realf = require( '@stdlib/complex/realf' );
186+
var imagf = require( '@stdlib/complex/imagf' );
187+
188+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
189+
var cy = new Complex64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
190+
191+
csrot.ndarray( 2, cx, 2, 1, cy, 2, 1, 0.8, 0.6 );
192+
193+
var z = cy.get( 3 );
194+
// returns <Complex64>
195+
196+
var re = realf( z );
197+
// returns ~-4.2
198+
199+
var im = imagf( z );
200+
// returns ~-4.8
201+
202+
z = cx.get( 1 );
203+
// returns <Complex64>
204+
205+
re = realf( z );
206+
// returns ~2.4
207+
208+
im = imagf( z );
209+
// returns ~3.2
210+
```
211+
212+
</section>
213+
214+
<!-- /.usage -->
215+
216+
<section class="notes">
217+
218+
## Notes
219+
220+
- If `N <= 0`, both functions leave `cx` and `cy` unchanged.
221+
- `csrot()` corresponds to the [BLAS][blas] level 1 function [`csrot`][csrot].
222+
223+
</section>
224+
225+
<!-- /.notes -->
226+
227+
<section class="examples">
228+
229+
## Examples
230+
231+
<!-- eslint no-undef: "error" -->
232+
233+
```javascript
234+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
235+
var filledarrayBy = require( '@stdlib/array/filled-by' );
236+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
237+
var ccopy = require( '@stdlib/blas/base/ccopy' );
238+
var zeros = require( '@stdlib/array/zeros' );
239+
var logEach = require( '@stdlib/console/log-each' );
240+
var csrot = require( '@stdlib/blas/base/csrot' );
241+
242+
function rand() {
243+
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
244+
}
245+
246+
// Generate random input arrays:
247+
var cx = filledarrayBy( 10, 'complex64', rand );
248+
var cxc = ccopy( cx.length, cx, 1, zeros( cx.length, 'complex64' ), 1 );
249+
250+
var cy = filledarrayBy( 10, 'complex64', rand );
251+
var cyc = ccopy( cy.length, cy, 1, zeros( cy.length, 'complex64' ), 1 );
252+
253+
// Apply a plane rotation:
254+
csrot( cx.length, cx, 1, cy, 1, 0.8, 0.6 );
255+
256+
// Print the results:
257+
logEach( '(%s,%s) => (%s,%s)', cxc, cyc, cx, cy );
258+
```
259+
260+
</section>
261+
262+
<!-- /.examples -->
263+
264+
<!-- C interface documentation. -->
265+
266+
* * *
267+
268+
<section class="c">
269+
270+
## C APIs
271+
272+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
273+
274+
<section class="intro">
275+
276+
</section>
277+
278+
<!-- /.intro -->
279+
280+
<!-- C usage documentation. -->
281+
282+
<section class="usage">
283+
284+
### Usage
285+
286+
```c
287+
#include "stdlib/blas/base/csrot.h"
288+
```
289+
290+
#### c_csrot( N, \*X, strideX, \*Y, strideY, c, s )
291+
292+
Applies a plane rotation.
293+
294+
```c
295+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f }; // interleaved real and imaginary components
296+
float y[] = { 5.0f, 6.0f, 7.0f, 8.0f };
297+
298+
c_csrot( 2, (void *)x, 1, (void *)Y, 1, 0.8, 0.6 );
299+
```
300+
301+
The function accepts the following arguments:
302+
303+
- **N**: `[in] CBLAS_INT` number of indexed elements.
304+
- **CX**: `[inout] void*` first input array.
305+
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.
306+
- **CY**: `[inout] void*` second input array.
307+
- **strideY**: `[in] CBLAS_INT` index increment for `CY`.
308+
- **c**: `[in] float` cosine of the angle of rotation.
309+
- **s**: `[in] float` sine of the angle of rotation.
310+
311+
```c
312+
void c_csrot( const CBLAS_INT N, void *CX, const CBLAS_INT strideX, void *CY, const CBLAS_INT strideY, const float c, const float s );
313+
```
314+
315+
</section>
316+
317+
<!-- /.usage -->
318+
319+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
320+
321+
<section class="notes">
322+
323+
</section>
324+
325+
<!-- /.notes -->
326+
327+
<!-- C API usage examples. -->
328+
329+
<section class="examples">
330+
331+
### Examples
332+
333+
```c
334+
#include "stdlib/blas/base/csrot.h"
335+
#include <stdio.h>
336+
337+
int main( void ) {
338+
// Create strided arrays:
339+
float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
340+
float y[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
341+
342+
// Specify the number of elements:
343+
const int N = 4;
344+
345+
// Specify stride lengths:
346+
const int strideX = 1;
347+
const int strideY = -1;
348+
349+
// Copy elements:
350+
c_csrot( N, (void *)x, strideX, (void *)y, strideY, 0.8f, 0.6f );
351+
352+
// Print the result:
353+
for ( int i = 0; i < N; i++ ) {
354+
printf( "x[ %i ] = %f + %fj\n", i, x[ i*2 ], x[ (i*2)+1 ] );
355+
printf( "y[ %i ] = %f + %fj\n", i, y[ i*2 ], y[ (i*2)+1 ] );
356+
}
357+
}
358+
```
359+
360+
</section>
361+
362+
<!-- /.examples -->
363+
364+
</section>
365+
366+
<!-- /.c -->
367+
368+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
369+
370+
<section class="related">
371+
372+
</section>
373+
374+
<!-- /.related -->
375+
376+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
377+
378+
<section class="links">
379+
380+
[blas]: http://www.netlib.org/blas
381+
382+
[csrot]: http://www.netlib.org/lapack/explore-html/da/df6/group__complex__blas__level1.html
383+
384+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
385+
386+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
387+
388+
</section>
389+
390+
<!-- /.links -->

0 commit comments

Comments
 (0)