|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# Binomial Coefficient |
| 22 | + |
| 23 | +> Compute the [binomial coefficient][binomial-coefficient] as a single-precision floating-point number. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +The [binomial coefficient][binomial-coefficient] of two nonnegative integers `n` and `k` is defined as |
| 28 | + |
| 29 | +<!-- <equation class="equation" label="eq:binomial_coefficient" align="center" raw="\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n" alt="Factorial formula for the Binomial coefficient."> --> |
| 30 | + |
| 31 | +```math |
| 32 | +\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n |
| 33 | +``` |
| 34 | + |
| 35 | +<!-- </equation> --> |
| 36 | + |
| 37 | +The [binomial coefficient][binomial-coefficient] can be generalized to negative integers `n` as follows: |
| 38 | + |
| 39 | +<!-- <equation class="equation" label="eq:binomial_coefficient_negative_integers" align="center" raw="\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right)" alt="Generalization of the binomial coefficient to negative n."> --> |
| 40 | + |
| 41 | +```math |
| 42 | +\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right) |
| 43 | +``` |
| 44 | + |
| 45 | +<!-- </equation> --> |
| 46 | + |
| 47 | +</section> |
| 48 | + |
| 49 | +<!-- /.intro --> |
| 50 | + |
| 51 | +<section class="usage"> |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +```javascript |
| 56 | +var binomcoeff = require( '@stdlib/math/base/special/binomcoeff' ); |
| 57 | +``` |
| 58 | + |
| 59 | +#### binomcoeff( n, k ) |
| 60 | + |
| 61 | +Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k` as a single-precision floating-point number. |
| 62 | + |
| 63 | +```javascript |
| 64 | +var v = binomcoeff( 8, 2 ); |
| 65 | +// returns 28 |
| 66 | + |
| 67 | +v = binomcoeff( 0, 0 ); |
| 68 | +// returns 1 |
| 69 | + |
| 70 | +v = binomcoeff( -4, 2 ); |
| 71 | +// returns 10 |
| 72 | + |
| 73 | +v = binomcoeff( 5, 3 ); |
| 74 | +// returns 10 |
| 75 | + |
| 76 | +v = binomcoeff( NaN, 3 ); |
| 77 | +// returns NaN |
| 78 | + |
| 79 | +v = binomcoeff( 5, NaN ); |
| 80 | +// returns NaN |
| 81 | + |
| 82 | +v = binomcoeff( NaN, NaN ); |
| 83 | +// returns NaN |
| 84 | +``` |
| 85 | + |
| 86 | +For negative `k`, the function returns `0`. |
| 87 | + |
| 88 | +```javascript |
| 89 | +var v = binomcoeff( 2, -1 ); |
| 90 | +// returns 0 |
| 91 | + |
| 92 | +v = binomcoeff( -3, -1 ); |
| 93 | +// returns 0 |
| 94 | +``` |
| 95 | + |
| 96 | +The function returns `NaN` for non-integer `n` or `k`. |
| 97 | + |
| 98 | +```javascript |
| 99 | +var v = binomcoeff( 2, 1.5 ); |
| 100 | +// returns NaN |
| 101 | + |
| 102 | +v = binomcoeff( 5.5, 2 ); |
| 103 | +// returns NaN |
| 104 | +``` |
| 105 | + |
| 106 | +</section> |
| 107 | + |
| 108 | +<!-- /.usage --> |
| 109 | + |
| 110 | +<section class="examples"> |
| 111 | + |
| 112 | +## Examples |
| 113 | + |
| 114 | +<!-- eslint no-undef: "error" --> |
| 115 | + |
| 116 | +```javascript |
| 117 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 118 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 119 | +var binomcoeff = require( '@stdlib/math/base/special/binomcoeff' ); |
| 120 | + |
| 121 | +var opts = { |
| 122 | + 'dtype': 'int32' |
| 123 | +}; |
| 124 | +var n = discreteUniform( 100, -10, 30, opts ); |
| 125 | +var k = discreteUniform( 100, 0, 20, opts ); |
| 126 | + |
| 127 | +logEachMap( 'binomcoeff(%d,%d) = %0.4f', n, k, binomcoeff ); |
| 128 | +``` |
| 129 | + |
| 130 | +</section> |
| 131 | + |
| 132 | +<!-- /.examples --> |
| 133 | + |
| 134 | +<!-- C interface documentation. --> |
| 135 | + |
| 136 | +* * * |
| 137 | + |
| 138 | +<section class="c"> |
| 139 | + |
| 140 | +## C APIs |
| 141 | + |
| 142 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 143 | + |
| 144 | +<section class="intro"> |
| 145 | + |
| 146 | +</section> |
| 147 | + |
| 148 | +<!-- /.intro --> |
| 149 | + |
| 150 | +<!-- C usage documentation. --> |
| 151 | + |
| 152 | +<section class="usage"> |
| 153 | + |
| 154 | +### Usage |
| 155 | + |
| 156 | +```c |
| 157 | +#include "stdlib/math/base/special/binomcoeff.h" |
| 158 | +``` |
| 159 | + |
| 160 | +#### stdlib_base_binomcoeff( n, k ) |
| 161 | + |
| 162 | +Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k` as a single-precision floating-point number. |
| 163 | + |
| 164 | +```c |
| 165 | +float v = stdlib_base_binomcoeff( 8, 2 ); |
| 166 | +// returns 28.0f |
| 167 | +``` |
| 168 | + |
| 169 | +The function accepts the following arguments: |
| 170 | + |
| 171 | +- **n**: `[in] int32_t` input value. |
| 172 | +- **k**: `[in] int32_t` input value. |
| 173 | + |
| 174 | +```c |
| 175 | +float stdlib_base_binomcoeff( const int32_t n, const int32_t k ); |
| 176 | +``` |
| 177 | +
|
| 178 | +</section> |
| 179 | +
|
| 180 | +<!-- /.usage --> |
| 181 | +
|
| 182 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 183 | +
|
| 184 | +<section class="notes"> |
| 185 | +
|
| 186 | +</section> |
| 187 | +
|
| 188 | +<!-- /.notes --> |
| 189 | +
|
| 190 | +<!-- C API usage examples. --> |
| 191 | +
|
| 192 | +<section class="examples"> |
| 193 | +
|
| 194 | +### Examples |
| 195 | +
|
| 196 | +```c |
| 197 | +#include "stdlib/math/base/special/binomcoeff.h" |
| 198 | +#include <stdio.h> |
| 199 | +#include <stdint.h> |
| 200 | +
|
| 201 | +int main( void ) { |
| 202 | + const int32_t a[] = { 24, 32, 48, 116, 33 }; |
| 203 | + const int32_t b[] = { 12, 6, 15, 52, 22 }; |
| 204 | +
|
| 205 | + float out; |
| 206 | + int i; |
| 207 | + for ( i = 0; i < 5; i++ ) { |
| 208 | + out = stdlib_base_binomcoeff( a[ i ], b[ i ] ); |
| 209 | + printf( "binomcoeff(%d, %d) = %f\n", a[ i ], b[ i ], out ); |
| 210 | + } |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +</section> |
| 215 | + |
| 216 | +<!-- /.examples --> |
| 217 | + |
| 218 | +</section> |
| 219 | + |
| 220 | +<!-- /.c --> |
| 221 | + |
| 222 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 223 | + |
| 224 | +<section class="related"> |
| 225 | + |
| 226 | +</section> |
| 227 | + |
| 228 | +<!-- /.related --> |
| 229 | + |
| 230 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 231 | + |
| 232 | +<section class="links"> |
| 233 | + |
| 234 | +[binomial-coefficient]: https://en.wikipedia.org/wiki/Binomial_coefficient |
| 235 | + |
| 236 | +</section> |
| 237 | + |
| 238 | +<!-- /.links --> |
0 commit comments