Skip to content

Commit

Permalink
feat: add math/base/special/gcdf
Browse files Browse the repository at this point in the history
PR-URL: #2997
Ref: #649

Co-authored-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Gunj Joshi <[email protected]>
Signed-off-by: Aayush Khanna <[email protected]>
Signed-off-by: Philipp Burckhardt <[email protected]>
  • Loading branch information
aayush0325 and Planeshifter authored Nov 11, 2024
1 parent 02735b4 commit f8bcfd8
Show file tree
Hide file tree
Showing 26 changed files with 2,468 additions and 0 deletions.
235 changes: 235 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/gcdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
<!--
@license Apache-2.0
Copyright (c) 2024 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# gcdf

> Compute the [greatest common divisor][gcd] (gcd) of two single-precision floating point numbers.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

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).

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var gcdf = require( '@stdlib/math/base/special/gcdf' );
```

#### gcdf( a, b )

Computes the [greatest common divisor][gcd] (gcd).

```javascript
var v = gcdf( 48, 18 );
// returns 6
```

If both `a` and `b` are `0`, the function returns `0`.

```javascript
var v = gcdf( 0, 0 );
// returns 0
```

Both `a` and `b` must have integer values; otherwise, the function returns `NaN`.

```javascript
var v = gcdf( 3.14, 18 );
// returns NaN

v = gcdf( 48, 3.14 );
// returns NaN

v = gcdf( NaN, 18 );
// returns NaN

v = gcdf( 48, NaN );
// returns NaN
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var gcdf = require( '@stdlib/math/base/special/gcdf' );

var a = discreteUniform( 100, 0, 50 );
var b = discreteUniform( a.length, 0, 50 );

var i;
for ( i = 0; i < a.length; i++ ) {
console.log( 'gcdf(%d,%d) = %d', a[ i ], b[ i ], gcdf( a[ i ], b[ i ] ) );
}
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/math/base/special/gcdf.h"
```

#### stdlib_base_gcdf( a, b )

Computes the greatest common divisor (gcd).

```c
float v = stdlib_base_gcdf( 48.0f, 18.0f );
// returns 6.0f
```

The function accepts the following arguments:

- **a**: `[in] float` input value.
- **b**: `[in] float` input value.

```c
float stdlib_base_gcdf( const float a, const float b );
```
</section>
<!-- /.usage -->
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">
</section>
<!-- /.notes -->
<!-- C API usage examples. -->
<section class="examples">
### Examples
```c
#include "stdlib/math/base/special/gcdf.h"
#include <stdio.h>
int main( void ) {
const float a[] = { 24.0f, 32.0f, 48.0f, 116.0f, 33.0f };
const float b[] = { 12.0f, 6.0f, 15.0f, 52.0f, 22.0f };
float out;
int i;
for ( i = 0; i < 5; i++ ) {
out = stdlib_base_gcdf( a[ i ], b[ i ] );
printf( "gcdf(%f, %f) = %f\n", a[ i ], b[ i ], out );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- 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. -->

<section class="references">

## References

- 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].

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[gcd]: https://en.wikipedia.org/wiki/Greatest_common_divisor

[@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pkg = require( './../package.json' ).name;
var gcdf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var z;
var i;

x = discreteUniform( 100, 1.0, 100.0 );
y = discreteUniform( 100, 1.0, 100.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = gcdf( x[ i%x.length ], y[ i%y.length ] );
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var gcdf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( gcdf instanceof Error )
};


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var x;
var y;
var z;
var i;

x = discreteUniform( 100, 1.0, 100.0 );
y = discreteUniform( 100, 1.0, 100.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = gcdf( x[ i%x.length ], y[ i%y.length ] );
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading

1 comment on commit f8bcfd8

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
math/base/special/gcdf $\color{green}336/336$
$\color{green}+100.00\%$
$\color{green}53/53$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}336/336$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.