octave-maintainers
[Top][All Lists]
Advanced

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

Re: Kronecker products as objects?


From: Jaroslav Hajek
Subject: Re: Kronecker products as objects?
Date: Wed, 24 Mar 2010 18:32:48 +0100

On Wed, Mar 24, 2010 at 5:46 PM, Søren Hauberg <address@hidden> wrote:
> ons, 24 03 2010 kl. 09:38 +0100, skrev David Bateman:
>> 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
>
> The idea in this function was that the full matrix should be cached such
> that we don't have to evaluate it more than once (it can get very
> expensive). I just realised that the code doesn't actually do that, as
> the 'full' function is modifying a copy of the object. So this idea of
> caching the full matrix doesn't seem to be implementable with an m-file
> class.
>

I can imagine doing this with some kind of dirty trick, but I say
don't bother with that. As you said, the full matrices can be huge, so
it may not even be the best idea to keep them in memory.

> I guess it will be better to simple raise an error when the user does
> something that requires the full matrix (informing the user to use
> 'full'). This is also what is done with sparse matrices, right?
>

Not really, sparse matrices are converted to full on a number of
occasions. But they don't cache. Diagonal matrices do cache the
conversion, but I thought really a lot about it and I was not 100%
sure it's the best idea.

>> and adding the functions
>>
>> function  retval = issparse (KP)
>>     retval = issparse(KP.A) || issparse(KP.B);
>> endfunction
>
> I'll add this one. Thanks.
>
>> function retval = sparse (KP)
>>   if (isempty (KP.full))
>>     KP.full = kron (KP.A, KP.B);
>>   endif
>>   retval = sparse (KP.full);
>> endfunction
>
> Ohh, I see 'kron' can deal with sparse matrices, so this won't be create
> the full matrix if 'A' and 'B' are sparse. I'll add this function.
>
>> 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.
>
> But then I can't handle complex matrices, right? I am unsure about the
> 'double'. Is it supposed to convert back into either a sparse or a full
> matrix or should it just ensure that that the matrices are double, i.e.
>
> function retval = double (KP)
>  retval = kronprod (double (KP.A), double (KP.B));
> endfunction
>
> ?


I also suggest you need not wait until the code is 100% complete. If
you commit early, more people will be able to help :)


-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

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