# Vectors


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

```{julia}
#| echo: false
#| results: "hidden"
using CalculusWithJulia
using CalculusWithJulia.WeaveSupport
using Plots
using Measures
using LaTeXStrings

#fig_size = (400, 300)
fig_size = (800, 600)

const frontmatter = (
        title = "Vectors",
        description = "Calculus with Julia: Vectors",
        tags = ["CalculusWithJulia", "precalc", "vectors"],
);

nothing
```

One of the first models learned in physics are the equations governing the laws of motion with constant acceleration: $x(t) = x_0 + v_0 t + 1/2 \cdot a t^2$. This is a consequence of Newton's second [law](http://tinyurl.com/8ylk29t) of motion applied to the constant acceleration case. A related formula for the velocity is $v(t) = v_0 + at$. The following figure is produced using these formulas applied to both the vertical position and the horizontal position:


```{julia}
#| hold: true
#| echo: false
px = 0.26mm
x0 = [0, 64]
v0 = [20, 0]
g  = [0, -32]

unit(v::Vector) = v / norm(v)
x_ticks = collect(0:10:80)
y_ticks = collect(0:10:80)


function make_plot(t)

    xn = (t) -> x0 + v0*t + 1/2*g*t^2
    vn = (t) -> v0 + g*t
    an = (t) -> g


    t = 1/10 + t*2/10

    ts = range(0, stop=2, length=100)
    xys = map(xn, ts)

    xs, ys = [p[1] for p in xys], [p[2] for p in xys]

    plt = Plots.plot(xs, ys, legend=false, size=fig_size, xlims=(0,45), ylims=(0,70))
    plot!(plt, zero, extrema(xs)...)
    arrow!(xn(t), 10*unit(xn(t)), color="black")
    arrow!(xn(t), 10*unit(vn(t)), color="red")
    arrow!(xn(t), 10*unit(an(t)), color="green")

    plt


end

imgfile = tempname() * ".gif"
caption = """

Position, velocity, and acceleration vectors (scaled) for projectile
motion. Vectors are drawn with tail on the projectile. The position
vector (black) points from the origin to the projectile, the velocity
vector (red) is in the direction of the trajectory, and the
acceleration vector (green) is a constant pointing downward.

"""

n = 8
anim = @animate for i=1:n
    make_plot(i)
end

gif(anim, imgfile, fps = 1)

ImageFile(imgfile, caption)
```

For the motion in the above figure, the object's $x$ and $y$ values change according to the same rule, but, as the acceleration is different in each direction, we get different formula, namely: $x(t) = x_0 + v_{0x} t$ and $y(t) = y_0 + v_{0y}t - 1/2 \cdot gt^2$.


It is common to work with *both* formulas at once. Mathematically, when graphing, we naturally pair off two values using Cartesian coordinates (e.g., $(x,y)$). Another means of combining related values is to use a *vector*. The notation for a vector varies, but to distinguish them from a point we will use $\langle x,~ y\rangle$. With this notation, we can use it to represent the position, the velocity, and the acceleration at time $t$ through:


$$
\begin{align}
\vec{x} &= \langle x_0 + v_{0x}t,~ -(1/2) g t^2 + v_{0y}t  + y_0 \rangle,\\
\vec{v} &= \langle v_{0x},~ -gt + v_{0y} \rangle, \text{ and }\\
\vec{a} &= \langle 0,~ -g \rangle.
\end{align}
$$

Don't spend time thinking about the formulas if they are unfamiliar. The point emphasized here is that we have used the notation $\langle x,~ y \rangle$ to collect the two values into a single object, which we indicate through a label on the variable name. These are vectors, and we shall see they find use far beyond this application.


Initially, our primary use of vectors will be as containers, but it is worthwhile to spend some time to discuss properties of vectors and their visualization.


A line segment in the plane connects two points $(x_0, y_0)$ and $(x_1, y_1)$. The length of a line segment (its magnitude) is given by the distance formula $\sqrt{(x_1 - x_0)^2 + (y_1 - y_0)^2}$.  A line segment can be given a direction by assigning an initial point and a terminal point. A directed line segment has both a direction and a magnitude. A vector is an abstraction where just these two properties $-$ a **direction** and a **magnitude** $-$ are intrinsic. While a directed line segment can be represented by a vector, a single vector describes all such line segments found by translation. That is, how the the vector is located when visualized is for convenience, it is not a characteristic of the vector. In the figure above, all vectors are drawn with their tails at the position of the projectile over time.


