Last updated: 2021-08-03

Checks: 7 0

Knit directory: VSA_altitude_hold/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210617) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 6a83009. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    renv/library/
    Ignored:    renv/local/
    Ignored:    renv/staging/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/vsa_basic_operators.Rmd) and HTML (docs/vsa_basic_operators.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 6a83009 Ross Gayler 2021-08-03 WIP
html 6a83009 Ross Gayler 2021-08-03 WIP
Rmd ebbb063 Ross Gayler 2021-08-02 WIP
html ebbb063 Ross Gayler 2021-08-02 WIP
Rmd 4ab6a03 Ross Gayler 2021-08-01 WIP
html 4ab6a03 Ross Gayler 2021-08-01 WIP
Rmd 2e6b9a9 Ross Gayler 2021-07-31 WIP
html 2e6b9a9 Ross Gayler 2021-07-31 WIP
Rmd a55717a Ross Gayler 2021-07-31 WIP
html a55717a Ross Gayler 2021-07-31 WIP
html dcdf309 Ross Gayler 2021-07-20 WIP
html 5f5c544 Ross Gayler 2021-07-20 WIP
html 3f691b8 Ross Gayler 2021-07-20 WIP
html b2c3085 Ross Gayler 2021-07-13 WIP
Rmd b57b029 Ross Gayler 2021-07-13 WIP
html b57b029 Ross Gayler 2021-07-13 WIP
Rmd 2fd00dc Ross Gayler 2021-07-13 WIP
html 2fd00dc Ross Gayler 2021-07-13 WIP
Rmd eccb5b1 Ross Gayler 2021-07-11 WIP
html f7a9155 Ross Gayler 2021-07-11 WIP
Rmd e040157 Ross Gayler 2021-07-11 WIP

This notebook …

The reasoning behind the design choices is explained in XXX.

1 Generate atomic vectors

For this project we are using bipolar vectors (\(V \in \{-1, +1\}^D\)).

The vectors will be dense. That is, there will be no zero elements. We will not be investigating the effect of sparsity in this project.

Define a function to create a randomly selected bipolar VSA vector.

# function to make an atomic VSA vector

vsa_mk_atom_bipolar <- function(
  vsa_dim, # integer - dimensionality of VSA vector
  seed = NULL # integer - seed for random number generator
) # value # one randomly selected VSA vector of dimension vsa_dim
{  
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  if(missing(vsa_dim))
    stop("vsa_dim must be specified")
  
  if(!(is.vector(vsa_dim, mode = "integer") && length(vsa_dim) == 1))
    stop("vsa_dim must be an integer")
  
  if(vsa_dim < 1)
    stop("vsa_dim must be (much) greater than zero")
  
  # check that the specified seed is an integer
  if(!is.null(seed) &&!(is.vector(seed, mode = "integer") && length(seed) == 1))
    stop("seed must be an integer")
  
  # if seed is set the the vector is fixed
  # otherwise it is randomised
  set.seed(seed)
  
  # Construct a random bipolar vector
  sample(c(-1L, 1L), size = vsa_dim, replace = TRUE)
}

The seed argument allows for a reproducible random selection.

The vector elements are integers rather than floats. This halves the required storage space and is a minor gesture towards optimisation. Much greater optimisation could be probably be achieved by using bit strings rather than integers, but that’s not worth the effort at this stage.

Where possible all other operations will be defined to accept floats and integers to allow moving away from a strictly bipolar representation if necessary.

Do some very small scale testing.

v1 <- vsa_mk_atom_bipolar(10L)
v2 <- vsa_mk_atom_bipolar(10L)

v1
 [1] -1  1  1 -1  1 -1  1 -1  1 -1
v2
 [1]  1 -1  1 -1 -1  1  1  1 -1  1
all(v1 == v2)
[1] FALSE
  • Multiple calls generate different vectors.
v1 <- vsa_mk_atom_bipolar(10L, seed = 1L)
v2 <- vsa_mk_atom_bipolar(10L, seed = 1L)

v1
 [1] -1  1 -1 -1  1 -1 -1 -1  1  1
v2
 [1] -1  1 -1 -1  1 -1 -1 -1  1  1
all(v1 == v2)
[1] TRUE
  • Setting the seed to the same value generates the same vector.

2 Vector measures

We really only need the cosine similarity of two vectors. However, define functions for the components of the cosine in case they are useful.

These functions will work for any vectors. They do not have to be bipolar vectors.

2.1 Vector magnitude

Define a function to calculate the of a VSA vector.

# function to calculate the magnitude of a VSA vector
# Allow for the possibility that the vector might not be bipolar

vsa_mag <- function(
  v1 # numeric - VSA vector (not necessarily bipolar)
) # value # numeric - magnitude of the VSA vector
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(v1)) 
    stop("VSA vector argument (v1) must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be an numeric vector")
  
  # No numerical analysis considerations 
  sqrt(sum(v1*v1))
}

