Learn R Programming

SpatioTemporal (version 0.9.2)

dot.prod: Computes Inner Product and Squared 2-norm

Description

dot.prod computes the inner (or dot/scalar) product between two vectors.

norm2 computes the squared 2-norm of all the elements in a matrix or vector.

Usage

dot.prod(v1, v2)

norm2(v1)

Arguments

v1,v2
Two vectors

Value

  • dot.prod returns the inner product of v1 and v2. norm2 returns the squared 2-norm of all elements in v1.

encoding

latin1

Details

If the vectors are of unequal length dot.prod will give a warning and then truncates the longer vector, discarding any excess elements before the computations.

See Also

Block matrices are created by make.sigma.B, make.sigma.B.full and make.sigma.nu. Other block matrix functions makeCholBlock, block.mult, calc.tF.times.mat, calc.iS.X, and sumLogDiag. This function is called by loglike.

Examples

Run this code
##Create two vectors of equal length
v1 <- rnorm(10)
v2 <- rnorm(10)

##compute the inner product between the vectors
dot.prod(v1,v2)
##or
sum(v1*v2)

##compute the square 2-norm of v1
norm2(v1)
##or
dot.prod(v1,v1)
##or
sum(v1*v1)

##If the vectors are of unequal length the longer vector
##gets truncated (with a warning). 
dot.prod(v1,c(v2,2))
if( abs(dot.prod(v1,v2)-sum(v1*v2)) > 1e-10 ){
  stop("dot.prod: Results not equal")
}
if( (abs(norm2(v1) - dot.prod(v1,v1)) > 1e-10) ||
    (abs(norm2(v1) - sum(v1*v1)) > 1e-10) ){
  stop("norm2: Results not equal")
}

Run the code above in your browser using DataLab