[前][次][番号順一覧][スレッド一覧]

ruby-changes:48742

From: marcandre <ko1@a...>
Date: Mon, 20 Nov 2017 11:18:28 +0900 (JST)
Subject: [ruby-changes:48742] marcandRe: r60858 (trunk): lib/matrix: accept vectors in {h|v}stack

marcandre	2017-11-20 11:18:23 +0900 (Mon, 20 Nov 2017)

  New Revision: 60858

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60858

  Log:
    lib/matrix: accept vectors in {h|v}stack

  Modified files:
    trunk/lib/matrix.rb
    trunk/test/matrix/test_matrix.rb
Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 60857)
+++ lib/matrix.rb	(revision 60858)
@@ -212,10 +212,10 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L212
   #   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 https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L233
   #   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 https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1487
     #
     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
Index: test/matrix/test_matrix.rb
===================================================================
--- test/matrix/test_matrix.rb	(revision 60857)
+++ test/matrix/test_matrix.rb	(revision 60858)
@@ -571,6 +571,8 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L571
     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 https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L588
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]