Skip to content

Commit e3480cb

Browse files
committed
feat: add blas/base/cher2
1 parent 1bbed39 commit e3480cb

File tree

13 files changed

+1732
-0
lines changed

13 files changed

+1732
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 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 Complex64Array = require( '@stdlib/array/complex64' );
28+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var cher2 = require( './../lib/cher2.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var alpha;
51+
var xbuf;
52+
var ybuf;
53+
var Abuf;
54+
var x;
55+
var y;
56+
var A;
57+
58+
xbuf = uniform( len*2, -100.0, 100.0, options );
59+
x = new Complex64Array( xbuf.buffer );
60+
ybuf = uniform( len*2, -100.0, 100.0, options );
61+
y = new Complex64Array( ybuf.buffer );
62+
Abuf = uniform( (len*len)*2, -100.0, 100.0, options );
63+
A = new Complex64Array( Abuf.buffer );
64+
65+
alpha = new Complex64( 1.0, 0.0 );
66+
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
cher2( 'row-major', 'lower', len, alpha, x, 1, y, 1, A, len );
81+
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
}
85+
b.toc();
86+
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
}
92+
}
93+
94+
95+
// MAIN //
96+
97+
/**
98+
* Main execution sequence.
99+
*
100+
* @private
101+
*/
102+
function main() {
103+
var len;
104+
var min;
105+
var max;
106+
var f;
107+
var i;
108+
109+
min = 1; // 10^min
110+
max = 4; // 10^max
111+
112+
for ( i = min; i <= max; i++ ) {
113+
len = pow( 10, i );
114+
f = createBenchmark( len );
115+
bench( pkg+':len='+len, f );
116+
}
117+
}
118+
119+
main();
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 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 Complex64Array = require( '@stdlib/array/complex64' );
28+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var cher2 = require( './../lib/ndarray.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var alpha;
51+
var xbuf;
52+
var ybuf;
53+
var Abuf;
54+
var x;
55+
var y;
56+
var A;
57+
58+
xbuf = uniform( len*2, -100.0, 100.0, options );
59+
x = new Complex64Array( xbuf.buffer );
60+
ybuf = uniform( len*2, -100.0, 100.0, options );
61+
y = new Complex64Array( ybuf.buffer );
62+
Abuf = uniform( (len*len)*2, -100.0, 100.0, options );
63+
A = new Complex64Array( Abuf.buffer );
64+
65+
alpha = new Complex64( 1.0, 0.0 );
66+
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
cher2( 'lower', len, alpha, x, 1, 0, y, 1, 0, A, len, 1, 0 );
81+
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
}
85+
b.toc();
86+
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
}
92+
}
93+
94+
95+
// MAIN //
96+
97+
/**
98+
* Main execution sequence.
99+
*
100+
* @private
101+
*/
102+
function main() {
103+
var len;
104+
var min;
105+
var max;
106+
var f;
107+
var i;
108+
109+
min = 1; // 10^min
110+
max = 4; // 10^max
111+
112+
for ( i = min; i <= max; i++ ) {
113+
len = pow( 10, i );
114+
f = createBenchmark( len );
115+
bench( pkg+':ndarray:len='+len, f );
116+
}
117+
}
118+
119+
main();
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
2+
{{alias}}( order, uplo, N, α, x, strideX, y, strideY, LDA )
3+
Performs the hermitian rank 2 operation
4+
`A = alpha*x*y**H + conjg( alpha )*y*x**H + A`, where `alpha` is a scalar,
5+
`x` and `y` are `N` element vectors and `A` is an `N` by `N` hermitian
6+
matrix.
7+
8+
Indexing is relative to the first index. To introduce an offset, use typed
9+
array views.
10+
11+
If `N` is equal to `0`, the function returns `A` unchanged.
12+
13+
If `α` is equal to `0`, the function returns `A` unchanged.
14+
15+
Parameters
16+
----------
17+
order: string
18+
Row-major (C-style) or column-major (Fortran-style) order.
19+
20+
uplo: string
21+
Specifies whether the upper or lower triangular matrix of `A` is
22+
supplied. Must be either 'upper' or 'lower'.
23+
24+
N: integer
25+
Number of elements along each dimension of `A`.
26+
27+
α: number
28+
Scalar constant.
29+
30+
x: Complex64Array
31+
First input array.
32+
33+
strideX: integer
34+
Index increment for `x`.
35+
36+
y: Complex64Array
37+
Second input array.
38+
39+
strideY: integer
40+
Index increment for `y`.
41+
42+
A: Complex64Array
43+
Input matrix.
44+
45+
LDA: integer
46+
Stride of the first dimension of `A` (a.k.a., leading dimension of the
47+
matrix `A`).
48+
49+
Returns
50+
-------
51+
A: Complex64Array
52+
Input matrix.
53+
54+
Examples
55+
--------
56+
// Standard usage:
57+
> var x = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
58+
> var y = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
59+
> var A = new {{alias:@stdlib/array/complex64}}( [ 1.0, 0.0, 0.0, 0.0, 5.0, 6.0, 7.0, 0.0 ] );
60+
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 1.0, 0.0 );
61+
> {{alias}}( 'row-major', 'lower', 2, alpha, x, 1, y, 1, A, 2 )
62+
<Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 27.0, 2.0, 57.0, 0.0 ]
63+
64+
// Advanced indexing:
65+
> var x = new {{alias:@stdlib/array/complex64}}( [ 3.0, 4.0, 1.0, 2.0 ] );
66+
> var y = new {{alias:@stdlib/array/complex64}}( [ 3.0, 4.0, 1.0, 2.0 ] );
67+
> var A = new {{alias:@stdlib/array/complex64}}( [ 1.0, 0.0, 0.0, 0.0, 5.0, 6.0, 7.0, 0.0 ] );
68+
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 1.0, 0.0 );
69+
> {{alias}}( 'row-major', 'lower', 2, alpha, x, -1, y, -1, A, 2 )
70+
<Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 27.0, 2.0, 57.0, 0.0 ]
71+
72+
// Using typed array views:
73+
> var x0 = new {{alias:@stdlib/array/complex64}}( [ 0.0, 0.0, 3.0, 4.0, 1.0, 2.0 ] );
74+
> var y0 = new {{alias:@stdlib/array/complex64}}( [ 0.0, 0.0, 3.0, 4.0, 1.0, 2.0 ] );
75+
> var x1 = new {{alias:@stdlib/array/complex64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 );
76+
> var y1 = new {{alias:@stdlib/array/complex64}}( y0.buffer, y0.BYTES_PER_ELEMENT*1 );
77+
> var A = new {{alias:@stdlib/array/complex64}}( [ 1.0, 0.0, 0.0, 0.0, 5.0, 6.0, 7.0, 0.0 ] );
78+
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 1.0, 0.0 );
79+
> {{alias}}( 'row-major', 'lower', 2, alpha, x1, -1, y1, -1, A, 2 )
80+
<Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 27.0, 2.0, 57.0, 0.0 ]
81+
82+
83+
{{alias}}.ndarray( uplo, N, α, x, strideX, offsetX, y, strideY, offsetY, A, sa1, sa2, offsetA )
84+
Performs the hermitian rank 2 operation
85+
`A = alpha*x*y**H + conjg( alpha )*y*x**H + A`, where `alpha` is a scalar,
86+
`x` and `y` are `N` element vectors and `A` is an `N` by `N` hermitian
87+
matrix using alternative indexing semantics.
88+
89+
While typed array views mandate a view offset based on the underlying
90+
buffer, the offset parameter supports indexing semantics based on a starting
91+
index.
92+
93+
Parameters
94+
----------
95+
uplo: string
96+
Specifies whether the upper or lower triangular matrix of `A` is
97+
supplied. Must be either 'upper' or 'lower'.
98+
99+
N: integer
100+
Number of elements along each dimension of `A`.
101+
102+
α: number
103+
Scalar constant.
104+
105+
x: Complex64Array
106+
First input array.
107+
108+
strideX: integer
109+
Index increment for `x`.
110+
111+
offsetX: integer
112+
Starting index for `x`.
113+
114+
y: Complex64Array
115+
Second input array.
116+
117+
strideY: integer
118+
Index increment for `y`.
119+
120+
offsetY: integer
121+
Starting index for `y`.
122+
123+
A: Complex64Array
124+
Input matrix.
125+
126+
sa1: integer
127+
Stride of the first dimension of `A`.
128+
129+
sa2: integer
130+
Stride of the second dimension of `A`.
131+
132+
offsetA: integer
133+
Starting index for `A`.
134+
135+
Returns
136+
-------
137+
A: Complex64Array
138+
Input matrix.
139+
140+
Examples
141+
--------
142+
> var x = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
143+
> var y = new {{alias:@stdlib/array/complex64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
144+
> var A = new {{alias:@stdlib/array/complex64}}( [ 1.0, 0.0, 0.0, 0.0, 5.0, 6.0, 7.0, 0.0 ] );
145+
> var alpha = new {{alias:@stdlib/complex/float32/ctor}}( 1.0, 0.0 );
146+
> {{alias}}.ndarray( 'lower', 2, alpha, x, 1, 0, y, 1, 0, A, 2, 1, 0 )
147+
<Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 27.0, 2.0, 57.0, 0.0 ]
148+
149+
See Also
150+
--------

0 commit comments

Comments
 (0)