# Functions


{{< include ../_common_code.qmd >}}

This section will use the following add-on packages:


```{julia}
using CalculusWithJulia
using Plots
plotly()
```


---


A mathematical [function](http://en.wikipedia.org/wiki/Function_(mathematics)) is defined abstractly by:


> **Function:** A function is a *relation* which assigns to each element in the domain a *single* element in the range. A **relation** is a set of ordered pairs, $(x,y)$. The set of first coordinates is the domain, the set of second coordinates the range of the relation.



That is, a function gives a correspondence between  values in its domain with  values in its range.


This definition is abstract, as functions can be very general. With single-variable calculus, we generally specialize to real-valued functions of a single variable (*univariate, scalar functions*). These typically have the correspondence given by a rule, such as $f(x) = x^2$ or $f(x) = \sqrt{x}$. The function's domain may be implicit (as in all $x$ for which the rule is defined) or may be explicitly given as part of the rule. The function's range is then the image of its domain, or the set of all $f(x)$ for each $x$ in the domain ($\{f(x): x \in \text{ domain}\}$).


Some examples of mathematical functions are:


$$
f(x) = \cos(x), \quad g(x) = x^2 - x, \quad h(x) = \sqrt{x}, \quad
s(x) = \begin{cases} -1 & x < 0\\1&x>0\end{cases}.
$$

For these examples, the domain of both $f(x)$ and $g(x)$ is all real values of $x$, where as for $h(x)$ it is implicitly just the set of non-negative numbers, $[0, \infty)$. Finally, for $s(x)$, we can see that the domain is defined for every $x$ but $0$.


In general the range is harder to identify than the domain, and this is the case for these functions too. For $f(x)$ we may know the $\cos$ function is trapped in $[-1,1]$ and it is intuitively clear than all values in that set are possible. The function $h(x)$ would have range $[0,\infty)$.  The $s(x)$ function is either $-1$ or $1$, so only has two possible values in its range.  What about $g(x)$? It is a parabola that opens upward, so any $y$ values below the $y$ value of its vertex will not appear in the range. In this case, the symmetry indicates that the vertex will be at $(1/2, -1/4)$, so the range is $[-1/4, \infty)$.

::: {#fig-domain-range-1}
```{julia}
#| echo: false
plt = let
    gr()
	# domain/range shade
	λ = 1.2
	a, b = .1, 3
	f(x) = (x-1/2) + sin((x-1)^2)

	empty_style = (xaxis=([], false),
                    yaxis=([], false),
                    framestyle=:origin,
                    legend=false)
	axis_style = (arrow=true, side=:head, line=(:gray, 1))
	text_style = (10,)
	fn_style =  (;line=(:black, 3))
	fn2_style =  (;line=(:red, 4))
	mark_style = (;line=(:gray, 1, :dot))

	domain_style = (;fill=(:orange, 0.35))
	range_style = (; fill=(:blue, 0.35))

	xs = range(a,b, 1000)
	y0,y1 = extrema(f.(xs))
	Δy = (y1-y0)/60
	Δx = (b - a)/75

	plot(;  empty_style...) # aspect_ratio=:equal,
	plot!([-.25,3.25],[0,0]; axis_style...)
	plot!([0,0],  [min(-2Δy, y0 - Δy), y1 + 4Δy]; axis_style... )

	plot!(f, a, b; fn_style...)
	plot!(Shape([a,b,b,a], Δy * [-1,-1,1,1] ); domain_style...)
	plot!(Shape(Δx*[-1,1,1,-1], [y0,y0,y1,y1]); range_style...)

	plot!([a,a], [0, f(a)]; mark_style...)
	plot!([b,b], [0, f(b)]; mark_style...)
	plot!([a, b], [f(a), f(a)]; mark_style...)
	plot!([a, b], [f(b), f(b)]; mark_style...)
end
plt
```

Figure of the plot of a function over an interval $[a,b]$ highlighting the domain and the range.
:::

::: {#fig-domain-range-2}
```{julia}
#| echo: false
plt = let
	a, b = 0, 2pi
	λ = 1.1
	Δx, Δy = .033, .1
	δx = 0.05
	f(x) = sec(x)
	g(x) = abs(f(x)) < 5 ? f(x) : NaN

	empty_style = (xaxis=([], false),
                    yaxis=([], false),
                    framestyle=:origin,
                    legend=false)
	axis_style = (arrow=true, side=:head, line=(:gray, 1))
	text_style = (10,)
	fn_style =  (;line=(:black, 3))
	fn2_style =  (;line=(:red, 4))
	mark_style = (;line=(:gray, 1, :dot))

	domain_style = (;fill=(:orange, 0.35))
	range_style = (; fill=(:blue, 0.35))


	plot(; empty_style...)
	plot!(g, a+0.1, b; fn_style...)
	plot!(λ*[a,b],[0,0]; axis_style...)
	plot!([0,0], λ*[-5,5+1/2]; axis_style...)
	vline!([pi/2, 3pi/2]; line=(:gray, :dash))

	plot!(Shape([a,a+pi/2-δx,a+pi/2-δx,a], Δy*[-1,-1,1,1]); domain_style...)
	plot!(Shape([a+pi/2+δx, a+pi/2+pi-δx,a+pi/2+pi-δx,a+pi/2+δx],
			  Δy*[-1,-1,1,1]); domain_style...)
	plot!(Shape([3pi/2 + δx, 2pi, 2pi, 3pi/2+δx],
			  Δy*[-1,-1,1,1]); domain_style...)
	plot!(Shape(Δx*[-1,1,1,-1], [-5, -5,-1,-1]); range_style...)
	plot!(Shape(Δx*[-1,1,1,-1], [ 5,  5,1,1]);  range_style...)
end
plt
```


```{julia}
#| echo: false
plotly()
nothing
```

This figure shows that the domain of a function may be a collection of intervals. (In this case the $\sec$ function is not defined at $\pi/2 + k \pi$ for integer $k$) and the range may be a collection of intervals. (In this case, the $\sec$ function never has a value in $(-1,1)$.
:::


:::{.callout-note}
## Note
**Thanks to Euler (1707-1783):** The formal idea of a function is a relatively modern concept in mathematics.  According to [Dunham](http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/dunham1.pdf),

:::

Euler defined a function as an "analytic expression composed in any way   whatsoever of the variable quantity and numbers or constant   quantities." He goes on to indicate that as Euler matured, so did   his notion of function, ending up closer to the modern idea of a   correspondence not necessarily tied to a particular formula or   “analytic expression.” He finishes by saying: "It is fair to say   that we now study functions in analysis because of him."


We will see that defining functions within `Julia` can be as simple a concept as Euler started with, but that the more abstract concept has a great advantage that is exploited in the design of the language.


## Defining simple mathematical functions


The notation `Julia` uses to define simple mathematical functions could not be more closely related to how they are written mathematically. For example, the functions $f(x)$, $g(x)$, and $h(x)$ above may be defined by:


```{julia}
f(x) = cos(x)
g(x) = x^2 - x
h(x) = sqrt(x)
```

The left-hand side of the equals sign is an assignment. In this use, a function with a given signature is defined and attached to a method table for the given function name. The right-hand side is simply `Julia` code to compute the *rule* corresponding to the function.


Calling the function also follows standard math notation:


```{julia}
f(pi), g(2), h(4)
```

For typical cases like the three above, there isn't really much new to learn.


:::{.callout-note}
## The equals sign is used differently between math and Julia
The equals sign in `Julia`  always indicates either an assignment or a mutation of the object on the left side. The definition of a function above is an *assignment*, in that a function is added (or modified) in a table holding the methods associated with the function's name.

The equals sign restricts the expressions available on the *left*-hand side to a) a variable name, for assignment; b) mutating an object at an index, as in `xs[1]`; c) mutating a property of a struct; or d) a function assignment following this form `function_name(args...)`.

