# Vector

Functions over vectors are divided into:

**Numerical vector functions**operate on numerical vectors.**Sequence functions**operate on vectors whose elements can be of arbitrary type and need not be numerical.**Vector aggregate functions**operate on collections (bags) of vectors.

Notice that the select vector statement provides a powerful mechanism for constructing new vectors through queries in terms of functions.

## Numerical vector functions

The following infix *operators* over numeric vectors `v`

and `w`

, matrices `a`

and `b`

, and numbers `lambda`

are defined:

operation | description | implemented as function |
---|---|---|

`v + w` | element-wise addition | plus |

`v .+ w` | -"- | plus |

`lambda + w` | add number to elements | plus |

`v + lambda` | -"- | plus |

`v - w` | element-wise subtractions | minus |

`v .- w` | -"- | minus |

`-v` | negate elements | uminus |

`v - lambda` | subtract number from elements | minus |

`lambda - w` | subtract elements from number | minus |

`v * w` | scalar product | times |

`a * b` | matrix product | times |

`v * b` | scalar products | times |

`a * w` | scalar products | times |

`v .* w` | element-wise multiplications | elemtimes |

`lambda * w` | multiply elements with number | times |

`v * lambda` | -"- | times |

`v ./ w` | element-wise division | elemdiv |

`v / lambda` | divide elements with number | div |

`lambda / w` | divide number with elements | div |

`v .^ lambda` | element-wise exponent | elempower |

### Examples

**Vector addition:**

*Add vector elements:*

`[1,2,3]+[4,5,6];`

[1,2,3].+[4,5,6]

To run this code block you must be logged in and your studio instance must be started.

*Number and vector addition:*

`5+[1,2,3];`

[1,2,3]+5

To run this code block you must be logged in and your studio instance must be started.

*Add nested vector elements:*

`[[1],2,3]+[[4],5,6];`

[[1],2,3].+[[4],5,6]

To run this code block you must be logged in and your studio instance must be started.

**Vector subtraction:**

*Subtract vector elements:*

`[1,2,3]-[4,5,6];`

[1,2,3].-[4,5,6]

To run this code block you must be logged in and your studio instance must be started.

*Number and vector subtraction:*

`5-[1,2,3];`

[1,2,3]-5

To run this code block you must be logged in and your studio instance must be started.

**Vector division:**

*Number and vector division:*

`[1,2,3]/2;`

3/[1,2,3]

To run this code block you must be logged in and your studio instance must be started.

*Element division:*

`[1,2,3]./[4,5,6]`

To run this code block you must be logged in and your studio instance must be started.

*Nested element division:*

`[[1],2,3]./[[4],5,6]`

To run this code block you must be logged in and your studio instance must be started.

**Multiply vector elements:**

*Scalar product:*

`[1,2,3]*[4,5,6]`

To run this code block you must be logged in and your studio instance must be started.

*Number and vector multiplication:*

`[1,2,3]*5;`

5*[1,2,3]

To run this code block you must be logged in and your studio instance must be started.

*Element multiplication:*

`[1,2,3].*[4,5,6]`

To run this code block you must be logged in and your studio instance must be started.

*Nested element multiplication:*

`[[1],2,3].*[[4],5,[6]]`

To run this code block you must be logged in and your studio instance must be started.

**Raise each element to a given exponent:**

`[1,2,3] .^ 2`

To run this code block you must be logged in and your studio instance must be started.

**Round each element in a vector of numbers to two decimals:**

`roundto([3.14159,2.71828],2)`

To run this code block you must be logged in and your studio instance must be started.

**Compute the max value of vector elements:**

`max([1,2,3]);`

max(["a","b","c"])

To run this code block you must be logged in and your studio instance must be started.

**Compute the min value of vector elements:**

`min([1,2,3]);`

min(["a","b","c"])

To run this code block you must be logged in and your studio instance must be started.

## Transforming vectors

The function `section(v,l,u), `

returns a *splice* of a vector `v`

, e.g.:

`section([1,2,3,4,5],2,4)`

To run this code block you must be logged in and your studio instance must be started.

The function `skip(v,n)`

returns the part of vector `v`

starting at position `n+1`

, e.g.:

`skip(["a","b","c"],2)`

