# Inequalities, Logical expressions


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 of 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.

`Julia` provides numeric comparisons that allow this notation to be exactly mirrored:

```
x, y = 1, 2
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 use of `==` is necessary, as `=` is used for assignment.

The `!` operator takes a boolean value and negates it. It uses prefix notation:

```
!true
```

For  convenience, `a != b` can be used in place of `!(a == b)`.

## Algebra of inequalities

To illustrate, let's see that the algebra of expressions works as expected.

For example, if $a < b$ then for any $c$ it is also true that $a + c < b + c$.

We can't "prove" this through examples, but we can investigate it by
the choice of various values of $a$, $b$, and $c$. For example:

```
a,b,c = 1,2,3
a < b, a + c < b + c
```

Or in reverse:

```
a,b,c = 3,2,1
a < b, a + c < b + c
```

Trying other choices will show that the two answers are either both `false` or both `true`.

```
alert(""" Well, almost, when `Inf` or `NaN` are involved, this may not hold, for example `1 + Inf < 2 + Inf` is actually `false`. """)
```


So adding or subtracting any finite value from an inequality will preserve the inequality, just as it does for equations.

What about addition and multiplication?

Consider the case $a < b$ and $c > 0$. Then $ca < cb$. Here we investigate using 3 random values (which will be positive):

```
a,b,c = rand(3)   # 3 random numbers in (0,1)
a < b, c*a < c*b
```

Whenever these two commands are run, the two logical values should be identical, even though the specific values of `a`, `b`, and `c` will vary.


The restriction that $c > 0$ is needed. For example, if $c = -1$, then we have $a < b$ if and only if $-a > -b$. That is the inequality is "flipped."

```
a,b = rand(2)
a < b, -a > -b
```

Again, whenever this is run, the two logical values should be the same.
The values $a$ and $-a$ are the same distance from $0$, but on opposite sides. Hence if $0 < a < b$, then $b$ is farther from $0$ than $a$, so $-b$ will be farther from $0$ than $-a$, which in this case says $-b < -a$, as expected.

Finally, we have the case of division. The relation of $x$ and $1/x$
(for $x > 0$) is that the farther $x$ is from $0$, the closer $1/x$ is
to $0$. So large values of $x$ make small values of $1/x$. This leads
to this fact for $a,b > 0$: $a < b$ if and only if $1/a > 1/b$.

We can check with random values again:

```
a,b = rand(2)
a < b, 1/a > 1/b
```

In summary we verified 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`.

## Some examples

We now show some inequalities highlighted on this  [Wikipedia](http://en.wikipedia.org/wiki/Inequality_%28mathematics%29) page.

Numerically investigate the fact $\exp(x) \geq 1 + x$ by showing it is
true for three different values of $x$. We pick $x=-1$, $0$, and $1$:

```
x = -1; exp(x) >= 1 + x
x =  0; exp(x) >= 1 + x
x =  1; exp(x) >= 1 + x
```

Now, let's investigate that for any distinct real numbers, $a$ and $b$ that

$$~
\frac{e^b - e^a}{b - a} > e^{(a+b)/2}
~$$

For this, we use `rand(2)` to generate two random numbers in $(0,1)$:

```
a, b = rand(2)
(exp(b) - exp(a)) / (b-a) > exp((a+b)/2)
```

This should evaluate to `true` for any random choice of `a` and `b` returned by `rand(2)`.


Finally, let's investigate the fact that the harmonic mean, $2/(1/a +
1/b)$ is less than or equal to the geometric mean, $\sqrt{ab}$, which
is less than or equal to the quadratic mean, $\sqrt{a^2 +
b^2}/\sqrt{2}$, using two randomly chosen values:

```
a, b = rand(2)
h = 2 / (1/a + 1/b)
g = (a * b) ^ (1 / 2)
q = sqrt((a^2 + b^2) / 2)
h <= g, g <= q
```




## Chaining, combining expressions: absolute values


The absolute value notation can be defined through cases:

$$~
\lvert x\rvert = \begin{cases}
x & x \geq 0\\
-x & \text{otherwise}.
\end{cases}
~$$

