Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,7 @@ def ** (other)
# * #norm
# * #normalize
# * #r
# * #round
# * #size
#
# Conversion to other data types:
Expand Down Expand Up @@ -1790,6 +1791,13 @@ def []=(i, v)
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.
#
Expand Down
6 changes: 6 additions & 0 deletions test/matrix/test_matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ def o.to_ary; [1,2,3]; end
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
Expand Down
4 changes: 4 additions & 0 deletions test/matrix/test_vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ def test_r
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
Expand Down