I have not taken any numerical analysis considerations into account, so don’t hold any strong expectations for accuracy in extreme cases.

Do some very small scale testing.

vsa_mag(0)
[1] 0
vsa_mag(1)
[1] 1
vsa_mag(2)
[1] 2
vsa_mag(-2)
[1] 2
  • The magnitude of a scalar is its absolute value.
vsa_mk_atom_bipolar(9L) %>% vsa_mag()
[1] 3
vsa_mk_atom_bipolar(100L) %>% vsa_mag()
[1] 10
vsa_mk_atom_bipolar(1e4L) %>% vsa_mag()
[1] 100
vsa_mk_atom_bipolar(1e8L) %>% vsa_mag()
[1] 10000
  • The magnitude of a bipolar vector is the square root of its dimensionality.

As the vector dimensionality is increased the operations take longer to execute and eventually something will break, e.g. you will run out of RAM or the dimensionality will be too large to be represented as an integer.

(vsa_mk_atom_bipolar(100L) * 1L) %>% vsa_mag()
[1] 10
(vsa_mk_atom_bipolar(100L) * 1.3) %>% vsa_mag()
[1] 13
(vsa_mk_atom_bipolar(100L) * -5) %>% vsa_mag()
[1] 50
  • Rescaling the vector is equivalent to rescaling its magnitude (\(\Vert kV \Vert = \vert k \vert \Vert V \Vert\)).

2.2 Vector dot product

Define a function to calculate the dot product of two VSA vectors.

# function to calculate the dot product  of two VSA vectors
# Allow for the possibility that the vectors might not be bipolar

vsa_dotprod <- function(
  v1, v2 # numeric - VSA vectors of identical dimension (not necessarily bipolar)
) # value # numeric - cosine similarity of the VSA vectors
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(v1) || missing(v2)) 
    stop("two VSA vector arguments (v1, v2) must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be a numeric vector")
  
  if(!is.vector(v2, mode = "numeric"))
    stop("v2 must be a numeric vector")
  
  vsa_dim <- length(v1)
  
  if(length(v2) != vsa_dim)
    stop("v1 and v2 must be the same length")
  
  # No numerical analysis considerations 
  sum(v1*v2)
}

I have not taken any numerical analysis considerations into account, so don’t hold any strong expectations for accuracy in extreme cases.

Do some very small scale testing.

vsa_dotprod(1, 1)
[1] 1
vsa_dotprod(1, 0)
[1] 0
vsa_dotprod(1, 3)
[1] 3
vsa_dotprod(2, 3)
[1] 6
vsa_dotprod(-2, 3)
[1] -6
vsa_dotprod(-2, -3)
[1] 6
  • The dot product of two scalars is their product.
vsa_dotprod(vsa_mk_atom_bipolar(9L, seed = 42L), vsa_mk_atom_bipolar(9L, seed = 42L))
[1] 9
vsa_dotprod(vsa_mk_atom_bipolar(100L, seed = 43L), vsa_mk_atom_bipolar(100L, seed = 43L))
[1] 100
vsa_dotprod(vsa_mk_atom_bipolar(1e4L, seed = 44L), vsa_mk_atom_bipolar(1e4L, seed = 44L))
[1] 10000
vsa_dotprod(vsa_mk_atom_bipolar(1e8L, seed = 45L), vsa_mk_atom_bipolar(1e8L, seed = 45L))
[1] 100000000
  • The dot product of a vector with itself is equal to the square of its magnitude.
  • The dot product of a bipolar vector with itself is equal to its dimensionality.
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] -36
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 192
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 72
vsa_dotprod(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] -106
  • The dot product of two randomly selected high dimensional vectors is approximately zero (relative to the dimensionality).

2.3 Vector cosine similarity

Define a function to calculate the cosine of the angle between two VSA vectors.

# function to calculate the cosine similarity  of two VSA vectors
# Allow for the possibility that the vectors might not be bipolar