Whereas function definitions and usage in `Julia` mirrors standard math notation; equations in math are not so mirrored in `Julia`. In mathematical equations, the left-hand of an equation is typically a complicated algebraic expression. Not so with `Julia`, where the left hand side of the equals sign is prescribed and quite limited.

:::

### The domain of a function in Julia


Functions in `Julia` have an implicit domain, just as they do mathematically. In the case of $f(x)$ and $g(x)$, the right-hand side is defined for all real values of $x$, so the domain is all $x$. For $h(x)$ this isn't the case, of course. Trying to call $h(x)$ when $x < 0$ will give an error:


```{julia}
#| error: true
h(-1)
```

The `DomainError` is one of many different error types `Julia` has, in this case it is quite apt: the value $-1$ is not in the domain of the function.


### Equations, functions, calling a function


Mathematically we tend to blur the distinction between the equation


$$
y = 5/9 \cdot (x - 32)
$$

and the function


$$
f(x) = 5/9 \cdot (x - 32)
$$

In fact, the graph of a function $f(x)$ is simply defined as the graph of the equation $y=f(x)$.


There **is** a distinction in `Julia`. The last command here


```{julia}
x = -40
y = 5/9 * (x - 32)
```

will evaluate the expression on the right-hand side with the value of `x` bound at the time of assignment to `y`, whereas assignment to a function


```{julia}
#| hold: true
f(x) = 5/9 * (x - 32)
f(72)				## room temperature
```

will create a function object with a value of `x` determined at a later time---the time the function is called. So the value of `x` defined when the function is created is not important here (as the value of `x` used by `f` is passed in as an argument).


