1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/matrix.rb: Add Vector#round. Patch by Jordan Stephens.

[Fixes GH-802]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49866 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2015-03-05 23:45:42 +00:00
parent ce85cd55c6
commit b81950f480
5 changed files with 26 additions and 0 deletions

View file

@ -1,3 +1,8 @@
Fri Mar 6 08:45:26 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/matrix.rb: Add Vector#round. Patch by Jordan Stephens.
[Fixes GH-802]
Fri Mar 6 07:33:03 2015 Koichi Sasada <ko1@atdot.net>
* gc.c (obj_info): show node name too.

3
NEWS
View file

@ -44,6 +44,9 @@ with all sufficient information, see the ChangeLog file.
* 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.

View file

@ -1713,6 +1713,7 @@ end
# * #norm
# * #normalize
# * #r
# * #round
# * #size
#
# Conversion to other data types:
@ -1790,6 +1791,13 @@ class Vector
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.
#

View file

@ -190,6 +190,12 @@ class TestMatrix < Test::Unit::TestCase
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

View file

@ -157,6 +157,10 @@ class TestVector < Test::Unit::TestCase
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