We can visualize a (two-dimensional) vector as an arrow in space. This arrow has two components. We represent a vector mathematically as $\langle x,~ y \rangle$. For example, the vector connecting the point $(x_0, y_0)$ to $(x_1, y_1)$ is $\langle x_1 - x_0,~ y_1 - y_0 \rangle$.


The magnitude of a vector comes from the distance formula applied to a line segment, and is $\| \vec{v} \| = \sqrt{x^2 + y^2}$.


```{julia}
#| hold: true
#| echo: false
## generic vector
p0 = [0,0]
a1 = [4,1]
b1 = [-2,2]
unit(v::Vector) = v / norm(v)

plt = plot(legend=false, size=fig_size)
arrow!(p0, a1, color="blue")
arrow!([1,1], unit(a1), color="red")
annotate!([(2, .4, L"v"), (1.6, 1.05, L"\hat{v}")])

imgfile = tempname() * ".png"
png(plt, imgfile)

caption = "A vector and its unit vector. They share the same direction, but the unit vector has a standardized magnitude."

ImageFile(imgfile, caption)
```

We call the values $x$ and $y$ of the vector $\vec{v} = \langle x,~ y \rangle$ the components of the $v$.


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{v}$ and tip at the tip of $\vec{w}$.


```{julia}
#| hold: true
#| echo: false
## vector_addition_image

p0 = [0,0]
a1 = [4,1]
b1 = [-2,2]


plt = Plots.plot(legend=false, size=fig_size)
arrow!(p0, a1, color="blue")
arrow!(p0+a1, b1, color="red")
arrow!(p0, a1+b1, color="black")
annotate!([(2, .25, L"a"), (3, 2.25, L"b"), (1.35, 1.5, L"a+b")])

imgfile = tempname() * ".png"
png(plt, imgfile)

caption = "The sum of two vectors can be visualized by placing the tail of one at the tip of the other"

ImageFile(imgfile, caption)
```

```{julia}
#| hold: true
#| echo: false
## vector_subtraction_image

p0 = [0,0]
a1 = [4,1]
b1 = [-2,2]

plt = plot(legend=false, size=fig_size)
arrow!(p0, a1, color="blue")
arrow!(p0, b1, color="red")
arrow!(b1, a1-b1, color="black")
annotate!(plt, [(-1, .5, L"a"), (2.45, .5, L"b"), (1, 1.75, L"a-b")])


imgfile = tempname() * ".png"
png(plt, imgfile)

caption = "The difference of two vectors can be visualized by placing the tail of one at the tip of the other"

ImageFile(imgfile, caption)
```

The concept of scalar multiplication and addition, allow the decomposition of vectors into standard vectors. The standard unit vectors in two dimensions are $e_x = \langle 1,~ 0 \rangle$ and $e_y = \langle 0,~ 1 \rangle$. Any two dimensional vector can be written uniquely as $a e_x + b e_y$ for some pair of scalars $a$ and $b$ (or as, $\langle a, b \rangle$). This is true more generally where the two vectors are not the standard unit vectors - they can be *any* two non-parallel vectors.


```{julia}
#| hold: true
#| echo: false
### {{{vector_decomp}}}

p0 = [0,0]
aa = [1,2]
bb = [2,1]
cc = [4,3]
alpha = 2/3
beta = 5/3

plt = plot(legend=false, size=fig_size)
arrow!(p0, cc, color="black", width=1)
arrow!(p0, aa, color="black", width=1)
arrow!(alpha*aa, bb, color="black", width=1)
arrow!(p0, alpha*aa, color="orange", width=4, opacity=0.5)
arrow!(alpha*aa, beta*bb, color="orange", width=4, opacity=0.5)
#annotate!(collect(zip([2, .5, 1.75], [1.25,1.0,2.25], [L"c",L"2/3 \cdot a", L"5/3 \cdot b"])))


imgfile = tempname() * ".png"
png(plt, imgfile)

caption = raw"""

The vector ``\langle 4,3 \rangle`` is written as
``2/3 \cdot\langle 1,2 \rangle + 5/3 \cdot\langle 2,1 \rangle``.
Any vector ``\vec{c}``
can be written uniquely as
``\alpha\cdot\vec{a} + \beta \cdot \vec{b}``
provided ``\vec{a}`` and ``\vec{b}`` are not parallel.

"""

ImageFile(imgfile, caption)
```

