소스 검색

Docs updates

Robert Bennett 11 달 전
부모
커밋
ead727bfca
3개의 변경된 파일71개의 추가작업 그리고 18개의 파일을 삭제
  1. 38 7
      book/src/list-functions-math.md
  2. 22 0
      book/src/list-functions-other.md
  3. 11 11
      numbat/modules/math/combinatorics.nbt

+ 38 - 7
book/src/list-functions-math.md

@@ -633,33 +633,64 @@ fn median<D: Dim>(xs: List<D>) -> D
 Defined in: `math::combinatorics`
 
 ### `factorial` (Factorial)
-The product of the integers 1 through n, also written n!.
+The product of the integers 1 through n. Numbat also supports calling this via the postfix operator `n!`.
 More information [here](https://en.wikipedia.org/wiki/Factorial).
 
 ```nbt
 fn factorial(n: Scalar) -> Scalar
 ```
 
+<details>
+<summary>Examples</summary>
+
+<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=factorial%284%29')""></button></div><code class="language-nbt hljs numbat">factorial(4)
+
+    = 24
+</code></pre>
+
+<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=4%21')""></button></div><code class="language-nbt hljs numbat">4!
+
+    = 24
+</code></pre>
+
+</details>
+
 ### `falling_factorial` (Falling factorial)
-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.
+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.
 More information [here](https://en.wikipedia.org/wiki/Falling_and_rising_factorials).
 
 ```nbt
 fn falling_factorial(n: Scalar, k: Scalar) -> Scalar
 ```
 
+<details>
+<summary>Examples</summary>
+
+<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=falling%5Ffactorial%284%2C%202%29')""></button></div><code class="language-nbt hljs numbat">falling_factorial(4, 2)
+
+    = 12
+</code></pre>
+
+</details>
+
 ### `binom` (Binomial coefficient)
-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.
+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.
 More information [here](https://en.wikipedia.org/wiki/Binomial_coefficient).
 
 ```nbt
 fn binom(n: Scalar, k: Scalar) -> Scalar
 ```
 
+<details>
+<summary>Examples</summary>
+
+<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=binom%285%2C%202%29')""></button></div><code class="language-nbt hljs numbat">binom(5, 2)
+
+    = 10
+</code></pre>
+
+</details>
+
 ## Random sampling, distributions
 
 Defined in: `core::random`, `math::distributions`

+ 22 - 0
book/src/list-functions-other.md

@@ -129,6 +129,28 @@ fn is_nonzero<D: Dim>(value: D) -> Bool
 
 </details>
 
+### `is_int`
+Returns true if the input is an integer number of its units.
+
+```nbt
+fn is_int<D: Dim>(value: D) -> Bool
+```
+
+<details>
+<summary>Examples</summary>
+
+<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=is%5Fint%2830%20seconds%29')""></button></div><code class="language-nbt hljs numbat">is_int(30 seconds)
+
+    = true    [Bool]
+</code></pre>
+
+<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code"  onclick=" window.open('https://numbat.dev/?q=is%5Fint%280%2E5%20minutes%29')""></button></div><code class="language-nbt hljs numbat">is_int(0.5 minutes)
+
+    = false    [Bool]
+</code></pre>
+
+</details>
+
 ## Quantities
 
 Defined in: `core::quantities`

+ 11 - 11
numbat/modules/math/combinatorics.nbt

@@ -3,32 +3,32 @@ use core::functions
 use math::transcendental
 
 @name("Factorial")
-@description("The product of the integers 1 through n, also written n!")
+@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.")
+@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 || fract(k) != 0 then
+	if k < 0 || !is_int(k) then
 		error("in falling_factorial(n, k), k must be a nonnegative integer")
-	else if k == 0 then
+	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.")
+@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 fract(k) != 0 then
+	if !is_int(k) then
 		error("in binom(n, k), k must be an integer")
-	else if k < 0 || (k > n && fract(n) == 0) then
+	else if k < 0 || (k > n && is_int(n)) then
 		0
 	else
 		falling_factorial(n, k) / k!