To run this code block you must be logged in and your studio instance must be started.

The function `permute(v,indl)`

permutes the elements of a vector, e.g.:

`permute([1,2,3,4,5],[2,4])`

To run this code block you must be logged in and your studio instance must be started.

## Matrices

The type `Matrix`

is an alias for type `Vector of Vector of Number`

.

Matrix multiplication is used when the function `times`

(operator `*`

) is applied on matrices. The shapes of `a`

and `b`

must match, e.g.:

`[[1,2],[3,4],[5,6]] * [[7,8],[9,10]]`

To run this code block you must be logged in and your studio instance must be started.

The scalar product is computed when the function `times`

(operator `*`

) is applied on vectors and on combinations of vectors and matrices, e.g.two scalar multiplications:

`[4,5,6] * [[1,2,3],[6,7,8]]`

To run this code block you must be logged in and your studio instance must be started.

The function `transpose(m)`

trasposes the matrix `m`

, e.g.:

`transpose([[1,2,3],[6,7,8]])`

To run this code block you must be logged in and your studio instance must be started.

## Vector aggregate functions

Dimension-wise aggregates over bags of vectors can be computed using the function `aggv()`

.

*Example:*

` aggv((select [i, i + 10]`

from Integer i

where i in range(1, 10)), thefunction('mean'))

To run this code block you must be logged in and your studio instance must be started.

**Notice** that the function `mean()`

is overloaded and `thefunction()`

has to be use to get the generic `mean()`

function.

Each dimension in a bag of vector of number can be normalized using one of the normalization functions `meansub()`

, `zscore()`

, or `maxmin()`

:

` meansub(Bag of Vector of Number b) -> Bag of Vector of Number`

zscore(Bag of Vector of Number b) -> Bag of Vector of Number

maxmin(Bag of Vector of Number b) -> Bag of Vector of Number

`meansub()`

transforms each dimension to a `N(0, s)`

distribution (assuming that the dimension was `N(u, s)`

distributed) by subtracting the mean u of each dimension.

`zscore()`

transforms each dimension to a `N(0, 1)`

distribution by also dividing by the standard deviation of each dimension.

`maxmin()`

transforms each dimension to be on the `[0, 1]`

interval by applying the transformation `(w - min) ./ (max - min)`

to each vector w in bag b where max and min are computed using `aggv(b, #' max')`

and `aggv(b, #'min')`

respectively.

*Example:*

` meansub((select [i, i/2 + 10]`

from Integer i

where i in range(1, 5)))

To run this code block you must be logged in and your studio instance must be started.

## Functions

`abs(Vector v)->Vector of Number`

The absolute values in vector `v`

`aggv(Bag of Vector bv,Function fn)->Vector of Number`

Apply aggregate function `fn`

on each position of the vectors in `bv`

`argmax(Vector v)->Integer`

Index of the first largest element in `v`

`argmin(Vector v)->Integer`

Index of the first smallest element in `v`

`concat(Vector v,Vector w)->Vector`

Concatenate vectors `v`

and `w`

`dim(Vector v)->Integer`

Size of vector `v`

`div(Number lambda,Vector w)->Vector of Number`

Divide `lambda`

with each element in vector `w`

, `lambda / w`

`div(Vector v,Number lambda)->Vector of Number`

Divide elements in vector `v`

with `lambda`

, `v / lambda`

`elemdiv(Vector v,Vector w)->Vector of Number`

Divide elements in vector `v`

with elements in `w`

, `v ./ w`

`elempower(Vector v,Number exp)->Vector of Number`

Compute `power(e,exp)`

for each element `e`

in vector `v`

, `v .^ exp`

`elemtimes(Vector v,Vector w)->Vector of Number`

Multiply vectrors `v`

and `w`

element by element, `v .* w`

`euclid(Vector v,Vector w)->Real`

Euclidean distance between vectors `v`

and `w`

`fft(Vector v)->Vector of Complex`

Full FFT over a vector

`geodist(Vector v,Vector w)->Real`

The surface distance in meters between geographic
position vectors `[latitude, longitude]`

, `v`

and `w`

`ifft(Vector v)->Vector of Real`

Inverse full FFT

`integer_vector(Vector of Real v)->Vector of Integer`