The two operations of scalar multiplication and vector addition are defined in a component-by-component basis. We will see that there are many other circumstances where performing the same action on each component in a vector is desirable.


---


When a vector is placed with its tail at the origin, it can be described in terms of the angle it makes with the $x$ axis, $\theta$, and its length, $r$. The following formulas apply:


$$
r = \sqrt{x^2 + y^2}, \quad \tan(\theta) = y/x.
$$

If we are given $r$ and $\theta$, then the vector is $v = \langle r \cdot \cos(\theta),~ r \cdot \sin(\theta) \rangle$.


```{julia}
#| hold: true
#| echo: false
## vector_rtheta
p0 = [0,0]

plt = plot(legend=false, size=fig_size)
arrow!(p0, [2,3], color="black")
arrow!(p0, [2,0], color="orange")
arrow!(p0+[2,0], [0,3], color="orange")
annotate!(plt, collect(zip([.25, 1,1,1.75], [.25, 1.85,.25,1], [L"t",L"r", L"r \cdot \cos(t)", L"r \cdot \sin(t)"]))) #["θ","r", "r ⋅ cos(θ)", "r ⋅ sin(θ)"]

imgfile = tempname() * ".png"
png(plt, imgfile)

caption = raw"""

A vector ``\langle x, y \rangle`` can be written as ``\langle r\cdot
\cos(\theta), r\cdot\sin(\theta) \rangle`` for values ``r`` and
``\theta``. The value ``r`` is a magnitude, the direction parameterized by
``\theta``."""

ImageFile(imgfile, caption)
```

## Vectors in Julia


A vector in `Julia` can be represented by its individual components, but it is more convenient to combine them into a collection using the `[,]` notation:


```{julia}
x, y = 1, 2
v = [x, y]        # square brackets, not angles
```

The basic vector operations are implemented for vector objects. For example, the vector `v` has scalar multiplication defined for it:


```{julia}
10 * v
```

The `norm` function returns the magnitude of the vector (by default):


```{julia}
import LinearAlgebra: norm
```

```{julia}
norm(v)
```

A unit vector is then found by scaling by the reciprocal of the magnitude:


```{julia}
v / norm(v)
```

In addition, if `w` is another vector, we can add and subtract:


```{julia}
w = [3, 2]
v + w, v - 2w
```

We see above that scalar multiplication, addition, and subtraction can be done without new notation. This is because the usual operators have methods defined for vectors.


Finally, to find an angle $\theta$ from a vector $\langle x,~ y\rangle$, we can employ the `atan` function using two arguments:


```{julia}
norm(v), atan(y, x) # v  = [x, y]
```

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


In `Julia` there are many uses for vectors outside of physics applications. A vector in `Julia` is just a one-dimensional collection of similarly typed values and a special case of an array. Such objects find widespread usage. For example:


  * In plotting graphs with `Julia`, vectors are used to hold the $x$ and $y$ coordinates of a collection of points to plot and connect with straight lines. There can be hundreds of such points in a plot.
  * Vectors are a natural container to hold the roots of a polynomial or zeros of a function.
  * Vectors may be used to record the state of an iterative process.
  * Vectors are naturally used to represent a data set, such as arise when collecting survey data.


Creating higher-dimensional vectors is similar to creating a two-dimensional vector, we just include more components:


```{julia}
fibs = [1, 1, 2, 3, 5, 8, 13]
```

Later we will discuss different ways to modify the values of a vector to create new ones, similar to how scalar multiplication does.


As mentioned, vectors in  `Julia`  are comprised of elements of a similar type, but the type is not limited to numeric values. For example, a vector of strings might be useful for text processing, a vector of Boolean values can naturally arise, some applications are even naturally represented in terms of vectors of vectors (such as happens when plotting a collection points). Look at the output of these two vectors:


