Browse Source

Minor reorganization

David Peter 1 year ago
parent
commit
b6a362b8b0

+ 9 - 2
book/build.py

@@ -82,18 +82,25 @@ list_of_functions(
     "Mathematical functions",
     [
         "core::functions",
-        "core::quantities",
         "math::functions",
         "math::trigonometry_extra",
         "core::random",
         "math::statistics",
+        "extra::quadratic_equation",
+        "numerics::diff",
+        "numerics::solve",
     ],
 )
 
 list_of_functions(
     "other",
     "Other",
-    ["core::error", "physics::temperature_conversion", "chemistry::elements"],
+    [
+        "core::quantities",
+        "core::error",
+        "physics::temperature_conversion",
+        "chemistry::elements",
+    ],
 )
 
 subprocess.run(["mdbook", "build"], text=True)

+ 20 - 20
book/src/list-functions-lists.md

@@ -3,7 +3,7 @@
 # Lists
 
 ### `len`
-Get the length of a list
+Get the length of a list.
 
 ```nbt
 fn len<A>(xs: List<A>) -> Scalar
@@ -27,7 +27,7 @@ fn tail<A>(xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `cons`
-Prepend an element to a list
+Prepend an element to a list.
 
 ```nbt
 fn cons<A>(x: A, xs: List<A>) -> List<A>
