Skip to content

Commit f8bcfd8

Browse files
feat: add math/base/special/gcdf
PR-URL: #2997 Ref: #649 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Signed-off-by: Aayush Khanna <96649223+aayush0325@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 02735b4 commit f8bcfd8

File tree

26 files changed

+2468
-0
lines changed

26 files changed

+2468
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# gcdf
22+
23+
> Compute the [greatest common divisor][gcd] (gcd) of two single-precision floating point numbers.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
The [greatest common divisor][gcd] (gcd) of two non-zero integers `a` and `b` is the largest positive integer which divides both `a` and `b` without a remainder. The gcd is also known as the **greatest common factor** (gcf), **highest common factor** (hcf), **highest common divisor**, and **greatest common measure** (gcm).
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<!-- Package usage documentation. -->
36+
37+
<section class="usage">
38+
39+
## Usage
40+
41+
```javascript
42+
var gcdf = require( '@stdlib/math/base/special/gcdf' );
43+
```
44+
45+
#### gcdf( a, b )
46+
47+
Computes the [greatest common divisor][gcd] (gcd).
48+
49+
```javascript
50+
var v = gcdf( 48, 18 );
51+
// returns 6
52+
```
53+
54+
If both `a` and `b` are `0`, the function returns `0`.
55+
56+
```javascript
57+
var v = gcdf( 0, 0 );
58+
// returns 0
59+
```
60+
61+
Both `a` and `b` must have integer values; otherwise, the function returns `NaN`.
62+
63+
```javascript
64+
var v = gcdf( 3.14, 18 );
65+
// returns NaN
66+
67+
v = gcdf( 48, 3.14 );
68+
// returns NaN
69+
70+
v = gcdf( NaN, 18 );
71+
// returns NaN
72+
73+
v = gcdf( 48, NaN );
74+
// returns NaN
75+
```
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
82+
83+
<section class="notes">
84+
85+
</section>
86+
87+
<!-- /.notes -->
88+
89+
<!-- Package usage examples. -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
99+
var gcdf = require( '@stdlib/math/base/special/gcdf' );
100+
101+
var a = discreteUniform( 100, 0, 50 );
102+
var b = discreteUniform( a.length, 0, 50 );
103+
104+
var i;
105+
for ( i = 0; i < a.length; i++ ) {
106+
console.log( 'gcdf(%d,%d) = %d', a[ i ], b[ i ], gcdf( a[ i ], b[ i ] ) );
107+
}
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- C interface documentation. -->
115+
116+
* * *
117+
118+
<section class="c">
119+
120+
## C APIs
121+
122+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
123+
124+
<section class="intro">
125+
126+
</section>
127+
128+
<!-- /.intro -->
129+
130+
<!-- C usage documentation. -->
131+
132+
<section class="usage">
133+
134+
### Usage
135+
136+
```c
137+
#include "stdlib/math/base/special/gcdf.h"
138+
```
139+
140+
#### stdlib_base_gcdf( a, b )
141+
142+
Computes the greatest common divisor (gcd).
143+
144+
```c
145+
float v = stdlib_base_gcdf( 48.0f, 18.0f );
146+
// returns 6.0f
147+
```
148+
149+
The function accepts the following arguments:
150+
151+
- **a**: `[in] float` input value.
152+
- **b**: `[in] float` input value.
153+
154+
```c
155+
float stdlib_base_gcdf( const float a, const float b );
156+
```
157+
158+
</section>
159+
160+
<!-- /.usage -->
161+
162+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
163+
164+
<section class="notes">
165+
166+
</section>
167+
168+
<!-- /.notes -->
169+
170+
<!-- C API usage examples. -->
171+
172+
<section class="examples">
173+
174+
### Examples
175+
176+
```c
177+
#include "stdlib/math/base/special/gcdf.h"
178+
#include <stdio.h>
179+
180+
int main( void ) {
181+
const float a[] = { 24.0f, 32.0f, 48.0f, 116.0f, 33.0f };
182+
const float b[] = { 12.0f, 6.0f, 15.0f, 52.0f, 22.0f };
183+
184+
float out;
185+
int i;
186+
for ( i = 0; i < 5; i++ ) {
187+
out = stdlib_base_gcdf( a[ i ], b[ i ] );
188+
printf( "gcdf(%f, %f) = %f\n", a[ i ], b[ i ], out );
189+
}
190+
}
191+
```
192+
193+
</section>
194+
195+
<!-- /.examples -->
196+
197+
</section>
198+
199+
<!-- /.c -->
200+
201+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
202+
203+
<section class="references">
204+
205+
## References
206+
207+
- Stein, Josef. 1967. "Computational problems associated with Racah algebra." _Journal of Computational Physics_ 1 (3): 397–405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967].
208+
209+
</section>
210+
211+
<!-- /.references -->
212+
213+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
214+
215+
<section class="related">
216+
217+
</section>
218+
219+
<!-- /.related -->
220+
221+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="links">
224+
225+
[gcd]: https://en.wikipedia.org/wiki/Greatest_common_divisor
226+
227+
[@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2
228+
229+
<!-- <related-links> -->
230+
231+
<!-- </related-links> -->
232+
233+
</section>
234+
235+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 bench = require( '@stdlib/bench' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var gcdf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var z;
36+
var i;
37+
38+
x = discreteUniform( 100, 1.0, 100.0 );
39+
y = discreteUniform( 100, 1.0, 100.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
z = gcdf( x[ i%x.length ], y[ i%y.length ] );
44+
if ( isnanf( z ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnanf( z ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var gcdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( gcdf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var z;
45+
var i;
46+
47+
x = discreteUniform( 100, 1.0, 100.0 );
48+
y = discreteUniform( 100, 1.0, 100.0 );
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
z = gcdf( x[ i%x.length ], y[ i%y.length ] );
53+
if ( isnanf( z ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( z ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)