diff --git a/CHANGELOG.md b/CHANGELOG.md index de5fd4a0d..ba6182c0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -144,6 +144,28 @@ This release closes the following issue: +
+ +#### [@stdlib/math/base/special/gcdf](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/gcdf) + +
+ +
+ +##### Features + +- [`f8bcfd8`](https://github.com/stdlib-js/stdlib/commit/f8bcfd832483d46068c710b6854d5f97bcb778fd) - add `math/base/special/gcdf` [(#2997)](https://github.com/stdlib-js/stdlib/pull/2997) + +
+ + + +
+ +
+ + + @@ -180,6 +202,7 @@ A total of 3 people contributed to this release. Thank you to the following cont
+- [`f8bcfd8`](https://github.com/stdlib-js/stdlib/commit/f8bcfd832483d46068c710b6854d5f97bcb778fd) - **feat:** add `math/base/special/gcdf` [(#2997)](https://github.com/stdlib-js/stdlib/pull/2997) _(by Aayush Khanna, Philipp Burckhardt)_ - [`6556a46`](https://github.com/stdlib-js/stdlib/commit/6556a46aa3a6dffdff6becd4fb98d32421b3e7f2) - **feat:** add `math/base/special/cfloorf` [(#3058)](https://github.com/stdlib-js/stdlib/pull/3058) _(by Aayush Khanna)_ - [`7fd112c`](https://github.com/stdlib-js/stdlib/commit/7fd112c799e3a864d975357b2066e45f4196653e) - **feat:** add `math/base/special/fmodf` [(#3059)](https://github.com/stdlib-js/stdlib/pull/3059) _(by Gunj Joshi, Philipp Burckhardt)_ - [`545a050`](https://github.com/stdlib-js/stdlib/commit/545a05093d99910b39773f57d3df2da9232b24f5) - **feat:** add `math/base/special/ahaversinf` [(#3076)](https://github.com/stdlib-js/stdlib/pull/3076) _(by Aayush Khanna)_ diff --git a/base/special/gcdf/README.md b/base/special/gcdf/README.md new file mode 100644 index 000000000..28dfc89d5 --- /dev/null +++ b/base/special/gcdf/README.md @@ -0,0 +1,235 @@ + + +# gcdf + +> Compute the [greatest common divisor][gcd] (gcd) of two single-precision floating point numbers. + + + +
+ +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). + +
+ + + + + +
+ +## 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 +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```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 ] ) ); +} +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### 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 ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/gcdf.h" +#include + +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 ); + } +} +``` + +
+ + + +
+ + + + + +
+ +## 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]. + +
+ + + + + + + + + + + + + + diff --git a/base/special/gcdf/benchmark/benchmark.js b/base/special/gcdf/benchmark/benchmark.js new file mode 100644 index 000000000..3fdca681f --- /dev/null +++ b/base/special/gcdf/benchmark/benchmark.js @@ -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( './../../../../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(); +}); diff --git a/base/special/gcdf/benchmark/benchmark.native.js b/base/special/gcdf/benchmark/benchmark.native.js new file mode 100644 index 000000000..8db45590b --- /dev/null +++ b/base/special/gcdf/benchmark/benchmark.native.js @@ -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( './../../../../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(); +}); diff --git a/base/special/gcdf/benchmark/c/native/Makefile b/base/special/gcdf/benchmark/c/native/Makefile new file mode 100644 index 000000000..f69e9da2b --- /dev/null +++ b/base/special/gcdf/benchmark/c/native/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/base/special/gcdf/benchmark/c/native/benchmark.c b/base/special/gcdf/benchmark/c/native/benchmark.c new file mode 100644 index 000000000..db4620ff0 --- /dev/null +++ b/base/special/gcdf/benchmark/c/native/benchmark.c @@ -0,0 +1,135 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gcdf.h" +#include +#include +#include +#include +#include + +#define NAME "gcdf" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static float rand_float( void ) { + int r = rand(); + return (float)r / ( (float)RAND_MAX + 1.0f ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + float a; + float b; + float y; + double t; + int i; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + a = round( 500.0f * rand_float() ); + b = round( 500.0f * rand_float() ); + y = stdlib_base_gcdf( a, b ); + if ( y != y ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y != y ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::native::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/base/special/gcdf/binding.gyp b/base/special/gcdf/binding.gyp new file mode 100644 index 000000000..ec3992233 --- /dev/null +++ b/base/special/gcdf/binding.gyp @@ -0,0 +1,170 @@ +# @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. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/base/special/gcdf/docs/repl.txt b/base/special/gcdf/docs/repl.txt new file mode 100644 index 000000000..5efee3cba --- /dev/null +++ b/base/special/gcdf/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( a, b ) + Computes the greatest common divisor (gcd) of two single-precision + floating point numbers. + + If both `a` and `b` are `0`, the function returns `0`. + + Both `a` and `b` must have integer values; otherwise, the function returns + `NaN`. + + Parameters + ---------- + a: integer + First number. + + b: integer + Second number. + + Returns + ------- + out: integer + Greatest common divisor. + + Examples + -------- + > var v = {{alias}}( 48, 18 ) + 6 + + See Also + -------- + diff --git a/base/special/gcdf/docs/types/index.d.ts b/base/special/gcdf/docs/types/index.d.ts new file mode 100644 index 000000000..2de00fcd4 --- /dev/null +++ b/base/special/gcdf/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Computes the greatest common divisor (gcd) of two single-precision floating point numbers. +* +* ## Notes +* +* - If both `a` and `b` are `0`, the function returns `0`. +* - Both `a` and `b` must have integer values; otherwise, the function returns `NaN`. +* +* @param a - first number +* @param b - second number +* @returns greatest common divisor +* +* @example +* var v = gcdf( 48, 18 ); +* // returns 6 +* +* @example +* var v = gcdf( 3.14, 18 ); +* // returns NaN +* +* @example +* var v = gcdf( NaN, 18 ); +* // returns NaN +*/ +declare function gcdf( a: number, b: number ): number; + + +// EXPORTS // + +export = gcdf; diff --git a/base/special/gcdf/docs/types/test.ts b/base/special/gcdf/docs/types/test.ts new file mode 100644 index 000000000..1ad842a5d --- /dev/null +++ b/base/special/gcdf/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @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. +*/ + +import gcdf = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + gcdf( 8, 2 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than two numbers... +{ + gcdf( true, 3 ); // $ExpectError + gcdf( false, 2 ); // $ExpectError + gcdf( '5', 1 ); // $ExpectError + gcdf( [], 1 ); // $ExpectError + gcdf( {}, 2 ); // $ExpectError + gcdf( ( x: number ): number => x, 2 ); // $ExpectError + + gcdf( 9, true ); // $ExpectError + gcdf( 9, false ); // $ExpectError + gcdf( 5, '5' ); // $ExpectError + gcdf( 8, [] ); // $ExpectError + gcdf( 9, {} ); // $ExpectError + gcdf( 8, ( x: number ): number => x ); // $ExpectError + + gcdf( [], true ); // $ExpectError + gcdf( {}, false ); // $ExpectError + gcdf( false, '5' ); // $ExpectError + gcdf( {}, [] ); // $ExpectError + gcdf( '5', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + gcdf(); // $ExpectError + gcdf( 3 ); // $ExpectError +} diff --git a/base/special/gcdf/examples/c/Makefile b/base/special/gcdf/examples/c/Makefile new file mode 100644 index 000000000..6aed70daf --- /dev/null +++ b/base/special/gcdf/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/base/special/gcdf/examples/c/example.c b/base/special/gcdf/examples/c/example.c new file mode 100644 index 000000000..b93ce69b5 --- /dev/null +++ b/base/special/gcdf/examples/c/example.c @@ -0,0 +1,32 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gcdf.h" +#include + +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 ); + } +} diff --git a/base/special/gcdf/examples/index.js b/base/special/gcdf/examples/index.js new file mode 100644 index 000000000..f7db1685d --- /dev/null +++ b/base/special/gcdf/examples/index.js @@ -0,0 +1,30 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var gcdf = require( './../lib' ); + +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 ] ) ); +} diff --git a/base/special/gcdf/include.gypi b/base/special/gcdf/include.gypi new file mode 100644 index 000000000..575cb043c --- /dev/null +++ b/base/special/gcdf/include.gypi @@ -0,0 +1,53 @@ +# @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. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + ' b ) { + t = b; + b = a; + a = t; + } + b -= a; // b=0 iff b=a + } + // Restore common factors of 2... + return k * a; +} + + +// EXPORTS // + +module.exports = gcdf; diff --git a/base/special/gcdf/lib/bitwise_binary_gcd.js b/base/special/gcdf/lib/bitwise_binary_gcd.js new file mode 100644 index 000000000..94084faef --- /dev/null +++ b/base/special/gcdf/lib/bitwise_binary_gcd.js @@ -0,0 +1,81 @@ +/** +* @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'; + +/** +* Computes the greatest common divisor (gcd) of two single-precision floating point numbers using the binary GCD algorithm and bitwise operations. +* +* ## 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]. +* +* [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2 +* +* @private +* @param {integer32} a - first number +* @param {integer32} b - second number +* @returns {integer32} greatest common divisor +* +* @example +* var v = gcdf( 48, 18 ); +* // returns 6 +*/ +function gcdf( a, b ) { + var k = 0; + var t; + + // Simple cases: + if ( a === 0 ) { + return b; + } + if ( b === 0 ) { + return a; + } + // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`... + while ( (a & 1) === 0 && (b & 1) === 0 ) { + a >>>= 1; // right shift + b >>>= 1; // right shift + k += 1; + } + // Reduce `a` to an odd number... + while ( (a & 1) === 0 ) { + a >>>= 1; // right shift + } + // Henceforth, `a` is always odd... + while ( b ) { + // Remove all factors of 2 in `b`, as they are not common... + while ( (b & 1) === 0 ) { + b >>>= 1; // right shift + } + // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)... + if ( a > b ) { + t = b; + b = a; + a = t; + } + b -= a; // b=0 iff b=a + } + // Restore common factors of 2... + return a << k; +} + + +// EXPORTS // + +module.exports = gcdf; diff --git a/base/special/gcdf/lib/index.js b/base/special/gcdf/lib/index.js new file mode 100644 index 000000000..49b29efae --- /dev/null +++ b/base/special/gcdf/lib/index.js @@ -0,0 +1,40 @@ +/** +* @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'; + +/** +* Compute the greatest common divisor (gcd) of two single-precision floating point numbers. +* +* @module @stdlib/math/base/special/gcdf +* +* @example +* var gcd = require( '@stdlib/math/base/special/gcdf' ); +* +* var v = gcdf( 48, 18 ); +* // returns 6 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/special/gcdf/lib/main.js b/base/special/gcdf/lib/main.js new file mode 100644 index 000000000..c5bd420c7 --- /dev/null +++ b/base/special/gcdf/lib/main.js @@ -0,0 +1,83 @@ +/** +* @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 isnanf = require( './../../../../base/assert/is-nanf' ); +var isIntegerf = require( './../../../../base/assert/is-integerf' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var INT32_MAX = require( '@stdlib/constants/int32/max' ); +var bitwisef = require( './bitwise_binary_gcd.js' ); +var largeIntegersf = require( './binary_gcd.js' ); + + +// MAIN // + +/** +* Computes the greatest common divisor (gcd) of two single-precision floating point numbers. +* +* @param {integer} a - first number +* @param {integer} b - second number +* @returns {integer} greatest common divisor +* +* @example +* var v = gcdf( 48, 18 ); +* // returns 6 +* +* @example +* var v = gcdf( 3.14, 18 ); +* // returns NaN +* +* @example +* var v = gcdf( NaN, 18 ); +* // returns NaN +*/ +function gcdf( a, b ) { + if ( isnanf( a ) || isnanf( b ) ) { + return NaN; + } + if ( + a === PINF || + b === PINF || + a === NINF || + b === NINF + ) { + return NaN; + } + if ( !( isIntegerf( a ) && isIntegerf( b ) ) ) { + return NaN; + } + if ( a < 0 ) { + a = -a; + } + if ( b < 0 ) { + b = -b; + } + if ( a <= INT32_MAX && b <= INT32_MAX ) { + return bitwisef( a, b ); + } + return largeIntegersf( a, b ); +} + + +// EXPORTS // + +module.exports = gcdf; diff --git a/base/special/gcdf/lib/native.js b/base/special/gcdf/lib/native.js new file mode 100644 index 000000000..67ab26aff --- /dev/null +++ b/base/special/gcdf/lib/native.js @@ -0,0 +1,51 @@ +/** +* @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 addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Computes the greatest common divisor (gcd) of two single-precision floating point numbers. +* +* @private +* @param {number} a - first number +* @param {number} b - second number +* @returns {number} greatest common divisor +* +* @example +* var v = gcdf( 0.0, 0.0 ); +* // returns 0.0 +* +* @example +* var v = gcdf( 48.0, 18.0 ); +* // returns 6.0 +*/ +function gcdf( a, b ) { + return addon( a, b ); +} + + +// EXPORTS // + +module.exports = gcdf; diff --git a/base/special/gcdf/manifest.json b/base/special/gcdf/manifest.json new file mode 100644 index 000000000..de47933d8 --- /dev/null +++ b/base/special/gcdf/manifest.json @@ -0,0 +1,87 @@ +{ + "options": { + "task": "build" + }, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "task": "build", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/napi/binary", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/fmodf", + "@stdlib/math/base/assert/is-integerf", + "@stdlib/constants/float32/pinf", + "@stdlib/constants/float32/ninf", + "@stdlib/constants/int32/max" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/fmodf", + "@stdlib/math/base/assert/is-integerf", + "@stdlib/constants/float32/pinf", + "@stdlib/constants/float32/ninf", + "@stdlib/constants/int32/max" + ] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/fmodf", + "@stdlib/math/base/assert/is-integerf", + "@stdlib/constants/float32/pinf", + "@stdlib/constants/float32/ninf", + "@stdlib/constants/int32/max" + ] + } + ] +} diff --git a/base/special/gcdf/package.json b/base/special/gcdf/package.json new file mode 100644 index 000000000..ff2e1d434 --- /dev/null +++ b/base/special/gcdf/package.json @@ -0,0 +1,199 @@ +{ + "name": "@stdlib/math/base/special/gcdf", + "version": "0.0.0", + "description": "Compute the greatest common divisor (gcd).", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "gypfile": true, + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "math", + "mathematics", + "euclid", + "euclidean", + "stein", + "binary gcd", + "greatest common divisor", + "greatest common factor", + "highest common factor", + "greatest common measure", + "highest common divisor", + "gcd", + "gcf", + "hcf", + "gcm", + "arithmetic", + "integer" + ], + "__stdlib__": { + "scaffold": { + "$schema": "math/base@v1.0", + "base_alias": "gcd", + "alias": "gcd", + "pkg_desc": "compute the greatest common divisor (gcd)", + "desc": "computes the greatest common divisor (gcd)", + "short_desc": "greatest common divisor (gcd)", + "parameters": [ + { + "name": "a", + "desc": "first number", + "type": { + "javascript": "integer", + "jsdoc": "integer", + "c": "double", + "dtype": "float64" + }, + "domain": [ + { + "min": "-infinity", + "max": "infinity" + } + ], + "rand": { + "prng": "random/base/discrete-uniform", + "parameters": [ + 0, + 50 + ] + }, + "example_values": [ + 1, + 2, + 4, + 5, + 9, + 10, + 15, + 20, + 21, + 25, + 28, + 30, + 33, + 35, + 39, + 40, + 42, + 45, + 48, + 50 + ] + }, + { + "name": "b", + "desc": "second number", + "type": { + "javascript": "integer", + "jsdoc": "integer", + "c": "double", + "dtype": "float64" + }, + "domain": [ + { + "min": "-infinity", + "max": "infinity" + } + ], + "rand": { + "prng": "random/base/discrete-uniform", + "parameters": [ + 0, + 50 + ] + }, + "example_values": [ + 1, + 2, + 4, + 5, + 9, + 10, + 15, + 20, + 21, + 25, + 28, + 30, + 33, + 35, + 39, + 40, + 42, + 45, + 48, + 50 + ] + } + ], + "returns": { + "desc": "greatest common divisor", + "type": { + "javascript": "number", + "jsdoc": "number", + "c": "double", + "dtype": "float64" + } + }, + "keywords": [ + "binary gcd", + "greatest common divisor", + "greatest common factor", + "highest common factor", + "greatest common measure", + "highest common divisor", + "gcd", + "gcf", + "hcf", + "gcm" + ], + "extra_keywords": [] + } + } +} diff --git a/base/special/gcdf/src/Makefile b/base/special/gcdf/src/Makefile new file mode 100644 index 000000000..bcf18aa46 --- /dev/null +++ b/base/special/gcdf/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/base/special/gcdf/src/addon.c b/base/special/gcdf/src/addon.c new file mode 100644 index 000000000..2f5253df6 --- /dev/null +++ b/base/special/gcdf/src/addon.c @@ -0,0 +1,23 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gcdf.h" +#include "stdlib/math/base/napi/binary.h" + +// cppcheck-suppress shadowFunction +STDLIB_MATH_BASE_NAPI_MODULE_FF_F( stdlib_base_gcdf ) diff --git a/base/special/gcdf/src/main.c b/base/special/gcdf/src/main.c new file mode 100644 index 000000000..41667d05a --- /dev/null +++ b/base/special/gcdf/src/main.c @@ -0,0 +1,181 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gcdf.h" +#include "stdlib/math/base/special/fmodf.h" +#include "stdlib/math/base/assert/is_nanf.h" +#include "stdlib/math/base/assert/is_integerf.h" +#include "stdlib/constants/float32/pinf.h" +#include "stdlib/constants/float32/ninf.h" +#include "stdlib/constants/int32/max.h" +#include + +/** +* Computes the greatest common divisor (gcd) using the binary GCD algorithm. +* +* @param a first number +* @param b second number +* @return greatest common divisor +* +* @example +* float out = largeIntegersf(16777216.0, 65536.0); +* // returns 65536.0 +*/ +static float largeIntegersf( const float a, const float b ) { + float ac; + float bc; + float k; + float t; + + // Simple cases: + if ( a == 0.0 ) { + return b; + } + if ( b == 0.0 ) { + return a; + } + ac = a; + bc = b; + k = 1.0; + + // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`... + while ( stdlib_base_fmodf( ac, 2.0 ) == 0.0 && stdlib_base_fmodf( bc, 2.0 ) == 0.0 ) { + ac /= 2.0; // right shift + bc /= 2.0; // right shift + k *= 2.0; // left shift + } + // Reduce `a` to an odd number... + while ( stdlib_base_fmodf( ac, 2.0 ) == 0.0 ) { + ac /= 2.0; // right shift + } + // Henceforth, `a` is always odd... + while ( bc ) { + // Remove all factors of 2 in `b`, as they are not common... + while ( stdlib_base_fmodf( bc, 2.0 ) == 0.0 ) { + bc /= 2.0; // right shift + } + // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)... + if ( ac > bc ) { + t = bc; + bc = ac; + ac = t; + } + bc -= ac; // b=0 iff b=a + } + // Restore common factors of 2... + return k * ac; +} + +/** +* Computes the greatest common divisor (gcd) using the binary GCD algorithm and bitwise operations. +* +* @param a first number +* @param b second number +* @return greatest common divisor +* +* @example +* float out = bitwisef( 48.0, 18.0 ); +* // returns 6.0 +*/ +static float bitwisef( const float a, const float b ) { + int32_t ac; + int32_t bc; + int32_t k; + int32_t t; + + // Simple cases: + if ( a == 0.0 ) { + return b; + } + if ( b == 0.0 ) { + return a; + } + ac = (int32_t)a; + bc = (int32_t)b; + k = 0; + + // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`... + while ( (ac & 1) == 0 && (bc & 1) == 0 ) { + ac >>= 1; // right shift + bc >>= 1; // right shift + k += 1; + } + // Reduce `a` to an odd number... + while ( (ac & 1) == 0 ) { + ac >>= 1; // right shift + } + // Henceforth, `a` is always odd... + while ( bc ) { + // Remove all factors of 2 in `b`, as they are not common... + while ( (bc & 1) == 0 ) { + bc >>= 1; // right shift + } + // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)... + if ( ac > bc ) { + t = bc; + bc = ac; + ac = t; + } + bc -= ac; // b=0 iff b=a + } + // Restore common factors of 2... + return ac << k; +} + +/** +* Computes the greatest common divisor (gcd). +* +* @param a first number +* @param b second number +* @return greatest common divisor +* +* @example +* float out = stdlib_base_gcdf( 48.0, 18.0 ); +* // returns 6.0 +*/ +float stdlib_base_gcdf( const float a, const float b ) { + float ac; + float bc; + + if ( stdlib_base_is_nanf( a ) || stdlib_base_is_nanf( b ) ) { + return 0.0/0.0; // NaN + } + if ( + a == STDLIB_CONSTANT_FLOAT32_PINF || + b == STDLIB_CONSTANT_FLOAT32_PINF || + a == STDLIB_CONSTANT_FLOAT32_NINF || + b == STDLIB_CONSTANT_FLOAT32_NINF + ) { + return 0.0/0.0; // NaN + } + if ( !( stdlib_base_is_integerf( a ) && stdlib_base_is_integerf( b ) ) ) { + return 0.0/0.0; // NaN + } + ac = a; + bc = b; + if ( ac < 0 ) { + ac = -ac; + } + if ( bc < 0 ) { + bc = -bc; + } + if ( ac <= STDLIB_CONSTANT_INT32_MAX && bc <= STDLIB_CONSTANT_INT32_MAX ) { + return bitwisef( ac, bc ); + } + return largeIntegersf( ac, bc ); +} diff --git a/base/special/gcdf/test/test.js b/base/special/gcdf/test/test.js new file mode 100644 index 000000000..7277bbcc8 --- /dev/null +++ b/base/special/gcdf/test/test.js @@ -0,0 +1,162 @@ +/** +* @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 tape = require( 'tape' ); +var isnanf = require( './../../../../base/assert/is-nanf' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var gcdf = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gcdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `NaN`', function test( t ) { + var v; + + v = gcdf( NaN, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( 10, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( NaN, NaN ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `+infinity`', function test( t ) { + var v; + + v = gcdf( PINF, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( 10, PINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( PINF, PINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `-infinity`', function test( t ) { + var v; + + v = gcdf( NINF, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( 10, NINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( NINF, NINF ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is not an integer value', function test( t ) { + var v; + + v = gcdf( 3.14, 10 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( 10, 3.14 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + v = gcdf( 3.14, 6.18 ); + t.strictEqual( isnanf( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the greatest common divisor', function test( t ) { + var v; + + v = gcdf( 0, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = gcdf( 1, 0 ); + t.strictEqual( v, 1, 'returns expected value' ); + + v = gcdf( 0, 1 ); + t.strictEqual( v, 1, 'returns expected value' ); + + v = gcdf( 6, 4 ); + t.strictEqual( v, 2, 'returns expected value' ); + + v = gcdf( 6, -4 ); + t.strictEqual( v, 2, 'returns expected value' ); + + v = gcdf( -6, -4 ); + t.strictEqual( v, 2, 'returns expected value' ); + + v = gcdf( 15, 20 ); + t.strictEqual( v, 5, 'returns expected value' ); + + v = gcdf( 20, 15 ); + t.strictEqual( v, 5, 'returns expected value' ); + + v = gcdf( 35, -21 ); + t.strictEqual( v, 7, 'returns expected value' ); + + v = gcdf( 48, 18 ); + t.strictEqual( v, 6, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing large integers (>= 2**31 - 1)', function test( t ) { + var TWO_100 = 1.2676506002282294e+30; + var TWO_53 = 9007199254740992; + var v; + + v = gcdf( TWO_100, 0 ); + t.strictEqual( v, TWO_100, 'returns expected value' ); + + v = gcdf( 0, TWO_53 ); + t.strictEqual( v, TWO_53, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( TWO_100, TWO_53 ); + t.strictEqual( v, TWO_53, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( TWO_100, 73453 ); + t.strictEqual( v, 1, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( TWO_100, 3491832 ); + t.strictEqual( v, 8, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( 3491832, TWO_100 ); + t.strictEqual( v, 8, 'returns expected value' ); + + t.end(); +}); diff --git a/base/special/gcdf/test/test.native.js b/base/special/gcdf/test/test.native.js new file mode 100644 index 000000000..68be4f9ac --- /dev/null +++ b/base/special/gcdf/test/test.native.js @@ -0,0 +1,171 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( './../../../../base/assert/is-nanf' ); +var PINF = require( '@stdlib/constants/float32/pinf' ); +var NINF = require( '@stdlib/constants/float32/ninf' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var gcdf = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( gcdf instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gcdf, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `NaN`', opts, function test( t ) { + var v; + + v = gcdf( NaN, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( 10, NaN ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( NaN, NaN ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `+infinity`', opts, function test( t ) { + var v; + + v = gcdf( PINF, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( 10, PINF ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( PINF, PINF ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is `-infinity`', opts, function test( t ) { + var v; + + v = gcdf( NINF, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( 10, NINF ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( NINF, NINF ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `NaN` if either argument is not an integer value', opts, function test( t ) { + var v; + + v = gcdf( 3.14, 10 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( 10, 3.14 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = gcdf( 3.14, 6.18 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function computes the greatest common divisor', opts, function test( t ) { + var v; + + v = gcdf( 0, 0 ); + t.strictEqual( v, 0, 'returns expected value' ); + + v = gcdf( 1, 0 ); + t.strictEqual( v, 1, 'returns expected value' ); + + v = gcdf( 0, 1 ); + t.strictEqual( v, 1, 'returns expected value' ); + + v = gcdf( 6, 4 ); + t.strictEqual( v, 2, 'returns expected value' ); + + v = gcdf( 6, -4 ); + t.strictEqual( v, 2, 'returns expected value' ); + + v = gcdf( -6, -4 ); + t.strictEqual( v, 2, 'returns expected value' ); + + v = gcdf( 15, 20 ); + t.strictEqual( v, 5, 'returns expected value' ); + + v = gcdf( 20, 15 ); + t.strictEqual( v, 5, 'returns expected value' ); + + v = gcdf( 35, -21 ); + t.strictEqual( v, 7, 'returns expected value' ); + + v = gcdf( 48, 18 ); + t.strictEqual( v, 6, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing large integers (>= 2**31 - 1)', opts, function test( t ) { + var TWO_100 = 1.2676506002282294e+30; + var TWO_53 = 9007199254740992; + var v; + + v = gcdf( TWO_100, 0 ); + t.strictEqual( v, TWO_100, 'returns expected value' ); + + v = gcdf( 0, TWO_53 ); + t.strictEqual( v, TWO_53, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( TWO_100, TWO_53 ); + t.strictEqual( v, TWO_53, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( TWO_100, 73453 ); + t.strictEqual( v, 1, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( TWO_100, 3491832 ); + t.strictEqual( v, 8, 'returns expected value' ); + + // Verified on Wolfram Alpha: + v = gcdf( 3491832, TWO_100 ); + t.strictEqual( v, 8, 'returns expected value' ); + + t.end(); +});