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