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

lib/matrix: accept vectors in {h|v}stack

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60858 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2017-11-20 02:18:23 +00:00
parent aa95a42190
commit 62646c8d67
2 changed files with 9 additions and 5 deletions

View file

@ -212,10 +212,10 @@ class Matrix
# Matrix.vstack(x, y) # => Matrix[[1, 2], [3, 4], [5, 6], [7, 8]]
#
def Matrix.vstack(x, *matrices)
raise TypeError, "Expected a Matrix, got a #{x.class}" unless x.is_a?(Matrix)
x = CoercionHelper.coerce_to_matrix(x)
result = x.send(:rows).map(&:dup)
matrices.each do |m|
raise TypeError, "Expected a Matrix, got a #{m.class}" unless m.is_a?(Matrix)
m = CoercionHelper.coerce_to_matrix(m)
if m.column_count != x.column_count
raise ErrDimensionMismatch, "The given matrices must have #{x.column_count} columns, but one has #{m.column_count}"
end
@ -233,11 +233,11 @@ class Matrix
# Matrix.hstack(x, y) # => Matrix[[1, 2, 5, 6], [3, 4, 7, 8]]
#
def Matrix.hstack(x, *matrices)
raise TypeError, "Expected a Matrix, got a #{x.class}" unless x.is_a?(Matrix)
x = CoercionHelper.coerce_to_matrix(x)
result = x.send(:rows).map(&:dup)
total_column_count = x.column_count
matrices.each do |m|
raise TypeError, "Expected a Matrix, got a #{m.class}" unless m.is_a?(Matrix)
m = CoercionHelper.coerce_to_matrix(m)
if m.row_count != x.row_count
raise ErrDimensionMismatch, "The given matrices must have #{x.row_count} rows, but one has #{m.row_count}"
end
@ -1487,7 +1487,7 @@ class Matrix
#
def self.coerce_to(obj, cls, meth) # :nodoc:
return obj if obj.kind_of?(cls)
raise TypeError, "Expected a #{cls} but got a #{obj.class}" unless obj.respond_to? meth
begin
ret = obj.__send__(meth)
rescue Exception => e

View file

@ -571,6 +571,8 @@ class TestMatrix < Test::Unit::TestCase
assert_equal @e1, @e1.hstack(@e1)
assert_equal Matrix.empty(0,6), @e2.hstack(@e2)
assert_equal SubMatrix, SubMatrix.hstack(@e1).class
# From Vectors:
assert_equal Matrix[[1, 3],[2, 4]], Matrix.hstack(Vector[1,2], Vector[3, 4])
end
def test_vstack
@ -586,6 +588,8 @@ class TestMatrix < Test::Unit::TestCase
assert_equal Matrix.empty(4,0), @e1.vstack(@e1)
assert_equal @e2, @e2.vstack(@e2)
assert_equal SubMatrix, SubMatrix.vstack(@e1).class
# From Vectors:
assert_equal Matrix[[1],[2],[3]], Matrix.vstack(Vector[1,2], Vector[3])
end
def test_eigenvalues_and_eigenvectors_symmetric