sparse(i, j, v) for N-dim input

View: New views
4 Messages — Rating Filter:   Alert me  

sparse(i, j, v) for N-dim input

by Kim Hansen-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How about making
  S = sparse (I, J, SV, M, N, NZMAX)
work for multi dimensional I, J and SV ?

The workaround sparse(I(:), J(:), SV(:)) is easy, but I would like
sparse() to check that the dimensions of I, J, and SV were the same
and then create the sparse matrix also for 3+-dimensional input
matrices.

I can implement the change, I just want to hear if it is a bad idea
before I start coding.

--
Kim Hansen
Vadgårdsvej 3, 2.tv
2860 Søborg
Fastnet: 3956 2437 -- Mobil: 3091 2437


Re: sparse(i, j, v) for N-dim input

by dbateman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Kim Hansen-5 wrote:
How about making
  S = sparse (I, J, SV, M, N, NZMAX)
work for multi dimensional I, J and SV ?

The workaround sparse(I(:), J(:), SV(:)) is easy, but I would like
sparse() to check that the dimensions of I, J, and SV were the same
and then create the sparse matrix also for 3+-dimensional input
matrices.
I'm no sure I understand what you mean. If the size of the matrix is determined by the size of I and J, and I and J are either dense with elements that won't be include in the final matrix and so occupy already much more memory than the final matrix, or they are sparse and you already have the structure of the matrix coded as a sparse matrix and a call to spfun would make more sense

If you are taking the dimensions from the size of I, J and SV, then M and N are useless as well.

I can implement the change, I just want to hear if it is a bad idea before I start coding.
I think taking the dimension of the matrix form I and J is a bad idea, N-dimensional sparse matrices however isn't a bad idea, but I suspect its a lot more work than you realize. Look in liboctave/Sparse.cc at the assign and index methods and realize that you'll basically have to duplicate that or fix it for multidimensional matrices while getting all the corner cases of N-dimensional indexing/assignment correct. So yes I think N-dimensional sparse matrices might be useful, but perhaps not at the cost it would take to do it especially as we can get most of the benefits of N-dimensional sparse matrices with a cell array of sparse matrices and using cellfun.

Regards
David

Re: sparse(i, j, v) for N-dim input

by Kim Hansen-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 15, 2008 at 6:25 PM, dbateman <dbateman@...> wrote:

>
> Kim Hansen-5 wrote:
>>
>> How about making
>>   S = sparse (I, J, SV, M, N, NZMAX)
>> work for multi dimensional I, J and SV ?
>>
>> The workaround sparse(I(:), J(:), SV(:)) is easy, but I would like
>> sparse() to check that the dimensions of I, J, and SV were the same
>> and then create the sparse matrix also for 3+-dimensional input
>> matrices.
>>
>
> I'm no sure I understand what you mean. If the size of the matrix is

I'll try with an example:

octave:1> a=reshape(1:8,[2 2 2]);
octave:2> sparse(a,a,a,8,8)
error: invalid conversion of NDArray to Matrix
error: invalid conversion of NDArray to Matrix
error: invalid conversion of NDArray to Matrix
octave:2> sparse(a(:),a(:),a(:),8,8)
ans =

Compressed Column Sparse (rows = 8, cols = 8, nnz = 8)

  (1, 1) ->  1
  (2, 2) ->  2
  (3, 3) ->  3
  (4, 4) ->  4
  (5, 5) ->  5
  (6, 6) ->  6
  (7, 7) ->  7
  (8, 8) ->  8

octave:3>

I just think that statement number 2 should work and return the same
as the 3rd statement.

--
Kim Hansen
Vadgårdsvej 3, 2.tv
2860 Søborg
Fastnet: 3956 2437 -- Mobil: 3091 2437


Re: sparse(i, j, v) for N-dim input

by David Bateman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Kim Hansen wrote:

> On Thu, May 15, 2008 at 6:25 PM, dbateman <dbateman@...> wrote:
>  
>> Kim Hansen-5 wrote:
>>    
>>> How about making
>>>   S = sparse (I, J, SV, M, N, NZMAX)
>>> work for multi dimensional I, J and SV ?
>>>
>>> The workaround sparse(I(:), J(:), SV(:)) is easy, but I would like
>>> sparse() to check that the dimensions of I, J, and SV were the same
>>> and then create the sparse matrix also for 3+-dimensional input
>>> matrices.
>>>
>>>      
>> I'm no sure I understand what you mean. If the size of the matrix is
>>    
>
> I'll try with an example:
>
> octave:1> a=reshape(1:8,[2 2 2]);
> octave:2> sparse(a,a,a,8,8)
> error: invalid conversion of NDArray to Matrix
> error: invalid conversion of NDArray to Matrix
> error: invalid conversion of NDArray to Matrix
> octave:2> sparse(a(:),a(:),a(:),8,8)
> ans =
>
> Compressed Column Sparse (rows = 8, cols = 8, nnz = 8)
>
>   (1, 1) ->  1
>   (2, 2) ->  2
>   (3, 3) ->  3
>   (4, 4) ->  4
>   (5, 5) ->  5
>   (6, 6) ->  6
>   (7, 7) ->  7
>   (8, 8) ->  8
>
> octave:3>
>
> I just think that statement number 2 should work and return the same
> as the 3rd statement.
>
>  

Sure why not. Then in that case its a fairly small change to
src/DLD-FUNCTIONS/sparse.cc that is needed.

D.