From murdoch at stats.uwo.ca  Tue May 20 10:59:57 2003
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Tue May 20 15:58:36 2003
Subject: [Rd] Extending %*%
In-Reply-To: <te2jcvkqn8uq9it881386oqi1n4j8ncakh@4ax.com>
References: <te2jcvkqn8uq9it881386oqi1n4j8ncakh@4ax.com>
Message-ID: <61bkcv4hqepnl5evk8eubbvudqekfhdadv@4ax.com>

On Mon, 19 May 2003 21:56:24 -0400, you wrote in message
<te2jcvkqn8uq9it881386oqi1n4j8ncakh@4ax.com>:

>I have lists of matrices stored in various ways.  I'd like to extend
>%*% to work on these.  Is this possible, or should I create my own new
>operator?

In answer to my own question:  yes, it's possible, using the S4
classes and methods.  It would look something like this:


setClass("special", representation(m = "list"))

setMethod("%*%", 
                signature(x = "special", y="special"),
                function(x,y) 
		new("special", 
			m = mapply("%*%", x@m, y@m, SIMPLIFY=FALSE))
)


A1 <- A2 <- A3 <- B1 <- B2 <- B3 <- diag(c(2,3))

A <- new("special", m = list(A1, A2, A3))

B <- new("special", m = list(A1, A2, A3))

A %*% B

(Thanks Uwe for the compact expression to implement the method!)

Duncan Murdoch


[Rd] Extending %*%
Duncan Murdoch