vsa_cos_sim <- function(
  v1, v2 # numeric - VSA vectors of identical dimension (not necessarily bipolar)
) # value # numeric - cosine similarity of the VSA vectors
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(v1) || missing(v2)) 
    stop("two VSA vector arguments must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be an numeric vector")
  
  if(!is.vector(v2, mode = "numeric"))
    stop("v2 must be an numeric vector")
  
  vsa_dim <- length(v1)
  
  if(length(v2) != vsa_dim)
    stop("v1 and v2 must be the same length")
  
  vsa_dotprod(v1, v2) / (vsa_mag(v1) * vsa_mag(v2))
}

I have not taken any numerical analysis considerations into account, so don’t hold any strong expectations for accuracy in extreme cases.

Do some very small scale testing.

Don’t bother looking at pairs of scalars, because although the cosine of the angle between them is defined it is not interesting. (The vectors are parallel by definition.)

vsa_cos_sim(vsa_mk_atom_bipolar(9L, seed = 42L), vsa_mk_atom_bipolar(9L, seed = 42L))
[1] 1
vsa_cos_sim(vsa_mk_atom_bipolar(100L, seed = 43L), vsa_mk_atom_bipolar(100L, seed = 43L))
[1] 1
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L, seed = 44L), vsa_mk_atom_bipolar(1e4L, seed = 44L))
[1] 1
vsa_cos_sim(vsa_mk_atom_bipolar(1e8L, seed = 45L), vsa_mk_atom_bipolar(1e8L, seed = 45L))
[1] 1
  • The cosine of the angle of a vector with itself is one. (The vectors are coincident.)
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L, seed = 42L), 1 * vsa_mk_atom_bipolar(1e4L, seed = 42L))
[1] 1
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L, seed = 43L), 2 * vsa_mk_atom_bipolar(1e4L, seed = 43L))
[1] 1
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L, seed = 44L), -2 * vsa_mk_atom_bipolar(1e4L, seed = 44L))
[1] -1
  • Scaling has no effect on the direction of a vector
  • The sign of scaling does effect the sign of the direction of a vector.
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] -0.0028
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 0.0104
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 0.0106
vsa_cos_sim(vsa_mk_atom_bipolar(1e4L), vsa_mk_atom_bipolar(1e4L))
[1] 6e-04
  • The dot product of two randomly selected high dimensional vectors is approximately zero.

3 Negate vector

Define a function to calculate the negation of a VSA vector. This simply reverses the direction of the vector.

# Function to calculate the negation of a VSA vector
# (Reverse the direction of the vector)
# Allow for the possibility that the vector might not be bipolar

vsa_negate <- function(
  v1 # numeric - VSA vector (not necessarily bipolar)
) # value # negation of input VSA vector
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(v1)) 
    stop("VSA vector argument (v1) must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be an numeric vector")
  
  -v1
}

This function will work for any vector. It does not have to be a bipolar vector.

Do some very small scale testing.

# make some example vectors
v1 <- vsa_mk_atom_bipolar(1e4L)
v2 <- vsa_mk_atom_bipolar(1e4L)

vsa_mag(v1) == (vsa_negate(v1) %>% vsa_mag())
[1] TRUE
vsa_mag(v2) == (vsa_negate(v2) %>% vsa_mag())
[1] TRUE
  • Negation does not change the magnitude of a vector.
vsa_cos_sim(v1, vsa_negate(v1))
[1] -1
vsa_cos_sim(v2, vsa_negate(v2))
[1] -1
vsa_cos_sim(v1, v2)
[1] -0.0026
vsa_cos_sim(v1, vsa_negate(v2))
[1] 0.0026
  • Negation reverses the direction of a vector.

4 Permute vector

Define function to permute a vector.

These will be functions to:

  • Randomly generate the specification of a permutation
  • Generate the specification for the inverse of a given permutation
  • Apply a permutation to a VSA vector

4.1 Generate permutation

Define a function to randomly generate the specification of a permutation.

# function to make a permutation

