Skip to content

Commit c7c8ce4

Browse files
committed
feat: add blas/ext/base/zfill
1 parent e722c97 commit c7c8ce4

33 files changed

+3772
-0
lines changed
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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+
# zfill
22+
23+
> Fill a double-precision complex floating-point strided array with a specified scalar constant.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zfill = require( '@stdlib/blas/ext/base/zfill' );
31+
```
32+
33+
#### zfill( N, alpha, x, stride )
34+
35+
Fills a double-precision complex floating-point strided array `x` with a specified scalar constant `alpha`.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
var Complex128Array = require( '@stdlib/array/complex128' );
40+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
41+
var realf = require( '@stdlib/complex/float64/real' );
42+
var imagf = require( '@stdlib/complex/float64/imag' );
43+
44+
var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
45+
var x = new Complex128Array( arr );
46+
47+
var alpha = new Complex128( 10.0, 10.0 );
48+
49+
zfill( x.length, alpha, x, 1 );
50+
51+
var y = x.get( 0 );
52+
// returns <Complex128>
53+
54+
var re = realf( y );
55+
// returns 10.0
56+
57+
var im = imagf( y );
58+
// returns 10.0
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **N**: number of indexed elements.
64+
- **alpha**: scalar constant.
65+
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
66+
- **stride**: index increment.
67+
68+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to fill every other element
69+
70+
```javascript
71+
var Float64Array = require( '@stdlib/array/float64' );
72+
var Complex128Array = require( '@stdlib/array/complex128' );
73+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
74+
var realf = require( '@stdlib/complex/float64/real' );
75+
var imagf = require( '@stdlib/complex/float64/imag' );
76+
77+
var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
78+
var x = new Complex128Array( arr );
79+
80+
var alpha = new Complex128( 10.0, 10.0 );
81+
82+
zfill( 2, alpha, x, 2 );
83+
84+
var y = x.get( 0 );
85+
// returns <Complex128>
86+
87+
var re = realf( y );
88+
// returns 10.0
89+
90+
var im = imagf( y );
91+
// returns 10.0
92+
93+
y = x.get( 1 );
94+
// returns <Complex128>
95+
96+
re = realf( y );
97+
// returns 3.0
98+
99+
im = imagf( y );
100+
// returns 4.0
101+
```
102+
103+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
104+
105+
```javascript
106+
var Float64Array = require( '@stdlib/array/float64' );
107+
var Complex128Array = require( '@stdlib/array/complex128' );
108+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
109+
var realf = require( '@stdlib/complex/float64/real' );
110+
var imagf = require( '@stdlib/complex/float64/imag' );
111+
112+
// Create the underlying floating-point array
113+
var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
114+
115+
// Initial array:
116+
var x0 = new Complex128Array( arr );
117+
118+
// Create an offset view:
119+
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
120+
121+
// Define a scalar constant:
122+
var alpha = new Complex128( 10.0, 10.0 );
123+
124+
// Fill every other element:
125+
zfill( 2, alpha, x1, 2 );
126+
127+
var y = x0.get( 0 );
128+
// returns <Complex128>
129+
130+
var re = realf( y );
131+
// returns 1.0
132+
133+
var im = imagf( y );
134+
// returns 2.0
135+
136+
y = x0.get( 1 );
137+
// returns <Complex128>
138+
139+
re = realf( y );
140+
// returns 10.0
141+
142+
im = imagf( y );
143+
// returns 10.0
144+
```
145+
146+
#### zfill.ndarray( N, alpha, x, stride, offset )
147+
148+
Fills a double-precision complex floating-point strided array `x` with a specified scalar constant `alpha` using alternative indexing semantics.
149+
150+
```javascript
151+
var Float64Array = require( '@stdlib/array/float64' );
152+
var Complex128Array = require( '@stdlib/array/complex128' );
153+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
154+
var realf = require( '@stdlib/complex/float64/real' );
155+
var imagf = require( '@stdlib/complex/float64/imag' );
156+
157+
var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
158+
var x = new Complex128Array( arr );
159+
160+
var alpha = new Complex128( 10.0, 10.0 );
161+
162+
zfill.ndarray( x.length, alpha, x, 1, 0 );
163+
164+
var y = x.get( 0 );
165+
// returns <Complex128>
166+
167+
var re = realf( y );
168+
// returns 10.0
169+
170+
var im = imagf( y );
171+
// returns 10.0
172+
```
173+
174+
The function has the following additional parameters:
175+
176+
- **offset**: starting index.
177+
178+
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
179+
180+
```javascript
181+
var Float64Array = require( '@stdlib/array/float64' );
182+
var Complex128Array = require( '@stdlib/array/complex128' );
183+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
184+
var realf = require( '@stdlib/complex/float64/real' );
185+
var imagf = require( '@stdlib/complex/float64/imag' );
186+
187+
var arr = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
188+
var x = new Complex128Array( arr );
189+
190+
var alpha = new Complex128( 10.0, 10.0 );
191+
192+
zfill.ndarray( 2, alpha, x, 1, x.length-2 );
193+
194+
var y = x.get( 0 );
195+
// returns <Complex128>
196+
197+
var re = realf( y );
198+
// returns 1.0
199+
200+
var im = imagf( y );
201+
// returns 2.0
202+
203+
y = x.get( 1 );
204+
// returns <Complex128>
205+
206+
re = realf( y );
207+
// returns 10.0
208+
209+
im = imagf( y );
210+
// returns 10.0
211+
212+
y = x.get( 2 );
213+
// returns <Complex128>
214+
215+
re = realf( y );
216+
// returns 10.0
217+
218+
im = imagf( y );
219+
// returns 10.0
220+
```
221+
222+
</section>
223+
224+
<!-- /.usage -->
225+
226+
<section class="notes">
227+
228+
## Notes
229+
230+
- If `N <= 0`, both functions return the strided array unchanged.
231+
232+
</section>
233+
234+
<!-- /.notes -->
235+
236+
<section class="examples">
237+
238+
## Examples
239+
240+
<!-- eslint no-undef: "error" -->
241+
242+
```javascript
243+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
244+
var filledarrayBy = require( '@stdlib/array/filled-by' );
245+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
246+
var zfill = require( '@stdlib/blas/ext/base/zfill' );
247+
248+
function rand() {
249+
return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
250+
}
251+
252+
var x = filledarrayBy( 10, 'complex128', rand );
253+
var alpha = new Complex128( 10.0, 10.0 );
254+
255+
zfill( x.length, alpha, x, 1 );
256+
console.log( x.get( 0 ).toString() );
257+
```
258+
259+
</section>
260+
261+
<!-- /.examples -->
262+
263+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
264+
265+
<section class="related">
266+
267+
</section>
268+
269+
<!-- /.related -->
270+
271+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
272+
273+
<section class="links">
274+
275+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
276+
277+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
278+
279+
</section>
280+
281+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex128Array = require( '@stdlib/array/complex128' );
28+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var zfill = require( './../lib/zfill.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Create a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var xbuf;
51+
var z;
52+
var x;
53+
54+
xbuf = uniform( len*2, -100.0, 100.0, options );
55+
x = new Complex128Array( xbuf.buffer );
56+
57+
z = new Complex128( 1.0, 0.0 );
58+
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
zfill( x.length, z, x, 1 );
73+
if ( isnanf( xbuf[ i%(len*2) ] ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
}
77+
b.toc();
78+
if ( isnanf( xbuf[ i%(len*2) ] ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( pkg+':len='+len, f );
103+
}
104+
}
105+
106+
main();

0 commit comments

Comments
 (0)