arrProducts.R

Hadamard. Outer, and Kronecker product of a list of arrays
language: R
license: GPL 2

Code for Snippet:

                
# Hadamard product of a list of arrays
 
arrHadamard <- function (c, f = "*") {
    nmat <- length (c)
    fun <- match.fun (f)
    if (nmat == 0) stop("empty argument in arrHadamard")
    res <- c [[1]]
    if (nmat == 1) return  (res)
    for (i in (2 : nmat)) {
		res <- fun (res,c [[i]])
	 }
    return (res)
}
 
# outer product of a list of arrays
 
arrOuter <- function (x, fun = "*") {
    nmat<-length (x)
    if (nmat == 0) {
        stop ("empty argument in arrOuter")
    }
    res <- x [[1]]
    if (length (x) == 1) {
        return (res)
    }
    for (i in 2:nmat) {
        res <- outer (res, x [[i]], fun)
    }
    return (res)
}
 
# kronecker product of a list of arrays
 
arrKronecker <- function (x, fun = "*") {
    nmat <- length (x)
    if (nmat == 0) {
        stop ("empty argument in arrKronecker")
    }
    res <- x [[1]]
    if (length (x) == 1) {
        return (res)
    }
    for (i in (2 : nmat)) {
        res<-kronecker (res, x [[i]], fun)
    }
    return (res)
}
comments powered by Disqus

Info

Source Site:

Jan

Tags: Matrix Algebra Kronecker Product Hadamard Product

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 614
Date Added: 2013-03-06 18:00:46
Last Modified: 2013-04-17 22:12:19

Web Analytics