```{julia}
["one", "two", "three"]  # Array{T, 1} is shorthand for Vector{T}. Here T - the type - is String
```

```{julia}
[true, false, true]		# vector of Bool values
```

Finally, we mention that if `Julia` has values of different types it will promote them to a common type if possible. Here we combine three types of numbers, and see that each is promoted to `Float64`:


```{julia}
[1, 2.0, 3//1]
```

Whereas, in this example where there is no common type to promote the values to, a catch-all type of `Any` is used to hold the components.


```{julia}
["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.)


:::{.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 `v=[1,2]` **replaces** the binding for `vs`. The indexed assignment is then 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=false` can be given to get values in decreasing order:


```{julia}
sort(𝒗, rev=false)
```

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` 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 on 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}
𝒙s = [1,2,3,4,5]
[x^3 for x in 𝒙s]
```

Something similar can be done more succinctly:


```{julia}
𝒙s .^ 3
```

However, comprehensions have a value when more complicated expressions are desired as they work with an expression of `𝒙s`, 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)
𝐱s = [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}
𝐲s = [x^2 for x in 𝐱s]
```

Vectors can be compared together by combining them into a separate container, as follows:


```{julia}
[𝐱s 𝐲s]
```

(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


Vectors in `Julia` are a container, one of many different types. Another useful type for programming purposes are *tuples*. If 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 withing 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`).


Tuples are used in programming, as they don't typically require allocated memory to be used so they can be faster. Internal usages are for function arguments and function return types. Unlike vectors, tuples can be heterogeneous collections. (When commas are used to combine more than one output into a cell, a tuple is being used.) (Also, a big technical distinction is that tuples are also different from vectors and other containers in that tuple types are *covariant* in their parameters, not *invariant*.)


Unlike vectors, tuples can have names which can be used for referencing a value, similar to indexing but possibly more convenient. Named tuples are similar to *dictionaries* which are used to associate a key (like a name) with a value.


For example, here a named tuple is constructed, and then its elements referenced:


```{julia}
nt = (one=1, two="two", three=:three)  # heterogeneous values (Int, String, Symbol)
nt.one, nt[2], n[end]                  # named tuples have name or index access
```

## Questions


###### Question


Which command will create the vector $\vec{v} = \langle 4,~ 3 \rangle$?


```{julia}
#| hold: true
#| echo: false
choices = [
q"v = [4,3]",
q"v = {4, 3}",
q"v = '4, 3'",
q"v = (4,3)",
q"v = <4,3>"]
answ = 1
radioq(choices, answ)
```

###### Question


Which command will create the vector with components "4,3,2,1"?


```{julia}
#| hold: true
#| echo: false
choices = [q"v = [4,3,2,1]", q"v = (4,3,2,1)", q"v = {4,3,2,1}", q"v = '4, 3, 2, 1'", q"v = <4,3,2,1>"]
answ = 1
radioq(choices, answ)
```

###### Question


What is the magnitude of the vector $\vec{v} = \langle 10,~ 15 \rangle$?


```{julia}
#| hold: true
#| echo: false
v = [10, 15]
val = norm(v)
numericq(val)
```

###### Question


Which of the following is the unit vector in the direction of $\vec{v} = \langle 3,~ 4 \rangle$?


```{julia}
#| hold: true
#| echo: false
choices = [q"[3, 4]", q"[0.6, 0.8]", q"[1.0, 1.33333]", q"[1, 1]"]
answ = 2
radioq(choices, answ)
```

###### Question


What vector is in the same direction as $\vec{v} = \langle 3,~ 4 \rangle$ but is 10 times as long?


```{julia}
#| hold: true
#| echo: false
choices = [q"[3, 4]", q"[30, 40]", q"[9.48683, 12.6491 ]", q"[10, 10]"]
answ = 2
radioq(choices, answ)
```

###### Question


If $\vec{v} = \langle 3,~ 4 \rangle$ and $\vec{w} = \langle 1,~ 2 \rangle$  find $2\vec{v} + 5 \vec{w}$.


```{julia}
#| hold: true
#| echo: false
choices = [q"[4, 6]", q"[6, 8]", q"[11, 18]", q"[5, 10]"]
answ = 3
radioq(choices, answ)
```

