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

ruby-changes:36425

From: marcandre <ko1@a...>
Date: Thu, 20 Nov 2014 02:44:55 +0900 (JST)
Subject: [ruby-changes:36425] marcandRe: r48506 (trunk): * lib/matrix.rb: Vector#independent? and associated class method

marcandre	2014-11-20 02:44:46 +0900 (Thu, 20 Nov 2014)

  New Revision: 48506

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

  Log:
    * lib/matrix.rb: Vector#independent? and associated class method
      patch by gogo tanaka [#10451]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/lib/matrix.rb
    trunk/test/matrix/test_vector.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48505)
+++ ChangeLog	(revision 48506)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Nov 20 02:44:27 2014  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/matrix.rb: Vector#independent? and associated class method
+	  patch by gogo tanaka [#10451]
+
 Thu Nov 20 02:32:34 2014  Marc-Andre Lafortune  <ruby-core@m...>
 
 	* lib/matrix.rb: Add Vector#angle_with
Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 48505)
+++ lib/matrix.rb	(revision 48506)
@@ -1691,6 +1691,11 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1691
 # * #each2(v)
 # * #collect2(v)
 #
+# Properties of vectors:
+# * #angle_with(v)
+# * Vector.independent?(*vs)
+# * #independent?(*vs)
+#
 # Vector arithmetic:
 # * #*(x) "is matrix or number"
 # * #+(v)
@@ -1699,7 +1704,6 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1704
 # * #-@
 #
 # Vector functions:
-# * #angle_with(v)
 # * #inner_product(v), dot(v)
 # * #cross_product(v), cross(v)
 # * #collect
@@ -1833,6 +1837,41 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1837
   end
 
   #--
+  # PROPERTIES -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+  #++
+
+  #
+  # Returns +true+ iff all of vectors are linearly independent.
+  #
+  #   Vector.independent?(Vector[1,0], Vector[0,1])
+  #     => true
+  #
+  #   Vector.independent?(Vector[1,2], Vector[2,4])
+  #     => false
+  #
+  def Vector.independent?(*vs)
+    vs.each do |v|
+      raise TypeError, "expected Vector, got #{v.class}" unless v.is_a?(Vector)
+      Vector.Raise ErrDimensionMismatch unless v.size == vs.first.size
+    end
+    return false if vs.count > vs.first.size
+    Matrix[*vs].rank.eql?(vs.count)
+  end
+
+  #
+  # Returns +true+ iff all of vectors are linearly independent.
+  #
+  #   Vector[1,0].independent?(Vector[0,1])
+  #     => true
+  #
+  #   Vector[1,2].independent?(Vector[2,4])
+  #     => false
+  #
+  def independent?(*vs)
+    self.class.independent?(self, *vs)
+  end
+
+  #--
   # COMPARING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   #++
 
Index: NEWS
===================================================================
--- NEWS	(revision 48505)
+++ NEWS	(revision 48506)
@@ -173,6 +173,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L173
     * Vector#cross_product generalized to arbitrary dimensions.
     * Vector#dot and #cross are aliases for #inner_product and #cross_product.
     * Vector#angle_with returns the angle with its argument
+    * New instance and class method independent? to test linear independence.
 
 * Pathname
   * Pathname#/ is aliased to Pathname#+.
Index: test/matrix/test_vector.rb
===================================================================
--- test/matrix/test_vector.rb	(revision 48505)
+++ test/matrix/test_vector.rb	(revision 48506)
@@ -89,6 +89,25 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L89
     assert_equal(Vector[2.0,4.0,6.0], a)
   end
 
+  def test_independent?
+    assert(Vector.independent?(@v1, @w1))
+    assert(
+      Vector.independent?(
+        Vector.basis(size: 3, index: 0),
+        Vector.basis(size: 3, index: 1),
+        Vector.basis(size: 3, index: 2),
+      )
+    )
+
+    refute(Vector.independent?(@v1, Vector[2,4,6]))
+    refute(Vector.independent?(Vector[2,4], Vector[1,3], Vector[5,6]))
+
+    assert_raise(Vector::TypeError) { Vector.independent?(@v1, 3) }
+    assert_raise(Vector::ErrDimensionMismatch) { Vector.independent?(@v1, Vector[2,4]) }
+
+    assert(@v1.independent?(Vector[1,2,4], Vector[1,3,4]))
+  end
+
   def test_mul
     assert_equal(Vector[2,4,6], @v1 * 2)
     assert_equal(Matrix[[1, 4, 9], [2, 8, 18], [3, 12, 27]], @v1 * Matrix[[1,4,9]])

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

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