Project

General

Profile

Feature #10903 ยป matrix_zip.patch

LitoNico (Lito Nicolai), 02/25/2015 04:43 AM

View differences:

lib/matrix.rb
443 443
  end
444 444
  alias map collect
445 445

  
446
  # Takes n matricies of same row and column count, and returns a matrix
447
  # of length n arrays.
448
  def zip(*args)
449
    memo = self.map {|x| [x]}
450
    args.each do |m|
451
      Matrix.Raise ErrDimensionMismatch unless
452
        m.row_count == memo.row_count && m.column_count == memo.column_count
453

  
454
      memo.each_with_index do |e, i, j|
455
        memo[i, j] << m[i, j]
456
      end
457
    end
458
    memo
459
  end
460

  
446 461
  #
447 462
  # Yields all elements of the matrix, starting with those of the first row,
448 463
  # or returns an Enumerator if no block given.
test/matrix/test_matrix.rb
279 279
    assert_equal(Matrix[[1, 4, 9], [16, 25, 36]], @m1.collect {|x| x ** 2 })
280 280
  end
281 281

  
282
  def test_zip
283
    assert_equal(Matrix[[[1, 1], [2, 2], [3, 3]],
284
                        [[4, 4], [5, 5], [6, 6]]], @m1.zip(@m2))
285
    assert_equal(Matrix[[[1, 1, 1], [2, 2, 2], [3, 3, 3]],
286
                        [[4, 4, 4], [5, 5, 5], [6, 6, 6]]], @m1.zip(@m2, @m2))
287
  end
288

  
282 289
  def test_minor
283 290
    assert_equal(Matrix[[1, 2], [4, 5]], @m1.minor(0..1, 0..1))
284 291
    assert_equal(Matrix[[2], [5]], @m1.minor(0..1, 1..1))