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

lib/matrix: Add explicit coercion #to_matrix

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60857 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2017-11-20 02:18:12 +00:00
parent e33b1690d0
commit aa95a42190
3 changed files with 26 additions and 0 deletions

View file

@ -1380,6 +1380,13 @@ class Matrix
}
end
#
# Explicit conversion to a Matrix. Returns self
#
def to_matrix
self
end
#
# Returns an array of arrays that describe the rows of the matrix.
#
@ -1494,6 +1501,10 @@ class Matrix
def self.coerce_to_int(obj)
coerce_to(obj, Integer, :to_int)
end
def self.coerce_to_matrix(obj)
coerce_to(obj, Matrix, :to_matrix)
end
end
include CoercionHelper
@ -2039,6 +2050,13 @@ class Vector
@elements.dup
end
#
# Return a single-column matrix from this vector
#
def to_matrix
Matrix.column_vector(self)
end
def elements_to_f
warn "#{caller(1, 1)[0]}: warning: Vector#elements_to_f is deprecated"
map(&:to_f)

View file

@ -218,6 +218,10 @@ class TestMatrix < Test::Unit::TestCase
assert_equal([[1], [1]], m2.to_a)
end
def test_to_matrix
assert @m1.equal? @m1.to_matrix
end
def test_columns
assert_equal(@m1, Matrix.columns([[1, 4], [2, 5], [3, 6]]))
end

View file

@ -177,6 +177,10 @@ class TestVector < Test::Unit::TestCase
assert_equal("Vector[1, 2, 3]", @v1.to_s)
end
def test_to_matrix
assert_equal Matrix[[1], [2], [3]], @v1.to_matrix
end
def test_inspect
assert_equal("Vector[1, 2, 3]", @v1.inspect)
end