Skip to content

Commit 143189d

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

File tree

10 files changed

+720
-0
lines changed

10 files changed

+720
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# cfill
22+
23+
> Fill a complex single-precision floating-point ndarray with a specified scalar constant.
24+
25+
## Usage
26+
27+
```javascript
28+
var cfill = require( '@stdlib/blas/ext/base/ndarray/cfill' );
29+
```
30+
31+
#### cfill( x, alpha )
32+
33+
Fills a complex single-precision floating-point ndarray `x` with a specified scalar constant `alpha`.
34+
35+
```javascript
36+
var Complex64Array = require( '@stdlib/array/complex64' );
37+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
38+
var ndarray = require( '@stdlib/ndarray/ctor' );
39+
40+
var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
41+
var x = new ndarray( 'complex64', buffer, [ 2 ], [ 1 ], 0, 'row-major' );
42+
43+
var alpha = new Complex64( 10.0, 10.0 );
44+
45+
cfill( x, alpha );
46+
47+
var v = x.get( 0 );
48+
// returns <Complex64>
49+
50+
var re = v.re;
51+
// returns 10.0
52+
53+
var im = v.im;
54+
// returns 10.0
55+
```
56+
57+
## Examples
58+
59+
```javascript
60+
var Complex64Array = require( '@stdlib/array/complex64' );
61+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
62+
var ndarray = require( '@stdlib/ndarray/ctor' );
63+
var realf = require( '@stdlib/complex/float32/real' );
64+
var imagf = require( '@stdlib/complex/float32/imag' );
65+
var cfill = require( '@stdlib/blas/ext/base/ndarray/cfill' );
66+
67+
var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
68+
var x = new ndarray( 'complex64', buffer, [ 3 ], [ 1 ], 0, 'row-major' );
69+
70+
var alpha = new Complex64( 10.0, 10.0 );
71+
72+
cfill( x, alpha );
73+
74+
var v = x.get( 0 );
75+
console.log( 'x[0] = %d + %di', realf( v ), imagf( v ) );
76+
// => 'x[0] = 10 + 10i'
77+
78+
v = x.get( 1 );
79+
console.log( 'x[1] = %d + %di', realf( v ), imagf( v ) );
80+
// => 'x[1] = 10 + 10i'
81+
82+
v = x.get( 2 );
83+
console.log( 'x[2] = %d + %di', realf( v ), imagf( v ) );
84+
// => 'x[2] = 10 + 10i'
85+
```
86+
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 isComplex64 = require( '@stdlib/assert/is-complex64' );
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( 100 );
41+
x = new ndarray( 'complex64', buf, [ 100 ], [ 1 ], 0, 'row-major' );
42+
alpha = new Complex64( 5.0, 5.0 );
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
cfill( x, alpha );
47+
if ( !isComplex64( x.get( 0 ) ) ) {
48+
b.fail( 'should return a Complex64' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isComplex64( x.get( 0 ) ) ) {
53+
b.fail( 'should return a Complex64' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: ndarray
9+
Input ndarray.
10+
11+
alpha: ComplexLike
12+
Scalar constant.
13+
14+
Returns
15+
-------
16+
out: ndarray
17+
Input ndarray.
18+
19+
Examples
20+
--------
21+
> var buf = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0 ] )
22+
<Complex64Array>
23+
> var x = {{alias:@stdlib/ndarray/ctor}}( 'complex64', buf, [ 2 ], [ 1 ], 0, 'row-major' )
24+
<ndarray>
25+
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 10.0, 10.0 )
26+
<Complex64>
27+
> {{alias}}( x, alpha )
28+
<ndarray>
29+
> var v = x.get( 0 )
30+
<Complex64>
31+
> var re = v.re
32+
10.0
33+
> var im = v.im
34+
10.0
35+
36+
See Also
37+
--------
38+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
* var v = x.get( 0 );
46+
* // returns <Complex64>
47+
*/
48+
declare function cfill( x: complex64ndarray, alpha: Complex64 ): complex64ndarray;
49+
50+
51+
// EXPORTS //
52+
53+
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( '10', alpha ); // $ExpectError
41+
cfill( 10, alpha ); // $ExpectError
42+
cfill( true, alpha ); // $ExpectError
43+
cfill( false, alpha ); // $ExpectError
44+
cfill( null, alpha ); // $ExpectError
45+
cfill( undefined, alpha ); // $ExpectError
46+
cfill( [], 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, '10' ); // $ExpectError
57+
cfill( x, 10 ); // $ExpectError
58+
cfill( x, true ); // $ExpectError
59+
cfill( x, false ); // $ExpectError
60+
cfill( x, null ); // $ExpectError
61+
cfill( x, undefined ); // $ExpectError
62+
cfill( x, [] ); // $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'

0 commit comments

Comments
 (0)