-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #645 from rben01/math-functions
Math functions
- Loading branch information
Showing
11 changed files
with
268 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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,35 @@ | ||
use core::error | ||
use core::functions | ||
use core::numbers | ||
use math::transcendental | ||
|
||
@name("Factorial") | ||
@description("The product of the integers 1 through n. Numbat also supports calling this via the postfix operator `n!`.") | ||
@url("https://en.wikipedia.org/wiki/Factorial") | ||
@example("factorial(4)") | ||
@example("4!") | ||
fn factorial(n: Scalar) -> Scalar = n! | ||
|
||
@name("Falling factorial") | ||
@description("Equal to $n⋅(n-1)⋅…⋅(n-k+2)⋅(n-k+1)$ (k terms total). If n is an integer, this is the number of k-element permutations from a set of size n. k must always be an integer.") | ||
@url("https://en.wikipedia.org/wiki/Falling_and_rising_factorials") | ||
@example("falling_factorial(4, 2)") | ||
fn falling_factorial(n: Scalar, k: Scalar) -> Scalar = | ||
if k < 0 || !is_integer(k) then | ||
error("in falling_factorial(n, k), k must be a nonnegative integer") | ||
else if is_zero(k) then | ||
1 | ||
else | ||
n * falling_factorial(n-1, k-1) | ||
|
||
@name("Binomial coefficient") | ||
@description("Equal to falling_factorial(n, k)/k!, this is the coefficient of $x^k$ in the series expansion of $(1+x)^n$ (see “binomial series”). If n is an integer, then this this is the number of k-element subsets of a set of size n, often read \"n choose k\". k must always be an integer.") | ||
@url("https://en.wikipedia.org/wiki/Binomial_coefficient") | ||
@example("binom(5, 2)") | ||
fn binom(n: Scalar, k: Scalar) -> Scalar = | ||
if !is_integer(k) then | ||
error("in binom(n, k), k must be an integer") | ||
else if k < 0 || (k > n && is_integer(n)) then | ||
0 | ||
else | ||
falling_factorial(n, k) / k! |
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
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
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