lots of cleanup

This commit is contained in:
jverzani
2026-08-11 17:17:08 -04:00
parent ae461659e0
commit 253295ff6e
91 changed files with 18284 additions and 7872 deletions

View File

@@ -18,7 +18,7 @@ plotly()
nothing
```
## Boolean values
## Boolean values, comparison operators
In mathematics it is common to test if an expression is true or false. For example, is the point $(1,2)$ inside the disc $x^2 + y^2 \leq 1$? We would check this by substituting $1$ for $x$ and $2$ for $y$, evaluating both sides of the inequality and then assessing if the relationship is true or false. In this case, we end up with a comparison of $5 \leq 1$, which we of course know is false.
@@ -36,12 +36,12 @@ x^2 + y^2 <= 1
The response is `false`, as expected. `Julia` provides [Boolean](http://en.wikipedia.org/wiki/Boolean_data_type) values `true` and `false` for such questions. The same process is followed as was described mathematically.
The set of numeric comparisons is nearly the same as the mathematical counterparts: `<`, `<=`, `==`, `>=`, `>`. The syntax for less than or equal can also be represented with the Unicode `≤` (generated by `\le[tab]`). Similarly, for greater than or equal, there is `\ge[tab]`.
The set of comparison operators is nearly the same as the mathematical counterparts: `<`, `<=`, `==`, `>=`, `>`. The syntax for less than or equal can also be represented with the Unicode `≤` (generated by `\le[tab]`). Similarly, for greater than or equal, there is `\ge[tab]`.
:::{.callout-warning}
## Warning
The use of `==` is necessary, as `=` is used for assignment and mutation.
The use of `==` is necessary, as `=` is used for assignment and mutation and `===` is used for *identicalness* from a hardware standpoint.
:::
@@ -54,6 +54,13 @@ The `!` operator takes a boolean value and negates it. It uses prefix notation:
For convenience, `a != b` can be used in place of `!(a == b)`.
----
The function `isapprox` has a unicode counterpart ` ≈` (typed through `\approx[tab]`) that allows numeric comparisons up to a tolerance. This is needed when comparing floating point values. For example, due to the inability to exactly represent the numbers in floating point the first of these comparisons is false:
```{julia}
1/10 + 2/10 == 3/10, 1/10 + 2/10 ≈ 3/10
```
## Algebra of inequalities
@@ -136,10 +143,13 @@ a < b, 1/a > 1/b
In summary we investigated numerically that the following hold:
* `a < b` if and only if `a + c < b + c` for all finite `a`, `b`, and `c`.
* `a < b` if and only if `c*a < c*b` for all finite `a` and `b`, and finite, positive `c`.
* `a < b` if and only if `-a > -b` for all finite `a` and `b`.
* `a < b` if and only if `1/a > 1/b` for all finite, positive `a` and `b`.
* `a < b` if and only if `a + c < b + c` for all finite `a`, `b`, and `c` in $[0,1]$.
* `a < b` if and only if `c*a < c*b` for all finite `a` and `b`, and finite, positive `c`, all in $[0,1]$.
* `a < b` if and only if `-a > -b` for all finite `a` and `b` in $[0,1]$.
* `a < b` if and only if `1/a > 1/b` for all finite, positive `a` and `b` in $[0,1]$.
### Examples
@@ -228,28 +238,42 @@ This is to be expected, but we could also have written:
Read aloud this would be "minus $7$ is less than $x$ minus $5$ **and** $x$ minus $5$ is less than $7$".
The "and" equations can be combined as above with a natural notation. However, an equation like $\lvert x - 5\rvert > 7$ would emphasize an **or** and be "$x$ minus $5$ less than minus $7$ **or** $x$ minus $5$ greater than $7$". Expressing this requires some new notation.
::: {.callout-note}
## Chaining expressions
The above (`-7 < x - 5 < 7`)---where two logical comparisons are used, is referred to as chaining. It is parsed differently than might be expected and is not read in as `(-7 < x - 5) < 7` which would compare a boolean to `7`. Chaining is neither left- or right-associative.
:::
The *boolean shortcut operators* `&&` and `||` implement "and" and "or". (There are also *bitwise* boolean operators `&` and `|`, but we only describe the former.)
## Short-circuit operators
The "and" equations can be combined as above with a natural notation through chaining. However, an equation like $\lvert x - 5\rvert > 7$ would emphasize an **or** and be "$x$ minus $5$ less than minus $7$ **or** $x$ minus $5$ greater than $7$". Expressing this requires some new notation.
The *boolean short-circuiting operators* `&&` and `||` implement "and" and "or".
Thus we could write $-7 < x-5 < 7$ as
```{julia}
(-7 < x - 5) && (x - 5 < 7)
(-7 < x - 5) && (x - 5 < 7) # and
```
and could write $\lvert x-5\rvert > 7$ as
```{julia}
(x - 5 < -7) || (x - 5 > 7)
(x - 5 < -7) || (x - 5 > 7) # or
```
(The first expression is false for $x=18$ and the second expression true, so the "or"ed result is `true` and the "and" result is `false`.)
The first expression is false for $x=18$ and the second expression true, so the "and" result is `false` and the "or"ed result is `true`.
These operators are called *short-circuiting* operators.
The operation `x && y` evaluates `x` and if true returns the evaluated value of `y`. If `x` is false, `y` is not evaluated (it is short-circuited).
The operation `x || y` evaluates `x` and if true return `true` and if false the value of `y` is evaluated and returned.
Both are idiomatically used for control flow (if `x` then `y` or if *not* `x` then `y` statements).
##### Example
@@ -262,6 +286,13 @@ A,B = true, false ## also true, true; false, true; and false, false
!(A && B) == !A || !B
```
::: {.callout-note}
## Bitwise `&` and `|`
Both the short-circuiting operators and the bitwise operators can evaluate truth tables---where Boolean values are compared---but the short-circuiting values *don't* need the second argument to evaluate to a Boolean. The bitwise operators also have a different precedence; the same as `*` and `/` and not lower precedence than the arithmetic operations.
:::
## Precedence
@@ -281,7 +312,6 @@ x - 5 < -7 || x - 5 > 7
So no, they were not in this case.
An operator (such as `<`, `>`, `||` above) has an associated associativity and precedence. The associativity is whether an expression like `a - b - c` is `(a-b) - c` or `a - (b-c)`. The former being left associative, the latter right. Of issue here is *precedence*, as in with two or more different operations, which happens first, second, $\dots$.
@@ -295,7 +325,7 @@ The table in the manual on [operator precedence and associativity](https://docs.
(This is different than the precedence of the bitwise boolean operators, which have `&` with "Multiplication" and `|` with "Addition", so `x-5 < 7 | x - 5 > 7` would need parentheses.)
A thorough understanding of the precedence rules can help eliminate unnecessary parentheses, but in most cases it is easier just to put them in.
A thorough understanding of the precedence rules can help eliminate unnecessary parentheses, but if in doubt, just put them in.
## Arithmetic with
@@ -308,7 +338,7 @@ For convenience, basic arithmetic can be performed with Boolean values, `false`
true + true + false, false * 1000
```
The first example shows a common means used to count the number of `true` values in a collection of Boolean values - just add them.
The first example shows a common means used to count the number of `true` values in a collection of Boolean values---just add them.
This can be cleverly exploited. For example, the following expression returns `x` when it is positive and $0$ otherwise:
@@ -328,7 +358,15 @@ This expression returns `x` if it is between $-10$ and $10$ and otherwise $-10$
(x < -10)*(-10) + (x >= -10)*(x < 10) * x + (x>=10)*10
```
The `clamp(x, a, b)` performs this task more generally, and is used as in `clamp(x, -10, 10)`.
The `clamp(x, a, b)` performs this task more generally.
### Example
The value of `im`, representing $i$ the imaginary number with $i^2 = -1$ is internally modeled using a Boolean `false` for the real part of a complex number and a value `true` for the imaginary part.
```{julia}
dump(im)
```
## Questions
@@ -398,6 +436,27 @@ answ = 3
radioq(choices, answ)
```
###### Question
Compute the following:
```{julia}
#| eval: false
u = typemax(1)
u < u + 1
```
Is the output as expected?
```{julia}
#| echo: false
explanation = """
No, for integer numbers in math, it is always the case that `u < u + 1`, but for this particular `u` there is *no larger* integer, so `u+1` "wraps around" to give a negative number which on comparison is less than `u`
"""
buttonq(["Yes", "No"], 2; explanation)
```
###### Question