vsa_mk_perm <- function(
  vsa_dim, # integer - dimensionality of VSA vector
  seed = NULL # integer - seed for random number generator
) # value # one randomly generated permutation specification
  # this is an integer vector of length vsa_dim
{  
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  if(missing(vsa_dim))
    stop("vsa_dim must be specified")
  
  if(!(is.vector(vsa_dim, mode = "integer") && length(vsa_dim) == 1))
    stop("vsa_dim must be an integer")
  
  if(vsa_dim < 1)
    stop("vsa_dim must be (much) greater than zero")
  
  # check that the specified seed is an integer
  if(!is.null(seed) &&!(is.vector(seed, mode = "integer") && length(seed) == 1))
    stop("seed must be an integer")
  
  # if seed is set the the vector is fixed
  # otherwise it is randomised
  set.seed(seed)
  
  # Construct a random permutation of 1:vsa_dim
  sample.int(vsa_dim)
}

The seed argument allows for a reproducible random selection.

The returned value is a vector of integers of length vsa_dim. This is a random permutation of the integers 1 : vsa_dim. When applied as a permutation each element of the specification indicates the input element to be copied to that position in the output.

Do some very small scale testing.

p1 <- vsa_mk_perm(10L, seed = 1L)
p2 <- vsa_mk_perm(10L, seed = 1L)

p1
 [1]  9  4  7  1  2  5  3 10  6  8
p2
 [1]  9  4  7  1  2  5  3 10  6  8
all(p1 == p2)
[1] TRUE
  • Setting the seed to the same value generates the same permutation.
p1 <- vsa_mk_perm(10L)
p2 <- vsa_mk_perm(10L)

p1
 [1] 10  3  9  1  5  7  2  8  4  6
p2
 [1]  9  1  5  2  7  8  6 10  4  3
all(p1 == p2)
[1] FALSE
  • Multiple calls generate different permutations

4.2 Generate inverse permutation

Define a function to generate the inverse of a given permutation.

# function to make a permutation

vsa_mk_inv_perm <- function(
  perm # integer vector - specification of a permutation
) # value # integer vector [length(perm)] - specification of inverse permutation
{  
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  if(missing(perm))
    stop("perm must be specified")
  
  if(!is.vector(perm, mode = "integer"))
    stop("perm must be an integer vector")
  
  if(!all(sort(perm) == 1:length(perm)))
    stop("perm must be a permutation of 1:length(perm)")
  
  # Invert the permutation
  Matrix::invPerm(perm)
}

The returned value is a vector of integers of length length(perm). This is a permutation of the integers 1 : length(perm). When applied as a permutation each element of the specification indicates the input element to be copied to that position in the output.

Do some very small scale testing.

# make some example permutation specifications
p_identity <- 1L:10L
p_reverse <- seq(10L, 1L, -1L)
p_left1 <- c(p_identity[-1], p_identity[1])
p_right1 <- c(p_identity[10], p_identity[-10])
p_random <-vsa_mk_perm(10L)

p_identity
 [1]  1  2  3  4  5  6  7  8  9 10
vsa_mk_inv_perm(p_identity)
 [1]  1  2  3  4  5  6  7  8  9 10
  • The identity permutation is its own inverse.
p_reverse
 [1] 10  9  8  7  6  5  4  3  2  1
vsa_mk_inv_perm(p_reverse)
 [1] 10  9  8  7  6  5  4  3  2  1
vsa_mk_inv_perm(p_reverse) %>% vsa_mk_inv_perm()
 [1] 10  9  8  7  6  5  4  3  2  1
  • The reverse permutation is its own inverse.
p_left1
 [1]  2  3  4  5  6  7  8  9 10  1
vsa_mk_inv_perm(p_left1)
 [1] 10  1  2  3  4  5  6  7  8  9
p_right1
 [1] 10  1  2  3  4  5  6  7  8  9
vsa_mk_inv_perm(p_left1) %>% vsa_mk_inv_perm()
 [1]  2  3  4  5  6  7  8  9 10  1
  • The inverse of left cyclic shift is right cyclic shift.
  • The inverse of the inverse transform is the identity transform
p_random
 [1]  7  4  2  1  6  8  5 10  9  3
vsa_mk_inv_perm(p_random)
 [1]  4  3 10  2  7  5  1  6  9  8
vsa_mk_inv_perm(p_random) %>% vsa_mk_inv_perm()
 [1]  7  4  2  1  6  8  5 10  9  3
  • The inverse of the inverse transform is the identity transform

4.3 Apply permutation

Define a function to apply the given permutation to a VSA vector.

# function to apply the specified permutation to the VSA vector

