em dash; sentence case
This commit is contained in:
@@ -240,13 +240,13 @@ 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).
|
||||
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.
|
||||
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}
|
||||
|
||||
@@ -315,7 +315,7 @@ s(x) =
|
||||
\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`.)
|
||||
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:
|
||||
@@ -357,7 +357,7 @@ For example, here is one way to define an absolute value function:
|
||||
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.
|
||||
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`:
|
||||
@@ -672,7 +672,7 @@ During this call, values for `m` and `b` are found from how the function is call
|
||||
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.
|
||||
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:
|
||||
@@ -726,7 +726,7 @@ The style isn't so different from using keyword arguments, save the extra step o
|
||||
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).
|
||||
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}
|
||||
@@ -749,13 +749,13 @@ 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.
|
||||
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.
|
||||
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}
|
||||
@@ -785,7 +785,7 @@ 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)`).
|
||||
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.
|
||||
|
||||
@@ -811,7 +811,7 @@ Representing the area of a rectangle in terms of two variables is easy, as the f
|
||||
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:
|
||||
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}
|
||||
@@ -858,7 +858,7 @@ $$
|
||||
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)$.
|
||||
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:
|
||||
@@ -881,7 +881,7 @@ function shift_right(f; c=0)
|
||||
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`.
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user