edits
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
|
||||
# Vectors
|
||||
# Vectors and containers
|
||||
|
||||
|
||||
{{< include ../_common_code.qmd >}}
|
||||
@@ -143,8 +142,9 @@ We call the values $x$ and $y$ of the vector $\vec{v} = \langle x,~ y \rangle$ t
|
||||
Two operations on vectors are fundamental.
|
||||
|
||||
|
||||
* Vectors can be multiplied by a scalar (a real number): $c\vec{v} = \langle cx,~ cy \rangle$. Geometrically this scales the vector by a factor of $\lvert c \rvert$ and switches the direction of the vector by $180$ degrees (in the $2$-dimensional case) when $c < 0$. A *unit vector* is one with magnitude $1$, and, except for the $\vec{0}$ vector, can be formed from $\vec{v}$ by dividing $\vec{v}$ by its magnitude. A vector's two parts are summarized by its direction given by a unit vector **and** its magnitude given by the norm.
|
||||
* Vectors can be added: $\vec{v} + \vec{w} = \langle v_x + w_x,~ v_y + w_y \rangle$. That is, each corresponding component adds to form a new vector. Similarly for subtraction. The $\vec{0}$ vector then would be just $\langle 0,~ 0 \rangle$ and would satisfy $\vec{0} + \vec{v} = \vec{v}$ for any vector $\vec{v}$. Vector addition, $\vec{v} + \vec{w}$, is visualized by placing the tail of $\vec{w}$ at the tip of $\vec{v}$ and then considering the new vector with tail coming from $\vec{v}$ and tip coming from the position of the tip of $\vec{w}$. Subtraction is different, place both the tails of $\vec{v}$ and $\vec{w}$ at the same place and the new vector has tail at the tip of $\vec{w}$ and tip at the tip of $\vec{v}$.
|
||||
* *Scalar multiplication*: Vectors can be multiplied by a scalar (a real number): $c\vec{v} = \langle cx,~ cy \rangle$. Geometrically this scales the vector by a factor of $\lvert c \rvert$ and switches the direction of the vector by $180$ degrees (in the $2$-dimensional case) when $c < 0$. A *unit vector* is one with magnitude $1$, and, except for the $\vec{0}$ vector, can be formed from $\vec{v}$ by dividing $\vec{v}$ by its magnitude. A vector's two parts are summarized by its direction given by a unit vector **and** its magnitude given by the norm.
|
||||
|
||||
* *Vector addition*: Vectors can be added: $\vec{v} + \vec{w} = \langle v_x + w_x,~ v_y + w_y \rangle$. That is, each corresponding component adds to form a new vector. Similarly for subtraction. The $\vec{0}$ vector then would be just $\langle 0,~ 0 \rangle$ and would satisfy $\vec{0} + \vec{v} = \vec{v}$ for any vector $\vec{v}$. Vector addition, $\vec{v} + \vec{w}$, is visualized by placing the tail of $\vec{w}$ at the tip of $\vec{v}$ and then considering the new vector with tail coming from $\vec{v}$ and tip coming from the position of the tip of $\vec{w}$. Subtraction is different, place both the tails of $\vec{v}$ and $\vec{w}$ at the same place and the new vector has tail at the tip of $\vec{w}$ and tip at the tip of $\vec{v}$.
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -334,7 +334,7 @@ Finally, to find an angle $\theta$ from a vector $\langle x,~ y\rangle$, we can
|
||||
norm(v), atan(y, x) # v = [x, y]
|
||||
```
|
||||
|
||||
## Higher dimensional vectors
|
||||
### Higher dimensional vectors
|
||||
|
||||
|
||||
Mathematically, vectors can be generalized to more than $2$ dimensions. For example, using $3$-dimensional vectors are common when modeling events happening in space, and $4$-dimensional vectors are common when modeling space and time.
|
||||
@@ -395,334 +395,6 @@ Whereas, in this example where there is no common type to promote the values to,
|
||||
["one", 2, 3.0, 4//1]
|
||||
```
|
||||
|
||||
## Indexing
|
||||
|
||||
|
||||
Getting the components out of a vector can be done in a manner similar to multiple assignment:
|
||||
|
||||
|
||||
```{julia}
|
||||
vs = [1, 2]
|
||||
v₁, v₂ = vs
|
||||
```
|
||||
|
||||
When the same number of variable names are on the left hand side of the assignment as in the container on the right, each is assigned in order.
|
||||
|
||||
|
||||
Though this is convenient for small vectors, it is far from being so if the vector has a large number of components. However, the vector is stored in order with a first, second, third, $\dots$ component. `Julia` allows these values to be referred to by *index*. This too uses the `[]` notation, though differently. Here is how we get the second component of `vs`:
|
||||
|
||||
|
||||
```{julia}
|
||||
vs[2]
|
||||
```
|
||||
|
||||
The last value of a vector is usually denoted by $v_n$. In `Julia`, the `length` function will return $n$, the number of items in the container. So `v[length(v)]` will refer to the last component. However, the special keyword `end` will do so as well, when put into the context of indexing. So `v[end]` is more idiomatic. (Similarly, there is a `begin` keyword that is useful when the vector is not $1$-based, as is typical but not mandatory.)
|
||||
|
||||
The functions `first` and `last` refer to the first and last components of a collection. An additional argument can be specified to take the first (or last) $n$ components. The function `only` will return the only component of a vector, if it has length $1$ and error otherwise.
|
||||
|
||||
|
||||
:::{.callout-note}
|
||||
## More on indexing
|
||||
There is [much more](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing) to indexing than just indexing by a single integer value. For example, the following can be used for indexing:
|
||||
|
||||
* a scalar integer (as seen)
|
||||
* a range
|
||||
* a vector of integers
|
||||
* a boolean vector
|
||||
|
||||
:::
|
||||
|
||||
Some add-on packages extend this further.
|
||||
|
||||
|
||||
### Assignment and indexing
|
||||
|
||||
|
||||
Indexing notation can also be used with assignment, meaning it can appear on the left hand side of an equals sign. The following expression replaces the second component with a new value:
|
||||
|
||||
|
||||
```{julia}
|
||||
vs[2] = 10
|
||||
```
|
||||
|
||||
The value of the right hand side is returned, not the value for `vs`. We can check that `vs` is then $\langle 1,~ 10 \rangle$ by showing it:
|
||||
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
vs = [1,2]
|
||||
vs[2] = 10
|
||||
vs
|
||||
```
|
||||
|
||||
The assignment `vs[2]` is different than the initial assignment `vs=[1,2]` in that, `vs[2]=10` **modifies** the container that `vs` points to, whereas `vs=[1,2]` **replaces** any binding for `vs`. The indexed assignment is more memory efficient when vectors are large. This point is also of interest when passing vectors to functions, as a function may modify components of the vector passed to it, though can't replace the container itself.
|
||||
|
||||
|
||||
## Some useful functions for working with vectors.
|
||||
|
||||
|
||||
As mentioned, the `length` function returns the number of components in a vector. It is one of several useful functions for vectors.
|
||||
|
||||
|
||||
The `sum` and `prod` function will add and multiply the elements in a vector:
|
||||
|
||||
|
||||
```{julia}
|
||||
v1 = [1,1,2,3,5,8]
|
||||
sum(v1), prod(v1)
|
||||
```
|
||||
|
||||
The `unique` function will throw out any duplicates:
|
||||
|
||||
|
||||
```{julia}
|
||||
unique(v1) # drop a `1`
|
||||
```
|
||||
|
||||
The functions `maximum` and `minimum` will return the largest and smallest values of an appropriate vector.
|
||||
|
||||
|
||||
```{julia}
|
||||
maximum(v1)
|
||||
```
|
||||
|
||||
(These should not be confused with `max` and `min` which give the largest or smallest value over all their arguments.)
|
||||
|
||||
|
||||
The `extrema` function returns both the smallest and largest value of a collection:
|
||||
|
||||
|
||||
```{julia}
|
||||
extrema(v1)
|
||||
```
|
||||
|
||||
Consider now
|
||||
|
||||
|
||||
```{julia}
|
||||
𝒗 = [1,4,2,3]
|
||||
```
|
||||
|
||||
The `sort` function will rearrange the values in `𝒗`:
|
||||
|
||||
|
||||
```{julia}
|
||||
sort(𝒗)
|
||||
```
|
||||
|
||||
The keyword argument, `rev=true` can be given to get values in decreasing order:
|
||||
|
||||
|
||||
```{julia}
|
||||
sort(𝒗, rev=true)
|
||||
```
|
||||
|
||||
For adding a new element to a vector the `push!` method can be used, as in
|
||||
|
||||
|
||||
```{julia}
|
||||
push!(𝒗, 5)
|
||||
```
|
||||
|
||||
To append more than one value, the `append!` function can be used:
|
||||
|
||||
|
||||
```{julia}
|
||||
append!(v1, [6,8,7])
|
||||
```
|
||||
|
||||
These two functions modify or mutate the values stored within the vector `𝒗` that passed as an argument. In the `push!` example above, the value `5` is added to the vector of $4$ elements. In `Julia`, a convention is to name mutating functions with a trailing exclamation mark. (Again, these do not mutate the binding of `𝒗` to the container, but do mutate the contents of the container.) There are functions with mutating and non-mutating definitions, an example is `sort` and `sort!`.
|
||||
|
||||
|
||||
If only a mutating function is available, like `push!`, and this is not desired a copy of the vector can be made. It is not enough to copy by assignment, as with `w = 𝒗`. As both `w` and `𝒗` will be bound to the same memory location. Rather, you call `copy` (or sometimes `deepcopy`) to make a new container with copied contents, as in `w = copy(𝒗)`.
|
||||
|
||||
|
||||
Creating new vectors of a given size is common for programming, though not much use will be made here. There are many different functions to do so: `ones` to make a vector of ones, `zeros` to make a vector of zeros, `trues` and `falses` to make Boolean vectors of a given size, and `similar` to make a similar-sized vector (with no particular values assigned).
|
||||
|
||||
|
||||
## Applying functions element by element to values in a vector
|
||||
|
||||
|
||||
Functions such as `sum` or `length` are known as *reductions* as they reduce the "dimensionality" of the data: a vector is in some sense $1$-dimensional, the sum or length are $0$-dimensional numbers. Applying a reduction is straightforward – it is just a regular function call.
|
||||
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
v = [1, 2, 3, 4]
|
||||
sum(v), length(v)
|
||||
```
|
||||
|
||||
Other desired operations with vectors act differently. Rather than reduce a collection of values using some formula, the goal is to apply some formula to *each* of the values, returning a modified vector. A simple example might be to square each element, or subtract the average value from each element. An example comes from statistics. When computing a variance, we start with data $x_1, x_2, \dots, x_n$ and along the way form the values $(x_1-\bar{x})^2, (x_2-\bar{x})^2, \dots, (x_n-\bar{x})^2$.
|
||||
|
||||
|
||||
Such things can be done in *many* different ways. Here we describe two, but will primarily utilize the first.
|
||||
|
||||
|
||||
### Broadcasting a function call
|
||||
|
||||
|
||||
If we have a vector, `xs`, and a function, `f`, to apply to each value, there is a simple means to achieve this task. By adding a "dot" between the function name and the parenthesis that enclose the arguments, instructs `Julia` to "broadcast" the function call. The details allow for more flexibility, but, for this purpose, broadcasting will take each value in `xs` and apply `f` to it, returning a vector of the same size as `xs`. When more than one argument is involved, broadcasting will try to fill out different sized objects.
|
||||
|
||||
|
||||
For example, the following will find, using `sqrt`, the square root of each value in a vector:
|
||||
|
||||
|
||||
```{julia}
|
||||
xs = [1, 1, 3, 4, 7]
|
||||
sqrt.(xs)
|
||||
```
|
||||
|
||||
This would find the sine of each number in `xs`:
|
||||
|
||||
|
||||
```{julia}
|
||||
sin.(xs)
|
||||
```
|
||||
|
||||
For each function, the `.(` (and not `(`) after the name is the surface syntax for broadcasting.
|
||||
|
||||
|
||||
The `^` operator is an *infix* operator. Infix operators can be broadcast, as well, by using the form `.` prior to the operator, as in:
|
||||
|
||||
|
||||
```{julia}
|
||||
xs .^ 2
|
||||
```
|
||||
|
||||
Here is an example involving the logarithm of a set of numbers. In astronomy, a logarithm with base $100^{1/5}$ is used for star [brightness](http://tinyurl.com/ycp7k8ay). We can use broadcasting to find this value for several values at once through:
|
||||
|
||||
|
||||
```{julia}
|
||||
ys = [1/5000, 1/500, 1/50, 1/5, 5, 50]
|
||||
base = (100)^(1/5)
|
||||
log.(base, ys)
|
||||
```
|
||||
|
||||
Broadcasting with multiple arguments allows for mixing of vectors and scalar values, as above, making it convenient when parameters are used.
|
||||
|
||||
|
||||
As a final example, the task from statistics of centering and then squaring can be done with broadcasting. We go a bit further, showing how to compute the [sample variance](http://tinyurl.com/p6wa4r8) of a data set. This has the formula
|
||||
|
||||
|
||||
$$
|
||||
\frac{1}{n-1}\cdot ((x_1-\bar{x})^2 + \cdots + (x_n - \bar{x})^2).
|
||||
$$
|
||||
|
||||
This can be computed, with broadcasting, through:
|
||||
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
import Statistics: mean
|
||||
xs = [1, 1, 2, 3, 5, 8, 13]
|
||||
n = length(xs)
|
||||
(1/(n-1)) * sum(abs2.(xs .- mean(xs)))
|
||||
```
|
||||
|
||||
This shows many of the manipulations that can be made with vectors. Rather than write `.^2`, we follow the definition of `var` and chose the possibly more performant `abs2` function which, in general, efficiently finds $|x|^2$ for various number types. The `.-` uses broadcasting to subtract a scalar (`mean(xs)`) from a vector (`xs`). Without the `.`, this would error.
|
||||
|
||||
|
||||
:::{.callout-note}
|
||||
## Note
|
||||
The `map` function is very much related to broadcasting and similarly named functions are found in many different programming languages. (The "dot" broadcast is mostly limited to `Julia` and mirrors a similar usage of a dot in `MATLAB`.) For those familiar with other programming languages, using `map` may seem more natural. Its syntax is `map(f, xs)`.
|
||||
|
||||
:::
|
||||
|
||||
### Comprehensions
|
||||
|
||||
|
||||
In mathematics, set notation is often used to describe elements in a set.
|
||||
|
||||
|
||||
For example, the first $5$ cubed numbers can be described by:
|
||||
|
||||
|
||||
$$
|
||||
\{x^3: x \text{ in } 1, 2,\dots, 5\}
|
||||
$$
|
||||
|
||||
Comprehension notation is similar. The above could be created in `Julia` with:
|
||||
|
||||
|
||||
```{julia}
|
||||
xs = [1,2,3,4,5]
|
||||
[x^3 for x in xs]
|
||||
```
|
||||
|
||||
Something similar can be done more succinctly:
|
||||
|
||||
|
||||
```{julia}
|
||||
xs .^ 3
|
||||
```
|
||||
|
||||
However, comprehensions have a value when more complicated expressions are desired as they work with an expression of `xs`, and not a pre-defined or user-defined function.
|
||||
|
||||
|
||||
Another typical example of set notation might include a condition, such as, the numbers divisible by $7$ between $1$ and $100$. Set notation might be:
|
||||
|
||||
|
||||
$$
|
||||
\{x: \text{rem}(x, 7) = 0 \text{ for } x \text{ in } 1, 2, \dots, 100\}.
|
||||
$$
|
||||
|
||||
This would be read: "the set of $x$ such that the remainder on division by $7$ is $0$ for all x in $1, 2, \dots, 100$."
|
||||
|
||||
|
||||
In `Julia`, a comprehension can include an `if` clause to mirror, somewhat, the math notation. For example, the above would become (using `1:100` as a means to create the numbers $1,2,\dots, 100$, as will be described in an upcoming section):
|
||||
|
||||
|
||||
```{julia}
|
||||
[x for x in 1:100 if rem(x,7) == 0]
|
||||
```
|
||||
|
||||
Comprehensions can be a convenient means to describe a collection of numbers, especially when no function is defined, but the simplicity of the broadcast notation (just adding a judicious ".") leads to its more common use in these notes.
|
||||
|
||||
|
||||
##### Example: creating a "T" table for creating a graph
|
||||
|
||||
|
||||
The process of plotting a function is usually first taught by generating a "T" table: values of $x$ and corresponding values of $y$. These pairs are then plotted on a Cartesian grid and the points are connected with lines to form the graph. Generating a "T" table in `Julia` is easy: create the $x$ values, then create the $y$ values for each $x$.
|
||||
|
||||
|
||||
To be concrete, let's generate $7$ points to plot $f(x) = x^2$ over $[-1,1]$.
|
||||
|
||||
|
||||
The first task is to create the data. We will soon see more convenient ways to generate patterned data, but for now, we do this by hand:
|
||||
|
||||
|
||||
```{julia}
|
||||
a, b, n = -1, 1, 7
|
||||
d = (b-a) // (n-1)
|
||||
xs = [a, a+d, a+2d, a+3d, a+4d, a+5d, a+6d] # 7 points
|
||||
```
|
||||
|
||||
To get the corresponding $y$ values, we can use a compression (or define a function and broadcast):
|
||||
|
||||
|
||||
```{julia}
|
||||
ys = [x^2 for x in xs]
|
||||
```
|
||||
|
||||
Vectors can be compared together by combining them into a separate container, as follows:
|
||||
|
||||
|
||||
```{julia}
|
||||
[xs ys]
|
||||
```
|
||||
|
||||
(If there is a space between objects they are horizontally combined. In our construction of vectors using `[]` we used a comma for vertical combination. More generally we should use a `;` for vertical concatenation.)
|
||||
|
||||
|
||||
In the sequel, we will typically use broadcasting for this task using two steps: one to define a function the second to broadcast it.
|
||||
|
||||
|
||||
:::{.callout-note}
|
||||
## Note
|
||||
The style generally employed here is to use plural variable names for a collection of values, such as the vector of $y$ values and singular names when a single value is being referred to, leading to expressions like "`x in xs`".
|
||||
|
||||
:::
|
||||
|
||||
## Other container types
|
||||
|
||||
We end this section with some general comments that are for those interested in a bit more, but in general aren't needed to understand most all of what follows later.
|
||||
@@ -761,12 +433,16 @@ Tuples are fixed-length containers where there is no expectation or enforcement
|
||||
|
||||
While a vector is formed by placing comma-separated values within a `[]` pair (e.g., `[1,2,3]`), a tuple is formed by placing comma-separated values within a `()` pair. A tuple of length $1$ uses a convention of a trailing comma to distinguish it from a parenthesized expression (e.g. `(1,)` is a tuple, `(1)` is just the value `1`).
|
||||
|
||||
|
||||
Vectors and tuples can appear at the same time: a vector of tuples--each of length $n$--can be used in plotting to specify points.
|
||||
|
||||
:::{.callout-note}
|
||||
## Well, actually...
|
||||
Technically, the tuple is formed just by the use of commas, which separate different expressions. The parentheses are typically used, as they clarify the intent and disambiguate some usage. In a notebook interface, it is useful to just use commas to separate values to output, as typically the only the last command is displayed. This usage just forms a tuple of the values and displays that.
|
||||
|
||||
:::
|
||||
|
||||
#### Named tuples
|
||||
|
||||
There are *named tuples* where each component has an associated name. Like a tuple these can be indexed by number and unlike regular tuples also by name.
|
||||
|
||||
@@ -795,12 +471,14 @@ The values in a named tuple can be accessed using the "dot" notation:
|
||||
nt.x1
|
||||
```
|
||||
|
||||
Alternatively, the index notation -- using a *symbol* for the name -- can be used:
|
||||
Alternatively, the index notation--using a *symbol* for the name--can be used:
|
||||
|
||||
```{julia}
|
||||
nt[:x1]
|
||||
```
|
||||
|
||||
(Indexing is described a bit later, but it is a way to pull elements out of a collection.)
|
||||
|
||||
Named tuples are employed to pass parameters to functions. To find the slope, we could do:
|
||||
|
||||
```{julia}
|
||||
@@ -820,11 +498,15 @@ x1 - x0
|
||||
:::
|
||||
|
||||
|
||||
### Associative arrays
|
||||
### Pairs, associative arrays
|
||||
|
||||
Named tuples associate a name (in this case a symbol) to a value. More generally an associative array associates to each key a value, where the keys and values may be of different types.
|
||||
|
||||
The `pair` notation, `key => value`, is used to make one association. A *dictionary* is used to have a container of associations. For example, this constructs a simple dictionary associating a spelled out name with a numeric value:
|
||||
The `pair` notation, `key => value`, is used to make one association between the first and second value.
|
||||
|
||||
A *dictionary* is used to have a container of associations.
|
||||
|
||||
This example constructs a simple dictionary associating a spelled out name with a numeric value:
|
||||
|
||||
```{julia}
|
||||
d = Dict("one" => 1, "two" => 2, "three" => 3)
|
||||
@@ -842,7 +524,449 @@ d["two"]
|
||||
|
||||
Named tuples are associative arrays where the keys are restricted to symbols. There are other types of associative arrays, specialized cases of the `AbstractDict` type with performance benefits for specific use cases. In these notes, dictionaries appear as output in some function calls.
|
||||
|
||||
Unlike vectors and tuples, dictionaries are not currently supported by broadcasting. This causes no loss in usefulness, as the values can easily be iterated over, but the convenience of the dot notation is lost.
|
||||
Unlike vectors and tuples, dictionaries are not currently supported by broadcasting. (To be described in the next section.) This causes no loss in usefulness, as the values can easily be iterated over, but the convenience of the dot notation is lost.
|
||||
|
||||
|
||||
## The container interface in Julia
|
||||
|
||||
There are numerous generic functions for working across the many different types of containers. Some are specific to containers which can be modified, some to associative arrays. But it is expected for different container types to implement as many as possible. We list a few here for completeness. Only a few will be used in these notes.
|
||||
|
||||
### Indexing
|
||||
|
||||
Vectors have an implied order: first element, second, last, etc. Tuples do as well. Matrices have two orders: by a row-column pair or by linear order where the first column precedes the second etc. Arrays are similar in that they have a linear order and can be accessed by their individual dimensions.
|
||||
|
||||
To access an element in a vector, say the second, the underlying `getindex` function is used. This is rarely typed, as the `[` notation is used. This notation is used in a style similar to a function call, the indexes go between matching pairs.
|
||||
|
||||
For example, we create a vector, tuple, and matrix:
|
||||
|
||||
```{julia}
|
||||
v = [1,2,3,4]
|
||||
t = (1,2,3,4)
|
||||
m = [1 2; 3 4]
|
||||
```
|
||||
|
||||
The second element of each is accessed similarly:
|
||||
|
||||
```{julia}
|
||||
v[2], t[2], m[2]
|
||||
```
|
||||
|
||||
(All of `v`, `t`, and `m` have $1$-based indexing.)
|
||||
|
||||
There is special syntax to reference the last index when used within the square braces:
|
||||
|
||||
```{julia}
|
||||
v[end], t[end], m[end]
|
||||
```
|
||||
|
||||
The last element is also returned by `last`:
|
||||
|
||||
```{julia}
|
||||
last(v), last(t), last(m)
|
||||
```
|
||||
|
||||
These use `lastindex` behind the scenes. There is also a `firstindex` which is associated with the `first` method:
|
||||
|
||||
|
||||
```{julia}
|
||||
first(v), first(t), first(m)
|
||||
```
|
||||
|
||||
For indexing by a numeric index, a container of numbers may be used. Containers can be generated different ways, here we just use a vector to get the second and third elements:
|
||||
|
||||
```{julia}
|
||||
I = [2,3]
|
||||
v[I], t[I], m[I]
|
||||
```
|
||||
|
||||
When indexing by a vector, the value will not be a scalar, even if there is only one element indicated.
|
||||
|
||||
Indexing can also be done by a mask of Boolean values with a matching length. This following mask should do the same as indexing by `I` above:
|
||||
|
||||
```{julia}
|
||||
J = [false, true, true, false]
|
||||
v[J], t[J], m[J]
|
||||
```
|
||||
|
||||
For the matrix, values can be referenced by row/column values. The following will extract the second row, first column:
|
||||
|
||||
```{julia}
|
||||
m[2, 1]
|
||||
```
|
||||
|
||||
*If* a container has *only* one entry, then the `only` method will return that element (not within the container). Here we use a tuple to illustrate to emphasize the trailing comma in construction:
|
||||
|
||||
```{julia}
|
||||
s = ("one", )
|
||||
```
|
||||
|
||||
```{julia}
|
||||
only(s)
|
||||
```
|
||||
|
||||
There will be an error with `only` should the container not have just one element.
|
||||
|
||||
### Mutating values
|
||||
|
||||
Vectors and matrices can have their elements changed or mutated; tuples can not. The process is similar to assignment--using an equals sign--but the left hand side has indexing notation to reference which values within the container are to be updated.
|
||||
|
||||
To change the last element of `v` to `0` we have:
|
||||
|
||||
```{julia}
|
||||
v[end] = 0
|
||||
v
|
||||
```
|
||||
|
||||
We might read this as assignment, but what happens is the underlying container has an element indicated by the index mutated. The `setindex!` function is called behind the scenes.
|
||||
|
||||
|
||||
The `setindex!` function will try to promote the value (`0` above) to the element type of the container. This can throw an error if the promotion isn't possible. For example, to specify an element as `missing` with `v[end] = missing` will error, as missing can't be promoted to an integer.
|
||||
|
||||
If more than one value is referenced in the assignment, then more than one value can be specified on the right-hand side.
|
||||
|
||||
Mutation is different from reassignment. A command like `v=[1,2,3,0]` would have had the same effect as `v[end] = 0`, but would be quite different. The first *replaces* the binding to `v` with a new container, the latter reaches into the container and replaces just a value it holds.
|
||||
|
||||
### Size and type
|
||||
|
||||
The `length` of a container is the number of elements in linear order:
|
||||
|
||||
```{julia}
|
||||
length(v), length(t), length(m)
|
||||
```
|
||||
|
||||
The `isempty` method will indicate if the length is 0, perhaps in a performant way:
|
||||
|
||||
```{julia}
|
||||
isempty(v), isempty([]), isempty(t), isempty(())
|
||||
```
|
||||
|
||||
|
||||
The `size` of a container, when defined, takes into account its shape or the dimensions:
|
||||
|
||||
```{julia}
|
||||
size(v), size(m) # no size defined for tuples
|
||||
```
|
||||
|
||||
Arrays, and hence vectors and matrices have an element type given by `eltype` (the `typeof` method returns the container type:
|
||||
|
||||
```{julia}
|
||||
eltype(v), eltype(t), eltype(m)
|
||||
```
|
||||
|
||||
(The element type of the tuple is `Int64`, but this is only because of this particular tuple. Tuples are typically heterogeneous containers--not homogeneous like vectors--and do not expect to have a common type. The `NTuple` type is for tuples with elements of the same type.)
|
||||
|
||||
### Modifying the length of a container
|
||||
|
||||
Vectors and some other containers allow elements to be added on or elements to be taken off. In computer science a queue is a collection that is ordered and has addition at one or the other end. Vectors can be used as a queue, though for just that task, there are more performant structures available.
|
||||
|
||||
Two key methods for queues are `push!` and `pop!`. We `push!` elements onto the end of the queue:
|
||||
|
||||
```{julia}
|
||||
push!(v, 5)
|
||||
```
|
||||
|
||||
The output is expected -- `5` was added to the end of `v`. What might not be expected is the underlying `v` is changed without assignment. (Actually `mutated`, the underlying container assigned to the symbol `v` is extended, not replaced.)
|
||||
|
||||
:::{.callout-note}
|
||||
## Trailing exclamation point convention
|
||||
The function `push!` has a trailing exclamation point which is a `Julia` convention to indicate one of the underlying arguments (traditionally the first) will be *mutated* by the function call.
|
||||
:::
|
||||
|
||||
The `pop!` function is somewhat of a reverse: it takes the last element and "pops" it off the queue, leaving the queue one element shorter and returning the last element.
|
||||
|
||||
```{julia}
|
||||
pop!(v)
|
||||
```
|
||||
|
||||
```{julia}
|
||||
v
|
||||
```
|
||||
|
||||
There are also `pushfirst!`, `popfirst!`, `insert!` and `deleteat!` methods.
|
||||
|
||||
|
||||
### Iteration
|
||||
|
||||
A very fundamental operation is to iterate over the elements of a collection one by one.
|
||||
|
||||
In computer science the `for` loop is the basic construct to iterate over values. This example will iterate over `v` and add each value to `tot` which is initialized to be `0`:
|
||||
|
||||
```{julia}
|
||||
tot = 0
|
||||
for e in v
|
||||
tot = tot + e
|
||||
end
|
||||
tot
|
||||
```
|
||||
|
||||
The `for` loop construct is central in many programming languages; in `Julia` for loops are very performant and very flexible, however, they are more verbose than needed. (In the above example we had to initialize an accumulator and then write three lines for the loop, whereas `sum(v)` would do the same--and in this case more flexibly, with just a single call.) Alternatives are usually leveraged--we mention a few.
|
||||
|
||||
Iterating over a vector can be done by *value*, as above, or by *index*. For the latter the `eachindex` method creates an iterable for the indices of the container. For rectangular objects, like matrices, there are also many uses for `eachrow` and `eachcol`, though not in these notes.
|
||||
|
||||
|
||||
There are a few basic patterns where alternatives to a `for` loop exist. We discuss two:
|
||||
|
||||
* mapping a function or expression over each element in the collection
|
||||
* a reduction where a larger dimensional object is summarized by a lower dimensional one. In the example above, the $1$-dimensional vector is reduced to a $0$-dimensional scalar by summing the elements.
|
||||
|
||||
#### Comprehensions
|
||||
|
||||
In mathematics, set notation is often used to describe elements in a set.
|
||||
|
||||
For example, the first $5$ cubed numbers can be described by:
|
||||
|
||||
|
||||
$$
|
||||
\{x^3: x \text{ in } 1, 2,\dots, 5\}
|
||||
$$
|
||||
|
||||
Comprehension notation is similar. The above could be created in `Julia` with:
|
||||
|
||||
|
||||
```{julia}
|
||||
xs = [1, 2, 3, 4, 5]
|
||||
[x^3 for x in xs]
|
||||
```
|
||||
|
||||
Comprehensions are one way of iterating over a collection and evaluating an expression on each element.
|
||||
|
||||
In the above, the value `x` takes on each value in `xs`. The variables may be tuples, as well.
|
||||
|
||||
The `enumerate` method wraps a container (or iterable) and iterates both the index and the value. This is useful, say for polynomials:
|
||||
|
||||
```{julia}
|
||||
x = 3
|
||||
as = [1, 2, 3] # evaluate a₀⋅x⁰, a₁⋅x¹, a₂⋅x²
|
||||
[a*x^(i-1) for (i, a) in enumerate(as)]
|
||||
```
|
||||
|
||||
(These values can then be easily summed to evaluate the polynomial.)
|
||||
|
||||
When iterating over `enumerate` a tuple is returned. The use of `(i, a)` to iterate over these tuples destructures the tuple into parts to be used in the expression.
|
||||
|
||||
The `zip` function also is useful to *pair* off iterators. Redoing the above to have the powers iterated over:
|
||||
|
||||
```{julia}
|
||||
as = [1, 2, 3]
|
||||
inds = [0, 1, 2]
|
||||
[a*x^i for (i, a) in zip(inds, as)]
|
||||
```
|
||||
|
||||
Like `enumerate`, the `zip` iterator has elements which are tuples.
|
||||
|
||||
|
||||
:::{.callout-note}
|
||||
## Note
|
||||
The style generally employed herein is to use plural variable names for a collection of values, such as the vector of $y$ values and singular names when a single value is being referred to, leading to expressions like "`x in xs`".
|
||||
|
||||
:::
|
||||
|
||||
|
||||
|
||||
#### Broadcasting a function call
|
||||
|
||||
If we have a vector, `xs`, and a function, `f`, to apply to each value, there is a simple means to achieve this task that is shorter than a `for` loop or the comprehension `[f(x) for x in s]`. By adding a "dot" between the function name and the parenthesis that enclose the arguments, instructs `Julia` to "broadcast" the function call. The details allow for more much flexibility, but, for this purpose, broadcasting will take each value in `xs` and apply `f` to it, returning a vector of the same size as `xs`. When more than one argument is involved, broadcasting will try to pad out different sized objects to the same shape. Broadcasting can also *fuse* combined function calls.
|
||||
|
||||
|
||||
For example, the following will find, using `sqrt`, the square root of each value in a vector:
|
||||
|
||||
|
||||
```{julia}
|
||||
xs = [1, 1, 3, 4, 7]
|
||||
sqrt.(xs)
|
||||
```
|
||||
|
||||
This call finds the sine of each number in `xs`:
|
||||
|
||||
|
||||
```{julia}
|
||||
sin.(xs)
|
||||
```
|
||||
|
||||
For each function call, the `.(` (and not `(`) after the name is the surface syntax for broadcasting.
|
||||
|
||||
|
||||
The `^` operator is an *infix* operator. Infix operators can be broadcast, as well, by using the form `.` prior to the operator, as in:
|
||||
|
||||
|
||||
```{julia}
|
||||
xs .^ 2
|
||||
```
|
||||
|
||||
Here is an example involving the logarithm of a set of numbers. In astronomy, a logarithm with base $100^{1/5}$ is used for star [brightness](http://tinyurl.com/ycp7k8ay). We can use broadcasting to find this value for several values at once through:
|
||||
|
||||
|
||||
```{julia}
|
||||
ys = [1/5000, 1/500, 1/50, 1/5, 5, 50]
|
||||
base = (100)^(1/5)
|
||||
log.(base, ys)
|
||||
```
|
||||
|
||||
Broadcasting with multiple arguments allows for mixing of vectors and scalar values, as above, making it convenient when parameters are used. In broadcasting, there are times where it is desirable to treat a container as a scalar-like argument, a common idiom is to wrap that container in a 1-element tuple.
|
||||
|
||||
|
||||
As a final example, the task from statistics of centering and then squaring can be done with broadcasting. We go a bit further, showing how to compute the [sample variance](http://tinyurl.com/p6wa4r8) of a data set. This has the formula
|
||||
|
||||
|
||||
$$
|
||||
\frac{1}{n-1}\cdot ((x_1-\bar{x})^2 + \cdots + (x_n - \bar{x})^2).
|
||||
$$
|
||||
|
||||
This can be computed, with broadcasting, through:
|
||||
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
import Statistics: mean
|
||||
xs = [1, 1, 2, 3, 5, 8, 13]
|
||||
n = length(xs)
|
||||
(1/(n-1)) * sum(abs2.(xs .- mean(xs)))
|
||||
```
|
||||
|
||||
This shows many of the manipulations that can be made with vectors. Rather than write `.^2`, we follow the definition of `var` and chose the possibly more performant `abs2` function which, in general, efficiently finds $|x|^2$ for various number types. The `.-` uses broadcasting to subtract a scalar (`mean(xs)`) from a vector (`xs`). Without the `.`, this would error.
|
||||
|
||||
Broadcasting is a widely used and powerful surface syntax which we will employ occasionally in the sequel.
|
||||
|
||||
|
||||
#### Mapping a function over a collection
|
||||
|
||||
The `map` function is very much related to broadcasting. Similarly named functions are found in many different programming languages. (The "dot" broadcast is mostly limited to `Julia` and mirrors a similar usage of a dot in `MATLAB`.) For those familiar with other programming languages, using `map` may seem more natural. Its syntax is `map(f, xs)`. There may be one or more iterable passed to `map`.
|
||||
|
||||
|
||||
For example, this will map `sin` over each value in `xs`, computing the same things as `sin.(xs)`:
|
||||
|
||||
```{julia}
|
||||
map(sin, xs)
|
||||
```
|
||||
|
||||
The `map` function can be used with one or more iterators.
|
||||
|
||||
The `map` function can also be used in combination with `reduce`, a reduction. Reductions take a container with one or more dimensions and reduces the number of dimensions. A example might be:
|
||||
|
||||
```{julia}
|
||||
sum(map(sin, xs))
|
||||
```
|
||||
|
||||
This has a performance drawback--there are two passes through the container, one to apply `sin` another to add.
|
||||
|
||||
The `mapreduce` function combines the map and reduce operations in one pass. It takes a third argument to reduce by in the second position. This is a *binary* operator. So this combination will map `sin` over `xs` and then add the results up:
|
||||
|
||||
```{julia}
|
||||
mapreduce(sin, +, xs)
|
||||
```
|
||||
|
||||
There are other specialized reduction functions that reverse the order of the mapper and the reducer. For example, we have `sum` (used above) and `prod` for adding and multiplying values in a collection:
|
||||
|
||||
```{julia}
|
||||
sum(xs), prod(xs)
|
||||
```
|
||||
|
||||
These are reductions, which which fall back to a `mapreduce` call. They require a starting value (`init`) of `0` and `1` (which in this case can be determined from `xs`). The `sum` and `prod` function also allow as a first argument an initial function to map over the collection:
|
||||
|
||||
```{julia}
|
||||
sum(sin, xs)
|
||||
```
|
||||
|
||||
|
||||
#### Other reductions
|
||||
|
||||
There are other reductions, which summarize a container. We mention those related to the maximum or minimum of a collection. For these examples, we have
|
||||
|
||||
```{julia}
|
||||
v = [1, 2, 3, 4]
|
||||
```
|
||||
|
||||
The largest value in a numeric collection is returned by `maximum`:
|
||||
|
||||
```{julia}
|
||||
maximum(v)
|
||||
```
|
||||
|
||||
Where this maximum occurred is returned by `argmax`:
|
||||
|
||||
```{julia}
|
||||
argmax(v)
|
||||
```
|
||||
|
||||
For `v` these are the same. But if we were to apply `sin` to `v` say, then the result may not be in order. This can be done with, say, a call to `map` and then `maximum`, but the functions allow an initial function to be specified:
|
||||
|
||||
```{julia}
|
||||
maximum(sin, v), argmax(sin, v)
|
||||
```
|
||||
|
||||
This combination is also the duty of `findmax`:
|
||||
|
||||
```{julia}
|
||||
findmax(sin, v)
|
||||
```
|
||||
|
||||
There are also `minimum`, `argmin`, and `findmin`.
|
||||
|
||||
The `extrema` function returns the maximum and minimum of the collection:
|
||||
|
||||
```{julia}
|
||||
extrema(v)
|
||||
```
|
||||
|
||||
:::{.callout-note}
|
||||
## `maximum` and `max`
|
||||
In `Julia` there are two related functions: `maximum` and `max`. The `maximum` function generically returns the largest element in a collection. The `max` function returns the maximum of its *arguments*.
|
||||
|
||||
That is, these return identical values:
|
||||
|
||||
```{julia}
|
||||
xs = [1, 3, 2]
|
||||
maximum(xs), max(1, 3, 2), max(xs...)
|
||||
```
|
||||
|
||||
The latter using *splatting* to iterate over each value in `xs` and pass it to `max` as an argument.
|
||||
:::
|
||||
|
||||
### Predicate functions
|
||||
|
||||
A few reductions work with *predicate* functions--those that return `true` or `false`. Let's use `iseven` as an example, which tests if a number is even.
|
||||
|
||||
We can check if *all* the alements of a container are even or if *any* of the elements of a container are even with `all` and `even`:
|
||||
|
||||
```{julia}
|
||||
xs = [1, 1, 2, 3, 5]
|
||||
all(iseven, xs), any(iseven, xs)
|
||||
```
|
||||
|
||||
|
||||
Related, we can count the number of `true` responses of the predicate function:
|
||||
|
||||
```{julia}
|
||||
count(iseven, xs)
|
||||
```
|
||||
|
||||
|
||||
#### methods for associative arrays
|
||||
|
||||
For dictionaries, the collection is unordered (by default), but iteration can still be done over "key-value" pairs.
|
||||
|
||||
In `Julia` a `Pair` matches a key and a value into one entity. Pairs are made with the `=>` notation with the `key` on the left and the value on the right.
|
||||
|
||||
|
||||
Dictionaries are a collection of pairs. The `Dict` constructor can be passed pairs directly:
|
||||
|
||||
```{julia}
|
||||
ascii = Dict("a"=>97, "b"=>98, "c"=>99) # etc.
|
||||
```
|
||||
|
||||
To iterate over these, the `pairs` iterator is useful:
|
||||
|
||||
```{julia}
|
||||
collect(pairs(ascii))
|
||||
```
|
||||
|
||||
(We used `collect` to iterate over values and return them as a vector.)
|
||||
|
||||
The keys are returned by `keys`, the values by `values`:
|
||||
|
||||
```{julia}
|
||||
keys(ascii)
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -993,6 +1117,7 @@ From [transum.org](http://www.transum.org/Maths/Exam/Online_Exercise.asp?Topic=V
|
||||
#| hold: true
|
||||
#| echo: false
|
||||
let
|
||||
gr()
|
||||
p = plot(xlim=(0,10), ylim=(0,5), legend=false, framestyle=:none)
|
||||
for j in (-3):10
|
||||
plot!(p, [j, j + 5], [0, 5*sqrt(3)], color=:blue, alpha=0.5)
|
||||
@@ -1058,6 +1183,12 @@ answ = 4
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
```{julia}
|
||||
#| echo: false
|
||||
plotly()
|
||||
nothing
|
||||
```
|
||||
|
||||
###### Question
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user