The interpretation of $\lvert x\rvert$, as the distance on the number line of $x$
from $0$, means that many relationships are naturally expressed in
terms of absolute values. For example, a simple shift: $\lvert x -c\rvert$ is
related to the distance $x$ is from the number $c$. As common as they are, the concept can still be confusing when inequalities are involved.


For example, the expression $\lvert x - 5\rvert < 7$ has solutions which are all
values of $x$ within $7$ units of $5$. This would be the values $-2 <
x < 12$. If this isn't immediately intuited, then formally $\lvert x - 5 \rvert <
7$ is a compact representation of a chain of inequalities: $-7 < x - 5
< 7$. (Which is really two combined inequalities: $-7 < x-5$ *and* $x
- 5 < 7$.)  We can "add" 5 to each side to get $-2 < x < 12$, using the
fact that adding by a finite number does not change the inequality
sign.


Julia's precedence for logical
expressions, allows such statements to mirror the mathematical
notation:

```
x = 18
abs(x - 5) < 7
```

This is to be expected, but we could also have written:

```
-7 < x - 5 < 7
```

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.


The *boolean operators* `&` and `|` implement "and" and "or." Thus we
could write $-7 < x-5 < 7$ as

```
(-7 < x - 5) & (x - 5 < 7)
```

and could write $\lvert x-5\rvert > 7$ as

```
(x - 5 < -7) | (x - 5 > 7)
```

(The first expression is false for $x-18$ and the second expression true.)

```
alert("""
The
[short circuit operators](http://julia.readthedocs.org/en/latest/manual/control-flow/#man-short-circuit-evaluation)
are `&&` and `||`. For simple Boolean values, they perform a related
task, though have a more general usage.
""")
```

##### Example