`irfft(Vector v)->Vector of Real`

`manhattan(Vector of Number v,Vector of Number w)->Number`

Manhattan distance between vectors `v`

and `w`

`max(Vector v)->Object`

The largest element in vector `v`

`maxmin(Bag of Vector of Number b)->Vector of Number`

Transform to [0, 1]: Subtract min, divide by (max - min)

`maxnorm(Vector of Number v,Vector of Number w)->Real`

Maxnorm distance between vectors `v`

and `w`

`mean(Vector v)->Real`

Average of vector of numbers `v`

`meansub(Bag of Vector of Number b)->Vector of Number`

Transform to N(0, s): Subtract mean(v)

`min(Vector v)->Object`

The smallest element in vector `v`

`minkowski(Vector of Number v,Vector of Number w,Number r)->Real`

Minkowski distance of degree `r`

between vectors `v`

and `w`

`minus(Vector v,Vector w)->Vector of Number r`

Subtract elements in vectors `v`

and `w`

, `v .- w`

`minus(Vector v,Number lambda)->Vector of Number`

Subtract `lambda`

from each element in vector `v`

, `v - lambda`

`minus(Number lambda,Vector w)->Vector of Number`

Subtract `lambda`

from each element in vector `w`

, `lambda - w`

`not_null(Vector v)->Boolean`

Are all elements in vector `v`

not null?

`numvector(Vector x)->Vector of Number`

Cast `x`

to vector of numbers

`ones(Number dim)->Vector of Integer`

Construct vector of `dim`

1:s

`permute(Vector v,Vector of Integer indl)->Vector`

Reorder vector `v`

on index positions in `indl`

`plus(Vector v,Vector w)->Vector of Number r`

Add elements in vectors `v`

and `w`

, `v .+ w`

`plus(Number lambda,Vector w)->Vector of Number`

Add `lambda`

to each element in vector `w`

, `lambda + w`

`plus(Vector v,Number lambda)->Vector of Number r`

Add `lambda`

to each element in vector `v`

, `v + lambda`

`rfft(Vector v)->Vector of Real`

RFFT over vectors of numbers

`section(Vector v,Number l,Number u)->Vector r`

The subvector of vector `v`

starting at position `p`

and ending at `u`

`setf(Vector v,Integer i,Object o)->Boolean`

`skip(Vector v,Number n)->Vector`

Skip first `n`

elements in vector `v`

`stdev(Vector v)->Real`

Standard deviation of vector of numbers `v`

`sum(Vector v)->Number`

The sum of the numbers in vector `v`

`times(Vector v,Vector w)->Number`

Scalar product of vectors `v`

and `w`

, `v * W`

`times(Vector v,Number lambda)->Vector of Number`

Multiply elements in vector `v`

with `lambda`

, `v * lambda`

`times(Number lambda,Vector w)->Vector of Number`

Multiply `lambda`

with elements in `w`

, `lambda * w`

`times(Vector of Number v,Matrix m)->Vector of Number`

Vector-matrix multiplication, `v * m`

`times(Matrix a,Matrix b)->Matrix _v1`

Matrix multiplication, `a * b`

`times(Matrix m,Vector of Number w)->Vector of Number`

Matrix-vector multiplication, `m * v`

`transpose(Matrix m)->Matrix _v1`

Transpose matrix `m`

`uminus(Vector v)->Vector of Number`

Negate numbers in vector `v`

, `-v`

`vdiff(Vector v,Vector w)->Vector`

Elements in vector `v`

that are not in vector `w`

`vectorize(Bag b)->Vector v`

`vmean(Bag of Vector of Number bv)->Vector of Number`

Mean vector for a given bag of vectors `bv`

`vref(Vector v,Number i)->Object`

Value of element `i`

in vector `v`

, same as `v[i]`

`whennull(Vector v,Object dflt)->Vector`

Value 'dflt' for nulls in 'v'

`whenzero(Vector v,Object dflt)->Vector`

Value 'dflt' for zeros in 'v'

`zeros(Number dim)->Vector of Integer`

Construct vector of `dim`

0:s

`zscore(Bag of Vector of Number b)->Bag of Vector of Number`

Transform elements in the vectors in `b`

into normal distributions
centered around zero