ruby-changes:37785
From: marcandre <ko1@a...>
Date: Fri, 6 Mar 2015 08:45:56 +0900 (JST)
Subject: [ruby-changes:37785] marcandRe: r49866 (trunk): * lib/matrix.rb: Add Vector#round. Patch by Jordan Stephens.
marcandre 2015-03-06 08:45:42 +0900 (Fri, 06 Mar 2015) New Revision: 49866 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49866 Log: * lib/matrix.rb: Add Vector#round. Patch by Jordan Stephens. [Fixes GH-802] 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 49865) +++ ChangeLog (revision 49866) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Mar 6 08:45:26 2015 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix.rb: Add Vector#round. Patch by Jordan Stephens. + [Fixes GH-802] + Fri Mar 6 07:33:03 2015 Koichi Sasada <ko1@a...> * gc.c (obj_info): show node name too. Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 49865) +++ lib/matrix.rb (revision 49866) @@ -1713,6 +1713,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1713 # * #norm # * #normalize # * #r +# * #round # * #size # # Conversion to other data types: @@ -1790,6 +1791,13 @@ class Vector https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1791 alias set_component []= private :[]=, :set_element, :set_component + # Returns a vector with entries rounded to the given precision + # (see Float#round) + # + def round(ndigits=0) + map{|e| e.round(ndigits)} + end + # # Returns the number of elements in the vector. # Index: NEWS =================================================================== --- NEWS (revision 49865) +++ NEWS (revision 49866) @@ -44,6 +44,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L44 * Base64.urlsafe_decode64: now it accepts not only correctly-padded input but also unpadded input. +* lib/matrix.rb + * Add Vector#round. https://github.com/ruby/ruby/pull/802 + * ext/coverage/coverage.c * Coverage.peek_result: new method to allow coverage to be captured without stopping the coverage tool. Index: test/matrix/test_vector.rb =================================================================== --- test/matrix/test_vector.rb (revision 49865) +++ test/matrix/test_vector.rb (revision 49866) @@ -157,6 +157,10 @@ class TestVector < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_vector.rb#L157 assert_equal(5, Vector[3, 4].r) end + def test_round + assert_equal(Vector[1.234, 2.345, 3.40].round(2), Vector[1.23, 2.35, 3.4]) + end + def test_covector assert_equal(Matrix[[1,2,3]], @v1.covector) end Index: test/matrix/test_matrix.rb =================================================================== --- test/matrix/test_matrix.rb (revision 49865) +++ test/matrix/test_matrix.rb (revision 49866) @@ -190,6 +190,12 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L190 assert_equal(@m1, Matrix[o, [4,5,6]]) end + def test_round + a = Matrix[[1.0111, 2.32320, 3.04343], [4.81, 5.0, 6.997]] + b = Matrix[[1.01, 2.32, 3.04], [4.81, 5.0, 7.0]] + assert_equal(a.round(2), b) + end + def test_rows assert_equal(@m1, Matrix.rows([[1, 2, 3], [4, 5, 6]])) end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/