Learn R Programming

tensorEVD (version 0.1.4)

Weighted sum: 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)

Value

Returns a matrix containing the Hadamard product.

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 A into the resulting Hadamard product. If 'missing', it is assumed to be equal to 1,...,nrow(A)

IDrowB

(integer/character) Vector of length m with either indices or row names mapping from rows of B into the resulting Hadamard product. If 'missing', it is assumed to be equal to 1,...,nrow(B)

IDcolA

(integer/character) (Optional) Similar to IDrowA, vector of length n for columns. If NULL, it is assumed to be equal to IDrowA if m=n

IDcolB

(integer/character) (Optional) Similar to IDrowB, vector of length n for columns. If NULL, it is assumed to be equal to IDrowB if m=n

drop

Either TRUE or FALSE to whether return a uni-dimensional vector when output is a matrix with either 1 row or 1 column as per the rows and cols arguments

make.dimnames

TRUE or FALSE to whether add rownames and colnames attributes to the output

inplace

TRUE or FALSE to whether operate directly on one input matrix (A or B) when this is used as is (i.e., is not indexed; therefore, needs to be of appropiate dimensions) in the Hadamard. When TRUE the output will be overwritten on the same address occupied by the non-indexed matrix. Default inplace=FALSE

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.

Examples

Run this code
  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)

Run the code above in your browser using DataLab