@@ -35,7 +35,7 @@ fn cons<A>(x: A, xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `is_empty`
-Check if a list is empty
+Check if a list is empty.
 
 ```nbt
 fn is_empty<A>(xs: List<A>) -> Bool
@@ -43,7 +43,7 @@ fn is_empty<A>(xs: List<A>) -> Bool
 (defined in *core::lists*)
 
 ### `concat`
-Concatenate two lists
+Concatenate two lists.
 
 ```nbt
 fn concat<A>(xs1: List<A>, xs2: List<A>) -> List<A>
@@ -51,7 +51,7 @@ fn concat<A>(xs1: List<A>, xs2: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `take`
-Get the first `n` elements of a list
+Get the first `n` elements of a list.
 
 ```nbt
 fn take<A>(n: Scalar, xs: List<A>) -> List<A>
@@ -59,7 +59,7 @@ fn take<A>(n: Scalar, xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `drop`
-Get everything but the first `n` elements of a list
+Get everything but the first `n` elements of a list.
 
 ```nbt
 fn drop<A>(n: Scalar, xs: List<A>) -> List<A>
@@ -67,7 +67,7 @@ fn drop<A>(n: Scalar, xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `element_at`
-Get the element at index `i` in a list
+Get the element at index `i` in a list.
 
 ```nbt
 fn element_at<A>(i: Scalar, xs: List<A>) -> A
@@ -75,7 +75,7 @@ fn element_at<A>(i: Scalar, xs: List<A>) -> A
 (defined in *core::lists*)
 
 ### `range`
-Generate a range of integer numbers from `start` to `end` (inclusive)
+Generate a range of integer numbers from `start` to `end` (inclusive).
 
 ```nbt
 fn range(start: Scalar, end: Scalar) -> List<Scalar>
@@ -83,7 +83,7 @@ fn range(start: Scalar, end: Scalar) -> List<Scalar>
 (defined in *core::lists*)
 
 ### `cons_end`
-Append an element to the end of a list
+Append an element to the end of a list.
 
 ```nbt
 fn cons_end<A>(xs: List<A>, x: A) -> List<A>
@@ -91,7 +91,7 @@ fn cons_end<A>(xs: List<A>, x: A) -> List<A>
 (defined in *core::lists*)
 
 ### `reverse`
-Reverse the order of a list
+Reverse the order of a list.
 
 ```nbt
 fn reverse<A>(xs: List<A>) -> List<A>
@@ -99,7 +99,7 @@ fn reverse<A>(xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `map`
-Generate a new list by applying a function to each element of the input list
+Generate a new list by applying a function to each element of the input list.
 
 ```nbt
 fn map<A, B>(f: Fn[(A) -> B], xs: List<A>) -> List<B>
@@ -107,7 +107,7 @@ fn map<A, B>(f: Fn[(A) -> B], xs: List<A>) -> List<B>
 (defined in *core::lists*)
 
 ### `filter`
-Filter a list by a predicate
+Filter a list by a predicate.
 
 ```nbt
 fn filter<A>(p: Fn[(A) -> Bool], xs: List<A>) -> List<A>
@@ -115,7 +115,7 @@ fn filter<A>(p: Fn[(A) -> Bool], xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `foldl`
-Fold a function over a list
+Fold a function over a list.
 
 ```nbt
 fn foldl<A, B>(f: Fn[(A, B) -> A], acc: A, xs: List<B>) -> A
@@ -123,7 +123,7 @@ fn foldl<A, B>(f: Fn[(A, B) -> A], acc: A, xs: List<B>) -> A
 (defined in *core::lists*)
 
 ### `sort_by_key`
-Sort a list of elements, using the given key function that maps the element to a quantity
+Sort a list of elements, using the given key function that maps the element to a quantity.
 
 ```nbt
 fn sort_by_key<A, D: Dim>(key: Fn[(A) -> D], xs: List<A>) -> List<A>
@@ -131,7 +131,7 @@ fn sort_by_key<A, D: Dim>(key: Fn[(A) -> D], xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `sort`
-Sort a list of quantities
+Sort a list of quantities.
 
 ```nbt
 fn sort<D: Dim>(xs: List<D>) -> List<D>
@@ -139,7 +139,7 @@ fn sort<D: Dim>(xs: List<D>) -> List<D>
 (defined in *core::lists*)
 
 ### `intersperse`
-Add an element between each pair of elements in a list
+Add an element between each pair of elements in a list.
 
 ```nbt
 fn intersperse<A>(sep: A, xs: List<A>) -> List<A>
@@ -147,7 +147,7 @@ fn intersperse<A>(sep: A, xs: List<A>) -> List<A>
 (defined in *core::lists*)
 
 ### `sum`
-Sum all elements of a list
+Sum all elements of a list.
 
 ```nbt
 fn sum<D: Dim>(xs: List<D>) -> D
@@ -155,7 +155,7 @@ fn sum<D: Dim>(xs: List<D>) -> D
 (defined in *core::lists*)
 
 ### `linspace`
-Generate a list of `n_steps` evenly spaced numbers from `start` to `end` (inclusive)
+Generate a list of `n_steps` evenly spaced numbers from `start` to `end` (inclusive).
 
 ```nbt
 fn linspace<D: Dim>(start: D, end: D, n_steps: Scalar) -> List<D>
@@ -163,7 +163,7 @@ fn linspace<D: Dim>(start: D, end: D, n_steps: Scalar) -> List<D>
 (defined in *core::lists*)
 
 ### `join`
-Convert a list of strings into a single string by concatenating them with a separator
+Convert a list of strings into a single string by concatenating them with a separator.
 
 ```nbt
 fn join(xs: List<String>, sep: String) -> String
@@ -171,7 +171,7 @@ fn join(xs: List<String>, sep: String) -> String
 (defined in *core::lists*)
 
 ### `split`
-Split a string into a list of strings using a separator
+Split a string into a list of strings using a separator.
 
 ```nbt
 fn split(input: String, separator: String) -> List<String>

+ 34 - 21
book/src/list-functions-math.md

@@ -50,33 +50,19 @@ fn mod<T: Dim>(a: T, b: T) -> T
 ```
 (defined in *core::functions*)
 
-### `unit_of`
-
-```nbt
-fn unit_of<T: Dim>(x: T) -> T
-```
-(defined in *core::quantities*)
-
-### `value_of`
-
-```nbt
-fn value_of<T: Dim>(x: T) -> Scalar
-```
-(defined in *core::quantities*)
-
 ### `is_nan`
 
 ```nbt
 fn is_nan<T: Dim>(n: T) -> Bool
 ```
-(defined in *core::quantities*)
+(defined in *math::functions*)
 
 ### `is_infinite`
 
 ```nbt
 fn is_infinite<T: Dim>(n: T) -> Bool
 ```
-(defined in *core::quantities*)
+(defined in *math::functions*)
 
 ### `sqrt` (Square root)
 More information [here](https://en.wikipedia.org/wiki/Square_root).
@@ -246,7 +232,7 @@ fn gamma(x: Scalar) -> Scalar
 (defined in *math::functions*)
 
 ### `maximum` (Maxmimum)
-Get the largest element of a list
+Get the largest element of a list.
 
 ```nbt
 fn maximum<D: Dim>(xs: List<D>) -> D
@@ -254,7 +240,7 @@ fn maximum<D: Dim>(xs: List<D>) -> D
 (defined in *math::functions*)
 
 ### `minimum` (Minimum)
-Get the smallest element of a list
+Get the smallest element of a list.
 
 ```nbt
 fn minimum<D: Dim>(xs: List<D>) -> D
@@ -270,7 +256,7 @@ fn mean<D: Dim>(xs: List<D>) -> D
 (defined in *math::functions*)
 
 ### `variance` (Variance)
-Calculate the population variance of a list of quantities
+Calculate the population variance of a list of quantities.
 More information [here](https://en.wikipedia.org/wiki/Variance).
 
 ```nbt
@@ -279,7 +265,7 @@ fn variance<D: Dim>(xs: List<D>) -> D^2
 (defined in *math::functions*)
 
 ### `stdev` (Standard deviation)
-Calculate the population standard deviation of a list of quantities
+Calculate the population standard deviation of a list of quantities.
 More information [here](https://en.wikipedia.org/wiki/Standard_deviation).
 
 ```nbt
@@ -288,7 +274,7 @@ fn stdev<D: Dim>(xs: List<D>) -> D
 (defined in *math::functions*)
 
 ### `median` (Median)
-Calculate the median of a list of quantities
+Calculate the median of a list of quantities.
 More information [here](https://en.wikipedia.org/wiki/Median).
 
 ```nbt
@@ -549,3 +535,30 @@ fn rand_pareto<T: Dim>(α: Scalar, min: T) -> T
 ```
 (defined in *math::statistics*)
 
+### `diff` (Numerical differentiation)
+Compute the numerical derivative of a function at a point using the central difference method.
+More information [here](https://en.wikipedia.org/wiki/Numerical_differentiation).
+
+```nbt
+fn diff<X: Dim, Y: Dim>(f: Fn[(X) -> Y], x: X) -> Y / X
+```
+(defined in *numerics::diff*)
+
+### `root_bisect` (Bisection method)
+Find the root of the function f in the interval [x1, x2] using the bisection method. The function f must be continuous and f(x1) × f(x2) < 0.
+More information [here](https://en.wikipedia.org/wiki/Bisection_method).
+
+```nbt
+fn root_bisect<A: Dim, B: Dim>(f: Fn[(A) -> B], x1: A, x2: A, x_tolerance: A, y_tolerance: B) -> A
+```
+(defined in *numerics::solve*)
+
+### `root_newton` (Newton's method)
+Find the root of the function f(x) and its derivative f'(x) using Newton's method.
+More information [here](https://en.wikipedia.org/wiki/Newton%27s_method).
+
+```nbt
+fn root_newton<A: Dim, B: Dim>(f: Fn[(A) -> B], f_prime: Fn[(A) -> B / A], x0: A, y_tolerance: B) -> A
+```
+(defined in *numerics::solve*)
+

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

@@ -2,6 +2,20 @@
 
 # Other
 
+### `unit_of`
+
+```nbt
+fn unit_of<T: Dim>(x: T) -> T
+```
+(defined in *core::quantities*)
+
+### `value_of`
+
+```nbt
+fn value_of<T: Dim>(x: T) -> Scalar
+```
+(defined in *core::quantities*)
+
 ### `error`
 Throw a user-defined error.
 

+ 6 - 1
numbat/examples/inspect.rs

@@ -67,7 +67,12 @@ fn inspect_functions_in_module(ctx: &Context, module: String) {
         }
 
         if let Some(ref description) = description {
-            println!("{}", description.trim());
+            let description = description.trim();
+            if description.ends_with('.') {
+                println!("{description}");
+            } else {
+                println!("{description}.");
+            }
         }
         if let Some(url) = url {
             println!("More information [here]({url}).");

+ 0 - 3
numbat/modules/core/quantities.nbt

@@ -2,6 +2,3 @@ use core::scalar
 
 fn unit_of<T: Dim>(x: T) -> T
 fn value_of<T: Dim>(x: T) -> Scalar = x / unit_of(x)
-
-fn is_nan<T: Dim>(n: T) -> Bool
-fn is_infinite<T: Dim>(n: T) -> Bool

+ 6 - 0
numbat/modules/math/functions.nbt

@@ -2,6 +2,12 @@ use core::scalar
 use core::lists
 use math::constants
 
+## Numerical properties
+
+fn is_nan<T: Dim>(n: T) -> Bool
+
+fn is_infinite<T: Dim>(n: T) -> Bool
+
 ## Basics
 
 @name("Square root")