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

ruby-changes:35751

From: marcandre <ko1@a...>
Date: Wed, 8 Oct 2014 04:30:14 +0900 (JST)
Subject: [ruby-changes:35751] marcandRe: r47833 (trunk): * lib/matrix.rb: Add Vector.basis.

marcandre	2014-10-08 04:29:53 +0900 (Wed, 08 Oct 2014)

  New Revision: 47833

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

  Log:
    * lib/matrix.rb: Add Vector.basis.
      Based on patch by gogo tanaka [#10072]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/lib/matrix.rb
    trunk/test/matrix/test_vector.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47832)
+++ ChangeLog	(revision 47833)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Oct  8 04:29:21 2014  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/matrix.rb: Add Vector.basis.
+	  Based on patch by gogo tanaka [#10072]
+
 Tue Oct  7 23:40:16 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* signal.c (rb_f_kill): get rid of deadlock as unhandled and
Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 47832)
+++ lib/matrix.rb	(revision 47833)
@@ -1624,6 +1624,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1624
 # To create a Vector:
 # * Vector.[](*array)
 # * Vector.elements(array, copy = true)
+# * Vector.basis(size: n, index: k)
 #
 # To access elements:
 # * #[](i)
@@ -1686,6 +1687,19 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1687
   end
 
   #
+  # Returns a standard basis +n+-vector, where k is the index.
+  #
+  #    Vector.basis(size:, index:) # => Vector[0, 1, 0]
+  #
+  def Vector.basis(size:, index:)
+    raise ArgumentError, "invalid size (#{size} for 1..)" if size < 1
+    raise ArgumentError, "invalid index (#{index} for 0...#{size})" unless 0 <= index && index < size
+    array = Array.new(size, 0)
+    array[index] = 1
+    new convert_to_array(array, false)
+  end
+
+  #
   # Vector.new is private; use Vector[] or Vector.elements to create.
   #
   def initialize(array)
Index: NEWS
===================================================================
--- NEWS	(revision 47832)
+++ NEWS	(revision 47833)
@@ -84,6 +84,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L84
       which is obtained by multiplying the first minor by (-1)**(row + column).
     * hstack and vstack are new instance and class methods to stack matrices
       horizontally and vertically.
+    * Vector.basis(size:, index:) returns the specified basis vector
 
 * Method
   * New methods:
Index: test/matrix/test_vector.rb
===================================================================
--- test/matrix/test_vector.rb	(revision 47832)
+++ test/matrix/test_vector.rb	(revision 47833)
@@ -10,6 +10,15 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L10
     @w1 = Vector[2,3,4]
   end
 
+  def test_basis
+    assert_equal(Vector[1, 0, 0], Vector.basis(size: 3, index: 0))
+    assert_raise(ArgumentError) { Vector.basis(size: -1, index: 2) }
+    assert_raise(ArgumentError) { Vector.basis(size: 4, index: -1) }
+    assert_raise(ArgumentError) { Vector.basis(size: 3, index: 3) }
+    assert_raise(ArgumentError) { Vector.basis(size: 3) }
+    assert_raise(ArgumentError) { Vector.basis(index: 3) }
+  end
+
   def test_identity
     assert_same @v1, @v1
     assert_not_same @v1, @v2

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

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