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

ruby-changes:48741

From: marcandre <ko1@a...>
Date: Mon, 20 Nov 2017 11:18:18 +0900 (JST)
Subject: [ruby-changes:48741] marcandRe: r60857 (trunk): lib/matrix: Add explicit coercion #to_matrix

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

  New Revision: 60857

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

  Log:
    lib/matrix: Add explicit coercion #to_matrix

  Modified files:
    trunk/lib/matrix.rb
    trunk/test/matrix/test_matrix.rb
    trunk/test/matrix/test_vector.rb
Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 60856)
+++ lib/matrix.rb	(revision 60857)
@@ -1381,6 +1381,13 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1381
   end
 
   #
+  # Explicit conversion to a Matrix. Returns self
+  #
+  def to_matrix
+    self
+  end
+
+  #
   # Returns an array of arrays that describe the rows of the matrix.
   #
   def to_a
@@ -1494,6 +1501,10 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1501
     def self.coerce_to_int(obj)
       coerce_to(obj, Integer, :to_int)
     end
+
+    def self.coerce_to_matrix(obj)
+      coerce_to(obj, Matrix, :to_matrix)
+    end
   end
 
   include CoercionHelper
@@ -2039,6 +2050,13 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L2050
     @elements.dup
   end
 
+  #
+  # Return a single-column matrix from this vector
+  #
+  def to_matrix
+    Matrix.column_vector(self)
+  end
+
   def elements_to_f
     warn "#{caller(1, 1)[0]}: warning: Vector#elements_to_f is deprecated"
     map(&:to_f)
Index: test/matrix/test_matrix.rb
===================================================================
--- test/matrix/test_matrix.rb	(revision 60856)
+++ test/matrix/test_matrix.rb	(revision 60857)
@@ -218,6 +218,10 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L218
     assert_equal([[1], [1]], m2.to_a)
   end
 
+  def test_to_matrix
+    assert @m1.equal? @m1.to_matrix
+  end
+
   def test_columns
     assert_equal(@m1, Matrix.columns([[1, 4], [2, 5], [3, 6]]))
   end
Index: test/matrix/test_vector.rb
===================================================================
--- test/matrix/test_vector.rb	(revision 60856)
+++ test/matrix/test_vector.rb	(revision 60857)
@@ -177,6 +177,10 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L177
     assert_equal("Vector[1, 2, 3]", @v1.to_s)
   end
 
+  def test_to_matrix
+    assert_equal Matrix[[1], [2], [3]], @v1.to_matrix
+  end
+
   def test_inspect
     assert_equal("Vector[1, 2, 3]", @v1.inspect)
   end

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

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