Skip to content

Commit 706a0dd

Browse files
committed
feat: add /blas/base/zrotg
1 parent 8785e54 commit 706a0dd

File tree

4 files changed

+316
-0
lines changed

4 files changed

+316
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
22+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
23+
var Complex128Array = require( '@stdlib/array/complex128' );
24+
var logEach = require( '@stdlib/console/log-each' );
25+
var zrotg = require( './../lib' );
26+
27+
var out;
28+
var i;
29+
30+
function rand() {
31+
return new Complex128( discreteUniform( -5, 5 ), discreteUniform( -5, 5 ) );
32+
}
33+
34+
for ( i = 0; i < 100; i++ ) {
35+
var a = rand();
36+
var b = rand();
37+
38+
zrotg( a, b, out );
39+
40+
console.log( out );
41+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 abs = require( '@stdlib/math/base/special/abs' );
24+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
25+
var cabs2 = require( '@stdlib/math/base/special/cabs2' );
26+
var EPS = require( '@stdlib/constants/float64/eps' );
27+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
28+
var real = require( '@stdlib/complex/float64/real' );
29+
var imag = require( '@stdlib/complex/float64/imag' );
30+
var cmul = require( '@stdlib/complex/float64/base/mul' );
31+
var conj = require( '@stdlib/complex/float64/conj' );
32+
var logEach = require( '@stdlib/console/log-each' );
33+
34+
35+
// MAIN //
36+
37+
/**
38+
* Constructs a Givens plane rotation.
39+
*
40+
* @param {Complex128} a - rotational elimination parameter
41+
* @param {Complex128} b - rotational elimination parameter
42+
* @param {Complex128Array} out - output array
43+
* @param {integer} stride - index increment
44+
* @param {NonNegativeInteger} offset - starting index
45+
* @returns {Complex128Array} output array
46+
*
47+
* @example
48+
* var Complex128Array = require( '@stdlib/array/complex128' );
49+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
50+
*
51+
* var a = new Complex128( 4.0, 3.0 );
52+
* // returns <Complex128>
53+
*
54+
* var b = new Complex128( 0.0, 0.0 );
55+
* // returns <Complex128>
56+
*
57+
* var out = zrotg( a, b, new Complex128Array( 4 ), 1, 0 );
58+
* // returns <Complex128Array>[ 4.0, 3.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]
59+
*/
60+
function zrotg( a, b, out, stride, offset ) {
61+
var c;
62+
var s;
63+
var r;
64+
var z;
65+
var a2;
66+
var b2;
67+
var c2;
68+
var d;
69+
var scale;
70+
var temp;
71+
72+
s = new Complex128( 0.0, 0.0 );
73+
r = new Complex128( 0.0, 0.0 );
74+
logEach('a: real=%s, imag=%s', real(a), imag(a));
75+
logEach('b: real=%s, imag=%s', real(b), imag(b));
76+
if ( real( b ) === 0.0 && imag( b ) === 0.0 ) {
77+
c = 1.0;
78+
r = a;
79+
z = 0.0;
80+
} else if ( real( a ) === 0.0 && imag( a ) === 0.0 ) {
81+
c = 0;
82+
if ( real( b ) === 0 ) {
83+
r = abs( imag( b ) );
84+
s = conj( b ) / r;
85+
} else if ( imag( b ) === 0 ) {
86+
r = abs( real( b ) );
87+
s = conj( b ) / r;
88+
} else {
89+
b2 = cabs2( b );
90+
d = sqrt( b2 );
91+
s = conj( b ) / d;
92+
r = d;
93+
}
94+
z = 1;
95+
} else {
96+
a2 = cabs2( a );
97+
b2 = cabs2( b );
98+
c2 = a2 + b2;
99+
100+
if ( a2 >= c2 * EPS ) {
101+
c = sqrt( a2 / c2);
102+
r = new Complex128( real( a ) / c, imag( a ) / c );
103+
scale = 1 / sqrt(a2 * c2);
104+
temp = new Complex128(real(a) * scale, imag(a) * scale);
105+
s = cmul(conj(b), temp);
106+
} else {
107+
d = sqrt( a2 * c2 );
108+
c = a2 / d;
109+
while( c < EPS ) {
110+
d *= 2;
111+
c = a2 / d;
112+
}
113+
while( c === 0.0 ) {
114+
c = 1;
115+
}
116+
r = new Complex128( real( a ) / c, imag( a ) / c );
117+
s = conj( b ) * ( a / d );
118+
}
119+
z = 1 / c;
120+
}
121+
a = r;
122+
out[offset] = a;
123+
out[offset + stride] = b;
124+
out[offset + 2 * stride] = c;
125+
out[offset + 3 * stride] = s;
126+
logEach('a: real=%s, imag=%s', real(a), imag(a));
127+
logEach('b: real=%s, imag=%s', real(b), imag(b));
128+
logEach('s: real=%s, imag=%s', real(s), imag(s));
129+
console.log( c );
130+
return out;
131+
}
132+
133+
134+
// EXPORTS //
135+
136+
module.exports = zrotg;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
* Construct a Givens plane rotation.
23+
*
24+
* @module @stdlib/blas/base/zrotg
25+
*
26+
* @example
27+
* var zrotg = require( '@stdlib/blas/base/zrotg' );
28+
*
29+
* var a = new Complex128( 4.0, 3.0 );
30+
* // returns <Complex128>
31+
*
32+
* var b = new Complex128( 0.0, 0.0 );
33+
* // returns <Complex128>
34+
*
35+
* var out = zrotg( a, b );
36+
* // returns <Complex128Array>[ 4.0, 3.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]
37+
*
38+
* a = new Complex128( 1.0, 3.0 );
39+
* // returns <Complex128>
40+
*
41+
* var b = new Complex128( 2.0, 4.0 );
42+
* // returns <Complex128>
43+
*
44+
* out = zrotg( a, b );
45+
* // returns <Complex128Array>[ ~1.732, ~5.1961, 2.0, 4.0, ~0.5773, ~0.8082, ~0.1154 ]
46+
*
47+
* @example
48+
* var Complex128Array = require( '@stdlib/array/complex128' );
49+
* var zrotg = require( '@stdlib/blas/base/zrotg' );
50+
*
51+
* var a = new Complex128( 4.0, 3.0 );
52+
* // returns <Complex128>
53+
*
54+
* var b = new Complex128( 0.0, 0.0 );
55+
* // returns <Complex128>
56+
*
57+
* var out = new Complex128Array( 4 );
58+
*
59+
* var y = zrotg.assign( a, b, out, 1, 0 );
60+
* // returns <Complex128Array>[ 4.0, 3.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]
61+
*
62+
* var bool = ( y === out );
63+
* // returns true
64+
*/
65+
66+
// MODULES //
67+
68+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
69+
var main = require( './main.js' );
70+
var assign = require( './assign.js' );
71+
72+
73+
// MAIN //
74+
75+
setReadOnly( main, 'assign', assign );
76+
77+
78+
// EXPORTS //
79+
80+
module.exports = main;
81+
82+
// exports: { "assign": "main.assign" }
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) 2023 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 Complex128Array = require( '@stdlib/array/complex128' );
24+
var fcn = require( './assign.js' );
25+
26+
27+
// MAIN //
28+
29+
/**
30+
* Constructs a Givens plane rotation.
31+
*
32+
* @param {number} a - rotational elimination parameter
33+
* @param {number} b - rotational elimination parameter
34+
* @returns {Complex128Array} output array
35+
*
36+
* @example
37+
* var Complex128Array = require( '@stdlib/array/complex128' );
38+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
39+
*
40+
* var a = new Complex128( 4.0, 3.0 );
41+
* // returns <Complex128>
42+
*
43+
* var b = new Complex128( 0.0, 0.0 );
44+
* // returns <Complex128>
45+
*
46+
* var out = zrotg( a, b, new Complex128Array( 4 ), 1, 0 );
47+
* // returns <Complex128Array>[ 4.0, 3.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]
48+
*/
49+
function zrotg( a, b ) {
50+
var out = new Complex128Array( 4 );
51+
return fcn( a, b, out, 1, 0 );
52+
}
53+
54+
55+
// EXPORTS //
56+
57+
module.exports = zrotg;

0 commit comments

Comments
 (0)