From 62646c8d673b6dd67dba008afdcb5ff1c2eb3614 Mon Sep 17 00:00:00 2001 From: marcandre Date: Mon, 20 Nov 2017 02:18:23 +0000 Subject: lib/matrix: accept vectors in {h|v}stack git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60858 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/matrix.rb | 10 +++++----- test/matrix/test_matrix.rb | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/matrix.rb b/lib/matrix.rb index 8c22d2b208..0a2b609b59 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -212,10 +212,10 @@ class Matrix # Matrix.vstack(x, y) # => Matrix[[1, 2], [3, 4], [5, 6], [7, 8]] # def Matrix.vstack(x, *matrices) - raise TypeError, "Expected a Matrix, got a #{x.class}" unless x.is_a?(Matrix) + x = CoercionHelper.coerce_to_matrix(x) result = x.send(:rows).map(&:dup) matrices.each do |m| - raise TypeError, "Expected a Matrix, got a #{m.class}" unless m.is_a?(Matrix) + m = CoercionHelper.coerce_to_matrix(m) if m.column_count != x.column_count raise ErrDimensionMismatch, "The given matrices must have #{x.column_count} columns, but one has #{m.column_count}" end @@ -233,11 +233,11 @@ class Matrix # Matrix.hstack(x, y) # => Matrix[[1, 2, 5, 6], [3, 4, 7, 8]] # def Matrix.hstack(x, *matrices) - raise TypeError, "Expected a Matrix, got a #{x.class}" unless x.is_a?(Matrix) + x = CoercionHelper.coerce_to_matrix(x) result = x.send(:rows).map(&:dup) total_column_count = x.column_count matrices.each do |m| - raise TypeError, "Expected a Matrix, got a #{m.class}" unless m.is_a?(Matrix) + m = CoercionHelper.coerce_to_matrix(m) if m.row_count != x.row_count raise ErrDimensionMismatch, "The given matrices must have #{x.row_count} rows, but one has #{m.row_count}" end @@ -1487,7 +1487,7 @@ class Matrix # def self.coerce_to(obj, cls, meth) # :nodoc: return obj if obj.kind_of?(cls) - + raise TypeError, "Expected a #{cls} but got a #{obj.class}" unless obj.respond_to? meth begin ret = obj.__send__(meth) rescue Exception => e diff --git a/test/matrix/test_matrix.rb b/test/matrix/test_matrix.rb index 0831ac6362..5b0bc968f7 100644 --- a/test/matrix/test_matrix.rb +++ b/test/matrix/test_matrix.rb @@ -571,6 +571,8 @@ class TestMatrix < Test::Unit::TestCase assert_equal @e1, @e1.hstack(@e1) assert_equal Matrix.empty(0,6), @e2.hstack(@e2) assert_equal SubMatrix, SubMatrix.hstack(@e1).class + # From Vectors: + assert_equal Matrix[[1, 3],[2, 4]], Matrix.hstack(Vector[1,2], Vector[3, 4]) end def test_vstack @@ -586,6 +588,8 @@ class TestMatrix < Test::Unit::TestCase assert_equal Matrix.empty(4,0), @e1.vstack(@e1) assert_equal @e2, @e2.vstack(@e2) assert_equal SubMatrix, SubMatrix.vstack(@e1).class + # From Vectors: + assert_equal Matrix[[1],[2],[3]], Matrix.vstack(Vector[1,2], Vector[3]) end def test_eigenvalues_and_eigenvectors_symmetric -- cgit v1.2.3