From: "Kroeger, Simon (ext)" Date: 2006-08-21T23:01:28+09:00 Subject: Re: You can write Fortran in any language > From: Bil Kleb [mailto:Bil.Kleb@NASA.gov] > Sent: Monday, August 21, 2006 3:20 PM > > How about the (i,j) beasts? E.g., > > sum_xy = Array.new(x.size){ Array.new(y.size){0.0} } > for k in 0..n-1 > for i in 0...x.size > for j in 0...y.size > sum_xy[i][j] += x[i][k]*y[j][k] > end > end > end > > Doubly embedded maps? > > Is there anyway to do all this without using indices? sum_xy = x.map do |i| y.map do |j| (0..n-1).inject(0) {|sum, k| sum + i[k] * j[k]} end end to get rid of the last index you would need zip or SyncEnumerator (which is slow). > Agreed. I'm happy to locally extend Enumerable... > > module Enumerable > def sum > self.inject(0){ |sum,e| sum + e } > end > end module Enumerable def sum inject{|sum, e| sum + e} end end hmm, at least if the enumerable isn't empty ... cheers Simon