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

ruby-changes:35758

From: marcandre <ko1@a...>
Date: Wed, 8 Oct 2014 05:18:45 +0900 (JST)
Subject: [ruby-changes:35758] marcandRe: r47840 (trunk): * lib/matrix.rb: Add @- and @+ for Matrix and Vector.

marcandre	2014-10-08 05:18:35 +0900 (Wed, 08 Oct 2014)

  New Revision: 47840

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

  Log:
    * lib/matrix.rb: Add @- and @+ for Matrix and Vector.
      patch by gogo tanaka [#10068] [#10069]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/lib/matrix.rb
    trunk/test/matrix/test_matrix.rb
    trunk/test/matrix/test_vector.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47839)
+++ ChangeLog	(revision 47840)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Oct  8 05:16:32 2014  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/matrix.rb: Add @- and @+ for Matrix and Vector.
+	  patch by gogo tanaka [#10068] [#10069]
+
 Wed Oct  8 04:58:48 2014  John Bachir  <j@j...>
 
 	* bootstraptest/test_io.rb (assert_finish):
@@ -55,7 +60,7 @@ Tue Oct  7 21:40:17 2014  Masaki Suketa https://github.com/ruby/ruby/blob/trunk/ChangeLog#L60
 	* ext/win32ole/win32ole_method.c: refactoring. add
 	  olemethod_data_get_struct to wrap Data_Get_Struct.
 	* ext/win32ole/win32ole_method.h: ditto.
-	
+
 	* ext/win32ole/win32ole_param.c (oleparam_ole_param):
 	  call olemethod_data_get_struct instead of Data_Get_Struct.
 
Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 47839)
+++ lib/matrix.rb	(revision 47840)
@@ -90,6 +90,8 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L90
 # * #inverse
 # * #inv
 # * #**
+# * #+@
+# * #-@
 #
 # Matrix functions:
 # * #determinant
@@ -1122,6 +1124,14 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1124
     end
   end
 
+  def +@
+    self
+  end
+
+  def -@
+    collect {|e| -e }
+  end
+
   #--
   # MATRIX FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   #++
@@ -1670,6 +1680,8 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1680
 # * #*(x) "is matrix or number"
 # * #+(v)
 # * #-(v)
+# * #+@
+# * #-@
 #
 # Vector functions:
 # * #inner_product(v)
@@ -1907,6 +1919,14 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1919
     end
   end
 
+  def +@
+    self
+  end
+
+  def -@
+    collect {|e| -e }
+  end
+
   #--
   # VECTOR FUNCTIONS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   #++
Index: NEWS
===================================================================
--- NEWS	(revision 47839)
+++ NEWS	(revision 47840)
@@ -147,6 +147,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L147
     * Matrix#laplace_expansion(row_or_column: num) returns the laplace_expansion
       along the +num+ -th row or column.
     * Vector.basis(size:, index:) returns the specified basis vector
+    * Unary - and + added for Vector and Matrix
 
 * Pathname
   * Pathname#/ is aliased to Pathname#+.
Index: test/matrix/test_vector.rb
===================================================================
--- test/matrix/test_vector.rb	(revision 47839)
+++ test/matrix/test_vector.rb	(revision 47840)
@@ -120,6 +120,15 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L120
     assert_equal(0, Vector[1, 2, 3] - o)
   end
 
+  def test_uplus
+    assert_equal(@v1, +@v1)
+  end
+
+  def test_negate
+    assert_equal(Vector[-1, -2, -3], -@v1)
+    assert_equal(@v1, -(-@v1))
+  end
+
   def test_inner_product
     assert_equal(1+4+9, @v1.inner_product(@v1))
   end
Index: test/matrix/test_matrix.rb
===================================================================
--- test/matrix/test_matrix.rb	(revision 47839)
+++ test/matrix/test_matrix.rb	(revision 47840)
@@ -62,6 +62,15 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L62
     assert_equal @m1.hash, @m3.hash
   end
 
+  def test_uplus
+    assert_equal(@m1, +@m1)
+  end
+
+  def test_negate
+    assert_equal(Matrix[[-1, -2, -3], [-4, -5, -6]], -@m1)
+    assert_equal(@m1, -(-@m1))
+  end
+
   def test_rank
     [
       [[0]],

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

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