What is R's crossproduct function?
According to the help function in R: crossprod (X,Y) = t(X)%*% Y is a faster implementation than the expression itself. It is a function of two matrices, and if you have two vectors corresponds to the dot product. @Hong-Ooi's comments explains why it is called crossproduct.
R - Compute Cross Product of Vectors (Physics)
Here is a generalized cross product:
xprod <- function(...) {
args <- list(...)
# Check for valid arguments
if (length(args) == 0) {
stop("No data supplied")
}
len <- unique(sapply(args, FUN=length))
if (length(len) > 1) {
stop("All vectors must be the same length")
}
if (len != length(args) + 1) {
stop("Must supply N-1 vectors of length N")
}
# Compute generalized cross product by taking the determinant of sub-matricies
m <- do.call(rbind, args)
sapply(seq(len),
FUN=function(i) {
det(m[,-i,drop=FALSE]) * (-1)^(i+1)
})
}
For your example:
> xprod(1:3, 4:6)
[1] -3 6 -3
This works for any dimension:
> xprod(c(0,1)) # 2d
[1] 1 0
> xprod(c(1,0,0), c(0,1,0)) # 3d
[1] 0 0 1
> xprod(c(1,0,0,0), c(0,1,0,0), c(0,0,1,0)) # 4d
[1] 0 0 0 -1
See https://en.wikipedia.org/wiki/Cross_product
R cross product matrix
Your desired matrix is the correlation matrix of myTab
without centering the variables. Hence, we may obtain it as follows:
crossprod(myTab %*% diag(1 / sqrt(colSums(myTab^2))))
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 1.0000000 0.4082483 0.0000000 0.3333333 0.0 0.5773503
# [2,] 0.4082483 1.0000000 0.0000000 0.4082483 0.0 0.3535534
# [3,] 0.0000000 0.0000000 1.0000000 0.5773503 0.0 0.0000000
# [4,] 0.3333333 0.4082483 0.5773503 1.0000000 0.0 0.5773503
# [5,] 0.0000000 0.0000000 0.0000000 0.0000000 1.0 0.5000000
# [6,] 0.5773503 0.3535534 0.0000000 0.5773503 0.5 1.0000000
Perform 'cross product' of two vectors, but with addition
expand.grid
can answer your second question:
expand.grid(1:6,1:6)
expand.grid(1:6,1:6,1:4)
Add labels to calculated cross product matrix in R
We can use dimnames
to assign the rownames and columnames. In this case, there is only colnames
of 'myTab' is needed as dimnames
dimnames(CProd.Matrix) <-rep(list(colnames(myTab)), 2)
CProd.Matrix
# 111 112 113 114 115 116
#111 1.0000000 0.4082483 0.0000000 0.3333333 0.0 0.5773503
#112 0.4082483 1.0000000 0.0000000 0.4082483 0.0 0.3535534
#113 0.0000000 0.0000000 1.0000000 0.5773503 0.0 0.0000000
#114 0.3333333 0.4082483 0.5773503 1.0000000 0.0 0.5773503
#115 0.0000000 0.0000000 0.0000000 0.0000000 1.0 0.5000000
#116 0.5773503 0.3535534 0.0000000 0.5773503 0.5 1.0000000
Now, based on the rownames, it can be subsetted
CProd.Matrix ["111",, drop = FALSE]
# 111 112 113 114 115 116
#111 1 0.4082483 0 0.3333333 0 0.5773503
sort the values after subsetting ?
t(apply(CProd.Matrix ["111",, drop = FALSE], 1, sort, decreasing = TRUE))
# 111 116 112 114 113 115
#111 1 0.5773503 0.4082483 0.3333333 0 0
How to find the orientation of three points in a two dimensional space given coordinates of those points?
This formula is used to calculate cross product of vectors q-p and q-r. You can see in Geometric Meaning section that cross product value
C = A x B = |A|*|B|*Sin(Theta), where Theta is angle between these vectors (point-to-point directions). Sin(Theta) = 0 for parallel vectors, positive when Theta < 180, negative otherwise.
Example:
clockwise triplet ABC: cross product of AB and AC vectors is >0
anticlockwise triplet ACD: cross product of AC and AD is negative.
how to calculate the Euclidean norm of a vector in R?
This is a trivial function to write yourself:
norm_vec <- function(x) sqrt(sum(x^2))
Related Topics
Inserting a New Row to Data Frame for Each Group Id
Drawing a Tangent to the Plot and Finding the X-Intercept Using R
Check to See If a Value Is Within a Range
Update Rows of Data Frame in R
R, Conditionally Remove Duplicate Rows
How to Apply a Gradient Fill to a Geom_Rect Object in Ggplot2
How to Access the Name of the Variable Assigned to the Result of a Function Within the Function
How to Add Axis Text in This Negative and Positive Bars Differently Using Ggplot2
Subtract Pairs of Columns Based on Matching Column
Ggplot2: Plotting Order of Factors Within a Geom
How to Plot Igraph Community with Defined Colors
Converting a "Map" Object to a "Spatialpolygon" Object
R: Selecting First of N Consecutive Rows Above a Certain Threshold Value
How to Dynamically Change Plotly Axis Based on Crosstalk Conditions
Ggplot Boxplot - Length of Whiskers with Logarithmic Axis
Error in Chol.Default(Cxx):The Leading Minor of Order Is Not Positive Definite