Skip to content

Commit 1f2e049

Browse files
committed
feat: add C / Fortran implementation for drotg
1 parent f5eb836 commit 1f2e049

File tree

5 files changed

+148
-22
lines changed

5 files changed

+148
-22
lines changed

lib/node_modules/@stdlib/blas/base/drotg/benchmark/c/benchmark.length.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ double benchmark( int iterations ) {
109109
s = 0.6;
110110
t = tic();
111111
for ( i = 0; i < iterations; i++ ) {
112-
c_drotg( x, y, c, s );
112+
c_drotg( &x, &y, &c, &s );
113113
if ( y != y ) {
114114
printf( "should not return NaN\n" );
115115
break;

lib/node_modules/@stdlib/blas/base/drotg/benchmark/fortran/benchmark.length.f

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616
! limitations under the License.
1717
!<
1818

19+
!> Benchmark `drotg`.
20+
!
21+
! ## Notes
22+
!
23+
! - Written in "free form" Fortran 95.
24+
!
25+
!<
1926
program bench
2027
implicit none
2128
! ..
2229
! Local constants:
2330
character(5), parameter :: name = 'drotg' ! if changed, be sure to adjust length
24-
integer, parameter :: iterations = 10000000
31+
integer, parameter :: iterations = 1000000
2532
integer, parameter :: repeats = 3
2633
integer, parameter :: min = 1
2734
integer, parameter :: max = 6
@@ -110,39 +117,42 @@ end subroutine print_results
110117
! Runs a benchmark.
111118
!
112119
! @param {integer} iterations - number of iterations
113-
! @param {integer} len - array length
114120
! @return {double} elapsed time in seconds
115121
! ..
116122
double precision function benchmark( iterations )
117123
! ..
118124
! External functions:
119125
interface
120126
subroutine drotg( da, db, c, s )
121-
double precision :: da, db, c, s
127+
double precision :: c, da, db, s
122128
end subroutine drotg
123129
end interface
124130
! ..
125131
! Scalar arguments:
126132
integer, intent(in) :: iterations
127133
! ..
128134
! Local scalars:
129-
double precision :: elapsed, r, x, y
130-
real :: t1, t2
135+
double precision :: elapsed, r1, r2
136+
real :: t1, t2
131137
integer :: i
132138
! ..
139+
! Local scalar:
140+
double precision :: da, db
141+
! ..
133142
! Intrinsic functions:
134143
intrinsic random_number, cpu_time
135144
! ..
136-
call random_number( r )
137-
x = ( r*200.0d0 ) - 100.0d0
138-
y = ( r*200.0d0 ) - 100.0d0
145+
call random_number( r1 )
146+
call random_number( r2 )
147+
da = (r1*100.0d0)-50.0d0
148+
db = (r2*100.0d0)-50.0d0
139149
! ..
140150
call cpu_time( t1 )
141151
! ..
142152
do i = 1, iterations
143-
call drotg( x, y, 1.0d0, 0.0d0 );
144-
if ( y /= y ) then
145-
print '(A)', 'should not return NaN'
153+
call drotg( da, db, 0.8d0, 0.6d0 )
154+
if ( db /= db ) then
155+
print '(A)', 'unexpected result'
146156
exit
147157
end if
148158
end do
@@ -151,8 +161,8 @@ end subroutine drotg
151161
! ..
152162
elapsed = t2 - t1
153163
! ..
154-
if ( y /= y ) then
155-
print '(A)', 'should not return NaN'
164+
if ( db /= db ) then
165+
print '(A)', 'unexpected result'
156166
end if
157167
! ..
158168
benchmark = elapsed
@@ -164,9 +174,9 @@ end function benchmark
164174
subroutine main()
165175
! ..
166176
! Local variables:
167-
integer :: count, iter, i, j
168-
double precision :: elapsed
169177
character(len=999) :: str, tmp
178+
double precision :: elapsed
179+
integer :: i, j, count, iter
170180
! ..
171181
! Intrinsic functions:
172182
intrinsic adjustl, trim

lib/node_modules/@stdlib/blas/base/drotg/src/addon.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ static napi_value addon( napi_env env, napi_callback_info info ) {
3333
napi_status status;
3434

3535
// Get callback arguments:
36-
size_t argc = 2;
36+
size_t argc = 3;
3737
napi_value argv[ 3 ];
3838
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
3939
assert( status == napi_ok );
4040

4141
// Check whether we were provided the correct number of arguments:
42-
if ( argc < 2 ) {
42+
if ( argc < 3 ) {
4343
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
4444
assert( status == napi_ok );
4545
return NULL;
4646
}
47-
if ( argc > 2 ) {
47+
if ( argc > 3 ) {
4848
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
4949
assert( status == napi_ok );
5050
return NULL;

lib/node_modules/@stdlib/blas/base/drotg/src/drotg.f

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555
subroutine drotg( da, db, c, s )
5656
implicit none
5757
! ..
58-
! Internal parameters:
59-
integer, parameter :: dp=kind(0.0d0) ! double-precision
60-
! ..
6158
! Scalar arguments:
6259
double precision :: c, da, db, s
6360
! ..
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) 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 resolve = require( 'path' ).resolve;
24+
var tape = require( 'tape' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var EPS = require( '@stdlib/constants/float64/eps' );
28+
var abs = require( '@stdlib/math/base/special/abs' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
31+
32+
// VARIABLES //
33+
34+
var drotg = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( drotg instanceof Error )
37+
};
38+
39+
40+
// TESTS //
41+
42+
tape( 'main export is a function', opts, function test( t ) {
43+
t.ok( true, __filename );
44+
t.strictEqual( typeof drotg, 'function', 'main export is a function' );
45+
t.end();
46+
});
47+
48+
tape( 'the function has an arity of 2', opts, function test( t ) {
49+
t.strictEqual( drotg.length, 2, 'returns expected value' );
50+
t.end();
51+
});
52+
53+
tape( 'the function computes a Givens plane rotation', opts, function test( t ) {
54+
var expected;
55+
var values;
56+
var delta;
57+
var tol;
58+
var out;
59+
var i;
60+
var j;
61+
var e;
62+
63+
expected = [
64+
[ 0.5, 1.0/0.6, 0.6, 0.8 ],
65+
[ 0.5, 0.6, 0.8, 0.6 ],
66+
[ 0.5, -1.0/0.6, -0.6, 0.8 ],
67+
[ -0.5, -0.6, 0.8, -0.6 ],
68+
[ -0.5, 1.0/0.6, 0.6, 0.8 ],
69+
[ 0.0, 0.0, 1.0, 0.0 ],
70+
[ 1.0, 1.0, 0.0, 1.0 ],
71+
[ 1.0, 0.0, 1.0, 0.0 ]
72+
];
73+
values = [
74+
[ 0.3, 0.4 ],
75+
[ 0.4, 0.3 ],
76+
[ -0.3, 0.4 ],
77+
[ -0.4, 0.3 ],
78+
[ -0.3, -0.4 ],
79+
[ 0.0, 0.0 ],
80+
[ 0.0, 1.0 ],
81+
[ 1.0, 0.0 ]
82+
];
83+
84+
for ( i = 0; i < values.length; i++ ) {
85+
e = new Float64Array( expected[i] );
86+
out = drotg( values[i][0], values[i][1] );
87+
for ( j = 0; j < out.length; j++ ) {
88+
if ( out[j] === expected[j] ) {
89+
t.strictEqual( out[j], e[j], 'returns expected value' );
90+
} else {
91+
delta = abs( out[j] - e[j] );
92+
tol = 1.5 * EPS * abs( e[j] );
93+
t.ok( delta <= tol, 'within tolerance. out: '+out[j]+'. expected: '+e[j]+'. delta: '+delta+'. tol: '+tol+'.' );
94+
}
95+
}
96+
}
97+
t.end();
98+
});
99+
100+
tape( 'the function returns an array of NaNs if provided a rotational elimination parameter equal to NaN', opts, function test( t ) {
101+
var actual;
102+
var i;
103+
104+
actual = drotg( NaN, 1.0 );
105+
for ( i = 0; i < actual.length; i++ ) {
106+
t.strictEqual( isnan( abs( actual[i] ) ), true, 'returns expected value' );
107+
}
108+
109+
actual = drotg( 1.0, NaN );
110+
for ( i = 0; i < actual.length; i++ ) {
111+
t.strictEqual( isnan( abs( actual[i] ) ), true, 'returns expected value' );
112+
}
113+
114+
actual = drotg( NaN, NaN );
115+
for ( i = 0; i < actual.length; i++ ) {
116+
t.strictEqual( isnan( abs( actual[i] ) ), true, 'returns expected value' );
117+
}
118+
t.end();
119+
});

0 commit comments

Comments
 (0)