Article View: aus.stats.s
Article #16083Re: Kronecker Products
From: Bill Venables
Date: Fri, 06 Mar 1992 02:14
Date: Fri, 06 Mar 1992 02:14
38 lines
2042 bytes
2042 bytes
In article <9203042028.AA05869@essss1.stat.ncsu.edu> nychka@stat.ncsu.edu
(Doug Nychka) writes:
>
> Is there an easy way to do Kronecker products in S?
> Doug Nychka
Here's the slow, for-loop way (I needed it in a hurry, once :-)
"%x%" <-
function(a, b)
{
if(!is.matrix(a) || !is.matrix(b))
stop("A and B must be matrices")
C <- NULL
for(i in 1:dim(a)[1]) {
D <- NULL
for(j in 1:dim(a)[2]) {
D <- cbind(D, a[i, j] * b)
}
C <- rbind(C, D)
}
C
}
After which definition you can just say "A %x% B"
However all the entries you want are in the array "outer(A, B)"
more-or-less in a flash. There is a then just the small problem of
arranging the four-way array as a two-way array afterwards.
It may even be preferable to think about your application a bit more and
actually use the four-way array, with Bill Dunlap's (?) recently posted
%tensor% function to do the subsequent work. In a sense the tensor product
"flattening out" of the four-way array is often a bit of a mathematical
mistake anyway.
--
Bill Venables, Dept. of Statistics, | Email: venables@stats.adelaide.edu.au
Univ. of Adelaide, South Australia. | Tel: +61 8 228 5412 Fax: ...232 5670
Technical Details
Message-ID:
<WVENABLE.92Mar6124420@spam.maths.adelaide.edu.au>Path:
arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!yoyo.aarnet.edu.au!sirius.ucs.adelaide.edu.au!spam!spam.maths.adelaide.edu.au!wvenableReferences:
<9203042028.AA05869@essss1.stat.ncsu.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa931,e7214b85021d85fa X-Google-Attributes: gidfa931,public X-Google-ArrivalTime: 1992-03-05 17:36:09 PST Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!yoyo.aarnet.edu.au!sirius.ucs.adelaide.edu.au!spam!spam.maths.adelaide.edu.au!wvenable From: wvenable@spam.maths.adelaide.edu.au (Bill Venables) Newsgroups: aus.stats.s Subject: Re: Kronecker Products Message-ID: <WVENABLE.92Mar6124420@spam.maths.adelaide.edu.au> Date: 6 Mar 92 02:14:20 GMT References: <9203042028.AA05869@essss1.stat.ncsu.edu> Sender: wvenable@spam.ua.oz Organization: Adelaide University. Lines: 38 In-reply-to: nychka@stat.ncsu.edu's message of 4 Mar 92 21:15:43 GMT