One of [DeMorgan's Laws](http://en.wikipedia.org/wiki/De_Morgan%27s_laws) states that
"not (A and B)" is the same as "(not A) or (not B)". This is a kind of
distributive law for "not", but note how the "and" changes to
"or". We can verify this law systematically. For example, the
following shows it true for 1 of the 4 possible cases for the pair
`A`, `B` to take:

```
A,B = true, false  ## also true, true; false, true; and false, false
!(A & B) == !A | !B
```

## Precedence

The question of when parentheses are needed and when they are not is answered by the
[precedence](http://julia.readthedocs.org/en/latest/manual/mathematical-operations/#operator-precedence)  rules
implemented. Earlier, we wrote

```
(x - 5 < -7) | (x - 5 > 7)
```

To represent $\lvert x-5\rvert > 7$. Were the parentheses necessary? Let's just check. 

```
x - 5 < -7 | x - 5 > 7
```

So yes, they were. The precedence rules perform `|` before `<` or `>`, so without the extra pair of parentheses, we would have

```
(x - 5 < ( (-7 | x) - 5)) > 7
```

which is not what is desired at all. (The value of `-7 | x` is `-5` --
as `|` does something completely different when the two arguments are
not boolean.)

A thorough understanding of the precedence rules can help eliminate
unnecessary parentheses, but in most cases it is easier just to put
them in.

## Arithmetic with

For convenience, basic arithmetic can be performed with Boolean
values, `false` becomes $0$ and true $1$. For example, both these
expressions make sense:

```
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.


This can be cleverly exploited. For example, this expression returns `x` when it is positive and $0$ otherwise:

```
(x > 0) * x
```

There is a built in function, `max` that can be used for this: `max(0, x)`.

This expression returns `x` if it is between $-10$ and $10$ and otherwise $-10$ or $10$ depending on whether $x$ is negative or positive.

```
(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)`.



## Questions

###### Question

Is `e^pi` or `pi^e` greater?

```
choices = [
"`e^pi` is greater than `pi^e`",
"`e^pi` is equal to `pi^e`",
"`e^pi` is less than `pi^e`"
]
ans = 1	   
radioq(choices, ans)
```

###### Question

Is sin(1000) positive?

```
ans = (sin(1000) > 0)
yesnoq(ans)
```

###### Question

Suppose you know $0 < a < b$. What can you say about the relationship between $-1/a$ and $-1/b$?

```
choices = [
L"-1/a < -1/b",
L"-1/a > -1/b",
L"-1/a \geq -1/b"]
ans = 3
radioq(choices, ans)
```


###### Suppose you know $a < 0 < b$, is it true that $1/a > 1/b$?

```
choices = ["Yes, it is always true.",
           "It can sometimes be true, though not always.",
	   L"It is never true, as $1/a$ is negative and $1/b$ is positive"]
ans = 3
radioq(choices, ans)
```

###### Question

The `airy` [function](http://en.wikipedia.org/wiki/Airy_function) is a
special function named after a British Astronomer who realized the
function's value in his studies of the rainbow. It is known that it is
always positive for $x > 0$, though not so for negative values of
$x$. Which of these indicates the first negative value : `airy(-1)<0`,
`airy(-2)<0`, ... `airy(-5) < 0`?

```
choices = ["`airy($i) < 0`" for i in -1:-1:-5]
ans = 3
radioq(choices, ans, keep_order=true)
```

###### Question

By trying three different values of $x > 0$ which of these could possibly be always true:

```
choices = ["`x^x <= (1/e)^(1/e)`",
           "`x^x == (1/e)^(1/e)`",
           "`x^x >= (1/e)^(1/e)`"]
ans = 3
radioq(choices, ans)
```

###### Question

Student logic says $(x+y)^p = x^p + y^p$. Of course, this isn't
correct for all $p$ and $x$. By trying a few points, which is true
when $x,y > 0$ and $0 < p < 1$:

```
choices = ["`(x+y)^p < x^p + y^p`",
           "`(x+y)^p == x^p + y^p`",
           "`(x+y)^p > x^p + y^p`"]
ans = 1
radioq(choices, ans)
```


###### Question

According to Wikipedia, one of the following inequalities is always
true for $a, b > 0$ (as proved by I. Ilani in
JSTOR,AMM,Vol.97,No.1,1990). Which one?


```
choices = ["`a^a + b^b <= a^b + b^a`",
           "`a^a + b^b >= a^b + b^a`",
	   "`a^b + b^a <= 1`"]
ans = 2
radioq(choices, ans)
```


###### Question
Is $3$ in the set $\lvert x - 2\rvert < 1/2$?

```
val = abs(3-2) < 1/2
yesnoq(val)
```

###### Question

Which of the following is equivalent to $\lvert x - a\rvert > b$:

```
choices = [L"-b < x - a < b",
         L" -b < x-a \text{ and } x - a < b",
         L" x - a < -b \text{ or } x - a > b"]
ans = 3
radioq(choices, ans)
```


###### Question
If $\lvert x - \pi\rvert < 1/10$ is $\lvert \sin(x) - \sin(\pi)\rvert < 1/10$?

Guess an answer based on a few runs of

```
Verbatim("""
x = pi + 0.2 * rand()
abs(x - pi) < 1/10, abs(sin(x) - sin(pi)) < 1/10
""")
```

```
booleanq(true)
```

###### Question

Does `12` satisfy $\lvert x - 3\rvert + \lvert x-9\rvert > 12$?

```
x = 12
val = (abs(x -3) + abs(x-9) > 12)
yesnoq(val)
```

###### Question

Which of these will show DeMorgan's law holds when both values are `false`:

```
choices = ["`!(false & false) == !false & !false`",
           "`!(false & false) == false !& false`",
	   "`!(false & false) == !false | !false`"]
ans = 3
radioq(choices, ans)
```

###### Question

For floating point numbers there are two special values `Inf` and `NaN`. For which of these is the answer always `false`:

```
choices = ["`Inf < 3.0` and `3.0 <= Inf`",
           "`NaN < 3.0` and `3.0 <= NaN`"]
ans = 2
radioq(choices, ans)
```

###### Question

The IEEE 754 standard  is about floating point numbers, for which there are the special values `Inf`, `-Inf`, `NaN`, and (surprisingly `-0`. Here are 4 facts that seem reasonable:

- Positive zero is equal but not greater than negative zero.

- `Inf` is equal to itself and greater than everything else except `NaN`.

- `-Inf` is equal to itself and less then everything else except `NaN`.

- `NaN` is not equal to, not less than, and not greater than anything, including itself.

Do all four seem to be the case within `Julia`? Find your answer by trial and error.

```
yesnoq(true)
```

