From aca345d4ccc1d06f75020b45371035c435d4e75c Mon Sep 17 00:00:00 2001 From: labpak <43479201+labpak@users.noreply.github.com> Date: Sat, 16 Nov 2019 19:59:19 +0300 Subject: [PATCH 1/4] Add each_with_index --- lib/matrix/matrix.rb | 123 ++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 41 deletions(-) diff --git a/lib/matrix/matrix.rb b/lib/matrix/matrix.rb index 250e054..54e0ec2 100644 --- a/lib/matrix/matrix.rb +++ b/lib/matrix/matrix.rb @@ -112,61 +112,34 @@ def self.convert(matrix) fast_matrix end - # + # # Yields all elements of the matrix, starting with those of the first row # # Matrix[ [1,2], [3,4] ].each { |e| puts e } # # => prints the numbers 1 to 4 - def each(which = :all) # :yield: e + def each(which = :all) # :yield: e return to_enum :each, which unless block_given? case which when :all - (0...row_count).each do |i| - (0...column_count).each do |j| - yield self[i, j] - end - end + each_all{|i, j| yield self[i, j]} when :diagonal - (0...[row_count, column_count].min).each do |i| - yield self[i, i] - end + each_diagonal{|i, j| yield self[i, j]} when :off_diagonal - (0...row_count).each do |i| - (0...column_count).each do |j| - if i != j - yield self[i, j] - end - end - end + each_off_diagonal{|i, j| yield self[i, j]} when :lower - (0...row_count).each do |i| - (0..[i,column_count-1].min).each do |j| - yield self[i, j] - end - end + each_lower{|i, j| yield self[i, j]} when :strict_lower - (1...row_count).each do |i| - (0...[i,column_count].min).each do |j| - yield self[i, j] - end - end + each_strict_lower{|i, j| yield self[i, j]} when :strict_upper - (0...row_count).each do |i| - (i+1...column_count).each do |j| - yield self[i, j] - end - end + each_strict_upper{|i, j| yield self[i, j]} when :upper - (0...row_count).each do |i| - (i...column_count).each do |j| - yield self[i, j] - end - end + each_upper{|i, j| yield self[i, j]} else raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper" end self end + # # Same as #each, but the row index and column index in addition to the element # @@ -179,17 +152,85 @@ def each(which = :all) # :yield: e # # 3 at 1, 0 # # 4 at 1, 1 # - def each_with_index - raise NotSupportedError unless block_given? + def each_with_index(which = :all) # :yield: e, row, column + return to_enum :each_with_index, which unless block_given? + case which + when :all + each_all{|i, j| yield self[i, j], i, j} + when :diagonal + each_diagonal{|i, j| yield self[i, j], i, j} + when :off_diagonal + each_off_diagonal{|i, j| yield self[i, j], i, j} + when :lower + each_lower{|i, j| yield self[i, j], i, j} + when :strict_lower + each_strict_lower{|i, j| yield self[i, j], i, j} + when :strict_upper + each_strict_upper{|i, j| yield self[i, j], i, j} + when :upper + each_upper{|i, j| yield self[i, j], i, j} + else + raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper" + end + self + end + def each_all(&block) (0...row_count).each do |i| (0...column_count).each do |j| - yield self[i, j], i, j + block[i,j] + end + end + end + + def each_diagonal(&block) + (0...[row_count, column_count].min).each do |i| + block[i, i] + end + end + + def each_off_diagonal(&block) + (0...row_count).each do |i| + (0...column_count).each do |j| + if i != j + block[i, j] + end + end + end + end + + def each_lower(&block) + (0...row_count).each do |i| + (0..[i,column_count-1].min).each do |j| + block[i, j] + end + end + end + + def each_strict_lower(&block) + (1...row_count).each do |i| + (0...[i,column_count].min).each do |j| + block[i, j] end end - self end + def each_strict_upper(&block) + (0...row_count).each do |i| + (i+1...column_count).each do |j| + block[i, j] + end + end + end + + def each_upper(&block) + (0...row_count).each do |i| + (i...column_count).each do |j| + block[i, j] + end + end + end + # don't use (Issue#1) def each_with_index! (0...row_count).each do |i| From 7c4b03fb42b33c051a9495b08cc835f18fb09bdf Mon Sep 17 00:00:00 2001 From: labpak <43479201+labpak@users.noreply.github.com> Date: Sat, 16 Nov 2019 20:01:24 +0300 Subject: [PATCH 2/4] tests for each_with_index --- test/matrix/matrix_test.rb | 105 +++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/test/matrix/matrix_test.rb b/test/matrix/matrix_test.rb index 357aa9a..dfd8d74 100644 --- a/test/matrix/matrix_test.rb +++ b/test/matrix/matrix_test.rb @@ -259,6 +259,111 @@ def test_each_strict_upper_rec2 assert_equal [2, 3, 6], m.each(:strict_upper).to_a end + def test_each_wi_all_sq + m = Matrix[[1, 2], [3, 4]] + assert_equal [[1.0, 0, 0], [2.0, 0, 1], [3.0, 1, 0], [4.0, 1, 1]], m.each_with_index(:all).to_a + end + + def test_each_wi_all_rec1 + m = Matrix[[1, 2], [3, 4], [5, 6]] + assert_equal [[1.0, 0, 0], [2.0, 0, 1], [3.0, 1, 0], [4.0, 1, 1], [5.0, 2, 0], [6.0, 2, 1]], m.each_with_index(:all).to_a + end + + def test_each_wi_all_rec2 + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_equal [[1.0, 0, 0], [2.0, 0, 1], [3.0, 0, 2], [4.0, 1, 0], [5.0, 1, 1], [6.0, 1, 2]], m.each_with_index(:all).to_a + end + + def test_each_wi_diagonal_sq + m = Matrix[[1, 2], [3, 4]] + assert_equal [[1.0, 0, 0], [4.0, 1, 1]], m.each_with_index(:diagonal).to_a + end + + def test_each_wi_diagonal_rec1 + m = Matrix[[1, 2], [3, 4], [5, 6]] + assert_equal [[1.0, 0, 0], [4.0, 1, 1]], m.each_with_index(:diagonal).to_a + end + + def test_each_wi_diagonal_rec2 + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_equal [[1.0, 0, 0], [5.0, 1, 1]], m.each_with_index(:diagonal).to_a + end + + def test_each_wi_off_diagonal_sq + m = Matrix[[1, 2], [3, 4]] + assert_equal [[2.0, 0, 1], [3.0, 1, 0]], m.each_with_index(:off_diagonal).to_a + end + + def test_each_wi_off_diagonal_rec1 + m = Matrix[[1, 2], [3, 4], [5, 6]] + assert_equal [[2.0, 0, 1], [3.0, 1, 0], [5.0, 2, 0], [6.0, 2, 1]], m.each_with_index(:off_diagonal).to_a + end + + def test_each_wi_off_diagonal_rec2 + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_equal [[2.0, 0, 1], [3.0, 0, 2], [4.0, 1, 0], [6.0, 1, 2]], m.each_with_index(:off_diagonal).to_a + end + + def test_each_wi_lower_sq + m = Matrix[[1, 2], [3, 4]] + assert_equal [[1.0, 0, 0], [3.0, 1, 0], [4.0, 1, 1]], m.each_with_index(:lower).to_a + end + + def test_each_wi_lower_rec1 + m = Matrix[[1, 2], [3, 4], [5, 6]] + assert_equal [[1.0, 0, 0], [3.0, 1, 0], [4.0, 1, 1], [5.0, 2, 0], [6.0, 2, 1]], m.each_with_index(:lower).to_a + end + + def test_each_wi_lower_rec2 + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_equal [[1.0, 0, 0], [4.0, 1, 0], [5.0, 1, 1]], m.each_with_index(:lower).to_a + end + + def test_each_wi_strict_lower_sq + m = Matrix[[1, 2], [3, 4]] + assert_equal [[3.0, 1, 0]], m.each_with_index(:strict_lower).to_a + end + + def test_each_wi_strict_lower_rec1 + m = Matrix[[1, 2], [3, 4], [5, 6]] + assert_equal [[3.0, 1, 0], [5.0, 2, 0], [6.0, 2, 1]], m.each_with_index(:strict_lower).to_a + end + + def test_each_wi_strict_lower_rec2 + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_equal [[4.0, 1, 0]], m.each_with_index(:strict_lower).to_a + end + + def test_each_wi_upper_sq + m = Matrix[[1, 2], [3, 4]] + assert_equal [[1.0, 0, 0], [2.0, 0, 1], [4.0, 1, 1]], m.each_with_index(:upper).to_a + end + + def test_each_wi_upper_rec1 + m = Matrix[[1, 2], [3, 4], [5, 6]] + assert_equal [[1.0, 0, 0], [2.0, 0, 1], [4.0, 1, 1]], m.each_with_index(:upper).to_a + end + + def test_each_wi_upper_rec2 + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_equal [[1.0, 0, 0], [2.0, 0, 1], [3.0, 0, 2], [5.0, 1, 1], [6.0, 1, 2]], m.each_with_index(:upper).to_a + end + + def test_each_wi_strict_upper_sq + m = Matrix[[1, 2], [3, 4]] + assert_equal [[2.0, 0, 1]], m.each_with_index(:strict_upper).to_a + end + + def test_each_wi_strict_upper_rec1 + m = Matrix[[1, 2], [3, 4], [5, 6]] + assert_equal [[2.0, 0, 1]], m.each_with_index(:strict_upper).to_a + end + + def test_each_wi_strict_upper_rec2 + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_equal [[2.0, 0, 1], [3.0, 0, 2], [6.0, 1, 2]], m.each_with_index(:strict_upper).to_a + end + def test_greater m1 = Matrix[[1, 2, 3], [3, 2, 1]] m2 = Matrix[[2, 3, 6], [4, 4, 4]] From 056525c8b8193c8134015ab21c0d679b61e22eaa Mon Sep 17 00:00:00 2001 From: labpak <43479201+labpak@users.noreply.github.com> Date: Sun, 17 Nov 2019 11:39:03 +0300 Subject: [PATCH 3/4] Each simplified --- lib/matrix/matrix.rb | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/lib/matrix/matrix.rb b/lib/matrix/matrix.rb index 54e0ec2..20d902e 100644 --- a/lib/matrix/matrix.rb +++ b/lib/matrix/matrix.rb @@ -118,26 +118,9 @@ def self.convert(matrix) # Matrix[ [1,2], [3,4] ].each { |e| puts e } # # => prints the numbers 1 to 4 def each(which = :all) # :yield: e - return to_enum :each, which unless block_given? - case which - when :all - each_all{|i, j| yield self[i, j]} - when :diagonal - each_diagonal{|i, j| yield self[i, j]} - when :off_diagonal - each_off_diagonal{|i, j| yield self[i, j]} - when :lower - each_lower{|i, j| yield self[i, j]} - when :strict_lower - each_strict_lower{|i, j| yield self[i, j]} - when :strict_upper - each_strict_upper{|i, j| yield self[i, j]} - when :upper - each_upper{|i, j| yield self[i, j]} - else - raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper" - end - self + return to_enum :each, which unless block_given? + + each_with_index(which){ |elem, _, _| yield elem} end # From 543fd1e79bb7c720b47a38dd7249ee572d9d9690 Mon Sep 17 00:00:00 2001 From: labpak <43479201+labpak@users.noreply.github.com> Date: Sun, 17 Nov 2019 11:45:22 +0300 Subject: [PATCH 4/4] Added tests --- test/matrix/matrix_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/matrix/matrix_test.rb b/test/matrix/matrix_test.rb index dfd8d74..d5d0a0a 100644 --- a/test/matrix/matrix_test.rb +++ b/test/matrix/matrix_test.rb @@ -364,6 +364,16 @@ def test_each_wi_strict_upper_rec2 assert_equal [[2.0, 0, 1], [3.0, 0, 2], [6.0, 1, 2]], m.each_with_index(:strict_upper).to_a end + def test_each_argument_error + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_raises (ArgumentError){ m.each(:al).to_a } + end + + def test_each_wi_argument_error + m = Matrix[[1, 2, 3], [4, 5, 6]] + assert_raises (ArgumentError){ m.each_with_index(:al).to_a } + end + def test_greater m1 = Matrix[[1, 2, 3], [3, 2, 1]] m2 = Matrix[[2, 3, 6], [4, 4, 4]]