Skip to content

Commit

Permalink
feat: add blas/ext/base/zfill
Browse files Browse the repository at this point in the history
PR-URL: stdlib-js#2907
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
headlessNode and kgryte authored Sep 16, 2024
1 parent 8499db6 commit 7e11338
Show file tree
Hide file tree
Showing 36 changed files with 3,789 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/blas/ext/base/cfill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var Complex64 = require( '@stdlib/complex/float32/ctor' );
var realf = require( '@stdlib/complex/float32/real' );
var imagf = require( '@stdlib/complex/float32/imag' );

// Create the underlying floating-point array
// Create the underlying floating-point array:
var arr = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );

// Initial array:
Expand Down
28 changes: 14 additions & 14 deletions lib/node_modules/@stdlib/blas/ext/base/cfill/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
-5.0

// Using `N` and stride parameters:
> var x = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 5.0, -5.0 );
> x = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
> alpha = new {{alias:@stdlib/complex/float32/ctor}}( 5.0, -5.0 );
> {{alias}}( 2, alpha, x, 2 );
> var z = x.get( 0 );
> var re = {{alias:@stdlib/complex/float32/real}}( z )
> z = x.get( 0 );
> re = {{alias:@stdlib/complex/float32/real}}( z )
5.0
> var im = {{alias:@stdlib/complex/float32/imag}}( z )
> im = {{alias:@stdlib/complex/float32/imag}}( z )
-5.0
> z = x.get( 1 );
> re = {{alias:@stdlib/complex/float32/real}}( z )
Expand All @@ -60,12 +60,12 @@
// Using view offsets:
> var x0 = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
> var x1 = new {{alias:@stdlib/array/complex64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 5.0, -5.0 );
> alpha = new {{alias:@stdlib/complex/float32/ctor}}( 5.0, -5.0 );
> {{alias}}( 2, alpha, x1, 1 );
> var z = x0.get( 0 );
> var re = {{alias:@stdlib/complex/float32/real}}( z )
> z = x0.get( 0 );
> re = {{alias:@stdlib/complex/float32/real}}( z )
1.0
> var im = {{alias:@stdlib/complex/float32/imag}}( z )
> im = {{alias:@stdlib/complex/float32/imag}}( z )
2.0
> z = x0.get( 1 );
> re = {{alias:@stdlib/complex/float32/real}}( z )
Expand Down Expand Up @@ -117,13 +117,13 @@
2.0

// Using an index offset:
> var x = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 5.0, -5.0 );
> x = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
> alpha = new {{alias:@stdlib/complex/float32/ctor}}( 5.0, -5.0 );
> {{alias}}.ndarray( 2, alpha, x, 2, 1 );
> var z = x.get( 0 );
> var re = {{alias:@stdlib/complex/float32/real}}( z )
> z = x.get( 0 );
> re = {{alias:@stdlib/complex/float32/real}}( z )
1.0
> var im = {{alias:@stdlib/complex/float32/imag}}( z )
> im = {{alias:@stdlib/complex/float32/imag}}( z )
2.0
> z = x.get( 1 );
> re = {{alias:@stdlib/complex/float32/real}}( z )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var addon = require( './cfill.native.js' );
* Fills a single-precision complex floating-point strided array with a specified scalar constant.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {number} alpha - scalar constant
* @param {ComplexLike} alpha - scalar constant
* @param {Complex64Array} x - input array
* @param {integer} stride - index increment
* @param {NonNegativeInteger} offset - starting index
Expand Down
281 changes: 281 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/zfill/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
<!--
@license Apache-2.0
Copyright (c) 2024 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.
-->

# zfill

> Fill a double-precision complex floating-point strided array with a specified scalar constant.
<section class="usage">

## Usage

```javascript
var zfill = require( '@stdlib/blas/ext/base/zfill' );
```

#### zfill( N, alpha, x, stride )

Fills a double-precision complex floating-point strided array `x` with a specified scalar constant `alpha`.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );

var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
var x = new Complex128Array( arr );

var alpha = new Complex128( 10.0, 10.0 );

zfill( x.length, alpha, x, 1 );

var y = x.get( 0 );
// returns <Complex128>

var re = real( y );
// returns 10.0

var im = imag( y );
// returns 10.0
```

The function has the following parameters:

- **N**: number of indexed elements.
- **alpha**: scalar constant.
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
- **stride**: index increment.

The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to fill every other element

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );

var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
var x = new Complex128Array( arr );

var alpha = new Complex128( 10.0, 10.0 );

zfill( 2, alpha, x, 2 );

var y = x.get( 0 );
// returns <Complex128>

var re = real( y );
// returns 10.0

var im = imag( y );
// returns 10.0

y = x.get( 1 );
// returns <Complex128>

re = real( y );
// returns 3.0

im = imag( y );
// returns 4.0
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );

// Create the underlying floating-point array:
var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );

// Initial array:
var x0 = new Complex128Array( arr );

// Create an offset view:
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

// Define a scalar constant:
var alpha = new Complex128( 10.0, 10.0 );

// Fill every other element:
zfill( 2, alpha, x1, 2 );

var y = x0.get( 0 );
// returns <Complex128>

var re = real( y );
// returns 1.0

var im = imag( y );
// returns 2.0

y = x0.get( 1 );
// returns <Complex128>

re = real( y );
// returns 10.0

im = imag( y );
// returns 10.0
```

#### zfill.ndarray( N, alpha, x, stride, offset )

Fills a double-precision complex floating-point strided array `x` with a specified scalar constant `alpha` using alternative indexing semantics.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );

var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
var x = new Complex128Array( arr );

var alpha = new Complex128( 10.0, 10.0 );

zfill.ndarray( x.length, alpha, x, 1, 0 );

var y = x.get( 0 );
// returns <Complex128>

var re = real( y );
// returns 10.0

var im = imag( y );
// returns 10.0
```

The function has the following additional parameters:

- **offset**: starting index.

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 access only the last two elements of the strided array

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );

var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var x = new Complex128Array( arr );

var alpha = new Complex128( 10.0, 10.0 );

zfill.ndarray( 2, alpha, x, 1, x.length-2 );

var y = x.get( 0 );
// returns <Complex128>

var re = real( y );
// returns 1.0

var im = imag( y );
// returns 2.0

y = x.get( 1 );
// returns <Complex128>

re = real( y );
// returns 10.0

im = imag( y );
// returns 10.0

y = x.get( 2 );
// returns <Complex128>

re = real( y );
// returns 10.0

im = imag( y );
// returns 10.0
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- If `N <= 0`, both functions return the strided array unchanged.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var zfill = require( '@stdlib/blas/ext/base/zfill' );

function rand() {
return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
}

var x = filledarrayBy( 10, 'complex128', rand );
var alpha = new Complex128( 10.0, 10.0 );

zfill( x.length, alpha, x, 1 );
console.log( x.get( 0 ).toString() );
```

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

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

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

</section>

<!-- /.links -->
Loading

0 comments on commit 7e11338

Please sign in to comment.