[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: sparse matrix operations
From: |
E. Joshua Rigler |
Subject: |
Re: sparse matrix operations |
Date: |
Tue, 16 Nov 2004 14:23:56 -0700 |
Sorry, I guess I didn't make it clear that what I needed was the dot, or
vector products that give the diagonal elements of the full matrix
multiplication...NOT just an element-by-element multiplication.
Other solutions suggested in this thread (i.e., C=sum(A'.*B) work for
the specific case of extracting the diagonal when the product matrix C
is square, but I'm not sure I see how to generalize this to something
like:
C = sp_multiply(A,B,C_template);
...where A and B are MxN and NxP sparse matrices, respectively, and
C_template somehow defines which arbitrary dot products in the MxP
matrix C are to be calculated. Maybe something could be done with the
existing function "spfun", but that would break M**lab compatibility.
Thanks anyway, I'll be sure to share my final solution with this list
if/when I am able to hack it together and test it out. I've attached my
slow .m version for the curious.
-EJR
On Tue, 2004-11-16 at 12:23, Mike Miller wrote:
> On Tue, 16 Nov 2004, E. Joshua Rigler wrote:
>
> > It would be useful to have operations for sparse matrices (multiply,
> > add, etc.) that only bothered to do calculations to update those
> > non-zero elements of a sparse matrix on the left-hand side of an
> > equation.
> >
> > For example, suppose I only want the diagonal of the product of two
> > square matrices. As it stands right now in Octave (with octave-forge),
> > C=A*B will result in a full C matrix, and may potentially use up more
> > memory than I wish to allow. Since I only want the diagonal of C, I may
> > as well only perform those operations that place a vector product at
> > C(i,i), thereby saving CPU cycles and memory.
> >
> > I have a simple .m file that does this, but of course it is very slow in
> > the Octave interpreter since it must loop over each of the desired
> > non-zero elements of C. I was hoping somebody out there might have
> > something similar, but in a .oct format, or could show me a trick for
> > doing this with standard Octave operators, before I go and try to
> > reinvent this wheel.
>
>
> For diagonal matrices, why not just do this:
>
> C=diag(A).*diag(B);
>
> Then C will be a column vector. To get the full matrix C (=A*B), just do
> this:
>
> C=diag(diag(A).*diag(B));
>
> Mike
>
sp_multiply.m
Description: Text document