vsa_permute <- function(
  v1, # numeric - VSA vector (not necessarily bipolar)
  perm # integer vector - specification of a permutation
) # value # permutation of input VSA vector
{  
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(v1)) 
    stop("VSA vector argument (v1) must be specified")
  
  if(!is.vector(v1, mode = "numeric"))
    stop("v1 must be an numeric vector")
  
  if(missing(perm))
    stop("perm must be specified")
  
  if(!is.vector(perm, mode = "integer"))
    stop("perm must be an integer vector")
  
  if(!all(sort(perm) == 1:length(perm)))
    stop("perm must be a permutation of 1:length(perm)")
  
  # apply the permutation
  v1[perm]
}

The returned value is the input VSA vector permuted according to the specified permutation.

Do some very small scale testing.

# make some example VSA vectors
v1 <- 1L:10L
v2 <- 101L:110L

v1
 [1]  1  2  3  4  5  6  7  8  9 10
v1 %>% vsa_permute(p_identity)
 [1]  1  2  3  4  5  6  7  8  9 10
v2
 [1] 101 102 103 104 105 106 107 108 109 110
v2 %>% vsa_permute(p_identity)
 [1] 101 102 103 104 105 106 107 108 109 110
  • The identity permutation leaves the VSA vectors unchanged.
v1
 [1]  1  2  3  4  5  6  7  8  9 10
v1 %>% vsa_permute(p_reverse)
 [1] 10  9  8  7  6  5  4  3  2  1
v2
 [1] 101 102 103 104 105 106 107 108 109 110
v2 %>% vsa_permute(p_reverse)
 [1] 110 109 108 107 106 105 104 103 102 101
  • The reverse permutation reverses the VSA vectors.
v1
 [1]  1  2  3  4  5  6  7  8  9 10
v1 %>% vsa_permute(p_left1)
 [1]  2  3  4  5  6  7  8  9 10  1
v2
 [1] 101 102 103 104 105 106 107 108 109 110
v2 %>% vsa_permute(p_left1)
 [1] 102 103 104 105 106 107 108 109 110 101
  • The left cyclic shift permutation performs a left cyclic shift of the VSA vectors.
v1
 [1]  1  2  3  4  5  6  7  8  9 10
v1 %>% vsa_permute(vsa_mk_inv_perm(p_left1))
 [1] 10  1  2  3  4  5  6  7  8  9
v2
 [1] 101 102 103 104 105 106 107 108 109 110
v2 %>% vsa_permute(vsa_mk_inv_perm(p_left1))
 [1] 110 101 102 103 104 105 106 107 108 109
  • The inverse of the left cyclic shift permutation performs a right cyclic shift of the VSA vectors.
v1
 [1]  1  2  3  4  5  6  7  8  9 10
v1 %>% vsa_permute(p_random)
 [1]  7  4  2  1  6  8  5 10  9  3
v1 %>% vsa_permute(p_random) %>% vsa_permute(vsa_mk_inv_perm(p_random))
 [1]  1  2  3  4  5  6  7  8  9 10
  • The the composition of a random permutation and its inverse are equivalent to the identity permutation.

5 Multiply vectors

Define a function to multiply an arbitrary number of VSA vectors.

# function to multiply an arbitrary number of VSA vectors

vsa_multiply <- function(
  ... # >= 2 VSA vectors of identical dimension as arguments to multiply
) # value # one VSA vector, the weighted sum (sampled) of the argument vectors
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  args_list <- list(...)
  args_n <- length(args_list)
  
  if(args_n < 2) 
    stop("number of source VSA vector arguments must be >= 2")
  
  if(!all(sapply(args_list, is.vector, mode = "numeric")))
    stop("all source VSA vectors must be numeric vectors")
  
  vsa_dim <- length(args_list[[1]])
  
  if(!all(sapply(args_list, length) == vsa_dim))
    stop("all source VSA vectors must be the same length")
  
  ### Construct the result vector
  # as.data.frame(args_list)
  purrr::reduce(args_list, `*`)
}

Do some very small scale testing.

v1 <- 1:5
v2 <- 2:6

v1
[1] 1 2 3 4 5
v2
[1] 2 3 4 5 6
vsa_multiply(v1, v1)
[1]  1  4  9 16 25
vsa_multiply(v1, v1, v2)
[1]   2  12  36  80 150
  • Multiplication is the element-wise product of the argument vectors.
vsa_multiply(1, 1)
[1] 1
vsa_multiply(1, -1)
[1] -1
vsa_multiply(-1, -1)
[1] 1
  • Signs work as expected.

