Weighted sum {tensorEVD} | R Documentation |
Weighted sum
Description
Computes the Hadamard product between two matrices
Usage
Sum(a = 1, A, b = 1, B, IDrowA, IDrowB,
IDcolA = NULL, IDcolB = NULL,
make.dimnames = FALSE, drop = TRUE,
inplace = FALSE)
Arguments
a |
(numeric) A constant to multiply the first matrix by |
A |
(numeric) Numeric matrix |
b |
(numeric) A constant to multiply the second matrix by |
B |
(numeric) Numeric matrix |
IDrowA |
(integer/character) Vector of length m with either indices or row names mapping from rows of |
IDrowB |
(integer/character) Vector of length m with either indices or row names mapping from rows of |
IDcolA |
(integer/character) (Optional) Similar to |
IDcolB |
(integer/character) (Optional) Similar to |
drop |
Either |
make.dimnames |
|
inplace |
|
Details
Computes the m × n weighted sum matrix between matrices A and B,
a(R1 A C'1) + b(R2 B C'2)
where R1 and R2 are incidence matrices mapping from rows of the resulting sum to rows of A and B, respectively; and C1 and C2 are incidence matrices mapping from columns of the resulting sum to columns of A and B, respectively.
Matrix R1 A C'1
can be obtained by matrix indexing as A[IDrowA,IDcolA]
, where IDrowA
and IDcolA
are integer vectors whose entries are, respectively, the row and column number of
A that are mapped at each row of
R1 and
C1, respectively.
Likewise, matrix
R2 B C'2
can be obtained as B[IDrowB,IDcolB]
, where IDrowB
and IDcolB
are integer vectors whose entries are, respectively, the row and column number of
B that are mapped at each row of
R2 and
C2, respectively. Therefore, the weighted sum can be obtained directly as
a*A[IDrowA,IDcolA] + b*B[IDrowB,IDcolB]
The function computes the Hadamard product directly from A and B without forming R1 A C'1 or R2 B C'2 matrices. The result can be multiplied by a constant a.
Value
Returns a matrix containing the Hadamard product.
Examples
require(tensorEVD)
# Generate rectangular matrices A (nrowA x ncolA) and B (nrowB x ncolB)
nA = c(10,15)
nB = c(12,8)
A = matrix(rnorm(nA[1]*nA[2]), nrow=nA[1])
B = matrix(rnorm(nB[1]*nB[2]), nrow=nB[1])
# Define IDs for a Hadamard of size n1 x n2
n = c(1000,500)
IDrowA = sample(nA[1], n[1], replace=TRUE)
IDrowB = sample(nB[1], n[1], replace=TRUE)
IDcolA = sample(nA[2], n[2], replace=TRUE)
IDcolB = sample(nB[2], n[2], replace=TRUE)
a = rnorm(1)
b = rnorm(1)
K1 = Sum(a, A, b, B, IDrowA, IDrowB, IDcolA, IDcolB)
# (it must equal to:)
K2 = a*A[IDrowA,IDcolA] + b*B[IDrowB,IDcolB]
all.equal(K1,K2)