-
-
Notifications
You must be signed in to change notification settings - Fork 907
feat: add math/base/special/gcdf
#2997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6cb67c5
feat: added C files
aayush0325 9f03892
feat: added js files and tests
aayush0325 e142976
feat: lib js files added
aayush0325 d7fa9cb
feat: added examples
aayush0325 0711fb5
feat: added benchmark
aayush0325 4c2151f
feat: added docs
aayush0325 4f2075e
fix: updated dependencies
aayush0325 0631b43
fix: typo in tests
aayush0325 9f8e83f
fix: linting js files
aayush0325 bc2ae67
fix: linting C files
aayush0325 805f0d5
fix: typo in README
aayush0325 efb354f
fix: updated benchmarks
aayush0325 8ee80fd
fix: removed unwanted files
aayush0325 aa18345
chore: updated year from 2018 to 2024 in license
aayush0325 7656dae
chore: changes from code review
aayush0325 e7c7069
chore: update package descriptions
aayush0325 604ada8
chore: typo in readme
aayush0325 5511406
feat: updated using fmodf
aayush0325 3ef61a7
chore: update copyright year
Planeshifter 9aaf1e3
refactor: load stdlib constant instead of redefining it
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
243 changes: 243 additions & 0 deletions
243
lib/node_modules/@stdlib/math/base/special/gcdf/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2018 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). | ||
aayush0325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
<!-- 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( 'gcd(%d,%d) = %d', a[ i ], b[ i ], gcdf( a[ i ], b[ i ] ) ); | ||
aayush0325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
``` | ||
|
||
</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.0, 18.0 ); | ||
// returns 6.0 | ||
aayush0325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
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.0, 32.0, 48.0, 116.0, 33.0 }; | ||
const float b[] = { 12.0, 6.0, 15.0, 52.0, 22.0 }; | ||
aayush0325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
float out; | ||
int i; | ||
for ( i = 0; i < 5; i++ ) { | ||
out = stdlib_base_gcdf( a[ i ], b[ i ] ); | ||
printf( "gcd(%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"> | ||
|
||
* * * | ||
|
||
## See Also | ||
|
||
- <span class="package-name">[`@stdlib/math/base/special/lcm`][@stdlib/math/base/special/lcm]</span><span class="delimiter">: </span><span class="description">compute the least common multiple (lcm).</span> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<!-- 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> --> | ||
|
||
[@stdlib/math/base/special/lcm]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/lcm | ||
|
||
<!-- </related-links> --> | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</section> | ||
|
||
<!-- /.links --> |
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/math/base/special/gcdf/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2018 The Stdlib Authors. | ||
aayush0325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
* | ||
* 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-nan' ); | ||
aayush0325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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(); | ||
}); |
63 changes: 63 additions & 0 deletions
63
lib/node_modules/@stdlib/math/base/special/gcdf/benchmark/benchmark.native.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-nan' ); | ||
aayush0325 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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(); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.