6 Add vectors

Vector addition in this project is performed by randomly sampling elements from each of the source VSA vectors. The sampling specification (the VSA vector argument to use as the source for each element of the output VSA vector) can be randomised per applicatioon of the addition function or can be set to a constant across applications of the addition function.

6.1 Generate sampling specification

Define a function to generate a sampling specification for addition of an arbitrary number of VSA vectors.

The VSA vector dimensionality and the number of vectors to be added must be specified.

The sampling weights correspond to the VSA vector arguments to the addition. The sampling weights must be non-negative and are normalised to sum to one so that the can be treated as probabilities.

# function to make a sampling specification for adding VSA vectors

vsa_mk_sample_spec <- function(
  vsa_dim, # integer - dimensionality of VSA vectors
  sample_wt, # numeric vector - VSA vector sampling weights
  seed = NULL # integer - seed for random number generator
) # value # one VSA vector, the weighted sum (sampled) of the argument vectors
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  if(missing(vsa_dim))
    stop("vsa_dim must be specified")
  
  if(!(is.vector(vsa_dim, mode = "integer") && length(vsa_dim) == 1))
    stop("vsa_dim must be an integer")
  
  if(vsa_dim < 1)
    stop("vsa_dim must be (much) greater than zero")
  
  if(!is.vector(sample_wt, mode = "numeric"))
    stop("sample_wt must be a numeric vector")
  
  if(length(sample_wt) < 2)
    stop("length(sample_wt) (the number of VSA vectors to be added) must be >= 2")
  
  if(min(sample_wt) < 0)
    stop("all weights must be >= 0")
  
  if(max(sample_wt) <= 0)
    stop("at least one weight must be > 0")
  
  # check that the specified seed is an integer
  if(!is.null(seed) && !(is.vector(seed, mode = "integer") && length(seed) == 1))
    stop("seed must be an integer")
  
  # if seed is set the sampling specification vector is fixed
  # otherwise it is randomised
  set.seed(seed)
  
  # For each element of the VSA vectors work out which source VSA vector to sample
  sample.int(n = length(sample_wt), size = vsa_dim, replace = TRUE, prob = sample_wt)
}

Do some very small scale testing.

vsa_mk_sample_spec(vsa_dim = 10L, sample_wt = c(1, 1, 1), seed = 42L)
 [1] 1 1 2 1 3 3 1 2 3 1
vsa_mk_sample_spec(vsa_dim = 10L, sample_wt = c(1, 1, 1), seed = 42L)
 [1] 1 1 2 1 3 3 1 2 3 1
  • Setting the seed makes the sampling sepcification reproducible.
vsa_mk_sample_spec(vsa_dim = 10L, sample_wt = c(1, 0))
 [1] 1 1 1 1 1 1 1 1 1 1
vsa_mk_sample_spec(vsa_dim = 10L, sample_wt = c(0, 1, 1))
 [1] 3 2 2 2 3 3 2 3 2 3
vsa_mk_sample_spec(vsa_dim = 10L, sample_wt = c(1, 3))
 [1] 1 2 2 2 2 2 2 2 2 1
  • The sample weights are converted to probabilities of selection.

6.2 Apply sampling specification

Define a function to calculate the weighted sum of an arbitrary number of VSA vectors.

# function to add (weighted sum) an arbitrary number of VSA vectors
# Weighted add is implemented as weighted sampling from the source vectors
# If sample_spec is given it specifies which argument vector is the source for each element of the output vector
# If sample_wt is given the sample specification is generated randomly
# If neither sample_spec or sample_wt is given then sample_wt is constructed with equal weight for each argument vector

