ruby-changes:45903
From: marcandre <ko1@a...>
Date: Wed, 15 Mar 2017 05:09:37 +0900 (JST)
Subject: [ruby-changes:45903] marcandRe: r57976 (trunk): * lib/matrix.rb: Add Vector.zero and Vector#zero?
marcandre 2017-03-15 05:09:30 +0900 (Wed, 15 Mar 2017) New Revision: 57976 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57976 Log: * lib/matrix.rb: Add Vector.zero and Vector#zero? Patch by Chia-sheng Chen [#13208] Modified files: trunk/lib/matrix.rb trunk/test/matrix/test_vector.rb Index: test/matrix/test_vector.rb =================================================================== --- test/matrix/test_vector.rb (revision 57975) +++ test/matrix/test_vector.rb (revision 57976) @@ -11,6 +11,13 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L11 @w1 = Vector[2,3,4] end + def test_zero + assert_equal Vector[0, 0, 0, 0], Vector.zero(4) + assert_equal Vector[], Vector.zero(0) + assert_raise(ArgumentError) { Vector.zero(-1) } + assert Vector[0, 0, 0, 0].zero? + 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) } Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 57975) +++ lib/matrix.rb (revision 57976) @@ -1685,6 +1685,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1685 # * Vector.[](*array) # * Vector.elements(array, copy = true) # * Vector.basis(size: n, index: k) +# * Vector.zero(n) # # To access elements: # * #[](i) @@ -1697,6 +1698,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1698 # * #angle_with(v) # * Vector.independent?(*vs) # * #independent?(*vs) +# * #zero? # # Vector arithmetic: # * #*(x) "is matrix or number" @@ -1769,6 +1771,17 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1771 end # + # Return a zero vector. + # + # Vector.zero(3) => Vector[0, 0, 0] + # + def Vector.zero(size) + raise ArgumentError, "invalid size (#{size} for 0..)" if size < 0 + array = Array.new(size, 0) + new convert_to_array(array, false) + end + + # # Vector.new is private; use Vector[] or Vector.elements to create. # def initialize(array) @@ -1882,6 +1895,13 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1895 self.class.independent?(self, *vs) end + # + # Returns +true+ iff all elements are zero. + # + def zero? + all?(&:zero?) + end + #-- # COMPARING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/