Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
372 changes: 372 additions & 0 deletions lib/node_modules/@stdlib/blas/base/wasm/caxpy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,372 @@
<!--

@license Apache-2.0

Copyright (c) 2026 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.

-->

# caxpy

> Scale a single-precision complex floating-point vector by a single-precision complex floating-point constant and add the result to a single-precision complex floating-point vector.

<section class="usage">

## Usage

```javascript
var caxpy = require( '@stdlib/blas/base/wasm/caxpy' );
```

#### caxpy.main( N, alpha, x, strideX, y, strideY )

Scales values from `x` by `alpha` and adds the result to `y`.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var alpha = new Complex64( 2.0, 2.0 );

caxpy.main( 3, alpha, x, 1, y, 1 );
// y => <Complex64Array>[ -1.0, 7.0, -1.0, 15.0, -1.0, 23.0 ]
```

The function has the following parameters:

- **N**: number of indexed elements.
- **alpha**: scalar [`Complex64`][@stdlib/complex/float32/ctor] constant.
- **x**: first input [`Complex64Array`][@stdlib/array/complex64].
- **strideX**: index increment for `x`.
- **y**: second input [`Complex64Array`][@stdlib/array/complex64].
- **strideY**: index increment for `y`.

The `N` and stride parameters determine how values from `x` are scaled by `alpha` and added to `y`. For example, to scale every other value in `x` by `alpha` and add the result to every other value of `y`,

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
var y = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var alpha = new Complex64( 2.0, 2.0 );

caxpy.main( 2, alpha, x, 2, y, 2 );
// y => <Complex64Array>[ -1.0, 7.0, 1.0, 1.0, -1.0, 23.0, 1.0, 1.0 ]
```

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 Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );

// Initial arrays...
var x0 = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
var y0 = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );

// Define a scalar constant:
var alpha = new Complex64( 2.0, 2.0 );

// Create offset views...
var x1 = new Complex64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var y1 = new Complex64Array( y0.buffer, y0.BYTES_PER_ELEMENT*2 ); // start at 3rd element

// Scales values of `x0` by `alpha` starting from second index and add the result to `y0` starting from third index...
caxpy.main( 2, alpha, x1, 1, y1, 1 );
// y0 => <Complex64Array>[ 1.0, 1.0, 1.0, 1.0, -1.0, 15.0, -1.0, 23.0 ]
```

#### caxpy.ndarray( N, alpha, x, strideX, offsetX, y, strideY, offsetY )

Scales values from `x` by `alpha` and adds the result to `y` using alternative indexing semantics.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var alpha = new Complex64( 2.0, 2.0 );

caxpy.ndarray( 3, alpha, x, 1, 0, y, 1, 0 );
// y => <Complex64Array>[ -1.0, 7.0, -1.0, 15.0, -1.0, 23.0 ]
```

The function has the following additional parameters:

- **offsetX**: starting index for `x`.
- **offsetY**: starting index for `y`.

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 scale values in the first input strided array starting from the second element and add the result to the second input array starting from the second element,

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
var y = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
var alpha = new Complex64( 2.0, 2.0 );

caxpy.ndarray( 3, alpha, x, 1, 1, y, 1, 1 );
// y => <Complex64Array>[ 1.0, 1.0, -1.0, 15.0, -1.0, 23.0, -1.0, 31.0 ]
```

* * *

### Module

#### caxpy.Module( memory )

Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory.

<!-- eslint-disable n/no-sync -->

```javascript
var Memory = require( '@stdlib/wasm/memory' );

// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
var mem = new Memory({
'initial': 10,
'maximum': 100
});

// Create a BLAS routine:
var mod = new caxpy.Module( mem );
// returns <Module>

// Initialize the routine:
mod.initializeSync();
```

#### caxpy.Module.prototype.main( N, ap, xp, sx, yp, sy )

Scales values from `x` by `alpha` and adds the result to `y`.

<!-- eslint-disable n/no-sync -->

```javascript
var Memory = require( '@stdlib/wasm/memory' );
var zeros = require( '@stdlib/array/zeros' );
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var Float32Array = require( '@stdlib/array/float32' );
var Complex64Array = require( '@stdlib/array/complex64' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var caxpy = require( '@stdlib/blas/base/wasm/caxpy' );

// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
var mem = new Memory({
'initial': 10,
'maximum': 100
});

// Create a BLAS routine:
var mod = new caxpy.Module( mem );
// returns <Module>

// Initialize the routine:
mod.initializeSync();

// Define a vector data type:
var dtype = 'complex64';

// Specify a vector length:
var N = 3;

// Define pointers (i.e., byte offsets) for storing two vectors and a complex number:
var xptr = 0;
var yptr = N * bytesPerElement( dtype );
var aptr = 2 * N * bytesPerElement( dtype );

// Write vector values to module memory:
mod.write( xptr, new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ) );
mod.write( yptr, new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ) );

// Write a complex number to module memory:
mod.write( aptr, new Float32Array( [ 2.0, 2.0 ] ) );

// Perform computation:
mod.main( N, aptr, xptr, 1, yptr, 1 );

// Read out the results:
var view = zeros( N, dtype );
mod.read( yptr, view );

console.log( reinterpretComplex64( view, 0 ) );
// => <Float32Array>[ -1.0, 7.0, -1.0, 15.0, -1.0, 23.0 ]
```

The function has the following parameters:

- **N**: number of indexed elements.
- **ap**: pointer (i.e., byte offset) to a scalar [`Complex64`][@stdlib/complex/float32/ctor] constant.
- **xp**: first input [`Complex64Array`][@stdlib/array/complex64] pointer (i.e., byte offset).
- **sx**: index increment for `x`.
- **yp**: second input [`Complex64Array`][@stdlib/array/complex64] pointer (i.e., byte offset).
- **sy**: index increment for `y`.

#### caxpy.Module.prototype.ndarray( N, ap, xp, sx, ox, yp, sy, oy )

Scales values from `x` by `alpha` and adds the result to `y` using alternative indexing semantics.

<!-- eslint-disable n/no-sync -->

```javascript
var Memory = require( '@stdlib/wasm/memory' );
var zeros = require( '@stdlib/array/zeros' );
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var Float32Array = require( '@stdlib/array/float32' );
var Complex64Array = require( '@stdlib/array/complex64' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var caxpy = require( '@stdlib/blas/base/wasm/caxpy' );

// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB):
var mem = new Memory({
'initial': 10,
'maximum': 100
});

// Create a BLAS routine:
var mod = new caxpy.Module( mem );
// returns <Module>

// Initialize the routine:
mod.initializeSync();

// Define a vector data type:
var dtype = 'complex64';

// Specify a vector length:
var N = 3;

// Define pointers (i.e., byte offsets) for storing two vectors and a complex number:
var xptr = 0;
var yptr = N * bytesPerElement( dtype );
var aptr = 2 * N * bytesPerElement( dtype );

// Write vector values to module memory:
mod.write( xptr, new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ) );
mod.write( yptr, new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ) );

// Write a complex number to module memory:
mod.write( aptr, new Float32Array( [ 2.0, 2.0 ] ) );

// Perform computation:
mod.ndarray( N, aptr, xptr, 1, 0, yptr, 1, 0 );

// Read out the results:
var view = zeros( N, dtype );
mod.read( yptr, view );

console.log( reinterpretComplex64( view, 0 ) );
// => <Float32Array>[ -1.0, 7.0, -1.0, 15.0, -1.0, 23.0 ]
```

The function has the following additional parameters:

- **ox**: starting index for `x`.
- **oy**: starting index for `y`.

</section>

<!-- /.usage -->

<section class="notes">

* * *

## Notes

- If `N <= 0`, `y` is left unchanged.
- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `caxpy` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/blas/base/caxpy`][@stdlib/blas/base/caxpy]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/blas/base/caxpy`][@stdlib/blas/base/caxpy]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other.
- `caxpy()` corresponds to the [BLAS][blas] level 1 function [`caxpy`][caxpy].

</section>

<!-- /.notes -->

<section class="examples">

* * *

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var oneTo = require( '@stdlib/array/one-to' );
var ones = require( '@stdlib/array/ones' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var Complex64Array = require( '@stdlib/array/complex64' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var caxpy = require( '@stdlib/blas/base/wasm/caxpy' );

// Specify a vector length:
var N = 5;

// Create the input arrays:
var x = new Complex64Array( oneTo( N*2, 'float32' ).buffer );
var y = new Complex64Array( ones( N*2, 'float32' ).buffer );

// Create a complex number:
var alpha = new Complex64( 2.0, 2.0 );

// Perform computation:
caxpy.ndarray( N, alpha, x, 1, 0, y, 1, 0 );

// Print the results:
console.log( reinterpretComplex64( y, 0 ) );
// => <Float32Array>[ -1.0, 7.0, -1.0, 15.0, -1.0, 23.0, -1.0, 31.0, -1.0, 39.0 ]
```

</section>

<!-- /.examples -->

<!-- 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">

[blas]: http://www.netlib.org/blas

[caxpy]: https://www.netlib.org/lapack/explore-html/d5/d4b/group__axpy_ga0b7bac1f4d42514074a48f14f5f9caa0.html#ga0b7bac1f4d42514074a48f14f5f9caa0

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64

[@stdlib/complex/float32/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float32/ctor

[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory

[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper

[@stdlib/blas/base/caxpy]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/caxpy

</section>

<!-- /.links -->
Loading