Within `Julia`, we make note of the distinction between a function object versus a function call. In the definition `f(x)=cos(x)`, the variable `f` refers to a function object, whereas the expression `f(pi)` is a function call, resulting in a value. This mirrors the math notation where an $f$ is used when properties of a function are being emphasized (such as $f \circ g$ for composition) and $f(x)$ is used when the values related to the function are being emphasized (such as saying "the plot of the equation $y=f(x)$).


Distinguishing these  related but different concepts---expressions, equations, values from function calls, and function objects---is important when modeling mathematics on the computer.

::: {#fig-kidney}

```{julia}
#| echo: false
plt = let
    gr()
	# two kidneys and an arrow
	ts = range(0, 2pi, 200)
	a = 1
	x(t) = 6a*cos(t)  + sin(t) - 4a*cos(t)^5
	y(t) = 4a*sin(t)^3
	S = Shape(x.(ts), y.(ts))

	y1(t) = 6a*cos(t)  -3sin(t) - 4a*cos(t)^5
	x1(t) = 4a*sin(t)^3

	T = Shape(x1.(ts), y1.(ts))
	T = Plots.translate(T, 10, 0)

	plot(; empty_style...)
	plot!(S, fill=(:gray, 0.2), line=(:black, 1))
	plot!(T, fill=(:gray, 0.2), line=(:black, 1))

	P = (0,0)
	Q = (10, 0)
	scatter!([P,Q], marker=(:circle,))

	ts = reverse(range(pi/4+.1, 3pi/4-.1, 100))
	plot!(5 .+ 5*sqrt(2)*cos.(ts), -5 .+ 5*sqrt(2)*sin.(ts);
		  line=(:black, 1), arrow=true, side=:head)


	annotate!([
	    (P..., text(L"x", :top)),
		(Q..., text(L"f(x)", :top)),
	    (5, 3, text(L"f", :top)),
        (0, -6, text("Domain")),
		(10, -6, text("Range"))
	])

end
plt
```

```{julia}
#| echo: false
plotly()
nothing
```

Common illustration of an abstract function mapping a value $x$ in the domain to a value $y=f(x)$ in the range. In `Julia`, the values are named, as with `x`, or computed, as with `f(x)`. The function *object* `f` is like the named arrow, which is the name assigned to a particular mapping.
:::

### Cases, the ternary operator


The definition of $s(x)$ above has two cases:


$$
s(x) =
\begin{cases}
-1 & x < 0 \\
 1 & x > 0.
\end{cases}
$$

We learn to read this as: when $x$ is less than $0$, then the answer is $-1$. If $x$ is greater than $0$ the answer is $1.$ Often---but not in this example---there is an "otherwise" case to catch those values of $x$ that are not explicitly mentioned. As there is no such "otherwise" case here, we can see that this function has no definition when $x=0$. This function is often called the "sign" function and is also defined by $\lvert x\rvert/x$. (`Julia`'s `sign` function defines `sign(0)` to be `0`.)


How do we create conditional statements in `Julia`? Programming languages generally have "if-then-else" constructs to handle conditional evaluation. In `Julia`, the following code will handle the above condition:


```{julia}
#| eval: false
if x < 0
  -1
elseif x > 0
   1
end
```

The "otherwise" case would be caught with an `else` addition. So, for example, this would implement `Julia`'s definition of `sign` (which also assigns $0$ to $0$):


```{julia}
#| eval: false
if x < 0
  -1
elseif x > 0
   1
else
   0
end
```

The conditions for the `if` statements are expressions that evaluate to either `true` or `false`, such as generated by the Boolean operators `<`, `<=`, `==`, `!=`, `>=`, and `>`.


If familiar with `if` conditions, they are natural to use. However, for simpler cases of "if-else" `Julia` provides the more convenient *ternary* operator: `cond ? if_true : if_false`. (The name comes from the fact that there are three arguments specified.) The ternary operator checks the condition and if true returns the first expression, whereas if the condition is false the second condition is returned. (Another useful control flow construct is [short-circuit](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation) evaluation.)


For example, here is one way to define an absolute value function:


```{julia}
abs_val(x) = x >= 0 ? x : -x
```

The condition is `x >= 0`---or is `x` non-negative? If so, the value `x` is used, otherwise `-x` is used.


Here is a means to implement a function which takes the larger of `x` or `10`:


```{julia}
bigger_10(x) = x > 10 ? x : 10.0
```

(This could also utilize the `max` function: `f(x) = max(x, 10.0)`.)


Or similarly, a function to represent a cell phone plan where the first $5$ Gb of data are $11$ dollars and every additional GB is $3.50$:


```{julia}
cellplan(x) = x <= 5 ? 11.0 : 11.0 + 3.50 * (x-5)
```

:::{.callout-warning}
## Warning
Type stability. These last two definitions used `10.0` and `11.0` instead of the integers `10` and `11` for the answer. Why the extra typing? When `Julia` can predict the type of the output from the type of inputs, it can be more efficient. So when possible, we help out and ensure the output is always the same type.

:::

##### Example


The `ternary` operator can be used to define an explicit domain. For example, a falling body might have height given by $h(t) = 10 - 16t^2$. This model only applies for non-negative $t$ and non-negative $h$ values. So, in particular $0 \leq t \leq \sqrt{10/16}$. To implement this function we might have:


```{julia}
h(t) = 0 <= t <= sqrt(10/16) ? 10.0 - 16t^2 : error("t is not in the domain")
```

#### Nesting ternary operators


The function `s(x)` isn't quite so easy to implement, as there isn't an "otherwise" case. We could use an `if` statement, but instead illustrate using a second, nested ternary operator:


```{julia}
s(x) = x < 0 ? -1 :
    x > 0 ? 1 : error("0 is not in the domain")
```

With nested ternary operators, the advantage over the `if` condition is not always compelling, but for simple cases the ternary operator is quite useful.


## Functions defined with the "function" keyword


For more complicated functions, say one with a few steps to compute, an alternate form for defining a function can be used:


```{verbatim}
function function_name(function_arguments)
  ...function_body...
end
```

The last value computed is returned unless the `function_body` contains an explicit `return` statement.


For example, the following is a more verbose way to define $sq(x) = x^2$:


```{julia}
function sq(x)
  return x^2
end
```

The line `return x^2`, could have just been `x^2` as it is the last (and) only line evaluated.


:::{.callout-note}
## Note
The `return` keyword is not a function, so is not called with parentheses. An empty `return` statement will return a value of `nothing`.

:::

##### Example


Imagine we have the following complicated function related to the trajectory of a [projectile](http://www.researchgate.net/publication/230963032_On_the_trajectories_of_projectiles_depicted_in_early_ballistic_woodcuts) with wind resistance:


$$
	f(x) = \left(\frac{g}{k v_0\cos(\theta)} + \tan(\theta) \right) x + \frac{g}{k^2}\ln\left(1 - \frac{k}{v_0\cos(\theta)} x \right)
$$

Here $g$ is the gravitational constant $9.8$ and $v_0$, $\theta$, and $k$ parameters, which we take to be $200$, $45$ degrees and $1/2$ respectively. With these values, the above function can be computed when $x=100$ with:


```{julia}
function trajectory(x)
  g, v0, theta, k = 9.8, 200, 45*pi/180, 1/2
  a = v0 * cos(theta)

  (g/(k*a) + tan(theta))* x + (g/k^2) * log(1 - k/a*x)
end
```

```{julia}
trajectory(100)
```

By using a multi-line function our work is much easier to look over for errors.


##### Example: the secant method for finding a solution to $f(x) = 0$.


This next example, shows how using functions to collect a set of computations for simpler reuse can be very helpful.


An old method for finding a zero of an equation is the [secant method](https://en.wikipedia.org/wiki/Secant_method). We illustrate the method with the function $f(x) = x^2 - 2$. In an upcoming example we see how to create a function to evaluate the secant line between $(a,f(a))$ and $(b, f(b))$ at any point. In this example, we define a function to compute the $x$ coordinate of where the secant line crosses the $x$ axis. This can be defined as follows:


```{julia}
function secant_intersection(f, a, b)
   # solve 0 = f(b) + m * (x-b) where m is the slope of the secant line
   # x = b - f(b) / m
   m = (f(b) - f(a)) / (b - a)
   b - f(b) / m
end
```

We utilize this as follows. Suppose we wish to solve $f(x) = 0$ and we have two "rough" guesses for the answer. In our example, we wish to solve $q(x) = x^2 - 2$ and our "rough" guesses are $1$ and $2$. Call these values $a$ and $b$. We *improve* our rough guesses by finding a value $c$ which is the intersection point of the secant line.


```{julia}
q(x) = x^2 - 2
a, b = 1, 2
c = secant_intersection(q, a, b)
```

In our example, we see that in trying to find an answer to $f(x) = 0$ ( $\sqrt{2}\approx 1.414\dots$) our value found from the intersection point is a better guess than either $a=1$ or $b=2$:


```{julia}
#| echo: false
plt = let
gr()
plt = plot(q, a, b, linewidth=5, legend=false)
plot!(plt, zero, a, b)
plot!(plt, [a, b], q.([a, b]))
scatter!(plt, [c], [q(c)]; marker=(:circle,))
scatter!(plt, [a,b], [q(a), q(b)]; marker=(:square,))

annotate!(plt, [
(a, 0, text(L"a", 10,:top)),
(b, 0, text(L"b", 10, :top)),
(c, 0, text(L"c", 10, :bottom))
])
end
plt
```

```{julia}
#| echo: false
plotly()
nothing
```

Still,  `q(c)` is not really close to $0$:


```{julia}
q(c)
```

*But* it is much closer than either $q(a)$ or $q(b)$, so it is an improvement. This suggests renaming $a$ and $b$ with the old $b$ and $c$ values and trying again we might do better still:


```{julia}
#| hold: true
a, b = b, c
c = secant_intersection(q, a, b)
q(c)
```

Yes, now the function value at this new $c$ is even closer to $0$. Trying a few more times we see we just get closer and closer. Here we start again to see the progress


```{julia}
#| hold: true
a,b = 1, 2
for step in 1:6
    a, b = b, secant_intersection(q, a, b)
    current = (c=b, qc=q(b))
    @show current
end
```

Now our guess $c$ is basically the same as `sqrt(2)`. Repeating the above leads to only a slight improvement in the guess, as we are about as close as floating point values will allow.


Here we see a visualization with all these points. As can be seen, it quickly converges at the scale of the visualization, as we can't see much closer than `1e-2`.


```{julia}
#| hold: true
#| echo: false
f(x) = x^2 - 2
a, b = 1, 2
c = secant_intersection(f, a, b)

p = plot(f, a, b, linewidth=5, legend=false)
plot!(p, zero, a, b)
scatter!([a,b], [f(a), f(b)]; marker=(:square,))

plot!(p, [a,b], f.([a,b]));
scatter!(p, [c], [f(c)])

a, b = b, c
c = secant_intersection(f, a, b)
plot!(p, [a,b], f.([a,b]));
scatter!(p, [c], [f(c)])


a, b = b, c
c = secant_intersection(f, a, b)
plot!(p, [a,b], f.([a,b]));
scatter!(p, [c], [f(c)])
p
```

In most cases, this method can fairly quickly find a zero provided two good starting points are used.


## Parameters, function context (scope), keyword arguments


Consider two functions implementing the slope-intercept form and point-slope form of a line:


$$
f(x) = m \cdot x + b, \quad g(x) = y_0 + m \cdot (x - x_0).
$$

Both functions use the variable $x$, but there is no confusion, as we
learn that this is just a dummy variable to be substituted for and so
could have any name. Both also share a variable $m$ for a slope.
Where does the value for  $m$ come from?

In practice, there is a context that gives an answer. Despite the same name, there is no expectation that the slope will be the same for each function if the context is different. So when parameters are involved, a function involves a rule and a context to give specific values to the parameters. Euler had said initially that functions  composed of "the variable quantity and numbers or constant quantities." The term "variable," we still use, but instead of "constant quantities," we use the name "parameters." In computer language, instead of context, we use the word *scope*.


Something similar is also true with `Julia`.  Consider the example of writing a function to model a linear equation with slope $m=2$ and $y$-intercept $3$. A typical means to do this would be to define constants, and then use the familiar formula:


```{julia}
m, b = 2, 3
mxb(x) = m*x + b
```

This will work as expected. For example, $f(0)$ will be $b$  and $f(2)$ will be $7$:


```{julia}
mxb(0), mxb(2)
```

All fine, but what if somewhere later the values for $m$ and $b$ were *redefined*, say with $m,b = 3,2$?


Now what happens with $f(0)$? When $f$ was defined `b` was $3$, but now if we were to call `f`, `b` is $2$. Which value will we get? More generally, when `f` is being evaluated in what context does `Julia` look up the bindings for the variables it encounters? It could be that the values are assigned when the function is defined, or it could be that the values for the parameters are resolved when the function is called. If the latter, what context will be used?


Before discussing this, let's just see in this case:


```{julia}
#| hold: true
m, b = 3, 2
mxb(0)
```

So the `b` is found from the currently stored value. This fact can be exploited. we can write template-like functions, such as `f(x)=m*x+b` and reuse them just by updating the parameters separately.


How `Julia` resolves what a variable refers to is described in detail in the manual page [Scope of Variables](https://docs.julialang.org/en/v1/manual/variables-and-scoping/). In this case, the function definition finds variables in the context of where the function was defined, the main workspace, and not where it is called. As seen, this context can be modified after the function definition and prior to the function call. It is only when `b` is needed, that the context is consulted, so the most recent binding is retrieved.  Contexts allow the user to repurpose variable names without there being name collision. For example, we typically use `x` as a function argument, and different contexts allow this `x` to refer to different values.


Mostly this works as expected, but at times it can be complicated to reason about. In our example, definitions of the parameters can be forgotten, or the same variable name may have been used for some other purpose. The potential issue is with the parameters, the value for `x` is straightforward, as it is passed into the function.

However, we can also pass the parameters, such as $m$ and $b$, as arguments.  There are different styles employed.

For parameters, one suggestion is to use [keyword](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments) arguments. These allow the specification of parameters, but also give a default value. This can make usage explicit, yet still convenient. For example, here is an alternate way of defining a line with parameters `m` and `b`:


```{julia}
mxplusb(x; m=1, b=0) = m*x + b
```

The right-hand side is identical to before, but the left hand side is different. Arguments defined *after* a semicolon are keyword arguments. They are specified as `var=value` (or `var::Type=value` to restrict the type) where the value is used as the default, should a value not be specified when the function is called.


Calling a function with keyword arguments can be identical to before:


```{julia}
mxplusb(0)
```

During this call, values for `m` and `b` are found from how the function is called, not the main workspace. In this case, nothing is specified so the defaults of $m=1$ and $b=0$ are used. Whereas, this call will use the user-specified values for `m` and `b`:


```{julia}
mxplusb(0; m=3, b=2)
```

Keywords are used to mark the parameters whose values are to be changed from the default. Though one can use *positional arguments* for parameters---and there are good reasons to do so---using keyword arguments is a good practice if performance isn't paramount, as their usage is more explicit yet the defaults mean that a minimum amount of typing needs to be done.


Keyword arguments are widely used with plotting commands, as there are numerous options to adjust, but typically only a handful adjusted per call. The `Plots` package whose commands we illustrate throughout these notes starting with the next section has this in its docs: `Plots.jl` follows two simple rules with data and attributes:

* Positional arguments correspond to input data
* Keyword arguments correspond to attributes



##### Example


In the example for multi-line functions we hard coded many variables inside the body of the function. In practice it can be better to pass these in as parameters along the lines of:


```{julia}
#| hold: true
function trajectory(x; g = 9.8, v0 = 200, theta = 45*pi/180, k = 1/2)
  a = v0 * cos(theta)
  (g/(k*a) + tan(theta))* x + (g/k^2) * log(1 - k/a*x)
end
trajectory(100)
```

### The `f(x,p)` style for parameterization


An alternative to keyword arguments is to bundle the parameters into a container and pass them as a single argument to the function. The idiom in `Julia` is to use the *second* argument for parameters, or `f(x, p)` for the function argument specifications. This style is used in the very popular `SciML` suite of packages.


For example, here we use a *named tuple* to pass parameters to `f`:


```{julia}
#| hold: true
function trajectory(x, p)
    g, v0, theta, k = p.g, p.v0, p.theta, p.k # unpack parameters

    a = v0 * cos(theta)
    (g/(k*a) + tan(theta))* x + (g/k^2) * log(1 - k/a*x)
end

p = (g=9.8, v0=200, theta=45*pi/180, k=1/2)
trajectory(100, p)
```

The style isn't so different from using keyword arguments, save the extra step of unpacking the parameters. Unpacking as above has shortcut syntax to extract selected fields by name:

```{julia}
(; v0, theta) = p
v0, theta
```

The *big* advantage of bundling parameters into a container is consistency-–-the function is always called in an identical manner regardless of the number of parameters (or variables).


::: {.callout-note}
## Avoid global variables

Referring to a global parameter is common in math, but has a significant performance impact in `Julia`. Save for the simplest usage, it is much better to pass parameters to the function through one of several means that too rely on the value of the global state.

:::

## Multiple dispatch


The concept of a function is of much more general use than its restriction to mathematical functions of single real variable. A natural application comes from describing basic properties of geometric objects. The following function definitions likely will cause no great concern when skimmed over:


```{julia}
#| hold: true
Area(w, h) = w * h		                           # of a rectangle
Volume(r, h) = pi * r^2 * h	                       # of a cylinder
SurfaceArea(r, h) = pi * r * (r + sqrt(h^2 + r^2)) # of a right circular cone, including the base
```

The right-hand sides may or may not be familiar, but it should be reasonable to believe that if push came to shove, the formulas could be looked up. However, the left-hand sides are subtly different---they have two arguments, not one. In `Julia` it is trivial to define functions with multiple arguments---we just did.


Earlier we saw the `log` function can use a second argument to express the base. This function is basically defined by `log(b,x)=log(x)/log(b)`. The `log(x)` value is the natural log, and this definition just uses the change-of-base formula for logarithms.


But not so fast, on the left side is a function with two arguments and on the right side the functions have one argument---yet they share the same name. How does `Julia` know which to use? `Julia` uses the number, order, and *type* of the positional arguments passed to a function to determine which function definition to use. This is technically known as [multiple dispatch](http://en.wikipedia.org/wiki/Multiple_dispatch) or **polymorphism**. As a feature of the language, it can be used to greatly simplify the number of functions the user must learn. The basic idea is that many functions are "generic" in that they have methods which will work differently in different scenarios.


:::{.callout-warning}
## Warning
Multiple dispatch is very common in mathematics. For example, we learn different ways to add: integers (fingers, carrying), real numbers (align the decimal points), rational numbers (common denominators), complex numbers (add components), vectors (add components), polynomials (combine like monomials), ... yet we just use the same `+` notation for each operation. The concepts are related, the details different.

:::

`Julia` is similarly structured.  `Julia` terminology would be to call the operation "`+`" a *generic function* and the different implementations *methods* of "`+`". This allows the user to just need to know a smaller collection of generic concepts yet still have the power of detail-specific implementations.  To see how many different methods are defined in the base `Julia` language for the `+` operator, we can use the command `methods(+)`. As there are so many (well over $100$ when `Julia` is started), we illustrate how many different logarithm methods are implemented for "numbers:"


```{julia}
methods(log, (Number,))
```

(The arguments have *type annotations* such as `x::Float64` or `x::BigFloat`. `Julia` uses these to help resolve which method should be called for a given set of arguments. This allows for different operations depending on the variable type. For example, in this case, the `log` function for `Float64` values uses a fast algorithm, whereas for `BigFloat` values an algorithm that can handle multiple precision is used.)

##### Example

A common usage of multiple dispatch is, as is done with `log` above, to restrict the type of an argument and define a method for just this type. Types in `Julia` can be abstract or concrete. This distinction is important when constructing *composite types* (which we are not doing here), but otherwise not so important. In the following example, we use the abstract types `Integer`, `Real`, and `Complex` to define methods for a function we call `twotox`:

```{julia}
function twotox(x::Integer)
    (2//1)^x
end
twotox(x::Real) = (2.0)^x
twotox(x::Complex) = (2.0 + 0.0im)^x
```

This is for illustration purposes---the latter two are actually already done through `Julia`'s *promotion* mechanism---but we see that `twotox` will return a rational number when `x` is an integer unlike `Julia` which, when `x` is non-negative will return an integer and will otherwise will error or return a float (when `x` is a numeric literal, like `2^(-3)`).

The key to reading the above is the type annotation acts like a gatekeeper allowing in only variables of that type or a subtype of that type.

For example, the number `2` is parsed as a 64-bit integer (typically) and has concrete type `Int64` which is a subtype of `Integer`. So `twotox(2)` will use the first definition, and return a rational number. Whereas, the number `2.0` is parsed as a floating point number with concrete type `Float64` which is a subtype of `Real`, not `Integer`, so `twotox(2.0)` will use the second method defined above.

##### Example: An application of composition and multiple dispatch


As mentioned `Julia`'s multiple dispatch allows multiple functions with the same name. The function that gets selected depends not just on the type of the arguments, but also on the number of arguments given to the function. We can exploit this to simplify our tasks. For example, consider this optimization problem:


> For all rectangles of perimeter $20$, what is the one with largest area?



The start of this problem is to represent the area in terms of one variable. We see next that composition can simplify this task, which when done by hand requires a certain amount of algebra.


Representing the area of a rectangle in terms of two variables is easy, as the familiar formula of width times height applies:


```{julia}
Area(w, h) = w * h
```

But the other fact about this problem---that the perimeter is $20$---means that height depends on width. For this question, we can see that $P=2w + 2h$ so that---as a function---`height` depends on `w` as follows:


```{julia}
height(w) = (20  - 2*w)/2
```

By hand we would substitute this last expression into that for the area and simplify (to get $A=w\cdot (20-2 \cdot w)/2 = -w^2 + 10w$). However, within `Julia` we can let *composition* do the substitution and leave the algebraic simplification for `Julia` to do:


```{julia}
Area(w) = Area(w, height(w))
```

This might seem odd, just like with `log`, we now  have two *different* but related functions named `Area`. Julia will decide which to use based on the number of arguments when the function is called. This setup allows both to be used on the same line, as above. This usage style is not so common with many computer languages, but is a feature of `Julia` which is built around the concept of *generic* functions with multiple dispatch rules to decide which rule to call.


For example, jumping ahead a bit, the `plot` function of `Plots`  expects functions of a single numeric variable. Behind the scenes, then the function `A(w)` will be used in this graph:


```{julia}
plot(Area, 0, 10)
```

From the graph, we can see that that width for maximum area is $w=5$ and so $h=5$ as well.



## Other types of functions


`Julia` has both *generic* functions *and* *anonymous* functions. Generic functions participate in *multiple dispatch*, a central feature of `Julia`. Anonymous functions are very useful with higher-order programming (passing functions as arguments). These notes occasionally take advantage of anonymous functions for convenience.


### Anonymous functions


Simple mathematical functions have a domain and range which are a subset of the real numbers, and generally have a concrete mathematical rule. However, the definition of a function is much more abstract. We've seen that functions for computer languages can be more complicated too, with, for example, the possibility of multiple input values. Things can get more abstract still.


Take for example, the idea of the shift of a function. The following mathematical definition of a new function $g$ related to a function $f$:


$$
g(x) = f(x-c)
$$

has an interpretation---the graph of $g$ will be the same as the graph of $f$ shifted to the right by $c$ units. That is $g$ is a transformation of $f$. From one perspective, the act of replacing $x$ with $x-c$ transforms a function into a new function. Mathematically, when we focus on transforming functions, the word [operator](http://en.wikipedia.org/wiki/Operator_%28mathematics%29) is sometimes used. This concept of transforming a function can be viewed as a certain type of function, in an abstract enough way. The relation would be to just pair off the functions $(f,g)$ where $g(x) = f(x-c)$.


With `Julia` we can represent such operations. The simplest thing would be to do something like:


```{julia}
#| hold: true
f(x) = x^2 - 2x
g(x) = f(x - 3)
```

Then $g$ has the graph of $f$ shifted by 3 units to the right. Now `f` above refers to something in the main workspace, in this example a specific function. Better would be to allow `f` to be an argument of a function, like this:


```{julia}
function shift_right(f; c=0)
  function(x)
    f(x - c)
  end
end
```

That takes some parsing. In the body of the `shift_right` is the definition of a function. But this function has no name–-it is *anonymous*. But what it does should be clear---it subtracts $c$ from $x$ and evaluates $f$ at this new value. Since the last expression creates a function, this function is returned by `shift_right`.


So we could have done something more complicated like:


```{julia}
f(x) = x^2 - 2x
l = shift_right(f, c=3)
```

Then `l` is a function that is derived from `f`.


:::{.callout-note}
## Note
The value of `c` used when `l` is called is the one passed to `shift_right`. Functions like `l` that are returned by other functions also are called *closures*, as the context they are evaluated within includes the context of the function that constructs them.

:::

Anonymous functions can be created with the `function` keyword, but we will use the "arrow" notation, `arg->body`, to create them, The above, could have been defined as:


```{julia}
shift_right_alt(f; c=0) = x -> f(x-c)
```

When the `->` is seen a function is being created.


:::{.callout-warning}
## Warning
Generic versus anonymous functions. Julia has two types of functions, generic ones, as defined by `f(x)=x^2` and anonymous ones, as defined by `x -> x^2`. One gotcha is that `Julia` does not like to use the same variable name for the two types.  In general, Julia is a dynamic language, meaning variable names can be reused with different types of variables. But generic functions take more care, as when a new method is defined it gets added to a method table. So repurposing the name of a generic function for something else is not allowed. Similarly, repurposing an already defined variable name for a generic function is not allowed.

This comes up when we use functions that return functions as we have different styles that can be used: When we defined `l = shift_right(f, c=3)` the value of `l` is assigned to name an anonymous function for later use. This binding can be reused to define other variables.

However, we could have defined the function `l` through `l(x) = shift_right(f, c=3)(x)`, being explicit about what happens to the variable `x`. This would add a method to the generic function `l`. Meaning, we get an error if we tried to assign a variable to `l`, such as an expression like `l=3`. The latter style is inefficient, so is not preferred.

:::

##### Example: the secant line


A secant line is a line through two points on the graph of a function. If we have a function $f(x)$, and two $x$-values $x=a$ and $x=b$, then we can find the slope between the points $(a,f(a))$ and $(b, f(b))$ with:


$$
m = \frac{f(b) - f(a)}{b - a}.
$$

The point-slope form of a line then gives the equation of the tangent line as $y = f(a) + m \cdot (x - a)$.


To model this in `Julia`, we would want to turn the inputs `f`,`a`, `b` into a function that implements the secant line (functions are much easier to work with than equations). Here is how we can do it:


```{julia}
function secant(f, a, b)
   m = (f(b) - f(a)) / (b - a)
   x -> f(a) + m * (x - a)
end
```

The body of the function nearly mirrors the mathematical treatment. The main difference is in place of $y = \dots$ we have a `x -> ...` to create an anonymous function.


To illustrate the use, suppose $f(x) = x^2 - 2$ and we have the secant line between $a=1$ and $b=2$. The value at $x=3/2$ is given by:


```{julia}
#| hold: true
f(x) = x^2 - 2
a,b = 1, 2
secant(f,a,b)(3/2)
```

The last line employs double parentheses. The first pair, `secant(f,a,b)`, returns a function and the second pair, `(3/2)`, are used to call the returned function.


#### Closures


One main use of anonymous functions is to make [closures](https://en.wikipedia.org/wiki/Closure_(computer_programming)). We've touched on two  concepts: functions with parameters *and* functions as arguments to other functions. The creation of a function for a given set of parameters may be needed. Anonymous functions are used to create **closures** which capture the values of the parameters. For a simple example, `mxplusb` parameterizes any line, but to use a function to represent a specific line, a new function can be created:


```{julia}
#| hold: true
mxplusb(x; m=0, b=0) = m*x + b
specific_line(m,b) = x -> mxplusb(x; m=m, b=b)
```

The returned object will have its parameters (`m` and `b`) fixed when used.


In `Julia`, the functions `Base.Fix1` and `Base.Fix2` are provided to take functions of two variables and create callable objects of just one variable, with the other argument fixed. This partial function application is provided by a some of the logical comparison operators, which can be useful with filtering, say.


For example, `<(2)` is a funny looking way of expressing the function `x -> x < 2`. (Think of `x < y` as `<(x,y)` and then "fix" the value of `y` to be `2`.) This is useful with filtering by a predicate function, for example:


```{julia}
filter(<(2), 0:4)
```

which picks off the values of `0` and `1` in a somewhat obscure way but less verbose than `filter(x -> x < 2, 0:4)`.


The `Fix2` function is also helpful when using the `f(x, p)` form for passing parameters to a function. The result of `Base.Fix2(f, p)` is a function with its parameters fixed that can be passed along for plotting or other uses.

:::{.callout-note}
## Fix
In Julia v1.12 the `Fix` constructor can fix an arbitrary position of a variadic function.
:::

### The `do` notation


Many functions in `Julia` accept a function as the first argument. A common pattern for calling some function is `action(f, args...)` where `action` is the function that will act on another function `f` using the value(s) in `args...`. There `do` notation is syntactical sugar for creating an anonymous function which is useful when more complicated function bodies are needed.


Here is an artificial example to illustrate of a task we won't have cause to use in these notes, but is an important skill in some contexts. The `do` notation can be confusing to read, as it moves the function definition to the end and not the beginning, but is convenient to write and is used very often with the task of this example.


To save some text to a file requires a few steps: opening the file; writing to the file; closing the file. The `open` function does the first. One method has this signature `open(f::Function, args...; kwargs....)` and is documented to "Apply the function f to the result of `open(args...; kwargs...)` and close the   resulting file descriptor upon completion." Which is great, the open and close stages are handled by `Julia` and only the writing is up to the user.


The writing is done in the function of a body, so the `do` notation allows the creation of the function to be handled anonymously. In this context, the argument to this function will be an `IO` handle, which is typically called `io`.


So the pattern would be


```{julia}
#| eval: false
open("somefile.txt", "w") do io
    write(io, "Four score and seven")
    write(io, "years ago...")
end
```

The name of the file to open appears, how the file is to be opened (`w` means write, `r` would mean read), and then a function with argument `io` which writes two lines to `io`.

## Function application


The typical calling pattern for a function simply follows *mathematical* notation, that is `f(x)` calls the function `f` with the argument `x`. There are times – especially with function composition – that an alternative *piping* syntax is desirable. `Julia` provides the *infix* operation `|>` for piping, defining it by `|>(x, f) = f(x)`. This allows composition to work left to right, instead of right to left. For example, these two calls produce the same answer:


```{julia}
exp(sin(log(3))), 3 |> log |> sin |> exp
```

While convenient, piping works most naturally with functions of a single variable. When more than one variable is needed for a function, say `max`, then things get a bit cumbersome. Using anonymous functions or "fixing" arguments is then useful. For example, here is one way to implement `clamp(x, lo, hi)` which returns `x` when `lo < x < hi` and otherwise `lo` or `hi`, depending:

```{julia}
x, lo, hi = -10, -5, 5 # clampl(x, lo, hi) is -5
x |> x -> max(x, lo) |> x -> min(x, hi)
```

Or

```{julia}
x |> Base.Fix2(max, lo) |> Base.Fix2(min, hi)
```

For heavy use of chaining through function application there are various packages to simplify the notation (e.g. `Chain.jl`, `Pipe.jl`, `DataPipes.jl`, among many others).

## Questions


##### Question


State the domain and range of $f(x) = |x + 2|$.


```{julia}
#| hold: true
#| echo: false
choices = [
"Domain is all real numbers, range is all real numbers",
"Domain is all real numbers, range is all non-negative numbers",
"Domain is all non-negative numbers, range is all real numbers",
"Domain is all non-negative numbers, range is all non-negative numbers"
]
answ = 2
radioq(choices, answ)
```

##### Question


State the domain and range of $f(x) = 1/(x-2)$.


```{julia}
#| hold: true
#| echo: false
choices = [
"Domain is all real numbers, range is all real numbers",
L"Domain is all real numbers except $2$, range is all real numbers except $0$",
L"Domain is all non-negative numbers except $0$, range is all real numbers except $2$",
L"Domain is all non-negative numbers except $-2$, range is all non-negative numbers except $0$"
]
answ = 2
radioq(choices, answ)
```

##### Question


Which of these functions has a domain of all real $x$, but a range of $x > 0$?


```{julia}
#| hold: true
#| echo: false
choices = [
raw"``f(x) = 2^x``",
raw"``f(x) = 1/x^2``",
raw"``f(x) = |x|``",
raw"``f(x) = \sqrt{x}``"]
answ = 1
radioq(choices, answ)
```

##### Question

::: {#fig-floor-function}

```{julia}
#| echo: false
plt = let
    empty_style = (xticks=-4:4, yticks=-4:4,
                    framestyle=:origin,
                    legend=false)
	axis_style = (arrow=true, side=:head, line=(:gray, 1))
	text_style = (10,)
	fn_style =  (;line=(:black, 3))
	fn2_style =  (;line=(:red, 4))
	mark_style = (;line=(:gray, 1, :dot))
	domain_style = (;fill=(:orange, 0.35), line=nothing)
	range_style = (; fill=(:blue, 0.35), line=nothing)

	ts = range(0, 2pi, 100)
	xys = sincos.(ts)
	xys = [.1 .* xy for xy in xys]

	plot(; empty_style..., aspect_ratio=:equal)
	plot!([-4.25,4.25], [0,0]; axis_style...)
	plot!([0,0], [-4.25, 4.25]; axis_style...)

	for k in -4:4
		P,Q = (k,k),(k+1,k)
		plot!([P,Q], line=(:black,1))
		S = Shape([k .+ xy for xy in xys])
		plot!(S; fill=(:black,))
		S = Shape([(k+1,k) .+ xy for xy in xys])
		plot!(S; fill=(:white,), line=(:black,1))
	end


	current()
end
plt
```

```{julia}
#| echo: false
plotly()
nothing
```

The `floor` function rounds down. For example, any value in $[k,k+1)$ rounds to $k$ for integer $k$.
:::

The figure shows the `floor` function which is useful in programming. It rounds down to the first integer value.


From the graph, what is the domain of the function?


```{julia}
#| hold: true
#| echo: false
choices = [
    "The entire real line",
    "The entire real line except for integer values",
    "The integers"
]
answer = 1
radioq(choices, answ)
```

From the graph, what is the range of the function?


```{julia}
#| hold: true
#| echo: false
choices = [
    "The entire real line",
    "The entire real line except for integer values",
    "The integers"
]
answer = 3
radioq(choices, answ)
```

(This graphic uses the convention that a filled in point is present, but an open point is not, hence each bar represents some $[k, k+1)$.)

###### Question


Which of these commands will make a function for $f(x) = \sin(x + \pi/3)$?


```{julia}
#| hold: true
#| echo: false
choices = [q"f = sin(x + pi/3)",
q"function f(x) = sin(x + pi/3)",
q"f(x)  = sin(x + pi/3)",
q"f: x -> sin(x + pi/3)",
q"f x = sin(x + pi/3)"]
answ = 3
radioq(choices, answ)
```

###### Question


Which of these commands will create a function for $f(x) = (1 + x^2)^{-1}$?


```{julia}
#| hold: true
#| echo: false
choices = [q"f(x) = (1 + x^2)^(-1)",
q"function f(x) = (1 + x^2)^(-1)",
q"f(x) := (1 + x^2)^(-1)",
q"f[x] =  (1 + x^2)^(-1)",
q"def f(x): (1 + x^2)^(-1)"
]
answ = 1
radioq(choices, answ)
```

###### Question


Will the following `Julia` commands create a function for


$$
f(x) = \begin{cases}
30 & x < 500\\
30 + 0.10 \cdot (x-500) & \text{otherwise.}
\end{cases}
$$

```{julia}
phone_plan(x) = x < 500 ? 30.0 : 30 + 0.10 * (x-500);
```

```{julia}
#| hold: true
#| echo: false
booleanq(true, labels=["Yes", "No"])
```

###### Question


The expression `max(0, x)` will be `0` if `x` is negative, but otherwise will take the value of `x`. Is this the same?


```{julia}
a_max(x) = x < 0 ? x : 0.0;
```

```{julia}
#| hold: true
#| echo: false
yesnoq(false)
```

###### Question


In statistics, the normal distribution has two parameters $\mu$ and $\sigma$ appearing as:


$$
f(x; \mu, \sigma) = \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{1}{2}\frac{(x-\mu)^2}{\sigma}}.
$$

Does this function implement this with the default values of $\mu=0$ and $\sigma=1$?


```{julia}
a_normal(x; mu=0, sigma=1) = 1/sqrt(2pi*sigma) * exp(-(1/2)*(x-mu)^2/sigma)
```

```{julia}
#| hold: true
#| echo: false
booleanq(true, labels=["Yes", "No"])
```

What value of $\mu$ is used if the function is called as `f(x, sigma=2.7)`?


```{julia}
#| hold: true
#| echo: false
numericq(0)
```

What value of $\mu$ is used if the function is called as `f(x, mu=70)`?


```{julia}
#| hold: true
#| echo: false
numericq(70)
```

What value of $\mu$ is used if the function is called as `f(x, mu=70, sigma=2.7)`?


```{julia}
#| hold: true
#| echo: false
numericq(70)
```

###### Question


`Julia` has keyword arguments (as just illustrated) but also positional arguments. These are matched by how the function is called. For example,


```{julia}
A(w, h) = w * h
```

when called as `A(10, 5)` will use 10 for `w` and `5` for `h`, as the order of `w` and `h` matches that of `10` and `5` in the call.


This is clear enough, but in fact positional arguments can have default values (then called [optional](https://docs.julialang.org/en/v1/manual/functions/#Optional-Arguments)) arguments). For example,


```{julia}
B(w, h=5) = w * h
```

Actually creates two functions: `B(w,h)` for when the call is, say, `B(10,5)` and `B(w)` when the call is `B(10)`.


Suppose a function `C` is defined by


```{julia}
C(x, mu=0, sigma=1) = 1/sqrt(2pi*sigma) * exp(-(1/2)*(x-mu)^2/sigma)
```

This is *nearly* identical to the last question, save for a comma instead of a semicolon after the `x`.


What value of `mu` is used by the call `C(1, 70, 2.7)`?


```{julia}
#| hold: true
#| echo: false
numericq(70)
```

What value of `mu` is used by the call `C(1, 70)`?


```{julia}
#| hold: true
#| echo: false
numericq(70)
```

What value of `mu` is used by the call `C(1)`?


```{julia}
#| hold: true
#| echo: false
numericq(0)
```

Will the call `C(1, mu=70)` use a value of `70` for `mu`?


```{julia}
#| hold: true
#| echo: false
choices = ["Yes, this will work just as it does for keyword arguments",
"No, there will be an error that the function does not accept keyword arguments"]
answ = 2
radioq(choices, answ)
```

###### Question


This function mirrors that of the built-in `clamp` function:


```{julia}
klamp(x, a, b) = x < a ? a : (x > b ? b : x)
```

Can you tell what it does?


```{julia}
#| hold: true
#| echo: false
choices = [
"If `x` is in `[a,b]` it returns `x`, otherwise it returns `a` when `x` is less than `a` and `b` when  `x` is greater than `b`.",
"If `x` is in `[a,b]` it returns `x`, otherwise it returns `NaN`",
"`x` is the larger of the minimum of `x` and `a` and the value of `b`, aka `max(min(x,a),b)`"
]
answ = 1
radioq(choices, answ)
```

###### Question


`Julia` has syntax for the composition of  functions $f$ and $g$ using the Unicode operator `∘` entered as `\circ[tab]`.


The notation to call a composition follows the math notation, where parentheses are necessary to separate the act of composition from the act of calling the function:


$$
(f \circ g)(x)
$$

For example


```{julia}
(sin ∘ cos)(pi/4)
```

What happens if you forget the extra parentheses and were to call `sin ∘ cos(pi/4)`?


```{julia}
#| hold: true
#| echo: false
choices = [
L"You still get $0.649...$",
"You get a `MethodError`, as `cos(pi/4)` is evaluated as a number and `∘` is not defined for functions and numbers",
"You get a `generic` function, but this won't be callable. If tried, it will give an method error."
]
answ = 2
radioq(choices, answ, keep_order=true)
```

:::{.callout-note}
## Note
The parentheses in `(sin ∘ cos)(pi/4)` are needed due to the order of operations, with `cos(pi/4)` being evaluated first in the expression `sin ∘ cos(pi/4)`. Alternatively, one can define a function `sc = sin ∘ cos` (without parentheses), then call it through `sc(pi/4)`.
:::

###### Question


The [pipe](https://docs.julialang.org/en/v1/manual/functions/#Function-composition-and-piping) notation `ex |> f` takes the output of `ex` and uses it as the input to the function `f`. That is composition. What is the value of this expression `1 |> sin |> cos`?


```{julia}
#| hold: true
#| echo: false
choices = [
"It is `0.6663667453928805`, the same as `cos(sin(1))`",
"It is `0.5143952585235492`, the same as `sin(cos(1))`",
"It gives an error"]
answ = 1
radioq(choices, answ, keep_order=true)
```

###### Question


`Julia` has implemented this *limited* set of algebraic operations on functions: `∘` for *composition* and `!` for *negation*. (Read `!` as "not.") The latter is useful for "predicate" functions (ones that return either `true` or `false`. What is output by this command?


```{julia}
#| eval: false
fn = !iseven
fn(3)
```

```{julia}
#| hold: true
#| echo: false
choices = ["`true`","`false`"]
answ = 1
radioq(choices, answ)
```

###### Question


Generic functions in `Julia` allow many algorithms to work without change for different number types. For example, [3000](https://pdfs.semanticscholar.org/1ef4/ee58a159dc7e437e190ec2839fb9a654596c.pdf) years ago, floating point numbers wouldn't have been used to carry out the secant method computations, rather rational numbers would have been. We can see the results of using rational numbers with no change to our key function, just by starting with rational numbers for `a` and `b`:


```{julia}
#| hold: true
secant_intersection(f, a, b) = b - f(b) * (b - a) / (f(b) - f(a))  # rewritten
f(x) = x^2 - 2
a, b = 1//1, 2//1
c = secant_intersection(f, a, b)
```

Now `c` is `4//3` and not `1.333...`. This works as the key operations used: division, squaring, subtraction all have different implementations for rational numbers that preserve this type.


Repeat the secant method two more times to find a better approximation for $\sqrt{2}$. What is the value of `c` found?


```{julia}
#| hold: true
#| echo: false
choices = [q"4//3", q"7//5", q"58//41", q"816//577"]
answ = 3
radioq(choices, answ, keep_order=true)
```

How small is the value of $f(c)$ for this value?


```{julia}
#| hold: true
#| echo: false
val = f(58/41)
numericq(val)
```

How close is this answer to the true value of $\sqrt{2}$?


```{julia}
#| hold: true
#| echo: false
choices = [L"about $8$ parts in $100$", L"about $1$ parts in $100$", L"about $4$ parts in $10,000$", L"about $2$ parts in $1,000,000$"]
answ = 3
radioq(choices, answ, keep_order=true)
```

(Finding a good approximation to $\sqrt{2}$ would be helpful to builders, for example, as it could be used to verify the trueness of a square room, say.)


###### Question


`Julia` does not have surface syntax for the *difference* of functions. This is a common thing to want when solving equations. The tools available solve $f(x)=0$, but problems may present as solving for $h(x) = g(x)$ or even $h(x) = c$, for some constant. Which of these solutions is **not** helpful if $h$ and $g$ are already defined?


```{julia}
#| hold: true
#| echo: false
choices = ["Just use `f = h - g`",
"Define `f(x) = h(x) - g(x)`",
"Use `x -> h(x) - g(x)` when the difference is needed"
]
answ = 1
radioq(choices, answ)
```

###### Question


Nasa has some learning materials on [stars](https://spacemath.gsfc.nasa.gov/stars.html) including one that describes how to count the number of stars brighter than some level. This formula is from [https://spacemath.gsfc.nasa.gov/stars/6Page103.pdf](https://spacemath.gsfc.nasa.gov/stars/6Page103.pdf):

$$
\log_{10}(N(m)) = -0.0003 m^3 + 0.0019 m^2 + 0.484 m -3.82, \quad 4.0 \leq m \leq 25.0.
$$

Where $N(m)$ counts the number of stars brighter than magnitude $m$ *per* square degree of space.

A [square degree](https://en.wikipedia.org/wiki/Square_degree) is a unit of a solid angle. An entire sphere has a solid angle of $4\pi$ and $4\pi \cdot (180/\pi)^2$ square degrees.


With this we can answer questions, such as:

> How many stars can we see in the sky?

Star [magnitude](https://en.wikipedia.org/wiki/Magnitude_(astronomy)) measures the brightness of celestial objects, with the sun on a log scale so that  a magnitude $1$ star is $100$ times brighter than a magnitude $6$ star. The sun has a value around $-27$, Sirius (the brightest visible star) around $-1.46$), Venus around $-5$. We will take a magnitude of $6$ or brighter  for visibility. (magnitudes less than $6$). The value of $N(6)$ is then

```{julia}
q(m) = -0.0003*m^3 + 0.0019*m^2 + 0.484*m - 3.82
N(m) = 10.0^(q(m))
n_6 = N(6)
```

The number of square degrees in the sky is

```{julia}
total_square_degrees = 4pi * (180/pi)^2
```

So the formula estimates this many visible stars in the entire sky:

```{julia}
n_6 * total_square_degrees
```

With a telescope, it is estimated that stars with brightness at magnitude $10.0$ can be seen. How many stars are visible in the sky with such a telescope?

```{julia}
#| echo: false
val = N(10) * total_square_degrees
atol = 10
numericq(val, atol)
```

A photograph is taken of a faint star cluster covering 5 square-degrees of space. The cluster contains stars of magnitude between $11.0$ and $15.0$. The stars seen in the photograph number $5237$. The astronomer estimates there are $5237$ *minus* the expected number of stars in this cluster. What did she estimate?

```{julia}
#| echo: false
expected_count = (N(15.0) - N(11.0)) * 5
actual_count = 5237
cluster = actual_count - expected_count
atol = 2
hint = """
The total expected number of stars in the one degree is `N(15.0) - N(11.0)`.
The count is then `5237 - 5*(N(15.0) - N(11.0))`.
"""
numericq(cluster, atol; hint)
```


If a star of magnitude $5$ difference is $100$ times brighter, what is the scale of the logarithm?

```{julia}
#| echo: false
explanation = raw"""
The base $a$ solve $\log_a(100x) - \log_a(x) = 5$. The logs can be combined and then $a$ can be solved for.
"""
choices = [raw"$5$",raw"$\sqrt[5]{100}$", raw"$\sqrt{100}$"]
answer = 2
buttonq(choices, answer; explanation)
```


###### Question


Identifying the range of a function can be a difficult task. We see in this question that in some cases, a package can be of assistance.


A mathematical interval is a set of values of the form


  * an open interval: $a < x < b$, or $(a,b)$;
  * a closed interval: $a \leq x \leq b$, or $[a,b]$;
  * or a half-open interval: $a < x \leq b$ or $a \leq x < b$, respectively $(a,b]$ or $[a,b)$.


They all contain all real numbers between the endpoints, the distinction is whether the endpoints are included or not.


A domain is some set, but typically that set is an interval such as *all real numbers* ($(-\infty,\infty)$), *all non-negative numbers* ($[0,\infty)$), or, say, *all positive numbers* ($(0,\infty)$).


The `IntervalArithmetic` package provides an easy  means to define closed intervals using the symbol `..`, but this is also used by the already loaded `CalculusWithJulia` package in different manner, so we use the fully qualified named constructor in the following to construct intervals:


```{julia}
import IntervalArithmetic
```

```{julia}
I1 = IntervalArithmetic.interval(-Inf, Inf)
```

```{julia}
I2 = IntervalArithmetic.interval(0, Inf)
```

The main feature of the package is not to construct intervals, but rather to *rigorously* bound with an interval the output of the image of a closed interval under a function. That is, for a function $f$ and *closed* interval $[a,b]$, a bound for the set $\{f(x) \text{ for } x \text{ in } [a,b]\}$. When `[a,b]` is the domain of $f$, then this is a bound for the range of $f$.


For example the function $f(x) = x^2 + 2$ had a domain of all real $x$, the range can be found with:


```{julia}
ab = IntervalArithmetic.interval(-Inf, Inf)
u(x) = x^2 + 2
u(ab)
```

For this problem, the actual range can easily be identified. Does the bound computed match exactly?


```{julia}
#| hold: true
#| echo: false
yesnoq("yes")
```

Does `sin(0..pi)` **exactly** match the interval of $[0,1]$?


```{julia}
#| hold: true
#| echo: false
yesnoq("no")
```

Guess why or why not?


```{julia}
#| hold: true
#| echo: false
choices = ["Well it does, because ``[0,1]`` is the range",
           """It does not. The bound found is a provably known bound. The small deviation is due to the possible errors in evaluation of the `sin` function near the floating point approximation of `pi`,
"""]
radioq(choices, 2)
```

Now consider the evaluation


```{julia}
#| hold: true
f(x) = x^x
I = IntervalArithmetic.interval(0, Inf)
f(I)
```

Make a graph of `f`. Does the interval found above provide a nearly exact estimate of the true range (as the previous two questions have)?


```{julia}
#| hold: true
#| echo: false
yesnoq("no")
```

Any thoughts on why?


```{julia}
#| hold: true
#| echo: false
choices = ["""
The guarantee of `IntervalArithmetic` is a *bound* on the interval, not the *exact* interval. In the case where the variable `x` appears more than once, it is treated formulaically as an *independent* quantity (meaning it has it full set of values considered in each instance) which is not the actual case mathematically. This is the "dependence problem" in interval arithmetic.""",
           """
The interval is a nearly exact estimate, as guaranteed by `IntervalArithmetic`.
"""]
radioq(choices, 1)
```
