Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > sparse multi-dimensional arrays

Reply
Thread Tools

sparse multi-dimensional arrays

 
 
Bill Birkett
Guest
Posts: n/a
 
      10-03-2006
I did like to convert some programs I wrote in Perl to Ruby. I'm
working with sparse three dimensional data. Here's an example in
Perl. The array @a has just two elements. The rest are undefined.

#!/usr/bin/perl -w

$a[0][0][0] = 0;
$a[100][100][100] = 100;

print "$a[0][0][0]\n";
print "$a[100][100][100]\n";

The output:

0
100

Is there an easy way to implement this sort of data structure in
Ruby? The Array class seems limited to one dimension. I looked at the
Matrix class, but there doesn't appear to be any way of assigning
values to the individual matrix elements (which seems pretty strange,
so maybe I'm overlooking the obvious).

-Bill
--

William B. Birkett - <(E-Mail Removed)>
Print Quality Consultant
Doppelganger, LLC - http://www.doplganger.com/

48799 Meadow Drive, Plymouth, MI 48170 (USA)
Office: (734) 927-4232 FAX: (734) 468-0580
Cell: (734) 516-4790

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      10-03-2006
On Tue, Oct 03, 2006 at 10:58:22PM +0900, Bill Birkett wrote:
> I did like to convert some programs I wrote in Perl to Ruby. I'm
> working with sparse three dimensional data. Here's an example in
> Perl. The array @a has just two elements. The rest are undefined.
>
> #!/usr/bin/perl -w
>
> $a[0][0][0] = 0;
> $a[100][100][100] = 100;
>
> print "$a[0][0][0]\n";
> print "$a[100][100][100]\n";
>
> The output:
>
> 0
> 100
>
> Is there an easy way to implement this sort of data structure in
> Ruby? The Array class seems limited to one dimension. I looked at the
> Matrix class, but there doesn't appear to be any way of assigning
> values to the individual matrix elements (which seems pretty strange,
> so maybe I'm overlooking the obvious).
>

It's pretty easy if you know the dimensions before hand, a bit more
hackery pokery if you don't.
a = Array.new(101) { Array.new(101) { Array.new(101) } }
a[0][0][0] = 0
a[100][100][100] = 100

puts a[0][0][0]
puts a[100][100][100]


 
Reply With Quote
 
 
 
 
Nathan Grant
Guest
Posts: n/a
 
      10-03-2006
> $a[0][0][0] = 0;
> $a[100][100][100] = 100;
>
> print "$a[0][0][0]\n";
> print "$a[100][100][100]\n";
>
> The output:
>
> 0
> 100


I saw this on inquirylabs.com recently [1]

hsh = Hash.new(&(p=lambda{|h,k| h[k] = Hash.new(&p)}))

hsh[0][0][0] = 0 >> 0
hsh[100][100][100] = 100 >> 100

HTH

Nathan

[1]
http://blog.inquirylabs.com/2006/09/...bitrary-depth/

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jason Nordwick
Guest
Posts: n/a
 
      10-03-2006
Close:

a={}
a[[0,0,0]], a[[100,100,100]] = 0, 100
puts "#{a[[0,0,0]]}"
puts "#{a[[100,100,100]]}"

But you might like this better (my Ruby isn't very good, so please excuse me if I overcomplicate these definitions):

class SparseMatrix < Hash
alias ldget :[]
alias ldset :[]=
def[](*i); self.oldget(i); end
def[]=(*i); v=i.pop; self.oldset(i,v); end
end

a = SparseMatrix.new
a[0,0,0], a[100,100,100] = 0, 100


irb(main):017:0> a
=> {[0, 0, 0]=>0, [100, 100, 100]=>100}
irb(main):018:0> puts "#{a[0,0,0]}"
0
=> nil
irb(main):019:0> puts "#{a[100,100,100]}"
100
=> nil
irb(main):020:0> puts "#{a[2,2,2]}"

=> nil
irb(main):021:0>

-j


Bill Birkett wrote:
> I did like to convert some programs I wrote in Perl to Ruby. I'm working
> with sparse three dimensional data. Here's an example in Perl. The array
> @a has just two elements. The rest are undefined.
>
> #!/usr/bin/perl -w
>
> $a[0][0][0] = 0;
> $a[100][100][100] = 100;
>
> print "$a[0][0][0]\n";
> print "$a[100][100][100]\n";
>
> The output:
>
> 0
> 100
>
> Is there an easy way to implement this sort of data structure in Ruby?
> The Array class seems limited to one dimension. I looked at the Matrix
> class, but there doesn't appear to be any way of assigning values to the
> individual matrix elements (which seems pretty strange, so maybe I'm
> overlooking the obvious).
>
> -Bill



 
Reply With Quote
 
Jason Nordwick
Guest
Posts: n/a
 
      10-03-2006

irb(main):005:0> hsh[1]
=> {}
irb(main):006:0> hsh[2]
=> {}
irb(main):007:0> hsh[3]
=> {}
irb(main):008:0> hsh
=> {1=>{}, 2=>{}, 3=>{}}

ugh.

-j


Nathan Grant wrote:
>> $a[0][0][0] = 0;
>> $a[100][100][100] = 100;
>>
>> print "$a[0][0][0]\n";
>> print "$a[100][100][100]\n";
>>
>> The output:
>>
>> 0
>> 100

>
> I saw this on inquirylabs.com recently [1]
>
> hsh = Hash.new(&(p=lambda{|h,k| h[k] = Hash.new(&p)}))
>
> hsh[0][0][0] = 0 >> 0
> hsh[100][100][100] = 100 >> 100
>
> HTH
>
> Nathan
>
> [1]
> http://blog.inquirylabs.com/2006/09/...bitrary-depth/
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Merging sparse arrays Samwyse Perl Misc 11 02-25-2006 02:06 AM
Do sparse arrays take up full memory? Ian Perl Misc 4 02-27-2005 10:33 PM
Iterating sparse arrays Christopher Benson-Manica Javascript 28 02-01-2005 02:14 AM
PIMv2 Sparse Mode Rygel Cisco 1 12-05-2004 04:38 PM



Advertisments