Skip to main content

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. Even if order by is specified the system may not preserve the order of the result of a set query if it is inside other operations. If it is required to preserve the order of some objects the type Vector has to be used.

A vector query can be one of the following:

  1. It can be a vector construction expression that creates a new vector from other objects.

  2. It can be a call to vector function returning vectors as result.

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

  4. In case the order of a query result should be preserved you can specify a select vector query that preserve the order in a query result by returning a vectors rather an unordered bag as the regular set query.

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

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

For vectors of numbers there is a built-in library of mathematical vector functions.

Example:

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

Vectors of numbers are more efficiently represented using arrays, for which a larger library of mathematical vector and array functions are available.

The following infix operators of mathematical functions over vectors v and w are defined:

operationdescriptionimplemented as function
v + welement-wise additionplus
v .+ w-"-plus
lambda + wadd number to elementsplus
v + lambda-"-plus
v - welement-wise subtractionsminus
v .- w-"-minus
-vnegate elementsuminus
v - lambdasubtract number from elementsminus
lambda - wsubtract elements from numberminus
v * wscalar producttimes
a * bmatrix producttimes
v * bscalar productstimes
a * wscalar productstimes
v .* welement-wise multiplicationselemtimes
lambda * wmultiply elements with numbertimes
v * lambda-"-times
v ./ welement-wise divisionelemdiv
v / lambdadivide elements with numberdiv
lambda / wdivide number with elementsdiv
v .^ lambdaelement-wise exponentelempower

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('avg'))
note

Notice that the function avg() is overloaded and thefunction() has to be used to get the generic avg() 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))

Select vector queries

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 for constructing vectors.

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.

Example:

select lambda
from Number lambda
where [1, 2] - lambda = [11, 12]

If the equation has no solution, the query will have no result.

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 ai in a where ai is not two and order the elements in v on index i."

Example: 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

Functions

The system functions for constructing, accessing, and transforming vectors are documented in Vector functions.