Skip to content

Commit

Permalink
feat: add math/base/special/factorial2
Browse files Browse the repository at this point in the history
Closes: #44
PR-URL: 	#1112
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
rgizz and kgryte authored Nov 17, 2023
1 parent ae9cdb7 commit c0dab03
Show file tree
Hide file tree
Showing 14 changed files with 969 additions and 0 deletions.
112 changes: 112 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/factorial2/README.md
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 -->
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();
});
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
Loading

1 comment on commit c0dab03

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Coverage Report

Package Statements Branches Functions Lines
math/base/special/factorial2 $\color{green}140/140$
$\color{green}+100.00\%$
$\color{green}17/17$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}140/140$
$\color{green}+100.00\%$

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

Please sign in to comment.