-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
math/base/special/factorial2
Closes: #44 PR-URL: #1112 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
- Loading branch information
Showing
14 changed files
with
969 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
lib/node_modules/@stdlib/math/base/special/factorial2/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<!-- | ||
@license Apache-2.0 | ||
Copyright (c) 2023 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. | ||
--> | ||
|
||
# factorial2 | ||
|
||
> [Double factorial][double-factorial] function. | ||
<section class="intro"> | ||
|
||
The [double factorial][double-factorial] of a number `n`, denoted `n!!`, is defined as the product of all the positive integers up to `n` that have the same parity (odd or even) as `n`. | ||
|
||
Thus, for example, `5!!` is `5 * 3 * 1 = 15` and `8!!` is `8 * 6 * 4 * 2 = 384`. | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var factorial2 = require( '@stdlib/math/base/special/factorial2' ); | ||
``` | ||
|
||
#### factorial2( n ) | ||
|
||
Evaluates the [double factorial][double-factorial] of `n`. | ||
|
||
```javascript | ||
var v = factorial2( 2 ); | ||
// returns 2 | ||
|
||
v = factorial2( 3 ); | ||
// returns 3 | ||
|
||
v = factorial2( 0 ); | ||
// returns 1 | ||
|
||
v = factorial2( 4 ); | ||
// returns 8 | ||
|
||
v = factorial2( 5 ); | ||
// returns 15 | ||
|
||
v = factorial2( NaN ); | ||
// returns NaN | ||
|
||
v = factorial2( 301 ); | ||
// returns Infinity | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var oneTo = require( '@stdlib/array/base/one-to' ); | ||
var factorial2 = require( '@stdlib/math/base/special/factorial2' ); | ||
|
||
var values = oneTo( 300 ); | ||
|
||
var i; | ||
for ( i = 0; i < values.length; i++ ) { | ||
console.log( 'f(%d): %d', values[ i ], factorial2( values[ i ] ) ); | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[double-factorial]: https://en.wikipedia.org/wiki/Double_factorial | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
48 changes: 48 additions & 0 deletions
48
lib/node_modules/@stdlib/math/base/special/factorial2/benchmark/benchmark.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2023 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var pkg = require( './../package.json' ).name; | ||
var factorial2 = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var y; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
y = factorial2( i%301 ); | ||
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(); | ||
}); |
110 changes: 110 additions & 0 deletions
110
lib/node_modules/@stdlib/math/base/special/factorial2/benchmark/cpp/boost/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#/ | ||
# @license Apache-2.0 | ||
# | ||
# Copyright (c) 2023 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 := @ | ||
endif | ||
|
||
# Specify the path to Boost: | ||
BOOST ?= | ||
|
||
# Determine the OS: | ||
# | ||
# [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 | ||
endif | ||
endif | ||
endif | ||
|
||
# Define the program used for compiling C++ source files: | ||
ifdef CXX_COMPILER | ||
CXX := $(CXX_COMPILER) | ||
else | ||
CXX := g++ | ||
endif | ||
|
||
# Define the command-line options when compiling C++ files: | ||
CXXFLAGS ?= \ | ||
-std=c++11 \ | ||
-O3 \ | ||
-Wall \ | ||
-pedantic | ||
|
||
# Determine whether to generate [position independent code][1]: | ||
# | ||
# [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 C++ targets: | ||
cxx_targets := benchmark.out | ||
|
||
|
||
# TARGETS # | ||
|
||
# Default target. | ||
# | ||
# This target is the default target. | ||
|
||
all: $(cxx_targets) | ||
|
||
.PHONY: all | ||
|
||
|
||
# Compile C++ source. | ||
# | ||
# This target compiles C++ source files. | ||
|
||
$(cxx_targets): %.out: %.cpp $(BOOST) | ||
$(QUIET) $(CXX) $(CXXFLAGS) $(fPIC) -I $(BOOST) -o $@ $< -lm | ||
|
||
|
||
# Run a benchmark. | ||
# | ||
# This target runs a benchmark. | ||
|
||
run: $(cxx_targets) | ||
$(QUIET) ./$< | ||
|
||
.PHONY: run | ||
|
||
|
||
# Perform clean-up. | ||
# | ||
# This target removes generated files. | ||
|
||
clean: | ||
$(QUIET) -rm -f *.o *.out | ||
|
||
.PHONY: clean |
Oops, something went wrong.
c0dab03
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report
The above coverage report was generated for the changes in this push.