From d9a959bebba033accbf2232f47beb79a46b12625 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 14 Apr 2024 10:09:03 +0000 Subject: [PATCH] Auto-generated commit --- base/special/kernel-log1p/LICENSE | 192 +++++++++++++++++ base/special/kernel-log1p/README.md | 195 ++++++++++++++++++ .../kernel-log1p/benchmark/benchmark.js | 51 +++++ .../benchmark/benchmark.native.js | 60 ++++++ .../special/kernel-log1p/benchmark/c/Makefile | 146 +++++++++++++ .../kernel-log1p/benchmark/c/benchmark.c | 136 ++++++++++++ base/special/kernel-log1p/binding.gyp | 170 +++++++++++++++ base/special/kernel-log1p/docs/repl.txt | 30 +++ .../kernel-log1p/docs/types/index.d.ts | 50 +++++ base/special/kernel-log1p/docs/types/test.ts | 43 ++++ base/special/kernel-log1p/examples/c/Makefile | 146 +++++++++++++ .../special/kernel-log1p/examples/c/example.c | 31 +++ base/special/kernel-log1p/examples/index.js | 30 +++ base/special/kernel-log1p/include.gypi | 53 +++++ .../stdlib/math/base/special/kernel_log1p.h | 40 ++++ base/special/kernel-log1p/lib/index.js | 47 +++++ base/special/kernel-log1p/lib/main.js | 156 ++++++++++++++ base/special/kernel-log1p/lib/native.js | 54 +++++ base/special/kernel-log1p/lib/polyval_p.js | 47 +++++ base/special/kernel-log1p/lib/polyval_q.js | 47 +++++ base/special/kernel-log1p/manifest.json | 67 ++++++ base/special/kernel-log1p/package.json | 64 ++++++ base/special/kernel-log1p/scripts/evalpoly.js | 133 ++++++++++++ base/special/kernel-log1p/src/Makefile | 70 +++++++ base/special/kernel-log1p/src/addon.c | 23 +++ base/special/kernel-log1p/src/main.c | 180 ++++++++++++++++ base/special/kernel-log1p/test/test.js | 72 +++++++ base/special/kernel-log1p/test/test.native.js | 81 ++++++++ 28 files changed, 2414 insertions(+) create mode 100644 base/special/kernel-log1p/LICENSE create mode 100644 base/special/kernel-log1p/README.md create mode 100644 base/special/kernel-log1p/benchmark/benchmark.js create mode 100644 base/special/kernel-log1p/benchmark/benchmark.native.js create mode 100644 base/special/kernel-log1p/benchmark/c/Makefile create mode 100644 base/special/kernel-log1p/benchmark/c/benchmark.c create mode 100644 base/special/kernel-log1p/binding.gyp create mode 100644 base/special/kernel-log1p/docs/repl.txt create mode 100644 base/special/kernel-log1p/docs/types/index.d.ts create mode 100644 base/special/kernel-log1p/docs/types/test.ts create mode 100644 base/special/kernel-log1p/examples/c/Makefile create mode 100644 base/special/kernel-log1p/examples/c/example.c create mode 100644 base/special/kernel-log1p/examples/index.js create mode 100644 base/special/kernel-log1p/include.gypi create mode 100644 base/special/kernel-log1p/include/stdlib/math/base/special/kernel_log1p.h create mode 100644 base/special/kernel-log1p/lib/index.js create mode 100644 base/special/kernel-log1p/lib/main.js create mode 100644 base/special/kernel-log1p/lib/native.js create mode 100644 base/special/kernel-log1p/lib/polyval_p.js create mode 100644 base/special/kernel-log1p/lib/polyval_q.js create mode 100644 base/special/kernel-log1p/manifest.json create mode 100644 base/special/kernel-log1p/package.json create mode 100644 base/special/kernel-log1p/scripts/evalpoly.js create mode 100644 base/special/kernel-log1p/src/Makefile create mode 100644 base/special/kernel-log1p/src/addon.c create mode 100644 base/special/kernel-log1p/src/main.c create mode 100644 base/special/kernel-log1p/test/test.js create mode 100644 base/special/kernel-log1p/test/test.native.js diff --git a/base/special/kernel-log1p/LICENSE b/base/special/kernel-log1p/LICENSE new file mode 100644 index 000000000..a7566ad6f --- /dev/null +++ b/base/special/kernel-log1p/LICENSE @@ -0,0 +1,192 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + +DEPENDENCIES & ATTRIBUTION + +The library links against the following external libraries or contains +implementations from the following external libraries, which have their own +licenses: + +* FreeBSD + +Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. diff --git a/base/special/kernel-log1p/README.md b/base/special/kernel-log1p/README.md new file mode 100644 index 000000000..4a3ecfdb2 --- /dev/null +++ b/base/special/kernel-log1p/README.md @@ -0,0 +1,195 @@ + + +# kernelLog1p + +> Compute `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. + +
+ +## Usage + +```javascript +var kernelLog1p = require( '@stdlib/math/base/special/kernel-log1p' ); +``` + +#### kernelLog1p( f ) + +Computes `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. + +```javascript +var v = kernelLog1p( 1.0 ); +// returns ~0.1931 + +v = kernelLog1p( 1.4142135623730951 ); +// returns ~0.4672 + +v = kernelLog1p( NaN ); +// returns NaN +``` + +
+ + + +
+ +## Notes + +- This function is a helper function for computing logarithms in base `e`. Argument reduction and adding the final term of the polynomial must be done by the caller for increased accuracy when different bases are used. For reference, see [`@stdlib/math/base/special/log2`][@stdlib/math/base/special/log2] and [`@stdlib/math/base/special/log10`][@stdlib/math/base/special/log10]. + +
+ + + +
+ +## Examples + + + +```javascript +var linspace = require( '@stdlib/array/base/linspace' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var kernelLog1p = require( '@stdlib/math/base/special/kernel-log1p' ); + +var x = linspace( sqrt( 2.0 ) / 2.0, sqrt( 2.0 ), 100 ); + +var i; +for ( i = 0; i < x.length; i++ ) { + console.log( 'kernelLog1p(%d) = %d', x[ i ], kernelLog1p( x[ i ] ) ); +} +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/kernel_log1p.h" +``` + +#### stdlib_base_kernel_log1p( f ) + +Computes `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. + +```c +double out = stdlib_base_kernel_log1p( 1.0 ); +// returns ~0.1931 + +out = stdlib_base_kernel_log1p( 1.4142135623730951 ); +// returns ~0.4672 +``` + +The function accepts the following arguments: + +- **f**: `[in] double` input value. + +```c +double stdlib_base_kernel_log1p( const double f ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/kernel_log1p.h" +#include + +int main( void ) { + const double x[] = { 0.7071, 0.7856, 0.8642, 0.9428, 1.0213, 1.0999, 1.1785, 1.2570, 1.3356, 1.4142 }; + + double out; + int i; + for ( i = 0; i < 10; i++ ) { + out = stdlib_base_kernel_log1p( x[ i ] ); + printf ( "x[ i ]: %lf, out: %lf\n", x[ i ], out ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/base/special/kernel-log1p/benchmark/benchmark.js b/base/special/kernel-log1p/benchmark/benchmark.js new file mode 100644 index 000000000..456055a73 --- /dev/null +++ b/base/special/kernel-log1p/benchmark/benchmark.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 bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var kernelLog1p = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() + 1.0 ) * 0.7071067811865476; + y = kernelLog1p( x ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/special/kernel-log1p/benchmark/benchmark.native.js b/base/special/kernel-log1p/benchmark/benchmark.native.js new file mode 100644 index 000000000..c2e6189c9 --- /dev/null +++ b/base/special/kernel-log1p/benchmark/benchmark.native.js @@ -0,0 +1,60 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var kernelLog1p = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( kernelLog1p instanceof Error ) +}; + + +// MAIN // + +bench( pkg+'::native', opts, function benchmark( b ) { + var x; + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = ( randu() + 1.0 ) * 0.7071067811865476; + y = kernelLog1p( x ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/special/kernel-log1p/benchmark/c/Makefile b/base/special/kernel-log1p/benchmark/c/Makefile new file mode 100644 index 000000000..f69e9da2b --- /dev/null +++ b/base/special/kernel-log1p/benchmark/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 := 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/kernel-log1p/benchmark/c/benchmark.c b/base/special/kernel-log1p/benchmark/c/benchmark.c new file mode 100644 index 000000000..470ddad13 --- /dev/null +++ b/base/special/kernel-log1p/benchmark/c/benchmark.c @@ -0,0 +1,136 @@ +/** +* @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. +*/ + +/** +* Benchmark `kernel_log1p`. +*/ +#include "stdlib/math/base/special/kernel_log1p.h" +#include +#include +#include +#include +#include + +#define NAME "kernel_log1p" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +void print_version() { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +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 +*/ +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 +*/ +double tic() { + 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 +*/ +double rand_double() { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +double benchmark() { + double elapsed; + double x; + double z; + double t; + int i; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + x = ( rand_double() + 1.0 ) * 0.7071067811865476; + z = stdlib_base_kernel_log1p( x ); + if ( z != z ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( z != z ) { + 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::%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/kernel-log1p/binding.gyp b/base/special/kernel-log1p/binding.gyp new file mode 100644 index 000000000..ec3992233 --- /dev/null +++ b/base/special/kernel-log1p/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/kernel-log1p/docs/repl.txt b/base/special/kernel-log1p/docs/repl.txt new file mode 100644 index 000000000..54f8a0178 --- /dev/null +++ b/base/special/kernel-log1p/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( f ) + Computes `log(1+f) - f` for `1+f` in ~[sqrt(2)/2, sqrt(2)]. + + This function provides a common means for computing logarithms in base e. + Argument reduction and adding the final term of the polynomial must be done + by the caller for increased accuracy when different bases are used. + + Parameters + ---------- + f: number + Input value. + + Returns + ------- + out: number + Function value. + + Examples + -------- + > var y = {{alias}}( 1.0 ) + ~0.1931 + > y = {{alias}}( 1.4142135623730951 ) + ~0.4672 + > y = {{alias}}( NaN ) + NaN + + See Also + -------- + diff --git a/base/special/kernel-log1p/docs/types/index.d.ts b/base/special/kernel-log1p/docs/types/index.d.ts new file mode 100644 index 000000000..fefc4ceb6 --- /dev/null +++ b/base/special/kernel-log1p/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 `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. +* +* ## Notes +* +* - This function provides a common means for computing logarithms in base `e`. Argument reduction and adding the final term of the polynomial must be done by the caller for increased accuracy when different bases are used. +* +* @param f - input value +* @returns function value +* +* @example +* var v = kernelLog1p( 1.0 ); +* // returns ~0.1931 +* +* @example +* var sqrt = require( '@stdlib/math/base/special/sqrt' ); +* +* var v = kernelLog1p( sqrt( 2.0 ) ); +* // returns ~0.4672 +* +* @example +* var v = kernelLog1p( NaN ); +* // returns NaN +*/ +declare function kernelLog1p( f: number ): number; + + +// EXPORTS // + +export = kernelLog1p; diff --git a/base/special/kernel-log1p/docs/types/test.ts b/base/special/kernel-log1p/docs/types/test.ts new file mode 100644 index 000000000..b89025803 --- /dev/null +++ b/base/special/kernel-log1p/docs/types/test.ts @@ -0,0 +1,43 @@ +/* +* @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 kernelLog1p = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + kernelLog1p( 0.5 ); // $ExpectType number +} + +// The compiler throws an error if the function is not provided a number... +{ + kernelLog1p( true ); // $ExpectError + kernelLog1p( false ); // $ExpectError + kernelLog1p( '5' ); // $ExpectError + kernelLog1p( [] ); // $ExpectError + kernelLog1p( {} ); // $ExpectError + kernelLog1p( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + kernelLog1p(); // $ExpectError + kernelLog1p( 0, 2 ); // $ExpectError +} diff --git a/base/special/kernel-log1p/examples/c/Makefile b/base/special/kernel-log1p/examples/c/Makefile new file mode 100644 index 000000000..6aed70daf --- /dev/null +++ b/base/special/kernel-log1p/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/kernel-log1p/examples/c/example.c b/base/special/kernel-log1p/examples/c/example.c new file mode 100644 index 000000000..5c750c05b --- /dev/null +++ b/base/special/kernel-log1p/examples/c/example.c @@ -0,0 +1,31 @@ +/** +* @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/kernel_log1p.h" +#include + +int main( void ) { + const double x[] = { 0.7071, 0.7856, 0.8642, 0.9428, 1.0213, 1.0999, 1.1785, 1.2570, 1.3356, 1.4142 }; + + double out; + int i; + for ( i = 0; i < 10; i++ ) { + out = stdlib_base_kernel_log1p( x[ i ] ); + printf ( "x[ i ]: %lf, out: %lf\n", x[ i ], out ); + } +} diff --git a/base/special/kernel-log1p/examples/index.js b/base/special/kernel-log1p/examples/index.js new file mode 100644 index 000000000..23435c16c --- /dev/null +++ b/base/special/kernel-log1p/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 linspace = require( '@stdlib/array/base/linspace' ); +var sqrt = require( './../../../../base/special/sqrt' ); +var kernelLog1p = require( './../lib' ); + +var x = linspace( sqrt( 2.0 ) / 2.0, sqrt( 2.0 ), 100 ); + +var i; +for ( i = 0; i < x.length; i++ ) { + console.log( 'kernelLog1p(%d) = %d', x[ i ], kernelLog1p( x[ i ] ) ); +} diff --git a/base/special/kernel-log1p/include.gypi b/base/special/kernel-log1p/include.gypi new file mode 100644 index 000000000..575cb043c --- /dev/null +++ b/base/special/kernel-log1p/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': [ + ' + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Computes `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. +*/ +double stdlib_base_kernel_log1p( const double f ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_MATH_BASE_SPECIAL_KERNEL_LOG1P_H diff --git a/base/special/kernel-log1p/lib/index.js b/base/special/kernel-log1p/lib/index.js new file mode 100644 index 000000000..86d2c605e --- /dev/null +++ b/base/special/kernel-log1p/lib/index.js @@ -0,0 +1,47 @@ +/** +* @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 `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. +* +* @module @stdlib/math/base/special/kernel-log1p +* +* @example +* var sqrt = require( '@stdlib/math/base/special/sqrt' ); +* var kernelLog1p = require( '@stdlib/math/base/special/kernel-log1p' ); +* +* var v = kernelLog1p( 1.0 ); +* // returns ~0.1931 +* +* v = kernelLog1p( sqrt( 2.0 ) ); +* // returns ~0.4672 +* +* v = kernelLog1p( NaN ); +* // returns NaN +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/special/kernel-log1p/lib/main.js b/base/special/kernel-log1p/lib/main.js new file mode 100644 index 000000000..b6c219f78 --- /dev/null +++ b/base/special/kernel-log1p/lib/main.js @@ -0,0 +1,156 @@ +/** +* @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. +* +* +* ## Notice +* +* The following copyright and license were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/k_log.h}. The implementation follows the original, but has been modified for JavaScript. +* +* ```text +* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. +* +* Developed at SunPro, a Sun Microsystems, Inc. business. +* Permission to use, copy, modify, and distribute this +* software is freely granted, provided that this notice +* is preserved. +* ``` +*/ + +'use strict'; + +// MODULES // + +var polyvalP = require( './polyval_p.js' ); +var polyvalQ = require( './polyval_q.js' ); + + +// MAIN // + +/** +* Computes `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. +* +* ## Method +* +* This function is a helper function for computing logarithms in base \\(e\\), and what follows describes the overall strategy for doing so. The argument reduction and adding the final term of the polynomial are done by the caller for increased accuracy when different bases are used. +* +* 1. Argument Reduction. Find \\(k\\) and \\(f\\) such that +* +* ```tex +* x = 2^k \cdot (1+f) +* ``` +* +* where \\(\sqrt(2)/2 < 1+f < \sqrt(2)\\). +* +* 2. Approximation of \\(\operatorname{log}(1+f)\\). Let +* +* ```tex +* \begin{align*} +* s &= \frac{f}{2+f} \\ +* &= 2s + \frac{2}{3} s^3 + \frac{2}{5} s^5 + \ldots \\ +* &= 2s + s R +* \end{align*} +* ``` +* +* based on +* +* ```tex +* \operatorname{log}(1+f) &= \operatorname{log}(1+s) - \operatorname{log}(1-s) +* ``` +* +* We use a special Reme algorithm on \\(\[0,0.1716]\\) to generate a polynomial of degree \\(14\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-58.45}\\). In other words, +* +* ```tex +* R(z) ~ L_{g1} s^2 + L_{g2} s^4 + L_{g3} s^6 + L_{g4} s^8 + L_{g5} s^{10} + L_{g6} s^{12} + L_{g7} s^{14} +* ``` +* +* where the values of \\(L_{g1}\\) to \\(L_{g7}\\) are the polynomial coefficients used in the program below and +* +* ```tex +* L_{g1} s^2 + \ldots + L_{g7} s^{14} - R(z) \leq 2^{-58.45} +* ``` +* +* Note that +* +* ```tex +* 2s = f - s \cdot f = f - h_{fsq} + (s \cdot h_{fsq}) +* ``` +* +* where \\(h_{fsq} = f^{2}/2\\). +* +* In order to guarantee an error in \\(\operatorname{log}\\) below 1 ulp, we compute \\(\operatorname{log}\\) by +* +* ```tex +* \begin{align*} +* \operatorname{log}(1+f) &= f - s (f - R) & \textrm{(if f is not too large)} \\ +* \operatorname{log}(1+f) &= f - (h_{fsq} - s (h_{fsq}+R)) & \textrm{(better accuracy)} +* \end{align*} +* +* 3. Finally, +* +* ```tex +* \begin{align*} +* \operatorname{log}(x) &= k \cdot \operatorname{ln2} + \operatorname{log}(1+f) \\ +* &= k \cdot \operatorname{ln2}_{hi} + (f-(h_{fsq}-(s \cdot (h_{fsq}+R) + k \cdot \operatorname{ln2}_{lo}))) +* \end{align*} +* ``` +* +* Here, \\(\operatorname{ln2}\\) is split into two floating point numbers: +* +* ```tex +* \operatorname{ln2} = \operatorname{ln2}_{hi} + \operatorname{ln2}_{lo} +* ``` +* +* where \\(n \cdot \operatorname{ln2}_{hi}\\) is always exact for \\(|n| < 2000\\). +* +* @param {number} f - input value +* @returns {number} function value +* +* @example +* var v = kernelLog1p( 1.0 ); +* // returns ~0.1931 +* +* @example +* var v = kernelLog1p( 1.4142135623730951 ); +* // returns ~0.4672 +* +* @example +* var v = kernelLog1p( NaN ); +* // returns NaN +*/ +function kernelLog1p( f ) { + var hfsq; + var t1; + var t2; + var s; + var z; + var R; + var w; + + s = f / ( 2.0 + f ); + z = s * s; + w = z * z; + t1 = w * polyvalP( w ); + t2 = z * polyvalQ( w ); + R = t2 + t1; + hfsq = 0.5 * f * f; + return s * ( hfsq + R ); +} + + +// EXPORTS // + +module.exports = kernelLog1p; diff --git a/base/special/kernel-log1p/lib/native.js b/base/special/kernel-log1p/lib/native.js new file mode 100644 index 000000000..e870e168a --- /dev/null +++ b/base/special/kernel-log1p/lib/native.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 addon = require( './../src/addon.node' ); + + +// MAIN // + +/** +* Computes `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. +* +* @private +* @param {number} f - input value +* @returns {number} function value +* +* @example +* var v = kernelLog1p( 1.0 ); +* // returns ~0.1931 +* +* @example +* var v = kernelLog1p( 1.4142135623730951 ); +* // returns ~0.4672 +* +* @example +* var v = kernelLog1p( NaN ); +* // returns NaN +*/ +function kernelLog1p( f ) { + return addon( f ); +} + + +// EXPORTS // + +module.exports = kernelLog1p; diff --git a/base/special/kernel-log1p/lib/polyval_p.js b/base/special/kernel-log1p/lib/polyval_p.js new file mode 100644 index 000000000..6a1309b25 --- /dev/null +++ b/base/special/kernel-log1p/lib/polyval_p.js @@ -0,0 +1,47 @@ +/** +* @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. +*/ + +/* This is a generated file. Do not edit directly. */ +'use strict'; + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + if ( x === 0.0 ) { + return 0.3999999999940942; + } + return 0.3999999999940942 + (x * (0.22222198432149784 + (x * 0.15313837699209373))); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/special/kernel-log1p/lib/polyval_q.js b/base/special/kernel-log1p/lib/polyval_q.js new file mode 100644 index 000000000..31521f311 --- /dev/null +++ b/base/special/kernel-log1p/lib/polyval_q.js @@ -0,0 +1,47 @@ +/** +* @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. +*/ + +/* This is a generated file. Do not edit directly. */ +'use strict'; + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + if ( x === 0.0 ) { + return 0.6666666666666735; + } + return 0.6666666666666735 + (x * (0.2857142874366239 + (x * (0.1818357216161805 + (x * 0.14798198605116586))))); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/special/kernel-log1p/manifest.json b/base/special/kernel-log1p/manifest.json new file mode 100644 index 000000000..6bfc69006 --- /dev/null +++ b/base/special/kernel-log1p/manifest.json @@ -0,0 +1,67 @@ +{ + "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/unary" + ] + }, + { + "task": "benchmark", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + }, + { + "task": "examples", + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/base/special/kernel-log1p/package.json b/base/special/kernel-log1p/package.json new file mode 100644 index 000000000..57345df6e --- /dev/null +++ b/base/special/kernel-log1p/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/math/base/special/kernel-log1p", + "version": "0.0.0", + "description": "Compute log(1+f) - f for 1+f in ~[sqrt(2)/2, sqrt(2)].", + "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", + "scripts": "./scripts", + "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", + "mathematics", + "math", + "log", + "kernel", + "logarithm" + ] +} diff --git a/base/special/kernel-log1p/scripts/evalpoly.js b/base/special/kernel-log1p/scripts/evalpoly.js new file mode 100644 index 000000000..a72d3c15c --- /dev/null +++ b/base/special/kernel-log1p/scripts/evalpoly.js @@ -0,0 +1,133 @@ +/** +* @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. +*/ + +/* +* This script compiles modules for evaluating polynomial functions. If any polynomial coefficients change, this script should be rerun to update the compiled files. +*/ +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var writeFileSync = require( '@stdlib/fs/write-file' ).sync; +var currentYear = require( '@stdlib/time/current-year' ); +var substringBefore = require( '@stdlib/string/substring-before' ); +var substringAfter = require( '@stdlib/string/substring-after' ); +var format = require( '@stdlib/string/format' ); +var licenseHeader = require( '@stdlib/_tools/licenses/header' ); +var compile = require( './../../../../base/tools/evalpoly-compile' ); +var compileC = require( './../../../../base/tools/evalpoly-compile-c' ); + + +// VARIABLES // + +// Polynomial coefficients ordered in ascending degree... +var P = [ + 3.999999999940941908e-01, // 3FD99999 9997FA04 + 2.222219843214978396e-01, // 3FCC71C5 1D8E78AF + 1.531383769920937332e-01 // 3FC39A09 D078C69F +]; +var Q = [ + 6.666666666666735130e-01, // 3FE55555 55555593 + 2.857142874366239149e-01, // 3FD24924 94229359 + 1.818357216161805012e-01, // 3FC74664 96CB03DE + 1.479819860511658591e-01 // 3FC2F112 DF3E5244 +]; + +// Header to add to output files: +var header = licenseHeader( 'Apache-2.0', 'js', { + 'year': currentYear(), + 'copyright': 'The Stdlib Authors' +}); +header += '\n/* This is a generated file. Do not edit directly. */\n'; + + +// FUNCTIONS // + +/** +* Inserts a compiled function into file content. +* +* @private +* @param {string} text - source content +* @param {string} id - function identifier +* @param {string} str - function string +* @returns {string} updated content +*/ +function insert( text, id, str ) { + var before; + var after; + var begin; + var end; + + begin = '// BEGIN: '+id; + end = '// END: '+id; + + before = substringBefore( text, begin ); + after = substringAfter( text, end ); + + return format( '%s// BEGIN: %s\n\n%s\n%s%s', before, id, str, end, after ); +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var fpath; + var copts; + var opts; + var file; + var str; + + opts = { + 'encoding': 'utf8' + }; + + fpath = resolve( __dirname, '..', 'lib', 'polyval_p.js' ); + str = header + compile( P ); + writeFileSync( fpath, str, opts ); + + fpath = resolve( __dirname, '..', 'lib', 'polyval_q.js' ); + str = header + compile( Q ); + writeFileSync( fpath, str, opts ); + + copts = { + 'dtype': 'double', + 'name': '' + }; + + fpath = resolve( __dirname, '..', 'src', 'main.c' ); + file = readFileSync( fpath, opts ); + + copts.name = 'polyval_p'; + str = compileC( P, copts ); + file = insert( file, copts.name, str ); + + copts.name = 'polyval_q'; + str = compileC( Q, copts ); + file = insert( file, copts.name, str ); + + writeFileSync( fpath, file, opts ); +} + +main(); diff --git a/base/special/kernel-log1p/src/Makefile b/base/special/kernel-log1p/src/Makefile new file mode 100644 index 000000000..bcf18aa46 --- /dev/null +++ b/base/special/kernel-log1p/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/kernel-log1p/src/addon.c b/base/special/kernel-log1p/src/addon.c new file mode 100644 index 000000000..daa620891 --- /dev/null +++ b/base/special/kernel-log1p/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/kernel_log1p.h" +#include "stdlib/math/base/napi/unary.h" + +// cppcheck-suppress shadowFunction +STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_kernel_log1p ) diff --git a/base/special/kernel-log1p/src/main.c b/base/special/kernel-log1p/src/main.c new file mode 100644 index 000000000..f33c68b3c --- /dev/null +++ b/base/special/kernel-log1p/src/main.c @@ -0,0 +1,180 @@ +/** +* @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. +* +* +* ## Notice +* +* The following copyright, license, and long comment were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/k_log.c}. The implementation follows the original, but has been modified according to project conventions. +* +* ```text +* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. +* +* Developed at SunPro, a Sun Microsystems, Inc. business. +* Permission to use, copy, modify, and distribute this +* software is freely granted, provided that this notice +* is preserved. +* ``` +*/ + +#include "stdlib/math/base/special/kernel_log1p.h" +#include + +/* Begin auto-generated functions. The following functions are auto-generated. Do not edit directly. */ + +// BEGIN: polyval_p + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @param x value at which to evaluate the polynomial +* @return evaluated polynomial +*/ +static double polyval_p( const double x ) { + return 0.3999999999940942 + (x * (0.22222198432149784 + (x * 0.15313837699209373))); +} + +// END: polyval_p + +// BEGIN: polyval_q + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @param x value at which to evaluate the polynomial +* @return evaluated polynomial +*/ +static double polyval_q( const double x ) { + return 0.6666666666666735 + (x * (0.2857142874366239 + (x * (0.1818357216161805 + (x * 0.14798198605116586))))); +} + +// END: polyval_q + +/* End auto-generated functions. */ + +/** +* Computes `log(1+f) - f` for `1+f` in `~[sqrt(2)/2, sqrt(2)]`. +* +* ## Method +* +* This function is a helper function for computing logarithms in base \\(e\\), and what follows describes the overall strategy for doing so. The argument reduction and adding the final term of the polynomial are done by the caller for increased accuracy when different bases are used. +* +* 1. Argument Reduction. Find \\(k\\) and \\(f\\) such that +* +* ```tex +* x = 2^k \cdot (1+f) +* ``` +* +* where \\(\sqrt(2)/2 < 1+f < \sqrt(2)\\). +* +* 2. Approximation of \\(\operatorname{log}(1+f)\\). Let +* +* ```tex +* \begin{align*} +* s &= \frac{f}{2+f} \\ +* &= 2s + \frac{2}{3} s^3 + \frac{2}{5} s^5 + \ldots \\ +* &= 2s + s R +* \end{align*} +* ``` +* +* based on +* +* ```tex +* \operatorname{log}(1+f) &= \operatorname{log}(1+s) - \operatorname{log}(1-s) +* ``` +* +* We use a special Reme algorithm on \\(\[0,0.1716]\\) to generate a polynomial of degree \\(14\\) to approximate \\(R\\). The maximum error of this polynomial approximation is bounded by \\(2^{-58.45}\\). In other words, +* +* ```tex +* R(z) ~ L_{g1} s^2 + L_{g2} s^4 + L_{g3} s^6 + L_{g4} s^8 + L_{g5} s^{10} + L_{g6} s^{12} + L_{g7} s^{14} +* ``` +* +* where the values of \\(L_{g1}\\) to \\(L_{g7}\\) are the polynomial coefficients used in the program below and +* +* ```tex +* L_{g1} s^2 + \ldots + L_{g7} s^{14} - R(z) \leq 2^{-58.45} +* ``` +* +* Note that +* +* ```tex +* 2s = f - s \cdot f = f - h_{fsq} + (s \cdot h_{fsq}) +* ``` +* +* where \\(h_{fsq} = f^{2}/2\\). +* +* In order to guarantee an error in \\(\operatorname{log}\\) below 1 ulp, we compute \\(\operatorname{log}\\) by +* +* ```tex +* \begin{align*} +* \operatorname{log}(1+f) &= f - s (f - R) & \textrm{(if f is not too large)} \\ +* \operatorname{log}(1+f) &= f - (h_{fsq} - s (h_{fsq}+R)) & \textrm{(better accuracy)} +* \end{align*} +* +* 3. Finally, +* +* ```tex +* \begin{align*} +* \operatorname{log}(x) &= k \cdot \operatorname{ln2} + \operatorname{log}(1+f) \\ +* &= k \cdot \operatorname{ln2}_{hi} + (f-(h_{fsq}-(s \cdot (h_{fsq}+R) + k \cdot \operatorname{ln2}_{lo}))) +* \end{align*} +* ``` +* +* Here, \\(\operatorname{ln2}\\) is split into two floating point numbers: +* +* ```tex +* \operatorname{ln2} = \operatorname{ln2}_{hi} + \operatorname{ln2}_{lo} +* ``` +* +* where \\(n \cdot \operatorname{ln2}_{hi}\\) is always exact for \\(|n| < 2000\\). +* +* @param f input value +* @return output value +* +* @example +* double out = stdlib_base_kernel_log1p( 1.0 ); +* // returns ~0.1931 +*/ +double stdlib_base_kernel_log1p( const double f ) { + double hfsq; + double t1; + double t2; + double R; + double s; + double z; + double w; + + s = f / ( 2.0 + f ); + z = s * s; + w = z * z; + t1 = w * polyval_p( w ); + t2 = z * polyval_q( w ); + R = t2 + t1; + hfsq = 0.5 * f * f; + return s * ( hfsq + R ); +} diff --git a/base/special/kernel-log1p/test/test.js b/base/special/kernel-log1p/test/test.js new file mode 100644 index 000000000..3d96c207d --- /dev/null +++ b/base/special/kernel-log1p/test/test.js @@ -0,0 +1,72 @@ +/** +* @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 isnan = require( './../../../../base/assert/is-nan' ); +var abs = require( './../../../../base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var kernelLog1p = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof kernelLog1p, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', function test( t ) { + var v = kernelLog1p( NaN ); + t.equal( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function correctly computes `log(1+f) - f` for `1+f` satisfying `sqrt( 2 ) / 2 < 1+f < sqrt( 2 )`', function test( t ) { + var expected; + var delta; + var tol; + var x; + var y; + var i; + var e; + + x = [ + 1.4142135623730951 + ]; + expected = [ + 0.46716000147788844 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = kernelLog1p( x[ i ] ); + e = expected[ i ]; + if ( y === e ) { + t.equal( y, e, 'x: '+x[ i ]+'. E: '+e ); + } else { + delta = abs( y - e ); + tol = 1.0 * EPS * abs( e ); + t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. y: '+y+'. E: '+e+'. tol: '+tol+'. Δ: '+delta+'.' ); + } + } + t.end(); +}); diff --git a/base/special/kernel-log1p/test/test.native.js b/base/special/kernel-log1p/test/test.native.js new file mode 100644 index 000000000..64c34c7d8 --- /dev/null +++ b/base/special/kernel-log1p/test/test.native.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'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var isnan = require( './../../../../base/assert/is-nan' ); +var abs = require( './../../../../base/special/abs' ); +var EPS = require( '@stdlib/constants/float64/eps' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var kernelLog1p = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( kernelLog1p instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof kernelLog1p, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) { + var v = kernelLog1p( NaN ); + t.equal( isnan( v ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function correctly computes `log(1+f) - f` for `1+f` satisfying `sqrt( 2 ) / 2 < 1+f < sqrt( 2 )`', opts, function test( t ) { + var expected; + var delta; + var tol; + var x; + var y; + var i; + var e; + + x = [ + 1.4142135623730951 + ]; + expected = [ + 0.46716000147788844 + ]; + + for ( i = 0; i < x.length; i++ ) { + y = kernelLog1p( x[ i ] ); + e = expected[ i ]; + if ( y === e ) { + t.equal( y, e, 'x: '+x[ i ]+'. E: '+e ); + } else { + delta = abs( y - e ); + tol = 1.0 * EPS * abs( e ); + t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. y: '+y+'. E: '+e+'. tol: '+tol+'. Δ: '+delta+'.' ); + } + } + t.end(); +});