Commit 2030870b authored by Francois DELOBEL's avatar Francois DELOBEL
Browse files

Added submatrix

parent eba7c1dd
......@@ -229,4 +229,31 @@ class TestMultiMatrix < MiniTest::Unit::TestCase
end
end
end
def test_submatrix
i = 0
@m.each_index do |*index|
@m[*index] = i
i += 1
end
ident = @m.sub_matrix(0..2,0..3,0..4)
@m.each_index do |i|
assert_equal( @m.at(i), ident.at(i) )
end
m2 = MultiMatrix.build(:int, [5, 5])
i = 0
m2.each_index do |*index|
m2[*index] = i
i += 1
end
small = m2.sub_matrix(1..2,1..2)
assert_equal(6, small[0,0])
assert_equal(7, small[0,1])
assert_equal(11, small[1,0])
assert_equal(12, small[1,1])
end
end
......@@ -262,6 +262,27 @@ class MultiMatrix
@data.clone.freeze
end
def sub_matrix(*sub_range)
cdr = sub_range.map{ |r| r.to_a }
car = cdr.shift
shift = sub_range.map(&:first)
size = sub_range.map(&:size)
destination = Array.new(sub_range.size)
matrix = MultiMatrix.build(@type, size)
car.product(*cdr) do |coordinates|
check_all_dimensions(coordinates)
coordinates.each_index do |i|
destination[i] = coordinates[i] - shift[i]
end
matrix[*destination] = self[*coordinates]
end
matrix
end
class DimensionError < Exception
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment