octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Kronecker products as objects?


From: David Bateman
Subject: Re: Kronecker products as objects?
Date: Wed, 24 Mar 2010 09:38:10 +0100
User-agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706)

Jaroslav Hajek wrote:
On Tue, Mar 23, 2010 at 9:02 PM, Søren Hauberg <address@hidden> wrote:
Hi All

I get the impression that Kronecker products are becoming more and more
popular in Machine Learning research. However, people only seem to be
using Kronecker products as part of their derivations, while the actual
programs don't use them. I don't really like this, as the resulting code
is often hard to follow.

The reason why people don't use Kronecker products in their code is that
the resulting matrices often are huge when you are working on real world
problems. So, I'm thinking: why not just have Kronecker products as a
special type of matrix, just like we have for diagonal matrices?

The attached code is a simple m-file implementation of such a class
('kronprod'). It implements the following functions (besides trivial
stuff like 'size', 'numel', 'display', ...):

       ctranspose
       transpose
       uplus
       uminus
       rank
       trace
       det
       mtimes
       mldivide
       mpower
       inv
       eig
       svd

This is just a prototype, but it seems to work quite well (it has no
error checking, documentation or fancy stuff like that, though). As an
example, I can do

 A = rand (200, 300);
 B = rand (400, 500);
 v1 = rand (rows (A)*rows (B), 1);

 tic, r1 = kronprod (A, B) \ v1; toc

which gives me

 Elapsed time is 0.601284 seconds.

If I use the current 'kron' function

 tic, r2 = kron (A, B) \ v1; toc

I instead get

 error: memory exhausted or requested size too large for range of
Octave's index type -- trying to return to prompt

In general, the code is quite simple as it is just an application of
properties of Kronecker products.

My question is then: is it worth the effort of trying to integrate this
properly into Octave (which would require putting corresponding C++ code
in 'liboctave') or should I just finish the class and stuff it in the
'linalg' package?

Søren



Brilliant idea. I say commit this to linear-algebra for the time being.


I'd suggest modifying the full function like

function retval = full (KP)
 if (isempty (KP.full))
   KP.full = kron (KP.A, KP.B);
 endif
 retval = full (KP.full);
endfunction

and adding the functions

function  retval = issparse (KP)
   retval = issparse(KP.A) || issparse(KP.B);
endfunction

function retval = sparse (KP)
 if (isempty (KP.full))
   KP.full = kron (KP.A, KP.B);
 endif
 retval = sparse (KP.full);
endfunction

function retval = double (KP)
 if (isempty (KP.full))
   KP.full = kron (KP.A, KP.B);
 endif
 retval = KP.full;
endfunction

then use the double function in the places you used full in your code. In that way your knonprod object can be pretty much agnostics whether its treating full or sparse matrices internally.

D.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]