Skip to content

Commit 6a43aee

Browse files
committed
feat: add blas/ext/base/ndarray/cfill
1 parent 01e0c7f commit 6a43aee

File tree

10 files changed

+677
-0
lines changed

10 files changed

+677
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# cfill
2+
3+
> Fill a complex single-precision floating-point ndarray with a specified scalar constant.
4+
5+
## Usage
6+
7+
```javascript
8+
var cfill = require( '@stdlib/blas/ext/base/ndarray/cfill' );
9+
```
10+
11+
#### cfill( x, alpha )
12+
13+
Fills a complex single-precision floating-point ndarray `x` with a specified scalar constant `alpha`.
14+
15+
```javascript
16+
var Complex64Array = require( '@stdlib/array/complex64' );
17+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
18+
var ndarray = require( '@stdlib/ndarray/ctor' );
19+
20+
var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
21+
var x = new ndarray( 'complex64', buffer, [ 2 ], [ 1 ], 0, 'row-major' );
22+
23+
var alpha = new Complex64( 10.0, 10.0 );
24+
25+
cfill( x, alpha );
26+
27+
var v = x.get( 0 );
28+
// returns <Complex64>
29+
30+
var re = realf( v );
31+
// returns 10.0
32+
33+
var im = imagf( v );
34+
// returns 10.0
35+
```
36+
37+
## Examples
38+
39+
```javascript
40+
var Complex64Array = require( '@stdlib/array/complex64' );
41+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
42+
var ndarray = require( '@stdlib/ndarray/ctor' );
43+
var realf = require( '@stdlib/complex/float32/real' );
44+
var imagf = require( '@stdlib/complex/float32/imag' );
45+
var cfill = require( '@stdlib/blas/ext/base/ndarray/cfill' );
46+
47+
var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
48+
var x = new ndarray( 'complex64', buffer, [ 3 ], [ 1 ], 0, 'row-major' );
49+
50+
var alpha = new Complex64( 10.0, 10.0 );
51+
52+
cfill( x, alpha );
53+
54+
var v = x.get( 0 );
55+
console.log( 'x[0] = %d + %di', realf( v ), imagf( v ) );
56+
// => 'x[0] = 10 + 10i'
57+
```
58+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 Complex64Array = require( '@stdlib/array/complex64' );
25+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var isComplex64Array = require( '@stdlib/assert/is-complex64array' );
28+
var pkg = require( './../package.json' ).name;
29+
var cfill = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var alpha;
36+
var buf;
37+
var x;
38+
var i;
39+
40+
buf = new Complex64Array( 10 );
41+
x = new ndarray( 'complex64', buf, [ 10 ], [ 1 ], 0, 'row-major' );
42+
alpha = new Complex64( 5.0, 3.0 );
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
cfill( x, alpha );
47+
if ( buf.length !== 10 ) {
48+
b.fail( 'should not change length' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isComplex64Array( buf ) ) {
53+
b.fail( 'should return a Complex64Array' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, alpha )
3+
Fills a complex single-precision floating-point ndarray with a specified
4+
scalar constant.
5+
6+
Parameters
7+
----------
8+
x: complex64ndarray
9+
Input ndarray.
10+
11+
alpha: Complex64
12+
Scalar constant.
13+
14+
Returns
15+
-------
16+
out: complex64ndarray
17+
Input ndarray.
18+
19+
Examples
20+
--------
21+
> var buf = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
22+
> var x = new {{alias:@stdlib/ndarray/ctor}}( 'complex64', buf, [ 2 ], [ 1 ], 0, 'row-major' );
23+
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 10.0, 10.0 );
24+
> {{alias}}( x, alpha );
25+
> x.get( 0 )
26+
<Complex64>
27+
28+
See Also
29+
--------
30+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { complex64ndarray } from '@stdlib/types/ndarray';
24+
import { Complex64 } from '@stdlib/types/complex';
25+
26+
/**
27+
* Fills a complex single-precision floating-point ndarray with a specified scalar constant.
28+
*
29+
* @param x - input ndarray
30+
* @param alpha - scalar constant
31+
* @returns input ndarray
32+
*
33+
* @example
34+
* var Complex64Array = require( '@stdlib/array/complex64' );
35+
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
36+
* var ndarray = require( '@stdlib/ndarray/ctor' );
37+
*
38+
* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
39+
* var x = new ndarray( 'complex64', buffer, [ 2 ], [ 1 ], 0, 'row-major' );
40+
*
41+
* var alpha = new Complex64( 10.0, 10.0 );
42+
*
43+
* cfill( x, alpha );
44+
*/
45+
declare function cfill( x: complex64ndarray, alpha: Complex64 ): complex64ndarray;
46+
47+
48+
// EXPORTS //
49+
50+
export = cfill;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import Complex64Array = require( '@stdlib/array/complex64' );
20+
import Complex64 = require( '@stdlib/complex/float32/ctor' );
21+
import ndarray = require( '@stdlib/ndarray/ctor' );
22+
import cfill = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
30+
const x = new ndarray( 'complex64', buffer, [ 2 ], [ 1 ], 0, 'row-major' );
31+
const alpha = new Complex64( 10.0, 10.0 );
32+
33+
cfill( x, alpha ); // $ExpectType complex64ndarray
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
37+
{
38+
const alpha = new Complex64( 10.0, 10.0 );
39+
40+
cfill( 123, alpha ); // $ExpectError
41+
cfill( true, alpha ); // $ExpectError
42+
cfill( false, alpha ); // $ExpectError
43+
cfill( null, alpha ); // $ExpectError
44+
cfill( undefined, alpha ); // $ExpectError
45+
cfill( '5', alpha ); // $ExpectError
46+
cfill( [ '1', '2' ], alpha ); // $ExpectError
47+
cfill( {}, alpha ); // $ExpectError
48+
cfill( ( x: number ): number => x, alpha ); // $ExpectError
49+
}
50+
51+
// The compiler throws an error if the function is provided a second argument which is not a complex number...
52+
{
53+
const buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
54+
const x = new ndarray( 'complex64', buffer, [ 2 ], [ 1 ], 0, 'row-major' );
55+
56+
cfill( x, 123 ); // $ExpectError
57+
cfill( x, true ); // $ExpectError
58+
cfill( x, false ); // $ExpectError
59+
cfill( x, null ); // $ExpectError
60+
cfill( x, undefined ); // $ExpectError
61+
cfill( x, '5' ); // $ExpectError
62+
cfill( x, [ '1', '2' ] ); // $ExpectError
63+
cfill( x, {} ); // $ExpectError
64+
cfill( x, ( x: number ): number => x ); // $ExpectError
65+
}
66+
67+
// The compiler throws an error if the function is provided an unsupported number of arguments...
68+
{
69+
const buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
70+
const x = new ndarray( 'complex64', buffer, [ 2 ], [ 1 ], 0, 'row-major' );
71+
const alpha = new Complex64( 10.0, 10.0 );
72+
73+
cfill(); // $ExpectError
74+
cfill( x ); // $ExpectError
75+
cfill( x, alpha, 10 ); // $ExpectError
76+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
var Complex64Array = require( '@stdlib/array/complex64' );
22+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
23+
var ndarray = require( '@stdlib/ndarray/ctor' );
24+
var realf = require( '@stdlib/complex/float32/real' );
25+
var imagf = require( '@stdlib/complex/float32/imag' );
26+
var cfill = require( './../lib' );
27+
28+
var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
29+
var x = new ndarray( 'complex64', buffer, [ 3 ], [ 1 ], 0, 'row-major' );
30+
31+
var alpha = new Complex64( 10.0, 10.0 );
32+
33+
cfill( x, alpha );
34+
35+
var v = x.get( 0 );
36+
console.log( 'x[0] = %d + %di', realf( v ), imagf( v ) );
37+
// => 'x[0] = 10 + 10i'
38+
39+
v = x.get( 1 );
40+
console.log( 'x[1] = %d + %di', realf( v ), imagf( v ) );
41+
// => 'x[1] = 10 + 10i'
42+
43+
v = x.get( 2 );
44+
console.log( 'x[2] = %d + %di', realf( v ), imagf( v ) );
45+
// => 'x[2] = 10 + 10i'
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/**
22+
* Fill a complex single-precision floating-point ndarray with a specified scalar constant.
23+
*
24+
* @module @stdlib/blas/ext/base/ndarray/cfill
25+
*
26+
* @example
27+
* var Complex64Array = require( '@stdlib/array/complex64' );
28+
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
29+
* var ndarray = require( '@stdlib/ndarray/ctor' );
30+
* var cfill = require( '@stdlib/blas/ext/base/ndarray/cfill' );
31+
*
32+
* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
33+
* var x = new ndarray( 'complex64', buffer, [ 2 ], [ 1 ], 0, 'row-major' );
34+
*
35+
* var alpha = new Complex64( 10.0, 10.0 );
36+
*
37+
* cfill( x, alpha );
38+
*
39+
* var v = x.get( 0 );
40+
* // returns <Complex64>
41+
*/
42+
43+
// MODULES //
44+
45+
var main = require( './main.js' );
46+
47+
48+
// EXPORTS //
49+
50+
module.exports = main;

0 commit comments

Comments
 (0)