# Vectors

The order of the objects in the bag returned by a regular set
query is
*not* predetermined unless an order
by clause is
specified. However, even if `order by`

is specified the system will
not preserve the order of the result of a set query if it is inside
in other operations.

If it is required to maintain the order of a set of data values the
data type *Vector* has to be used. The collection data type *Vector*
represents ordered collections of any kinds of objects; a common case
of a vector is a numerical vector of numbers. In case the order of a
query result is significant you can specify *vector queries* that
preserve the order in a query result by returning vectors rather than
the bags returned by regular set
queries. This is
particularly important when working with numerical vectors. A vector
query can be one of the following:

It can be a

**vector construction**expression that creates a new vector from other objects.It can be a call to

**vector function**returning vectors as result.It can be a

**vector indexing**expression that accesses vector elements by their indexes.It can be a

**select vector query**that returns an ordered vector rather than an unordered bag as the regular set query.It can be a regular

**set query**returning a set of constructed vectors.

## Vector constructionâ€‹

The vector constructor `[...]`

notation creates a single vector with
explicit contents. The following query constructs a vector of three
numbers:

`[1,sqrt(4),3]`

The following query constructs a bag of vectors holding the natural numbers up to ten together with their square roots:

`select [n,sqrt(n)]`

from Integer n

where n in range(10)

## Vector functionsâ€‹

Functions over vectors are divided into:

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

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

*Number and vector addition:*

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

[1,2,3]+5

*Add nested vector elements:*

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

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

**Vector subtraction:**

*Subtract vector elements:*

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

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

*Number and vector subtraction:*

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

[1,2,3]-5

**Vector division:**

*Number and vector division:*

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

3/[1,2,3]

*Element division:*

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

*Nested element division:*

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

**Multiply vector elements:**

*Scalar product:*

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

*Number and vector multiplication:*

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

5*[1,2,3]

*Element multiplication:*

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

*Nested element multiplication:*

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

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

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

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

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

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

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

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

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

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

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

### 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'))

**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)))

### Sequence functionsâ€‹

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

returns a *splice* of a vector `v`

, e.g.:

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

The function `skip(v,n)`

returns the part of vector `v`

starting at position `n+1`

, e.g.:

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

The function `permute(v,indl)`

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

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

## The select vector queryâ€‹

A *select vector* query provides a powerful way to construct new
vectors by queries. It has the same syntax as a set
query except for the
keywords `Vector of`

following the `select`

clause. The difference is
that whereas a set query returns a bag of objects, a *select vector*
query returns a single vector of objects.

Example:

`select Vector of i*2`

from Integer i

where i in [1,2,3]

order by i

Notice that the `order by`

clause normally should be present when
constructing vectors with a select vector
query in order to exactly specify the order
of the elements in the vector. If no `order by`

clause is present the
order of the elements in the vector is arbitrarily chosen by the
system being the order that is the most efficient to produce.

The built-in function `range()`

is often used constructing
vectors.

For example:

`select Vector of i`

from Integer i

where i in range(-4,5)

order by i

**Vector functions** and operators
can be used in queries.

For example:

`select lambda`

from Number lambda

where [1, 2] - lambda = [11, 12]

If the equation has no solution, the query will have no result, for example:

`select lambda`

from Integer lambda

where [1, 3] - lambda = [11, 12]

By contrast, this query returns the vector `[-10,-10]`

`select lambda`

from Vector of Integer lambda

where [1, 2] - lambda = [11, 12]

returns `[-10,-10]`

.

## Accessing vector elementsâ€‹

Vector elements can be accessed using the `[..]`

syntax. The first
element in a vector has index 1.

Example:

`select a[1] + a[2]`

from Vector of Integer a

where a = [1,2,3]

Index variables as integers can be used in queries to specify iteration over all possibles index positions of a vector.

Example:

`select Vector of a[i]`

from Vector a, Integer i

where a[i] != 2

and a = [1,2,3]

order by i

The query should be read as "Make a vector v of all vector elements
*a _{i}* in

*a*where

*a*is not two and order the elements in

_{i}*v*on index

*i*."

The following query multiplies the elements of the vectors bound to the session variables `:u`

and `:v`

:

`set :u = [1,2,3];`

set :v = [4,5,6];

select Vector of :u[i] * :v[i]

from Integer i

order by i