vsa_add <- function(
  ..., # >= 2 VSA vectors of identical dimension as arguments to add
  sample_spec, # integer vector - source (argument VSA vector) for each element of result
  sample_wt # numeric vector - argument vector sampling weights
) # value # one VSA vector, the weighted sum (sampled) of the argument vectors
{
  ### Set up the arguments ###
  # The OCD error checking is probably more useful as documentation
  
  args_list <- list(...)
  args_n <- length(args_list)
  
  if(args_n < 2) 
    stop("number of source VSA vector arguments must be >= 2")
  
  if(!all(sapply(args_list, is.vector, mode = "numeric")))
    stop("all source VSA vectors must be numeric vectors")
  
  vsa_dim <- length(args_list[[1]])
  
  if(!all(sapply(args_list, length) == vsa_dim))
    stop("all source VSA vectors must be the same length")
  
  if(!missing(sample_spec) && !missing(sample_wt))
    stop("at most one of wt and sample_spec can be given")
  
  if(!missing(sample_spec))
    # sample_spec supplied
  {
    if(!is.vector(sample_spec, mode = "integer"))
      stop("sample_spec must be an integer vector")
    
    if(length(sample_spec) != vsa_dim)
      stop("sample_spec must be same length as source VSA vectors")
    
    if(!all(sample_spec %in% 1:args_n))
      stop("each element of sample_spec must be the index of a source VSA vector")
  }
  else
    # sample spec not supplied - make a new random one
  {
    # create a sampling weight vector if not supplied
    if(missing(sample_wt))
      sample_wt <- rep(1, length.out = args_n) # equal weighting for all source VSA vectors
    
    if(length(sample_wt) != args_n)
      stop("number of weights must equal number of source VSA vectors")
    
    if(min(sample_wt) < 0)
      stop("all weights must be >= 0")
    
    if(max(sample_wt) <= 0)
      stop("at least one weight must be > 0")
    
    # For each element of the result work out which source VSA vector to sample
    # sample_spec <- sample.int(n = args_n, size = vsa_dim,
    #                           replace = TRUE, prob = sample_wt)
    sample_spec <- vsa_mk_sample_spec(vsa_dim, sample_wt)
  }
  
  ### Set up the selection matrix ###
  # Each row corresponds to an element of the output vector
  # Each row specifies the (row,col) cell to select from the VSA source vectors
  sel <- as.matrix(data.frame(row = 1L:vsa_dim, col = sample_spec),
                   ncol = 2, byrow = FALSE)
  
  ### Construct the result vector
  as.data.frame(args_list)[sel]
}

Do some very small scale testing.

# create vectors to add
# make unique values so they can be uniquely tracked
x1 <- 10L:19L
x2 <- 20L:29L
x3 <- 30L:39L

# specify the sampling
vsa_add(x1,x2,x3, sample_spec = c(1L,2L,3L,1L,2L,3L,1L,2L,3L,1L))
 [1] 10 21 32 13 24 35 16 27 38 19
vsa_add(x1,x2,x3, sample_spec = c(1L,2L,3L,1L,2L,3L,1L,2L,3L,1L))
 [1] 10 21 32 13 24 35 16 27 38 19
  • Sampling is fixed when sample_spec is specified.
vsa_add(x1,x2,x3, sample_wt = c(0, 0, 1))
 [1] 30 31 32 33 34 35 36 37 38 39
  • Extreme random weighting works as expected.
vsa_add(x1,x2,x3)
 [1] 10 11 32 33 14 25 16 17 18 39
vsa_add(x1,x2,x3)
 [1] 10 21 32 33 14 25 36 27 18 39
vsa_add(x1,x2,x3)
 [1] 30 21 32 13 14 15 16 37 28 19
  • Randomised sampling is different on every occasion.

sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 21.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] Matrix_1.3-4       purrr_0.3.4        dplyr_1.0.7        DiagrammeR_1.0.6.1
[5] magrittr_2.0.1     here_1.0.1         fs_1.5.0          

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7         pillar_1.6.2       compiler_4.1.0     later_1.2.0       
 [5] RColorBrewer_1.1-2 git2r_0.28.0       workflowr_1.6.2    tools_4.1.0       
 [9] digest_0.6.27      lattice_0.20-44    jsonlite_1.7.2     evaluate_0.14     
[13] lifecycle_1.0.0    tibble_3.1.3       pkgconfig_2.0.3    rlang_0.4.11      
[17] yaml_2.2.1         xfun_0.24          stringr_1.4.0      knitr_1.33        
[21] generics_0.1.0     vctrs_0.3.8        htmlwidgets_1.5.3  grid_4.1.0        
[25] tidyselect_1.1.1   rprojroot_2.0.2    glue_1.4.2         R6_2.5.0          
[29] fansi_0.5.0        rmarkdown_2.9      bookdown_0.22      whisker_0.4       
[33] promises_1.2.0.1   ellipsis_0.3.2     htmltools_0.5.1.1  renv_0.14.0       
[37] httpuv_1.6.1       utf8_1.2.2         stringi_1.7.3      visNetwork_2.0.9  
[41] crayon_1.4.1