###### Question


Let `v` be defined by:


```{julia}
#| hold: true
#| eval: false
v = [1, 1, 2, 3, 5, 8, 13, 21]
```

What is the length of `v`?


```{julia}
#| hold: true
#| echo: false
v = [1, 1, 2, 3, 5, 8, 13, 21]
val = length(v)
numericq(val)
```

What is the `sum` of `v`?


```{julia}
#| hold: true
#| echo: false
v = [1, 1, 2, 3, 5, 8, 13, 21]
val = sum(v)
numericq(val)
```

What is the `prod` of `v`?


```{julia}
#| hold: true
#| echo: false
v = [1,1,2,3,5,8,13,21]
val = prod(v)
numericq(val)
```

###### Question


From [transum.org](http://www.transum.org/Maths/Exam/Online_Exercise.asp?Topic=Vectors).


```{julia}
#| hold: true
#| echo: false
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)
    plot!(p, [j - 5, j], [5*sqrt(3), 0], color=:blue, alpha=0.5)
end
for i in 1/2:1/2:3
    plot!(p, [0,10],sqrt(3)*[i,i], color=:blue, alpha=0.5)
end

quiver!(p, [(3/2, 3/2*sqrt(3))], quiver=[(1,0)], color=:black,  linewidth=5)        # a
quiver!(p, [(2, sqrt(3))], quiver=[(1/2,-sqrt(3)/2)], color=:black,  linewidth=5)   # b

quiver!(p, [(3 + 3/2, 3/2*sqrt(3))], quiver=[(3,0)], color=:black,  linewidth=5)        # c
quiver!(p, [(4 , sqrt(3))], quiver=[(3/2,-sqrt(3)/2)], color=:black,  linewidth=5)        # d
quiver!(p, [(6+1/2 , sqrt(3)/2)], quiver=[(1/2, sqrt(3)/2)], color=:black,  linewidth=5)        # e

delta = 1/4
annotate!(p, [(2, 3/2*sqrt(3) -delta, L"a"),
              (2+1/4, sqrt(3), L"b"),
              (3+3/2+3/2, 3/2*sqrt(3)-delta, L"c"),
              (4+3/4, sqrt(3) - sqrt(3)/4-delta, L"d"),
              (6+3/4+delta, sqrt(3)/2 + sqrt(3)/4-delta, L"e")
              ])


p
```

The figure shows $5$ vectors.


Express vector **c** in terms of **a** and **b**:


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

Express vector **d** in terms of **a** and **b**:


```{julia}
#| hold: true
#| echo: false
choices = ["3a", "3b", "a + b", "a - b", "b-a"]
answ = 3
radioq(choices, answ)
```

Express vector **e** in terms of **a** and **b**:


```{julia}
#| hold: true
#| echo: false
choices = ["3a", "3b", "a + b", "a - b", "b-a"]
answ = 4
radioq(choices, answ)
```

###### Question


If `xs=[1, 2, 3, 4]` and `f(x) = x^2` which of these will not produce the vector `[1, 4, 9, 16]`?


```{julia}
#| hold: true
#| echo: false
choices = [q"f.(xs)", q"map(f, xs)", q"[f(x) for x in xs]", "All three of them work"]
answ = 4
radioq(choices, answ, keep_order=true)
```

###### Question


Let $f(x) = \sin(x)$ and $g(x) = \cos(x)$. In the interval $[0, 2\pi]$ the zeros of $g(x)$ are given by


```{julia}
zs = [pi/2, 3pi/2]
```

What construct will give the function values of $f$ at the zeros of $g$?


```{julia}
#| hold: true
#| echo: false
choices = [q"sin(zs)", q"sin.(zs)", q"sin(.zs)", q".sin(zs)"]
answ = 2
radioq(choices, answ, keep_order=true)
```

###### Question


If `zs = [1,4,9,16]` which of these commands will return `[1.0, 2.0, 3.0, 4.0]`?


```{julia}
#| hold: true
#| echo: false
choices = [
q"sqrt(zs)",
q"sqrt.(zs)",
q"zs^(1/2)",
q"zs^(1./2)"
]
answ = 2
radioq(choices, answ, keep_order=true)
```
